From f7d03f66cd47d4009a5119e865596e14608ec8fa Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Thu, 17 Aug 2017 22:03:33 -0400 Subject: [PATCH] Making more progress on category API endpoint --- api/BusinessLogic/Categories/Category.php | 5 ++++ .../Categories/CategoryHandler.php | 30 +++++++++++++++++++ api/DataAccess/Categories/CategoryGateway.php | 22 ++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/api/BusinessLogic/Categories/Category.php b/api/BusinessLogic/Categories/Category.php index 755188f3..8a1f2dc8 100644 --- a/api/BusinessLogic/Categories/Category.php +++ b/api/BusinessLogic/Categories/Category.php @@ -60,4 +60,9 @@ class Category { * @var bool Indication if the user has access to the Categories */ public $accessible; + + /** + * @var string + */ + public $description; } \ No newline at end of file diff --git a/api/BusinessLogic/Categories/CategoryHandler.php b/api/BusinessLogic/Categories/CategoryHandler.php index ca315998..4d1111a6 100644 --- a/api/BusinessLogic/Categories/CategoryHandler.php +++ b/api/BusinessLogic/Categories/CategoryHandler.php @@ -3,6 +3,7 @@ namespace BusinessLogic\Categories; +use BusinessLogic\ValidationModel; use DataAccess\Categories\CategoryGateway; class CategoryHandler { @@ -18,9 +19,38 @@ class CategoryHandler { * @param $heskSettings array */ function createCategory($category, $heskSettings) { + + $this->categoryGateway->createCategory($category, $heskSettings); } + /** + * @param $category Category + * @param $heskSettings array + * @param $creating bool + * @return ValidationModel + */ + function validate($category, $heskSettings, $creating = true) { + $validationModel = new ValidationModel(); + if (!$creating && $category->id < 1) { + $validationModel->errorKeys[] = 'ID_MISSING'; + } + + if ($category->backgroundColor === null || trim($category->backgroundColor) === '') { + $validationModel->errorKeys[] = 'BACKGROUND_COLOR_MISSING'; + } + + if ($category->foregroundColor === null || trim($category->foregroundColor) === '') { + $validationModel->errorKeys[] = 'FOREGROUND_COLOR_MISSING'; + } + + if ($category->name === null || trim($category->name) === '') { + $validationModel->errorKeys[] = 'NAME_MISSING'; + } + + return $validationModel; + } + function editCategory($category, $heskSettings) { diff --git a/api/DataAccess/Categories/CategoryGateway.php b/api/DataAccess/Categories/CategoryGateway.php index fde6f824..f72847c9 100644 --- a/api/DataAccess/Categories/CategoryGateway.php +++ b/api/DataAccess/Categories/CategoryGateway.php @@ -33,6 +33,7 @@ class CategoryGateway extends CommonDao { $category->displayBorder = $row['display_border_outline'] === '1'; $category->priority = intval($row['priority']); $category->manager = intval($row['manager']) == 0 ? NULL : intval($row['manager']); + $category->description = $row['description']; $results[$category->id] = $category; } @@ -44,14 +45,31 @@ class CategoryGateway extends CommonDao { /** * @param $category Category * @param $heskSettings array + * @return int The ID of the newly created category */ function createCategory($category, $heskSettings) { $this->init(); - $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "categories` () - VALUES ()"; + $newOrderRs = hesk_dbQuery("SELECT `cat_order` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "categories` ORDER BY `cat_order` DESC LIMIT 1"); + $newOrder = hesk_dbFetchAssoc($newOrderRs); + + $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "categories` + (`name`, `cat_order`, `autoassign`, `type`, `priority`, `manager`, `background_color`, `usage`, + `foreground_color`, `display_border_outline`, `description`) + VALUES ('" . hesk_dbEscape($category->name) . "', " . intval($newOrder['cat_order']) . ", + '" . $category->autoAssign ? 1 : 0 . "', '" . intval($category->type) . "', + '" . intval($category->priority) . "', " . $category->manager === null ? 'NULL' : intval($category->manager) . ", + '" . hesk_dbEscape($category->backgroundColor) . "', " . intval($category->usage) . ", + '" . hesk_dbEscape($category->foregroundColor) . "', '" . $category->displayBorder ? 1 : 0 . "', + '" . hesk_dbEscape($category->description) . "')"; + + hesk_dbQuery($sql); + + $id = hesk_dbInsertID(); $this->close(); + + return $id; } function updateCategory($category, $heskSettings) {