123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- <?php namespace App\Controllers;
- use CodeIgniter\Controller;
- use \App\Andymodel;
- use \App\Andy\ImgPreCheck;
- use \App\Andy\Utility;
- use CodeIgniter\I18n\Time;
- use \App\Models\ProductModel;
- class Product extends BaseController
- {
- protected $id;
- protected $portfolioId ;
- protected $productImages="productImages";
-
-
- public function __construct()
- {
- parent::__construct();
- $this->myTime = parent::getTime();
- $this->myDate= date("d/m/Y",$this->myTime);
-
- }
-
- public function productAddForm()
- {
- $data = [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>' '
- ];
- echo view('addProductForm',$data);
- }
-
-
- public function addProductDo()
-
- {
-
- $utilityHandle = new Utility();
-
- $info=$this->request->getVar('info');
- $info=$utilityHandle->removeScript($info);
- $info=$utilityHandle->convertQuotes($info);
- $info= $utilityHandle->newRegex($info);
- $title=$this->request->getVar('title');
- $title= $utilityHandle->badCharsRemoveAll($title);
- $slug=url_title($title);
- $category= $this->request->getVar('category');
- $price= $this->request->getVar('price');
- $price= $utilityHandle->badCharsRemoveAll($price);
- $file = $this->request->getFile('productImage');
- $nameImgFile= $file->getName();
-
-
- $image = new ImgPreCheck();
- $handle= new ProductModel();
-
- //first check file suffix to make sure image
- $logic= $image->extCheck($nameImgFile);
- if( $logic===0)
- {
- $data = [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>'thats not a valid image '
- ];
-
- echo view('addProductForm',$data);
- exit();
-
- }
- //now check to make sure image doresnt already exist
- $logic= $image->allReadyExists($this->productImages,$nameImgFile);
-
- if( $logic==true)
- {
- $data = [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>'that image already exists,'
- ];
-
- echo view('addProductForm',$data);
- exit();
-
- }
-
-
- //check fiel size is not overly large
-
- $imageSize= $image->getFileSize($file);
-
- if($imageSize >= 300000)
- {
- $data = [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>'that image is too large '
- ];
-
- echo view('addProductForm',$data);
- exit();
-
- }
-
- try{
-
- $file->move(ROOTPATH.'public/productImages',$nameImgFile);
- $handle = new ProductModel();
- $outcome= $handle->insertProduct($nameImgFile,$title,$slug,$category,$info,$price);
-
-
-
- }
- catch ( \Exception $e)
-
- {
-
- $data = [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>'something went wrong'.$e->getMessage()
- ];
-
- echo view('info', $data);
-
-
- }
-
-
- $data= [
- 'title' => 'any ',
- 'date'=>$this->myDate,
- 'info'=>'looks like product was added '
- ];
-
- echo view('info', $data);
-
-
-
- } //end add product do
- //start of new product function
- public function getCategory($theCategory)
- {
-
-
-
- //start
- $handle= new ProductModel();
- $dbEntries = $handle->getCountByCategory($theCategory);
-
-
- if ( ($dbEntries==0)OR ($dbEntries==null))
- {
- $data= [
-
- 'title' => 'products ',
- 'category' =>$theCategory,
- 'date' =>$this->myDate,
- 'info'=>'no products in this category at the monment '
- ];
- echo view('infoProduct', $data);
- die();
- }
-
-
-
- else {
- $result= $handle->useCategory($theCategory);
- $data = [
-
- 'result'=>$result,
- 'title'=>'paginate',
- 'portfolio' => $handle->paginate(5),
- 'pager' => $handle->pager,
- 'category'=>$theCategory,
- 'date'=>$this->myDate
- ];
-
-
- echo view('displayProducts',$data);
- }
-
-
-
-
- }//end new product category function
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function prodInfo($Id)
-
- {
-
-
- $handle = new ProductModel;
- $singleEntry= $handle->getOne($Id);
-
- $data = [
- 'title' => 'home page ',
- 'date'=>$this->myDate,
- 'prodInfo'=>$singleEntry
- ];
-
-
-
-
-
-
-
-
-
- echo view('productInfo',$data);
-
-
-
-
- }
-
-
-
- public function displayProdsToEdit()
- {
- $handle= new ProductModel();
- $result= $handle->getAll();
-
- $data = [
- 'title' => ' ',
- 'date'=>$this->myDate,
- 'products'=>$result
- ];
-
-
-
-
- echo view('editProductForm',$data);
-
-
- }
-
-
- public function editOneProduct($id)
-
- {
-
- $handle= new ProductModel();
- $result= $handle->getOne($id);
-
- $data = [
- 'title' => 'edit ',
- 'date'=>$this->myDate,
- 'product'=>$result,
- 'info'=>''
- ];
-
- echo view('editProductFormDo', $data);
- }
-
-
- public function processEditProduct()
- {
-
-
- $info=$this->request->getVar('info');
- $imageTitle=$this->request->getVar('title');
- $slug=url_title($imageTitle);
- $category= $this->request->getVar('category');
- $price= $this->request->getVar('price');
- $Id= $this->request->getVar('Id');
- $handle= new ProductModel();
- $logic= $handle->amendProduct($Id,$imageTitle,$slug,$category,$info,$price);
-
- if($logic == 1)
-
- {
- $data = [
- 'title' => 'product Edit ',
- 'date'=>$this->myDate,
- 'info'=>'looks like edit of product was successful'
- ];
- echo view('info',$data);
- exit();
- }
- else
- {
- $data = [
- 'title' => 'product Edit ',
- 'date'=>$this->myDate,
- 'info'=>'something went wrong with edit'
-
- ];
- echo view('info',$data);
- exit();
-
- }
-
- }//end of processing product edit
-
- public function delProdForm()
-
- {
- $handle = new ProductModel;
- $result= $handle->getAll();
-
- $data = [
- 'title' => ' ',
- 'date'=>$this->myDate,
- 'prods'=>$result,
- 'info'=>''
- ];
- echo view('removeProdForm',$data);
- }
- public function delProductDo()
- {
- $productId= $this->request->getVar('prodId');
- $handle= new ProductModel();
- $result= $handle->getOne($productId);
- $image= $result['image'];
-
-
-
- try
- {
- chmod(ROOTPATH.'public/productImages/'.$image,0777);
- unlink(ROOTPATH.'public/productImages/'.$image);
- $handle->deleteOne($productId);
- }
- catch ( \Exception $e)
- {
- $error= $e->getMessage();
- $data = [
- 'title' => 'info',
-
- 'info'=>'somwething went wrong with deleting blog'.$error,
- 'date'=>$this->myDate
- ];
-
- echo view('info', $data);
-
-
-
- die();
- //above try catch works
- }
-
- $data = [
- 'title' => 'info',
-
- 'info'=>'looks like product was deleted ',
- 'date'=>$this->myDate
- ];
-
- echo view('info', $data);
- //above works now need to check id typed in exitst, delete from database and then unlink image
- }
-
- }//end class
|