private function editAction()
 {
     // no id => redirect home
     if (!array_key_exists('id', $_GET)) {
         $oProduct = new Product();
     } else {
         $iId = intval($_GET['id']);
         $oProduct = ProductManager::get($iId);
     }
     //  if (array_key_exists('addProduct', $_POST)) {
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         $oProduct = new Product();
         $oProduct->setName($_POST['name']);
         $oProduct->setPrice($_POST['price']);
         $oProduct->setDescription($_POST['description']);
         if (array_key_exists('categories', $_POST)) {
             foreach ($_POST['categories'] as $iCategoryId) {
                 $oProduct->addCategory(CategoryManager::get($iCategoryId));
             }
         }
         if (array_key_exists('product-id', $_POST)) {
             // retourne Id du nouveau produit. Sinon null
             $iProductId = $_POST['product-id'];
             $oProduct->setId($iProductId);
             ProductManager::update($oProduct);
         } else {
             // retourne Id du nouveau produit créé. Sinon null
             $iProductId = ProductManager::create($oProduct);
             // Compléter l'objet par l'id du produit créé
             $oProduct->setId($iProductId);
         }
         if (!array_key_exists('image', $_POST)) {
             $temp = explode(".", $_FILES["image"]["name"]);
             $ext = $temp[count($temp) - 1];
             $newfilename = "images/product/" . $iProductId . '.' . $ext;
             $uploadfile = ROOT . $newfilename;
             move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);
             $oProduct->setImage($newfilename);
             ProductManager::update($oProduct);
         }
         $aComments = CommentManager::getAllFromProduct($oProduct);
         $aSimilarProducts = ProductManager::getRandom(5, 1);
         $aCategories = CategoryManager::getAll();
         require ROOT . 'src/ecommerce/view/product/show.php';
     } else {
         if (null === $oProduct) {
             $this->homeAction();
             return;
         }
         $aSimilarProducts = ProductManager::getRandom(5, 1);
         $aCategories = CategoryManager::getAll();
         require ROOT . 'src/ecommerce/view/product/edit.php';
     }
 }
 public static function getProduct($id)
 {
     $query = "SELECT *, produits.id as id_produit FROM produits\n\t\t\t\t\tLEFT JOIN images on produits.id=images.id_produit\n\t\t\t\t\tWHERE produits.id=" . $id . " AND deleted=0";
     if ($products = DBOperation::getAll($query)) {
         $result = null;
         foreach ($products as $product) {
             if (!isset($result)) {
                 $result = new Product($product["id_produit"], $product["nom"], $product["description"], $product["prix"]);
             }
             $result->setImage($product["link"]);
         }
         return $result;
     }
     return false;
 }
 public function __construct(Product $product, $quantity)
 {
     $this->id = $product->getId();
     $this->name = $product->getName();
     $this->description = $product->getDescription();
     $this->price = $product->getPrice();
     $this->images = $product->getImages();
     $this->setQuantity($quantity);
 }
 public static function update(Product $oProduct)
 {
     $sName = addslashes($oProduct->getName());
     $sDescription = addslashes($oProduct->getDescription());
     $sImage = addslashes($oProduct->getImage());
     $fPrice = floatval($oProduct->getPrice());
     //  get product id
     $iProductId = $oProduct->getId();
     $sQuery = "update product ";
     $sQuery .= "set name='{$sName}',description='{$sDescription}',image='{$sImage}',price={$fPrice}";
     $sQuery .= " where id = {$iProductId}";
     $bSuccess = DBOperation::exec($sQuery);
     if (!$bSuccess) {
         return false;
     }
     $sQuery = "delete from product_category where product_id = {$iProductId}";
     $bSuccess = DBOperation::exec($sQuery);
     // insert categories
     $aCategories = $oProduct->getCategories();
     if (count($aCategories) > 0) {
         foreach ($aCategories as $oCategory) {
             $sQuery = 'insert into product_category(product_id,category_id) values(';
             $sQuery .= "'{$iProductId}','{$oCategory->getId()}'";
             $sQuery .= ')';
             DBOperation::exec($sQuery);
         }
     }
     return true;
 }
 public static function validate(Product $oProduct, User $oUser)
 {
     $sQuery = " update comment";
     $sQuery .= " SET validated = 1";
     $sQuery .= " WHERE product_id = " . $oProduct->getId();
     $sQuery .= " AND user_email = '" . $oUser->getEmail() . "'";
     $iRetExec = DBOperation::exec($sQuery);
     if (null !== ($sLastSqlError = DBOperation::getLastSqlError())) {
         throw new \Exception($sLastSqlError);
     }
 }