Merge branch '3-3-0' into '357-calendar-planning-phase-two'
# Conflicts: # api/BusinessLogic/Security/UserContext.php # api/BusinessLogic/Security/UserPrivilege.php # api/index.php # language/en/text.phpmaster
commit
f53065f7f6
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace BusinessLogic\ServiceMessages;
|
||||
|
||||
|
||||
class GetServiceMessagesFilter {
|
||||
/* @var $includeStaffServiceMessages bool */
|
||||
public $includeStaffServiceMessages = true;
|
||||
|
||||
/* @var $includeDrafts bool */
|
||||
public $includeDrafts = true;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace BusinessLogic\ServiceMessages;
|
||||
|
||||
|
||||
class ServiceMessage extends \BaseClass {
|
||||
/* @var $id int */
|
||||
public $id;
|
||||
|
||||
/* @var $dateCreated string */
|
||||
public $dateCreated;
|
||||
|
||||
/* @var $createdBy int */
|
||||
public $createdBy;
|
||||
|
||||
/* @var $title string */
|
||||
public $title;
|
||||
|
||||
/* @var $message string */
|
||||
public $message;
|
||||
|
||||
/* @var $style string */
|
||||
public $style;
|
||||
|
||||
/* @var $published bool */
|
||||
public $published;
|
||||
|
||||
/* @var $order int */
|
||||
public $order;
|
||||
|
||||
/* @var $icon string */
|
||||
public $icon;
|
||||
|
||||
/* @var $locations string[] */
|
||||
public $locations;
|
||||
|
||||
/* @var $language string */
|
||||
public $language;
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace BusinessLogic\ServiceMessages;
|
||||
|
||||
|
||||
// TODO Test
|
||||
use BusinessLogic\Exceptions\ValidationException;
|
||||
use BusinessLogic\Navigation\Direction;
|
||||
use BusinessLogic\ValidationModel;
|
||||
use DataAccess\ServiceMessages\ServiceMessagesGateway;
|
||||
|
||||
class ServiceMessageHandler extends \BaseClass {
|
||||
/* @var $serviceMessageGateway ServiceMessagesGateway */
|
||||
private $serviceMessageGateway;
|
||||
|
||||
function __construct(ServiceMessagesGateway $serviceMessagesGateway) {
|
||||
$this->serviceMessageGateway = $serviceMessagesGateway;
|
||||
}
|
||||
|
||||
function createServiceMessage($serviceMessage, $heskSettings) {
|
||||
$this->validate($serviceMessage, $heskSettings);
|
||||
|
||||
if ($serviceMessage->icon === null) {
|
||||
switch ($serviceMessage->style) {
|
||||
case ServiceMessageStyle::NONE:
|
||||
$serviceMessage->icon = '';
|
||||
break;
|
||||
case ServiceMessageStyle::INFO:
|
||||
$serviceMessage->icon = 'fa fa-comment';
|
||||
break;
|
||||
case ServiceMessageStyle::NOTICE:
|
||||
$serviceMessage->icon = 'fa fa-exclamation-triangle';
|
||||
break;
|
||||
case ServiceMessageStyle::ERROR:
|
||||
$serviceMessage->icon = 'fa fa-times-circle';
|
||||
break;
|
||||
case ServiceMessageStyle::SUCCESS:
|
||||
$serviceMessage->icon = 'fa fa-check-circle';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->serviceMessageGateway->createServiceMessage($serviceMessage, $heskSettings);
|
||||
}
|
||||
|
||||
function getServiceMessages($heskSettings, $searchFilter) {
|
||||
return $this->serviceMessageGateway->getServiceMessages($heskSettings, $searchFilter);
|
||||
}
|
||||
|
||||
function editServiceMessage($serviceMessage, $heskSettings) {
|
||||
$this->validate($serviceMessage, $heskSettings, false);
|
||||
|
||||
if ($serviceMessage->icon === null) {
|
||||
switch ($serviceMessage->style) {
|
||||
case ServiceMessageStyle::NONE:
|
||||
$serviceMessage->icon = '';
|
||||
break;
|
||||
case ServiceMessageStyle::INFO:
|
||||
$serviceMessage->icon = 'fa fa-comment';
|
||||
break;
|
||||
case ServiceMessageStyle::NOTICE:
|
||||
$serviceMessage->icon = 'fa fa-exclamation-triangle';
|
||||
break;
|
||||
case ServiceMessageStyle::ERROR:
|
||||
$serviceMessage->icon = 'fa fa-times-circle';
|
||||
break;
|
||||
case ServiceMessageStyle::SUCCESS:
|
||||
$serviceMessage->icon = 'fa fa-check-circle';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->serviceMessageGateway->updateServiceMessage($serviceMessage, $heskSettings);
|
||||
}
|
||||
|
||||
function deleteServiceMessage($id, $heskSettings) {
|
||||
$this->serviceMessageGateway->deleteServiceMessage($id, $heskSettings);
|
||||
}
|
||||
|
||||
function sortServiceMessage($id, $direction, $heskSettings) {
|
||||
$serviceMessages = $this->serviceMessageGateway->getServiceMessages($heskSettings, new GetServiceMessagesFilter());
|
||||
$serviceMessage = null;
|
||||
foreach ($serviceMessages as $innerServiceMessage) {
|
||||
if (intval($innerServiceMessage->id) === intval($id)) {
|
||||
$serviceMessage = $innerServiceMessage;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($serviceMessage === null) {
|
||||
throw new \BaseException("Could not find service message with ID {$id}!");
|
||||
}
|
||||
|
||||
if ($direction === Direction::UP) {
|
||||
$serviceMessage->order -= 15;
|
||||
} else {
|
||||
$serviceMessage->order += 15;
|
||||
}
|
||||
|
||||
$this->serviceMessageGateway->updateServiceMessage($serviceMessage, $heskSettings);
|
||||
$this->serviceMessageGateway->resortAllServiceMessages($heskSettings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $serviceMessage ServiceMessage
|
||||
* @param bool $isNew
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate($serviceMessage, $heskSettings, $isNew = true) {
|
||||
$validationModel = new ValidationModel();
|
||||
if ($isNew && $serviceMessage->createdBy < 1) {
|
||||
$validationModel->errorKeys[] = 'MISSING_CREATOR';
|
||||
}
|
||||
|
||||
if ($serviceMessage->message === null || trim($serviceMessage->message) === '') {
|
||||
$validationModel->errorKeys[] = 'MISSING_MESSAGE';
|
||||
} else {
|
||||
$htmlPurifier = new \HeskHTMLPurifier($heskSettings['cache_dir']);
|
||||
$serviceMessage->message = $htmlPurifier->heskPurify($serviceMessage->message);
|
||||
}
|
||||
if ($serviceMessage->language === null || trim($serviceMessage->language) === '') {
|
||||
$validationModel->errorKeys[] = 'MISSING_LANGUAGE';
|
||||
}
|
||||
|
||||
$languageFound = false;
|
||||
foreach ($heskSettings['languages'] as $key => $value) {
|
||||
if ($value['folder'] === $serviceMessage->language || $serviceMessage->language === 'ALL') {
|
||||
$languageFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$languageFound && !in_array('MISSING_LANGUAGE', $validationModel->errorKeys)) {
|
||||
$validationModel->errorKeys[] = 'LANGUAGE_NOT_INSTALLED';
|
||||
}
|
||||
|
||||
if ($serviceMessage->title === null || trim($serviceMessage->title) === '') {
|
||||
$validationModel->errorKeys[] = 'MISSING_TITLE';
|
||||
}
|
||||
if ($serviceMessage->style === null || trim($serviceMessage->style) === '') {
|
||||
$validationModel->errorKeys[] = 'MISSING_STYLE';
|
||||
}
|
||||
try {
|
||||
ServiceMessageStyle::getIdForStyle($serviceMessage->style);
|
||||
} 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
|
||||
throw new ValidationException($validationModel);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace BusinessLogic\ServiceMessages;
|
||||
|
||||
|
||||
class ServiceMessageLocation {
|
||||
const CUSTOMER_HOME = 'CUSTOMER_HOME';
|
||||
const CUSTOMER_KB_HOME = 'CUSTOMER_KB_HOME';
|
||||
const CUSTOMER_VIEW_KB_ARTICLE = 'CUSTOMER_VIEW_KB_ARTICLE';
|
||||
const CUSTOMER_SUBMIT_TICKET = 'CUSTOMER_SUBMIT_TICKET';
|
||||
const CUSTOMER_VIEW_TICKET = 'CUSTOMER_VIEW_TICKET';
|
||||
const STAFF_LOGIN = 'STAFF_LOGIN';
|
||||
const STAFF_HOME = 'STAFF_HOME';
|
||||
const STAFF_KB_HOME = 'STAFF_KB_HOME';
|
||||
const STAFF_VIEW_KB_ARTICLE = 'STAFF_VIEW_KB_ARTICLE';
|
||||
const STAFF_SUBMIT_TICKET = 'STAFF_SUBMIT_TICKET';
|
||||
const STAFF_VIEW_TICKET = 'STAFF_VIEW_TICKET';
|
||||
|
||||
static function getAll() {
|
||||
return array(
|
||||
self::CUSTOMER_HOME,
|
||||
self::CUSTOMER_KB_HOME,
|
||||
self::CUSTOMER_VIEW_KB_ARTICLE,
|
||||
self::CUSTOMER_SUBMIT_TICKET,
|
||||
self::CUSTOMER_VIEW_TICKET,
|
||||
self::STAFF_LOGIN,
|
||||
self::STAFF_HOME,
|
||||
self::STAFF_KB_HOME,
|
||||
self::STAFF_VIEW_KB_ARTICLE,
|
||||
self::STAFF_SUBMIT_TICKET,
|
||||
self::STAFF_VIEW_TICKET,
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace BusinessLogic\ServiceMessages;
|
||||
|
||||
|
||||
class ServiceMessageStyle {
|
||||
const NONE = 'NONE'; // 0
|
||||
const SUCCESS = 'SUCCESS'; // 1
|
||||
const INFO = 'INFO'; // 2
|
||||
const NOTICE = 'NOTICE'; // 3
|
||||
const ERROR = 'ERROR'; // 4
|
||||
|
||||
static function getStyleById($id) {
|
||||
$styles = array(
|
||||
0 => self::NONE,
|
||||
1 => self::SUCCESS,
|
||||
2 => self::INFO,
|
||||
3 => self::NOTICE,
|
||||
4 => self::ERROR
|
||||
);
|
||||
|
||||
if (!isset($styles[$id])) {
|
||||
throw new \Exception("Style {$id} is not a valid service message style.");
|
||||
}
|
||||
|
||||
return $styles[$id];
|
||||
}
|
||||
|
||||
static function getIdForStyle($style) {
|
||||
$styles = array(
|
||||
self::NONE => 0,
|
||||
self::SUCCESS => 1,
|
||||
self::INFO => 2,
|
||||
self::NOTICE => 3,
|
||||
self::ERROR => 4
|
||||
);
|
||||
|
||||
if (!isset($styles[$style])) {
|
||||
throw new \Exception("Style {$style} is not a valid service message style.");
|
||||
}
|
||||
|
||||
return $styles[$style];
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Controllers\ServiceMessages;
|
||||
|
||||
use BusinessLogic\Exceptions\ApiFriendlyException;
|
||||
use BusinessLogic\Helpers;
|
||||
use BusinessLogic\Security\UserContext;
|
||||
use BusinessLogic\Security\UserPrivilege;
|
||||
use BusinessLogic\ServiceMessages\GetServiceMessagesFilter;
|
||||
use BusinessLogic\ServiceMessages\ServiceMessage;
|
||||
use BusinessLogic\ServiceMessages\ServiceMessageHandler;
|
||||
use Controllers\ControllerWithSecurity;
|
||||
use Controllers\JsonRetriever;
|
||||
|
||||
class ServiceMessagesController extends \BaseClass {
|
||||
/**
|
||||
* @param $userContext UserContext
|
||||
* @throws ApiFriendlyException
|
||||
*/
|
||||
function checkSecurity($userContext) {
|
||||
if (!$userContext->admin && !in_array(UserPrivilege::CAN_MANAGE_SERVICE_MESSAGES, $userContext->permissions)) {
|
||||
throw new ApiFriendlyException("User does not have permission to access the following URI: " . $_SERVER['REQUEST_URI'], "Access Forbidden", 403);
|
||||
}
|
||||
}
|
||||
|
||||
static function staticCheckSecurity($userContext) {
|
||||
if (!$userContext->admin && !in_array(UserPrivilege::CAN_MANAGE_SERVICE_MESSAGES, $userContext->permissions)) {
|
||||
throw new ApiFriendlyException("User does not have permission to access the following URI: " . $_SERVER['REQUEST_URI'], "Access Forbidden", 403);
|
||||
}
|
||||
}
|
||||
|
||||
function get() {
|
||||
/* @var $userContext UserContext */
|
||||
/* @var $hesk_settings array */
|
||||
global $applicationContext, $hesk_settings, $userContext;
|
||||
|
||||
$searchFilter = new GetServiceMessagesFilter();
|
||||
if ($userContext->isAnonymousUser()) {
|
||||
$searchFilter->includeDrafts = false;
|
||||
$searchFilter->includeStaffServiceMessages = false;
|
||||
} elseif (!$userContext->admin && !in_array(UserPrivilege::CAN_MANAGE_SERVICE_MESSAGES, $userContext->permissions)) {
|
||||
$searchFilter->includeDrafts = false;
|
||||
}
|
||||
|
||||
/* @var $handler ServiceMessageHandler */
|
||||
$handler = $applicationContext->get(ServiceMessageHandler::clazz());
|
||||
|
||||
return output($handler->getServiceMessages($hesk_settings, $searchFilter));
|
||||
}
|
||||
|
||||
function post() {
|
||||
global $applicationContext, $userContext, $hesk_settings;
|
||||
|
||||
$this->checkSecurity($userContext);
|
||||
|
||||
/* @var $handler ServiceMessageHandler */
|
||||
$handler = $applicationContext->get(ServiceMessageHandler::clazz());
|
||||
|
||||
$data = JsonRetriever::getJsonData();
|
||||
$element = $handler->createServiceMessage($this->buildElementModel($data, $userContext), $hesk_settings);
|
||||
|
||||
return output($element, 201);
|
||||
}
|
||||
|
||||
function put($id) {
|
||||
global $applicationContext, $hesk_settings, $userContext;
|
||||
|
||||
$this->checkSecurity($userContext);
|
||||
|
||||
/* @var $handler ServiceMessageHandler */
|
||||
$handler = $applicationContext->get(ServiceMessageHandler::clazz());
|
||||
|
||||
$data = JsonRetriever::getJsonData();
|
||||
$serviceMessage = $this->buildElementModel($data, null, false);
|
||||
$serviceMessage->id = $id;
|
||||
$element = $handler->editServiceMessage($serviceMessage, $hesk_settings);
|
||||
|
||||
return output($element);
|
||||
}
|
||||
|
||||
function delete($id) {
|
||||
global $applicationContext, $hesk_settings, $userContext;
|
||||
|
||||
$this->checkSecurity($userContext);
|
||||
|
||||
/* @var $handler ServiceMessageHandler */
|
||||
$handler = $applicationContext->get(ServiceMessageHandler::clazz());
|
||||
|
||||
$handler->deleteServiceMessage($id, $hesk_settings);
|
||||
|
||||
return http_response_code(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data array
|
||||
* @param $userContext UserContext
|
||||
* @return ServiceMessage
|
||||
*/
|
||||
private function buildElementModel($data, $userContext, $creating = true) {
|
||||
$serviceMessage = new ServiceMessage();
|
||||
|
||||
if (!$creating) {
|
||||
$serviceMessage->order = Helpers::safeArrayGet($data, 'order');
|
||||
}
|
||||
|
||||
if ($creating) {
|
||||
$serviceMessage->createdBy = $userContext->id;
|
||||
}
|
||||
|
||||
$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');
|
||||
$serviceMessage->language = Helpers::safeArrayGet($data, 'language');
|
||||
|
||||
$jsonLocations = Helpers::safeArrayGet($data, 'locations');
|
||||
|
||||
if ($jsonLocations !== null && !empty($jsonLocations)) {
|
||||
foreach ($jsonLocations as $key => $value) {
|
||||
$serviceMessage->locations[] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $serviceMessage;
|
||||
}
|
||||
|
||||
static function sort($id, $direction) {
|
||||
/* @var $userContext UserContext */
|
||||
global $applicationContext, $hesk_settings, $userContext;
|
||||
|
||||
self::staticCheckSecurity($userContext);
|
||||
|
||||
/* @var $handler ServiceMessageHandler */
|
||||
$handler = $applicationContext->get(ServiceMessageHandler::clazz());
|
||||
|
||||
$handler->sortServiceMessage(intval($id), $direction, $hesk_settings);
|
||||
}
|
||||
}
|
@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace DataAccess\ServiceMessages;
|
||||
|
||||
|
||||
use BusinessLogic\ServiceMessages\GetServiceMessagesFilter;
|
||||
use BusinessLogic\ServiceMessages\ServiceMessage;
|
||||
use BusinessLogic\ServiceMessages\ServiceMessageLocation;
|
||||
use BusinessLogic\ServiceMessages\ServiceMessageStyle;
|
||||
use DataAccess\CommonDao;
|
||||
|
||||
class ServiceMessagesGateway extends CommonDao {
|
||||
/**
|
||||
* @param $serviceMessage ServiceMessage
|
||||
* @return ServiceMessage
|
||||
*/
|
||||
function createServiceMessage($serviceMessage, $heskSettings) {
|
||||
$this->init();
|
||||
|
||||
// Get the latest service message order
|
||||
$res = hesk_dbQuery("SELECT `order` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` ORDER BY `order` DESC LIMIT 1");
|
||||
$row = hesk_dbFetchRow($res);
|
||||
$myOrder = intval($row[0]) + 10;
|
||||
|
||||
$style = ServiceMessageStyle::getIdForStyle($serviceMessage->style);
|
||||
$type = $serviceMessage->published ? 0 : 1;
|
||||
|
||||
// Insert service message into database
|
||||
hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` (`author`,`title`,`message`,`style`,`type`,`order`, `icon`, `mfh_language`) VALUES (
|
||||
'" . intval($serviceMessage->createdBy) . "',
|
||||
'" . hesk_dbEscape($serviceMessage->title) . "',
|
||||
'" . hesk_dbEscape($serviceMessage->message) . "',
|
||||
'" . hesk_dbEscape($style) . "',
|
||||
'{$type}',
|
||||
'{$myOrder}',
|
||||
'" . hesk_dbEscape($serviceMessage->icon) . "',
|
||||
'" . hesk_dbEscape($serviceMessage->language) . "'
|
||||
)");
|
||||
|
||||
$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));
|
||||
$row = hesk_dbFetchAssoc($rs);
|
||||
$serviceMessage->dateCreated = $row['dt'];
|
||||
$serviceMessage->order = intval($row['order']);
|
||||
|
||||
$this->close();
|
||||
|
||||
return $serviceMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $heskSettings
|
||||
* @param $searchFilter GetServiceMessagesFilter
|
||||
* @return ServiceMessage[]
|
||||
*/
|
||||
function getServiceMessages($heskSettings, $searchFilter) {
|
||||
$this->init();
|
||||
|
||||
$serviceMessages = array();
|
||||
|
||||
$sql = "SELECT DISTINCT `service_messages`.* FROM `". hesk_dbEscape($heskSettings['db_pfix']) . "service_messages` AS `service_messages` ";
|
||||
|
||||
if (!$searchFilter->includeStaffServiceMessages) {
|
||||
$sql .= "INNER JOIN `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location` AS `location`
|
||||
ON `location`.`service_message_id` = `service_messages`.`id` AND `location`.`location` LIKE 'CUSTOMER%' ";
|
||||
}
|
||||
|
||||
if (!$searchFilter->includeDrafts) {
|
||||
$sql .= "WHERE `type` = '0' ";
|
||||
}
|
||||
|
||||
$sql .= "ORDER BY `order`";
|
||||
|
||||
|
||||
$rs = hesk_dbQuery($sql);
|
||||
while ($row = hesk_dbFetchAssoc($rs)) {
|
||||
$serviceMessage = new ServiceMessage();
|
||||
$serviceMessage->id = $row['id'];
|
||||
$serviceMessage->published = intval($row['type']) !== 1;
|
||||
$serviceMessage->createdBy = intval($row['author']);
|
||||
$serviceMessage->order = intval($row['order']);
|
||||
$serviceMessage->dateCreated = $row['dt'];
|
||||
$serviceMessage->title = $row['title'];
|
||||
$serviceMessage->message = $row['message'];
|
||||
$serviceMessage->style = ServiceMessageStyle::getStyleById($row['style']);
|
||||
$serviceMessage->icon = $row['icon'];
|
||||
$serviceMessage->language = $row['mfh_language'];
|
||||
$serviceMessage->locations = array();
|
||||
|
||||
$locationSql = "SELECT `location` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "mfh_service_message_to_location`
|
||||
WHERE `service_message_id` = " . intval($serviceMessage->id);
|
||||
|
||||
if (!$searchFilter->includeStaffServiceMessages) {
|
||||
$locationSql .= " AND `location` LIKE 'CUSTOMER%'";
|
||||
}
|
||||
|
||||
$locationsRs = hesk_dbQuery($locationSql);
|
||||
while ($innerRow = hesk_dbFetchAssoc($locationsRs)) {
|
||||
$serviceMessage->locations[] = $innerRow['location'];
|
||||
}
|
||||
|
||||
$serviceMessages[] = $serviceMessage;
|
||||
}
|
||||
|
||||
$this->close();
|
||||
|
||||
return $serviceMessages;
|
||||
}
|
||||
|
||||
function updateServiceMessage($serviceMessage, $heskSettings) {
|
||||
$this->init();
|
||||
|
||||
$style = ServiceMessageStyle::getIdForStyle($serviceMessage->style);
|
||||
$type = $serviceMessage->published ? 0 : 1;
|
||||
|
||||
hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages`
|
||||
SET `title` = '" . hesk_dbEscape($serviceMessage->title) . "',
|
||||
`message` = '" . hesk_dbEscape($serviceMessage->message) . "',
|
||||
`style` = '" . intval($style) . "',
|
||||
`type` = '{$type}',
|
||||
`icon` = '" . hesk_dbEscape($serviceMessage->icon) . "',
|
||||
`order` = " . intval($serviceMessage->order) . ",
|
||||
`mfh_language` = '" . hesk_dbEscape($serviceMessage->language) . "'
|
||||
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);
|
||||
|
||||
$serviceMessage->createdBy = intval($otherFields['author']);
|
||||
$serviceMessage->dateCreated = $otherFields['dt'];
|
||||
|
||||
$this->close();
|
||||
|
||||
return $serviceMessage;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
$this->close();
|
||||
}
|
||||
|
||||
function resortAllServiceMessages($heskSettings) {
|
||||
$this->init();
|
||||
|
||||
$rs = hesk_dbQuery("SELECT `id` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages`
|
||||
ORDER BY `order` ASC");
|
||||
|
||||
$sortValue = 10;
|
||||
while ($row = hesk_dbFetchAssoc($rs)) {
|
||||
hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "service_messages`
|
||||
SET `order` = " . intval($sortValue) . "
|
||||
WHERE `id` = " . intval($row['id']));
|
||||
|
||||
$sortValue += 10;
|
||||
}
|
||||
|
||||
$this->close();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace v330\ServiceMessagesImprovements;
|
||||
|
||||
|
||||
class AddLanguageColumnToServiceMessages extends \AbstractUpdatableMigration {
|
||||
|
||||
function innerUp($hesk_settings) {
|
||||
$this->executeQuery("ALTER TABLE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages`
|
||||
ADD COLUMN `mfh_language` VARCHAR(255) NOT NULL DEFAULT 'ALL'");
|
||||
}
|
||||
|
||||
function innerDown($hesk_settings) {
|
||||
$this->executeQuery("ALTER TABLE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages`
|
||||
DROP COLUMN `mfh_language`");
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace v330\ServiceMessagesImprovements;
|
||||
|
||||
|
||||
class CreateServiceMessageToLocationTable extends \AbstractUpdatableMigration {
|
||||
|
||||
function innerUp($hesk_settings) {
|
||||
$this->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`");
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace v330\ServiceMessagesImprovements;
|
||||
|
||||
|
||||
use BusinessLogic\ServiceMessages\ServiceMessageLocation;
|
||||
|
||||
class UpdateExistingServiceMessagesLocations extends \AbstractUpdatableMigration {
|
||||
|
||||
function innerUp($hesk_settings) {
|
||||
$this->executeQuery("INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "mfh_service_message_to_location` (`service_message_id`, `location`)
|
||||
SELECT `id`, '" . hesk_dbEscape(ServiceMessageLocation::CUSTOMER_HOME) . "' FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages`");
|
||||
}
|
||||
|
||||
function innerDown($hesk_settings) {
|
||||
$this->executeQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "mfh_service_message_to_location`
|
||||
WHERE `service_message_id` IN (SELECT `id` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "service_messages`)");
|
||||
}
|
||||
}
|
@ -0,0 +1,361 @@
|
||||
var serviceMessages = [];
|
||||
|
||||
var g_styles = [];
|
||||
g_styles["ERROR"] = 4;
|
||||
g_styles["NOTICE"] = 3;
|
||||
g_styles["INFO"] = 2;
|
||||
g_styles["SUCCESS"] = 1;
|
||||
g_styles["NONE"] = 0;
|
||||
|
||||
$(document).ready(function() {
|
||||
loadTable();
|
||||
bindEditModal();
|
||||
bindFormSubmit();
|
||||
bindDeleteButton();
|
||||
bindCreateModal();
|
||||
bindSortButtons();
|
||||
bindPreview();
|
||||
});
|
||||
|
||||
|
||||
function loadTable() {
|
||||
$('#overlay').show();
|
||||
var heskUrl = $('p#hesk-path').text();
|
||||
var $tableBody = $('#table-body');
|
||||
|
||||
$.ajax({
|
||||
method: 'GET',
|
||||
url: heskUrl + 'api/index.php/v1/service-messages',
|
||||
headers: { 'X-Internal-Call': true },
|
||||
success: function(data) {
|
||||
$tableBody.html('');
|
||||
|
||||
if (data.length === 0) {
|
||||
$tableBody.append('<tr><td colspan="4">' + mfhLang.text('no_sm') + '</td></tr>');
|
||||
$('#overlay').hide();
|
||||
return;
|
||||
}
|
||||
|
||||
var first = true;
|
||||
var lastElement = null;
|
||||
$.each(data, function() {
|
||||
var $template = $($('#service-message-template').html());
|
||||
|
||||
$template.find('[data-property="id"]').attr('data-value', this.id);
|
||||
$template.find('span[data-property="title"]').html(
|
||||
getFormattedTitle(this.icon, this.title, this.style));
|
||||
$template.find('span[data-property="author"]').text(users[this.createdBy].name);
|
||||
if (this.published) {
|
||||
$template.find('span[data-property="type"]').text(mfhLang.text('sm_published'));
|
||||
} else {
|
||||
$template.find('span[data-property="type"]').text(mfhLang.text('sm_draft'));
|
||||
}
|
||||
$template.find('[data-property="language"]').text(this.language === 'ALL' ?
|
||||
mfhLang.text('all') :
|
||||
languages[this.language]);
|
||||
|
||||
$tableBody.append($template);
|
||||
|
||||
serviceMessages[this.id] = this;
|
||||
|
||||
lastElement = this;
|
||||
|
||||
if (first) {
|
||||
$template.find('[data-direction="up"]').css('visibility', 'hidden');
|
||||
first = false;
|
||||
}
|
||||
});
|
||||
|
||||
if (lastElement) {
|
||||
//-- Hide the down arrow on the last element
|
||||
$('[data-value="' + lastElement.id + '"]').parent().parent()
|
||||
.find('[data-direction="down"]').css('visibility', 'hidden');
|
||||
}
|
||||
},
|
||||
error: function(data) {
|
||||
mfhAlert.errorWithLog(mfhLang.text('error_retrieving_sm'), data.responseJSON);
|
||||
console.error(data);
|
||||
},
|
||||
complete: function() {
|
||||
$('#overlay').hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getFormattedTitle(icon, title, style) {
|
||||
var $template = $($('#service-message-title-template').html());
|
||||
|
||||
var alertClass = 'none';
|
||||
switch (style) {
|
||||
case 'ERROR':
|
||||
alertClass = 'alert alert-danger';
|
||||
break;
|
||||
case 'NOTICE':
|
||||
alertClass = 'alert alert-warning';
|
||||
break;
|
||||
case 'INFO':
|
||||
alertClass = 'alert alert-info';
|
||||
break;
|
||||
case 'SUCCESS':
|
||||
alertClass = 'alert alert-success';
|
||||
break;
|
||||
}
|
||||
$template.addClass(alertClass)
|
||||
.find('[data-property="icon"]').addClass(icon).end()
|
||||
.find('[data-property="title"]').text(title);
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
function getServiceMessagePreview(icon, title, message, style) {
|
||||
var $template = $('#service-message-preview-template').html();
|
||||
|
||||
var alertClass = 'none';
|
||||
switch (style) {
|
||||
case 'ERROR':
|
||||
alertClass = 'alert alert-danger';
|
||||
break;
|
||||
case 'NOTICE':
|
||||
alertClass = 'alert alert-warning';
|
||||
break;
|
||||
case 'INFO':
|
||||
alertClass = 'alert alert-info';
|
||||
break;
|
||||
case 'SUCCESS':
|
||||
alertClass = 'alert alert-success';
|
||||
break;
|
||||
}
|
||||
$template = $template.replace('none', alertClass)
|
||||
.replace('{{TITLE}}', title)
|
||||
.replace('{{MESSAGE}}', message);
|
||||
$template = $($template);
|
||||
if (icon !== '') {
|
||||
$template.find('i.fa').removeClass('fa').addClass(icon);
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
function bindEditModal() {
|
||||
$(document).on('click', '[data-action="edit"]', function() {
|
||||
var element = serviceMessages[$(this).parent().parent().find('[data-property="id"]').data('value')];
|
||||
var $modal = $('#service-message-modal');
|
||||
$modal.find('#preview-pane').html('').end()
|
||||
.find('input[name="location[]"]').prop('checked', false);
|
||||
|
||||
$modal.find('#edit-label').show();
|
||||
$modal.find('#create-label').hide();
|
||||
|
||||
$modal.find('input[name="style"][value="' + (g_styles[element.style]) + '"]').prop('checked', 'checked').end()
|
||||
.find('input[name="type"][value="' + (element.published ? 0 : 1) + '"]')
|
||||
.prop('checked', 'checked').end()
|
||||
.find('input[name="title"]').val(element.title).end()
|
||||
.find('input[name="id"]').val(element.id).end()
|
||||
.find('input[name="order"]').val(element.order).end()
|
||||
.find('select[name="language"]').val(element.language).end();
|
||||
setIcon(element.icon);
|
||||
|
||||
$.each(element.locations, function() {
|
||||
$modal.find('input[name="location[]"][value="' + this + '"]').prop('checked', 'checked');
|
||||
});
|
||||
|
||||
if ($('input[name="kb_wysiwyg"]').val() === "1") {
|
||||
tinyMCE.get('content').setContent(element.message);
|
||||
} else {
|
||||
$('textarea[name="message"]').val(element.message);
|
||||
}
|
||||
|
||||
$('.tab-pane#sm-contents').addClass('active');
|
||||
$('.tab-pane#properties').removeClass('active');
|
||||
$('.nav-tabs > li').removeClass('active');
|
||||
$('.nav-tabs > li:first').addClass('active');
|
||||
|
||||
|
||||
$modal.modal('show');
|
||||
});
|
||||
}
|
||||
|
||||
function bindCreateModal() {
|
||||
$('#create-button').click(function() {
|
||||
var $modal = $('#service-message-modal');
|
||||
$modal.find('#edit-label').hide().end()
|
||||
.find('#create-label').show().end()
|
||||
.find('input[name="style"][value="0"]').prop('checked', 'checked').end() // "None" style
|
||||
.find('input[name="type"][value="0"]').prop('checked', 'checked').end() // Published
|
||||
.find('input[name="title"]').val('').end()
|
||||
.find('input[name="id"]').val(-1).end()
|
||||
.find('input[name="order"]').val('').end()
|
||||
.find('#preview-pane').html('').end()
|
||||
.find('input[name="location[]"]').prop('checked', false)
|
||||
.find('select[name="language"]').val('ALL');
|
||||
setIcon('');
|
||||
|
||||
if ($('input[name="kb_wysiwyg"]').val() === "1") {
|
||||
tinyMCE.get('content').setContent('');
|
||||
} else {
|
||||
$('textarea[name="message"]').val('');
|
||||
}
|
||||
|
||||
$('.tab-pane#sm-contents').addClass('active');
|
||||
$('.tab-pane#properties').removeClass('active');
|
||||
$('.nav-tabs > li').removeClass('active');
|
||||
$('.nav-tabs > li:first').addClass('active');
|
||||
|
||||
$modal.modal('show');
|
||||
});
|
||||
}
|
||||
|
||||
function bindFormSubmit() {
|
||||
$('form#service-message').submit(function(e) {
|
||||
e.preventDefault();
|
||||
var heskUrl = $('p#hesk-path').text();
|
||||
|
||||
var $modal = $('#service-message-modal');
|
||||
|
||||
var styles = [];
|
||||
styles[0] = "NONE";
|
||||
styles[1] = "SUCCESS";
|
||||
styles[2] = "INFO";
|
||||
styles[3] = "NOTICE";
|
||||
styles[4] = "ERROR";
|
||||
|
||||
var domLocations = $modal.find('input[name="location[]"]:checked');
|
||||
|
||||
var locations = [];
|
||||
$.each(domLocations, function() {
|
||||
locations.push($(this).val());
|
||||
});
|
||||
|
||||
var data = {
|
||||
icon: $modal.find('input[name="icon"]').val(),
|
||||
title: $modal.find('input[name="title"]').val(),
|
||||
message: getMessage(),
|
||||
published: $modal.find('input[name="type"]:checked').val() === "0",
|
||||
style: styles[$modal.find('input[name="style"]:checked').val()],
|
||||
order: $modal.find('input[name="order"]').val(),
|
||||
language: $modal.find('select[name="language"]').val(),
|
||||
locations: locations
|
||||
};
|
||||
|
||||
var url = heskUrl + 'api/index.php/v1/service-messages/';
|
||||
var method = 'POST';
|
||||
|
||||
var serviceMessageId = parseInt($modal.find('input[name="id"]').val());
|
||||
if (serviceMessageId !== -1) {
|
||||
url += serviceMessageId;
|
||||
method = 'PUT';
|
||||
}
|
||||
|
||||
$modal.find('#action-buttons').find('.cancel-button').attr('disabled', 'disabled');
|
||||
$modal.find('#action-buttons').find('.save-button').attr('disabled', 'disabled');
|
||||
|
||||
$.ajax({
|
||||
method: 'POST',
|
||||
url: url,
|
||||
headers: {
|
||||
'X-Internal-Call': true,
|
||||
'X-HTTP-Method-Override': method
|
||||
},
|
||||
data: JSON.stringify(data),
|
||||
success: function(data) {
|
||||
if (serviceMessageId === -1) {
|
||||
mfhAlert.success(mfhLang.text('sm_added'));
|
||||
} else {
|
||||
mfhAlert.success(mfhLang.text('sm_mdf'));
|
||||
}
|
||||
$modal.modal('hide');
|
||||
loadTable();
|
||||
},
|
||||
error: function(data) {
|
||||
mfhAlert.errorWithLog(mfhLang.text('error_saving_updating_sm'), data.responseJSON);
|
||||
console.error(data);
|
||||
},
|
||||
complete: function(data) {
|
||||
$modal.find('#action-buttons').find('.cancel-button').removeAttr('disabled');
|
||||
$modal.find('#action-buttons').find('.save-button').removeAttr('disabled');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bindDeleteButton() {
|
||||
$(document).on('click', '[data-action="delete"]', function() {
|
||||
$('#overlay').show();
|
||||
|
||||
var heskUrl = $('p#hesk-path').text();
|
||||
var element = serviceMessages[$(this).parent().parent().find('[data-property="id"]').data('value')];
|
||||
|
||||
$.ajax({
|
||||
method: 'POST',
|
||||
url: heskUrl + 'api/index.php/v1/service-messages/' + element.id,
|
||||
headers: {
|
||||
'X-Internal-Call': true,
|
||||
'X-HTTP-Method-Override': 'DELETE'
|
||||
},
|
||||
success: function() {
|
||||
mfhAlert.success(mfhLang.text('sm_deleted'));
|
||||
loadTable();
|
||||
},
|
||||
error: function(data) {
|
||||
$('#overlay').hide();
|
||||
mfhAlert.errorWithLog(mfhLang.text('error_deleting_sm'), data.responseJSON);
|
||||
console.error(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bindSortButtons() {
|
||||
$(document).on('click', '[data-action="sort"]', function() {
|
||||
$('#overlay').show();
|
||||
var heskUrl = $('p#hesk-path').text();
|
||||
var direction = $(this).data('direction');
|
||||
var element = serviceMessages[$(this).parent().parent().parent().find('[data-property="id"]').data('value')];
|
||||
|
||||
$.ajax({
|
||||
method: 'POST',
|
||||
url: heskUrl + 'api/index.php/v1-internal/service-messages/' + element.id + '/sort/' + direction,
|
||||
headers: { 'X-Internal-Call': true },
|
||||
success: function() {
|
||||
loadTable();
|
||||
},
|
||||
error: function(data) {
|
||||
mfhAlert.errorWithLog(mfhLang.text('error_sorting_categories'), data.responseJSON);
|
||||
console.error(data);
|
||||
$('#overlay').hide();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function bindPreview() {
|
||||
$('.preview-button').click(function() {
|
||||
var styles = [];
|
||||
styles[0] = "NONE";
|
||||
styles[1] = "SUCCESS";
|
||||
styles[2] = "INFO";
|
||||
styles[3] = "NOTICE";
|
||||
styles[4] = "ERROR";
|
||||
|
||||
var $modal = $('#service-message-modal');
|
||||
var data = {
|
||||
icon: $modal.find('input[name="icon"]').val(),
|
||||
title: $modal.find('input[name="title"]').val(),
|
||||
message: getMessage(),
|
||||
published: $modal.find('input[name="type"]:checked').val() === "0",
|
||||
style: styles[$modal.find('input[name="style"]:checked').val()],
|
||||
order: $modal.find('input[name="order"]').val()
|
||||
};
|
||||
|
||||
var preview = getServiceMessagePreview(data.icon, data.title, data.message, data.style);
|
||||
$('#preview-pane').html(preview);
|
||||
});
|
||||
}
|
||||
|
||||
function getMessage() {
|
||||
if ($('input[name="kb_wysiwyg"]').val() === "1") {
|
||||
return tinyMCE.get('content').getContent();
|
||||
}
|
||||
|
||||
return $('textarea[name="message"]').val();
|
||||
}
|
Loading…
Reference in New Issue