/** * Convert a category array into a category object. * * @param array $aCategory category. * * @return Category converted object. */ private static function convertToObject($aCategory) { $oCategory = new Category(); $oCategory->setId(intval($aCategory['id'])); $oCategory->setName($aCategory['name']); $oCategory->setDescription($aCategory['description']); $oCategory->setImage($aCategory['image']); return $oCategory; }
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'; } }