Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public function editAction() {
  2.         $loadView = $this->loadView('Products');
  3.        
  4.         $loadModel = $this->loadModel('Products');
  5.        
  6.         //get product id and details
  7.        
  8.         $getProductId = $loadModel->getRoute('id');
  9.         $getProduct = $loadModel->getProduct($getProductId);
  10.        
  11.         //check if product exists
  12.        
  13.         if (empty($getProduct['product_id'])) {
  14.             $loadView->setMsg("Wrong ID, product doesn't exist.");
  15.             $loadView->error = 1;
  16.         }
  17.         else
  18.         {
  19.             $loadView->setVar('product', $getProduct);
  20.            
  21.             if (isset($_POST['submit']))
  22.             {
  23.                 //form validation
  24.                
  25.                 $requiredFields=array('partNumber', 'description', 'stockQTY', 'costPrice', 'sellingPrice', 'vatRate'); //required fields in form
  26.                
  27.                 if (!$loadModel->checkEmptyFields($requiredFields)) $loadView->setMsg('Fill all required fields.');
  28.                 else
  29.                 {
  30.                    
  31.                     if (!$loadModel->isNumeric($_POST['stockQTY']) ||
  32.                         !$loadModel->isNumeric($_POST['costPrice']) ||
  33.                         !$loadModel->isNumeric($_POST['sellingPrice']) ||
  34.                         !$loadModel->isNumeric($_POST['vatRate'])) $loadView->setMsg('Prices, stock qty should be numbers!');
  35.                     else
  36.                     {
  37.                             //check if image was uploaded
  38.                             if (is_uploaded_file($_FILES['image']['tmp_name'])) {
  39.                                 $allowedType = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "png" => "image/png");
  40.                
  41.                                 $uploadModel = $this->loadModel('Upload', $_FILES['image']);
  42.                                
  43.                                 if (!$uploadModel->checkType($allowedType)) { $loadView->setMsg('File type is wrong. Only JPG, JPEG, PNG'); $upload_error=1; }
  44.                                 elseif (!$uploadModel->uploadFile()) { $loadView->setMsg('File cant be uploaded. Contact Administrator.');  $upload_error=1; }
  45.                                 else
  46.                                 {
  47.                                     //updating image
  48.                                     $image = $uploadModel->newFileName;
  49.                                    
  50.                                     $values=array(
  51.                                         'product_id' => $getProduct['product_id'],
  52.                                         'image' => $image
  53.                                     );
  54.                                    
  55.                                     $loadModel->deleteImage($getProduct['image']);
  56.                                    
  57.                                     $loadModel->updateImage($values);
  58.                                 }
  59.                             }
  60.                            
  61.                             //updating product details
  62.                            
  63.                             $values = array(
  64.                                 'product_id' => $getProduct['product_id'],
  65.                                 'part_number' => $_POST['partNumber'],
  66.                                 'description' => htmlspecialchars($_POST['description']),
  67.                                 'stock_quantity' => $_POST['stockQTY'],
  68.                                 'cost_price' => $_POST['costPrice'],
  69.                                 'selling_price' => $_POST['sellingPrice'],
  70.                                 'vat_rate' => $_POST['vatRate']
  71.                             );
  72.                            
  73.                             $loadModel->editProduct($values);
  74.                            
  75.                             if (empty($upload_error)) { // redirect if everything is ok
  76.                                 $loadView->setFlashMsg('Product updated successfully!', 'success');
  77.                                 $this->redirect('index.php?controller=Products&action=list');
  78.                             }
  79.                             else
  80.                             {
  81.                                 //update product details to form, if upload error only
  82.                                 $getProduct = $loadModel->getProduct($getProductId);
  83.                                 $loadView->setVar('product', $getProduct);
  84.                             }
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.        
  90.         $loadView->editView(); //show template
  91.     }