diff --git a/api/BusinessLogic/ServiceMessages/ServiceMessage.php b/api/BusinessLogic/ServiceMessages/ServiceMessage.php index 1951b672..9a5aff94 100644 --- a/api/BusinessLogic/ServiceMessages/ServiceMessage.php +++ b/api/BusinessLogic/ServiceMessages/ServiceMessage.php @@ -30,4 +30,7 @@ class ServiceMessage extends \BaseClass { /* @var $icon string */ public $icon; + + /* @var $locations string[] */ + public $locations; } \ No newline at end of file diff --git a/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php b/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php index 7d7ffd59..e852f4cf 100644 --- a/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php +++ b/api/BusinessLogic/ServiceMessages/ServiceMessageHandler.php @@ -130,6 +130,17 @@ class ServiceMessageHandler extends \BaseClass { } catch (\Exception $e) { $validationModel->errorKeys[] = 'INVALID_STYLE'; } + if ($serviceMessage->locations === null || count($serviceMessage->locations) === 0) { + $validationModel->errorKeys[] = 'MISSING_LOCATIONS'; + } else { + $locations = ServiceMessageLocation::getAll(); + foreach ($serviceMessage->locations as $location) { + if (!in_array($location, $locations)) { + $validationModel->errorKeys[] = 'INVALID_LOCATION'; + break; + } + } + } if (count($validationModel->errorKeys) > 0) { // Validation failed diff --git a/api/BusinessLogic/ServiceMessages/ServiceMessageLocation.php b/api/BusinessLogic/ServiceMessages/ServiceMessageLocation.php new file mode 100644 index 00000000..7f918bc7 --- /dev/null +++ b/api/BusinessLogic/ServiceMessages/ServiceMessageLocation.php @@ -0,0 +1,34 @@ +createdBy = $userContext->id; } - $serviceMessage->title = $data['title']; - $serviceMessage->icon = $data['icon']; - $serviceMessage->message = $data['message']; - $serviceMessage->published = $data['published']; - $serviceMessage->style = $data['style']; + $serviceMessage->title = Helpers::safeArrayGet($data, 'title'); + $serviceMessage->icon = Helpers::safeArrayGet($data, 'icon'); + $serviceMessage->message = Helpers::safeArrayGet($data, 'message'); + $serviceMessage->published = Helpers::safeArrayGet($data, 'published'); + $serviceMessage->style = Helpers::safeArrayGet($data, 'style'); + + $jsonLocations = Helpers::safeArrayGet($data, 'locations'); + + if ($jsonLocations !== null && !empty($jsonLocations)) { + foreach ($jsonLocations as $key => $value) { + $serviceMessage->locations[] = $value; + } + } return $serviceMessage; } diff --git a/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php b/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php index 0f15c333..6a58b070 100644 --- a/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php +++ b/api/DataAccess/ServiceMessages/ServiceMessagesGateway.php @@ -36,6 +36,11 @@ class ServiceMessagesGateway extends CommonDao { $serviceMessage->id = hesk_dbInsertID(); + foreach ($serviceMessage->locations as $location) { + hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` + (`service_message_id`, `location`) VALUES (" . intval($serviceMessage->id) . ", '" . hesk_dbEscape($location) . "')"); + } + // Get the autogenerated fields $rs = hesk_dbQuery("SELECT `dt`, `order` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` WHERE `id` = " . intval($serviceMessage->id)); @@ -69,6 +74,14 @@ class ServiceMessagesGateway extends CommonDao { $serviceMessage->message = $row['message']; $serviceMessage->style = ServiceMessageStyle::getStyleById($row['style']); $serviceMessage->icon = $row['icon']; + $serviceMessage->locations = array(); + + $locationsRs = hesk_dbQuery("SELECT `location` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` + WHERE `service_message_id` = " . intval($serviceMessage->id)); + while ($innerRow = hesk_dbFetchAssoc($locationsRs)) { + $serviceMessage->locations[] = $innerRow['location']; + } + $serviceMessages[] = $serviceMessage; } @@ -92,6 +105,13 @@ class ServiceMessagesGateway extends CommonDao { `order` = " . intval($serviceMessage->order) . " WHERE `id` = " . intval($serviceMessage->id)); + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` + WHERE `service_message_id` = " . intval($serviceMessage->id)); + foreach ($serviceMessage->locations as $location) { + hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` + (`service_message_id`, `location`) VALUES (" . intval($serviceMessage->id) . ", '" . hesk_dbEscape($location) . "')"); + } + $otherFieldsRs = hesk_dbQuery("SELECT `dt`, `author`, `order` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` WHERE `id` = " . intval($serviceMessage->id)); $otherFields = hesk_dbFetchAssoc($otherFieldsRs); @@ -107,6 +127,9 @@ class ServiceMessagesGateway extends CommonDao { function deleteServiceMessage($id, $heskSettings) { $this->init(); + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` + WHERE `service_message_id` = " . intval($id)); + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` WHERE `id` = " . intval($id)); diff --git a/install/migrations/core.php b/install/migrations/core.php index 6526344d..db30e5ac 100644 --- a/install/migrations/core.php +++ b/install/migrations/core.php @@ -216,5 +216,7 @@ function getAllMigrations() { //3.2.1 - 3.2.2 160 => new UpdateMigration('3.2.1', '3.2.0', 160), 161 => new UpdateMigration('3.2.2', '3.2.1', 161), + // 3.3.0 + 162 => new \v330\CreateServiceMessageToLocationTable(162), ); } \ No newline at end of file diff --git a/install/migrations/v330/CreateServiceMessageToLocationTable.php b/install/migrations/v330/CreateServiceMessageToLocationTable.php new file mode 100644 index 00000000..066ab39c --- /dev/null +++ b/install/migrations/v330/CreateServiceMessageToLocationTable.php @@ -0,0 +1,16 @@ +executeQuery("CREATE TABLE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "mfh_service_message_to_location` + (`service_message_id` INT NOT NULL, `location` VARCHAR(100) NOT NULL)"); + } + + function innerDown($hesk_settings) { + $this->executeQuery("DROP TABLE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "mfh_service_message_to_location`"); + } +} \ No newline at end of file