public function create() { $data = array(); $insertInfo = array('name' => '', 'description' => ''); $errors = array(); if (isset($_POST['createCategory'])) { if (!isset($_POST['name']) || strlen($_POST['name']) < 3 || strlen($_POST['name']) > 255) { $errors['name'] = 'Incorrect name'; } if (!isset($_POST['description']) || strlen($_POST['description']) < 3 || strlen($_POST['description']) > 255) { $errors['description'] = 'Incorrect description'; } if (empty($errors)) { $insertInfo['name'] = $_POST['name']; $insertInfo['description'] = $_POST['description']; $table = 'categories'; $categoryEntity = new CategoryEntity(); $obj = $categoryEntity->init($insertInfo); $categoryCollection = new CategoryCollection(); $categoryCollection->save($obj); header('Location: index.php?c=category&m=index'); } } $data['errors'] = $errors; $data['insertInfo'] = $insertInfo; $this->loadView('category/create', $data); }
public function createCategory($account, $title) { $db = new Database(); $data = array($account, $title); $data = $db->execute("SELECT * FROM categories WHERE account = ? AND title = ?", $data); $data->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'CategoryEntity'); $categoryEntity = $data->fetch(); if ($categoryEntity) { return $categoryEntity; } else { $categoryEntity = new CategoryEntity(); $categoryEntity->setAccount($account); $categoryEntity->setTitle($title); $categoryEntity->persist(); return $categoryEntity; } }
/** * Update category * * @param CategoryEntity $category category object * @return CategoryEntity */ public function update(CategoryEntity $category) { $diff = $category->diff(); $updatedAt = []; if (count($diff) > 0) { $updatedAt = ['updatedAt' => date('Y-m-d H:i:s')]; $this->conn->table($this->table)->where('id', $category->id)->update(array_merge($diff, $updatedAt)); } return $this->createEntity(array_merge($category->getOriginal(), $diff, $updatedAt)); }
public static function createFromValues($category, array $values, $assetsPath = __DIR__ . '/../../../assets/') { switch ($category) { case 'products': $entity = ProductEntity::create($values, $assetsPath); break; case 'categories': $entity = CategoryEntity::create($values, $assetsPath); break; default: return; } return $entity; }
$category = $categoryCollection->getOne($_GET['id']); if (is_null($category)) { header('Location: categories.php'); } $insertInfo = array('name' => $category->getName(), 'description' => $category->getDescription()); $errors = array(); if (isset($_POST['editCategory'])) { $insertInfo = array('name' => isset($_POST['name']) ? $_POST['name'] : '', 'description' => isset($_POST['description']) ? $_POST['description'] : ''); if (!isset($_POST['name']) || strlen($_POST['name']) < 3 || strlen($_POST['name']) > 255) { $errors['name'] = 'Incorrect name'; } if (!isset($_POST['description']) || strlen($_POST['description']) < 3 || strlen($_POST['description']) > 255) { $errors['description'] = 'Incorrect description'; } if (empty($errors)) { $categoryEntity = new CategoryEntity(); $categoryEntity->setId($_GET['id']); $obj = $categoryEntity->init($insertInfo); $categoryCollection->save($obj); $_SESSION['flashMessage'] = 'You have 1 affected row'; header('Location: categories.php'); } } ?> <?php require_once 'common/sidebar.php'; ?>