private function editAction() { // no id => redirect home if (!array_key_exists('id', $_GET)) { $oCategory = new Category(); } else { $iId = intval($_GET['id']); $oCategory = CategoryManager::get($iId); } // if (array_key_exists('addProduct', $_POST)) { if ($_SERVER['REQUEST_METHOD'] == 'POST') { $oCategory = new Category(); $oCategory->setName($_POST['name']); $oCategory->setDescription($_POST['description']); if (array_key_exists('categories', $_POST)) { foreach ($_POST['categories'] as $iCategoryId) { $oCategory->addCategory(CategoryManager::get($iCategoryId)); } } if (array_key_exists('category-id', $_POST)) { // retourne Id du nouveau produit. Sinon null $iCategoryId = $_POST['category-id']; $oCategory->setId($iCategoryId); CategoryManager::update($oCategory); } else { // retourne Id du nouveau produit créé. Sinon null $iCategoryId = CategoryManager::create($oCategory); // Compléter l'objet par l'id du produit créé $oCategory->setId($iCategoryId); } $temp = explode(".", $_FILES["image"]["name"]); $ext = $temp[count($temp) - 1]; $newfilename = "images/category/" . $iCategoryId . '.' . $ext; $uploadfile = ROOT . $newfilename; move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile); $oCategory->setImage($newfilename); CategoryManager::update($oCategory); require ROOT . 'src/ecommerce/view/category/show.php'; } else { if (null === $oCategory) { $this->homeAction(); return; } $aCategories = CategoryManager::getAll(); require ROOT . 'src/ecommerce/view/category/edit.php'; } }
/** * Get $iLimit products from the category. * * @param Category $oCategory category. * @param int $iLimit limit to get * * @return array(Product) products from categories. */ public static function getAllFromCategory(Category $oCategory, $iLimit = false) { $sQuery = 'select * from product p, product_category pc '; $sQuery .= ' where pc.product_id = p.id'; $sQuery .= ' and pc.category_id = ' . $oCategory->getId(); $aProducts = array(); foreach (DBOperation::getAll($sQuery) as $aProduct) { $aProducts[] = self::convertToObject($aProduct); } if ($iLimit !== false) { $sQuery .= ' limit ' . $iLimit; } // TODO remove if ($iLimit == false) { if (count($aProducts) < $iLimit) { $iMaxSteps = $iLimit - count($aProducts); for ($iStep = 1; $iStep <= $iMaxSteps; $iStep++) { $aProducts[] = $aProducts[0]; } } } return $aProducts; }
public static function update(Category $oCategory) { $sName = addslashes($oCategory->getName()); $sDescription = addslashes($oCategory->getDescription()); $sImage = addslashes($oCategory->getImage()); // get product id $iCategoryId = $oCategory->getId(); $sQuery = "update category "; $sQuery .= "set name='{$sName}',description='{$sDescription}',image='{$sImage}'"; $sQuery .= " where id = {$iCategoryId}"; $bSuccess = DBOperation::exec($sQuery); if (!$bSuccess) { return false; } return true; }