Blog.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3. use \App\Models\BlogModel;
  4. use \App\Andy\Utility;
  5. use \App\Andy\ImgPreCheck;
  6. use CodeIgniter\I18n\Time;
  7. use \App\Andy\BlogManager;
  8. class Blog extends BaseController
  9. {
  10. protected $blogId;
  11. protected $myDate;
  12. protected $myTime;
  13. protected $blogImages="blogImages";
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->myTime = parent::getTime();
  18. $this->myDate= date("d/m/Y",$this->myTime);
  19. }
  20. public function altMethod()
  21. {
  22. $BlogHandle = new BlogManager(model(BlogModel::class));
  23. $result= $BlogHandle->list();
  24. var_dump($result);
  25. echo "place holder for trying dependency injection";
  26. }
  27. public function doCount()
  28. {
  29. $handle = new BlogModel();
  30. $mycount= $handle->count();
  31. }
  32. public function doPaginate()
  33. {
  34. $handle = new BlogModel();
  35. $newresult = $handle->getDesending();
  36. //$result= $handle->getAll();
  37. $data = [
  38. 'title'=>'paginate',
  39. //'blogs' => $handle->paginate(5),
  40. 'blogs'=>$newresult->paginate(5),
  41. 'pager' => $handle->pager,
  42. 'date'=>$this->myDate
  43. ];
  44. echo view('listBlogsPaginate',$data);
  45. }
  46. public function showArticle($theSlug)
  47. {
  48. $handle = new BlogModel();
  49. $result= $handle->getArticle($theSlug);
  50. $data =[
  51. 'title'=>$theSlug,
  52. 'blog'=>$result,
  53. 'date'=>$this->myDate
  54. ];
  55. echo view('blogArticle',$data);
  56. }
  57. public function listBlogs()
  58. {
  59. $handle = new BlogModel();
  60. $result= $handle->getAll();
  61. $data =[
  62. 'title'=>'andything',
  63. 'blogs'=>$result,
  64. 'date'=>$this->myDate
  65. ];
  66. echo view('listBlogs',$data);
  67. }
  68. public function blogForm()
  69. {
  70. // displays form to submit newblog
  71. $data =[
  72. 'title'=>'new blog',
  73. 'info'=>'&nbsp;',
  74. 'date'=>$this->myDate
  75. ];
  76. echo view('form4',$data);
  77. }
  78. public function newBlogArticle()
  79. {
  80. //accepts post data from form4 and processes form submission of data for new blog
  81. //what im going to do is filter out bad chars
  82. $title = $this->request->getVar('title');
  83. $date= $this->request->getVar('date');
  84. $article = $this->request->getVar('article');
  85. //now to remove ALL bad chars, can always do an edit
  86. $utilityHandle= new Utility();
  87. $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
  88. $cleanDate= $utilityHandle->badCharsRemoveAll($date);
  89. $cleanArticle1 = $utilityHandle->removeScript($article);
  90. $cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
  91. $cleanArticle = $utilityHandle->newRegex($cleanArticle2);
  92. $file = $this->request->getFile('userfile');
  93. $name= $file->getName();
  94. $tempfile = $file->getTempName();
  95. $slug= url_title($cleanTitle);
  96. //nowsome pre checks
  97. $handle = new ImgPreCheck();
  98. //ImgPreCheck is one of my classes
  99. $logic = $handle->allReadyExists($this->blogImages,$name);
  100. if($logic == "true")
  101. {
  102. $data =[
  103. 'title'=>'andything',
  104. 'info'=>'image with that name already exists',
  105. 'date'=>$this->myDate
  106. ];
  107. echo view('form4',$data);
  108. exit();
  109. }
  110. $logic2 = $handle->extCheck($name);
  111. if($logic2 == 0)
  112. {
  113. $data =[
  114. 'title'=>'andything',
  115. 'info'=>'that is not an image file',
  116. 'date'=>$this->myDate
  117. ];
  118. echo view('form4',$data);
  119. exit();
  120. }
  121. $size= $handle->getFileSize($tempfile);
  122. if($size > 500000)
  123. {
  124. $data =[
  125. 'title'=>'andything',
  126. 'info'=>'thats too large an image file',
  127. 'date'=>$this->myDate
  128. ];
  129. echo view('form4',$data);
  130. exit();
  131. }
  132. try{
  133. $file->move('blogImages',$name);
  134. $handle = new BlogModel();
  135. $blogLogic= $handle->insertBlog($cleanTitle,$cleanArticle,$name,$slug,$cleanDate);
  136. }
  137. catch ( \Exception $e)
  138. {
  139. $data = [
  140. 'title' => 'info',
  141. 'info'=>'somwething went wrong with new blog $e->getMessage(); ',
  142. 'date'=>$this->myDate
  143. ];
  144. echo view('info', $data);
  145. die();
  146. //above try catch works
  147. }
  148. if ($blogLogic == true)
  149. {
  150. $data =[
  151. 'title'=>'blog submission',
  152. 'info'=>'looks like new blog was created ',
  153. 'date'=>$date
  154. ];
  155. echo view('info',$data);
  156. exit();
  157. }
  158. }//end new blog
  159. public function delBlogForm()
  160. {
  161. $myTime = new Time('now');
  162. $date= $myTime->toLocalizedString('MMM d, yyyy');
  163. helper('form');
  164. helper('html');
  165. $handle = New BlogModel();
  166. $result= $handle->getAll();
  167. $data =[
  168. 'title'=>'andything',
  169. 'blogList'=>$result,
  170. 'info'=> '',
  171. 'date'=>$this->myDate
  172. ];
  173. echo view('removeBlogForm',$data);
  174. //end of newblog below
  175. }
  176. public function delBlog()
  177. {
  178. $myTime = new Time('now');
  179. $date= $myTime->toLocalizedString('MMM d, yyyy');
  180. helper('uri');
  181. helper('html');
  182. helper('form');
  183. helper('security');
  184. $this->blogId = $this->request->getVar('BlogId');
  185. $handle= new BlogModel();
  186. $result= $handle->getOneToDel($this->blogId);
  187. if($result==null)
  188. {
  189. $handle= new BlogModel();
  190. $result= $handle->getAll();
  191. $data = [
  192. 'title' => 'del ',
  193. 'blogList'=>$result,
  194. 'info'=>'no such item ',
  195. 'date'=>$this->myDate
  196. ];
  197. echo view('removeBlogForm',$data);
  198. exit();
  199. }
  200. $image= $result['image'];
  201. //first get rid of image
  202. try
  203. {
  204. // chmod(ROOTPATH.'public/blogImages/'.$image,0777);
  205. unlink(ROOTPATH.'public/blogImages/'.$image);
  206. //now need to remove entry from databse
  207. $logic= $handle->deleteOne($this->blogId);
  208. }
  209. catch ( \Exception $e)
  210. {
  211. $data = [
  212. 'title' => 'info',
  213. 'info'=>'somwething went wrong with deleting blog'.$e->getMessage(),
  214. 'date'=>$date
  215. ];
  216. echo view('info', $data);
  217. die();
  218. //above try catch works
  219. }
  220. if ($logic == True)
  221. {
  222. $data =[
  223. 'title'=>'del blog',
  224. 'info'=>'lookes like blog was deleted ',
  225. 'date'=>$this->myDate
  226. ];
  227. echo view('admin',$data);
  228. }
  229. }
  230. public function editBlogForm()
  231. {
  232. //will try utility class to strip bad chars
  233. //here another approach is to first convert all quotes to html entitiy
  234. //then in a second round remove bad chars
  235. $handle = new BlogModel();
  236. $result= $handle->getAll();
  237. $data =[
  238. 'title'=>'andything',
  239. 'blogs'=>$result,
  240. 'date'=>$this->myDate
  241. ];
  242. echo view('editBlogForm',$data);
  243. }
  244. public function editBlog($slug)
  245. {
  246. $handle= new BlogModel();
  247. $result= $handle->getArticle($slug);
  248. $data =[
  249. 'title'=>'andything',
  250. 'blog'=>$result,
  251. 'date'=>$this->myDate
  252. ];
  253. echo view('doEditBlog',$data);
  254. }
  255. public function processEditBlog()
  256. {
  257. $title = $this->request->getVar('title');
  258. $article = $this->request->getVar('article');
  259. $blogId= $this->request->getVar('Id');
  260. $slug= url_title($title);
  261. $utilityHandle= new Utility();
  262. $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
  263. $cleanArticle1 = $utilityHandle->removeScript($article);
  264. $cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
  265. $cleanArticle3 = $utilityHandle->newRegex($cleanArticle2);
  266. try
  267. {
  268. $handle= new BlogModel();
  269. $result= $handle->amendBlog($blogId,$cleanTitle,$cleanArticle3,$slug);
  270. }
  271. catch ( \Exception $e)
  272. {
  273. $data = [
  274. 'title' => 'info',
  275. 'info'=>'somwething went wrong with editing blog ',
  276. 'date'=>$this->myDate
  277. ];
  278. echo view('info', $data);
  279. die();
  280. //above try catch works
  281. }
  282. if ($result == 1)
  283. {
  284. $data =[
  285. 'title'=>'blog result',
  286. 'info'=>'lookes like edit of blog was successful',
  287. 'date'=>$this->myDate
  288. ];
  289. echo view('info',$data);
  290. }
  291. }
  292. }