Product.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php namespace App\Controllers;
  2. use CodeIgniter\Controller;
  3. use \App\Andymodel;
  4. use \App\Andy\ImgPreCheck;
  5. use \App\Andy\Utility;
  6. use CodeIgniter\I18n\Time;
  7. use \App\Models\ProductModel;
  8. class Product extends BaseController
  9. {
  10. protected $id;
  11. protected $portfolioId ;
  12. protected $productImages="productImages";
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->myTime = parent::getTime();
  17. $this->myDate= date("d/m/Y",$this->myTime);
  18. }
  19. public function productAddForm()
  20. {
  21. $data = [
  22. 'title' => 'any ',
  23. 'date'=>$this->myDate,
  24. 'info'=>' '
  25. ];
  26. echo view('addProductForm',$data);
  27. }
  28. public function addProductDo()
  29. {
  30. $utilityHandle = new Utility();
  31. $info=$this->request->getVar('info');
  32. $info=$utilityHandle->removeScript($info);
  33. $info=$utilityHandle->convertQuotes($info);
  34. $info= $utilityHandle->newRegex($info);
  35. $title=$this->request->getVar('title');
  36. $title= $utilityHandle->badCharsRemoveAll($title);
  37. $slug=url_title($title);
  38. $category= $this->request->getVar('category');
  39. $price= $this->request->getVar('price');
  40. $price= $utilityHandle->badCharsRemoveAll($price);
  41. $file = $this->request->getFile('productImage');
  42. $nameImgFile= $file->getName();
  43. $image = new ImgPreCheck();
  44. $handle= new ProductModel();
  45. //first check file suffix to make sure image
  46. $logic= $image->extCheck($nameImgFile);
  47. if( $logic===0)
  48. {
  49. $data = [
  50. 'title' => 'any ',
  51. 'date'=>$this->myDate,
  52. 'info'=>'thats not a valid image '
  53. ];
  54. echo view('addProductForm',$data);
  55. exit();
  56. }
  57. //now check to make sure image doresnt already exist
  58. $logic= $image->allReadyExists($this->productImages,$nameImgFile);
  59. if( $logic==true)
  60. {
  61. $data = [
  62. 'title' => 'any ',
  63. 'date'=>$this->myDate,
  64. 'info'=>'that image already exists,'
  65. ];
  66. echo view('addProductForm',$data);
  67. exit();
  68. }
  69. //check fiel size is not overly large
  70. $imageSize= $image->getFileSize($file);
  71. if($imageSize >= 300000)
  72. {
  73. $data = [
  74. 'title' => 'any ',
  75. 'date'=>$this->myDate,
  76. 'info'=>'that image is too large '
  77. ];
  78. echo view('addProductForm',$data);
  79. exit();
  80. }
  81. try{
  82. $file->move(ROOTPATH.'public/productImages',$nameImgFile);
  83. $handle = new ProductModel();
  84. $outcome= $handle->insertProduct($nameImgFile,$title,$slug,$category,$info,$price);
  85. }
  86. catch ( \Exception $e)
  87. {
  88. $data = [
  89. 'title' => 'any ',
  90. 'date'=>$this->myDate,
  91. 'info'=>'something went wrong'.$e->getMessage()
  92. ];
  93. echo view('info', $data);
  94. }
  95. $data= [
  96. 'title' => 'any ',
  97. 'date'=>$this->myDate,
  98. 'info'=>'looks like product was added '
  99. ];
  100. echo view('info', $data);
  101. } //end add product do
  102. //start of new product function
  103. public function getCategory($theCategory)
  104. {
  105. //start
  106. $handle= new ProductModel();
  107. $dbEntries = $handle->getCountByCategory($theCategory);
  108. if ( ($dbEntries==0)OR ($dbEntries==null))
  109. {
  110. $data= [
  111. 'title' => 'products ',
  112. 'category' =>$theCategory,
  113. 'date' =>$this->myDate,
  114. 'info'=>'no products in this category at the monment '
  115. ];
  116. echo view('infoProduct', $data);
  117. die();
  118. }
  119. else {
  120. $result= $handle->useCategory($theCategory);
  121. $data = [
  122. 'result'=>$result,
  123. 'title'=>'paginate',
  124. 'portfolio' => $handle->paginate(5),
  125. 'pager' => $handle->pager,
  126. 'category'=>$theCategory,
  127. 'date'=>$this->myDate
  128. ];
  129. echo view('displayProducts',$data);
  130. }
  131. }//end new product category function
  132. public function prodInfo($Id)
  133. {
  134. $handle = new ProductModel;
  135. $singleEntry= $handle->getOne($Id);
  136. $data = [
  137. 'title' => 'home page ',
  138. 'date'=>$this->myDate,
  139. 'prodInfo'=>$singleEntry
  140. ];
  141. echo view('productInfo',$data);
  142. }
  143. public function displayProdsToEdit()
  144. {
  145. $handle= new ProductModel();
  146. $result= $handle->getAll();
  147. $data = [
  148. 'title' => ' ',
  149. 'date'=>$this->myDate,
  150. 'products'=>$result
  151. ];
  152. echo view('editProductForm',$data);
  153. }
  154. public function editOneProduct($id)
  155. {
  156. $handle= new ProductModel();
  157. $result= $handle->getOne($id);
  158. $data = [
  159. 'title' => 'edit ',
  160. 'date'=>$this->myDate,
  161. 'product'=>$result,
  162. 'info'=>''
  163. ];
  164. echo view('editProductFormDo', $data);
  165. }
  166. public function processEditProduct()
  167. {
  168. $info=$this->request->getVar('info');
  169. $imageTitle=$this->request->getVar('title');
  170. $slug=url_title($imageTitle);
  171. $category= $this->request->getVar('category');
  172. $price= $this->request->getVar('price');
  173. $Id= $this->request->getVar('Id');
  174. $handle= new ProductModel();
  175. $logic= $handle->amendProduct($Id,$imageTitle,$slug,$category,$info,$price);
  176. if($logic == 1)
  177. {
  178. $data = [
  179. 'title' => 'product Edit ',
  180. 'date'=>$this->myDate,
  181. 'info'=>'looks like edit of product was successful'
  182. ];
  183. echo view('info',$data);
  184. exit();
  185. }
  186. else
  187. {
  188. $data = [
  189. 'title' => 'product Edit ',
  190. 'date'=>$this->myDate,
  191. 'info'=>'something went wrong with edit'
  192. ];
  193. echo view('info',$data);
  194. exit();
  195. }
  196. }//end of processing product edit
  197. public function delProdForm()
  198. {
  199. $handle = new ProductModel;
  200. $result= $handle->getAll();
  201. $data = [
  202. 'title' => ' ',
  203. 'date'=>$this->myDate,
  204. 'prods'=>$result,
  205. 'info'=>''
  206. ];
  207. echo view('removeProdForm',$data);
  208. }
  209. public function delProductDo()
  210. {
  211. $productId= $this->request->getVar('prodId');
  212. $handle= new ProductModel();
  213. $result= $handle->getOne($productId);
  214. $image= $result['image'];
  215. try
  216. {
  217. chmod(ROOTPATH.'public/productImages/'.$image,0777);
  218. unlink(ROOTPATH.'public/productImages/'.$image);
  219. $handle->deleteOne($productId);
  220. }
  221. catch ( \Exception $e)
  222. {
  223. $error= $e->getMessage();
  224. $data = [
  225. 'title' => 'info',
  226. 'info'=>'somwething went wrong with deleting blog'.$error,
  227. 'date'=>$this->myDate
  228. ];
  229. echo view('info', $data);
  230. die();
  231. //above try catch works
  232. }
  233. $data = [
  234. 'title' => 'info',
  235. 'info'=>'looks like product was deleted ',
  236. 'date'=>$this->myDate
  237. ];
  238. echo view('info', $data);
  239. //above works now need to check id typed in exitst, delete from database and then unlink image
  240. }
  241. }//end class