diff --git a/api/BusinessLogic/Calendar/CalendarHandler.php b/api/BusinessLogic/Calendar/CalendarHandler.php index f3374279..7f327997 100644 --- a/api/BusinessLogic/Calendar/CalendarHandler.php +++ b/api/BusinessLogic/Calendar/CalendarHandler.php @@ -15,4 +15,8 @@ class CalendarHandler extends \BaseClass { public function getEventsForStaff($startTime, $endTime, $searchEventsFilter, $heskSettings) { return $this->calendarGateway->getEventsForStaff($startTime, $endTime, $searchEventsFilter, $heskSettings); } + + public function updateEvent($calendarEvent, $userContext, $heskSettings) { + $this->calendarGateway->updateEvent($calendarEvent, $userContext, $heskSettings); + } } \ No newline at end of file diff --git a/api/BusinessLogic/Calendar/ReminderUnit.php b/api/BusinessLogic/Calendar/ReminderUnit.php index 866ad8e9..e45b2169 100644 --- a/api/BusinessLogic/Calendar/ReminderUnit.php +++ b/api/BusinessLogic/Calendar/ReminderUnit.php @@ -23,4 +23,19 @@ class ReminderUnit { return 'UNKNOWN'; } } + + static function getByName($name) { + switch ($name) { + case 'MINUTE': + return self::MINUTE; + case 'HOUR': + return self::HOUR; + case 'DAY': + return self::DAY; + case 'WEEK': + return self::WEEK; + default: + return null; + } + } } \ No newline at end of file diff --git a/api/Controllers/Calendar/CalendarController.php b/api/Controllers/Calendar/CalendarController.php index 6a9a6861..1a180fc1 100644 --- a/api/Controllers/Calendar/CalendarController.php +++ b/api/Controllers/Calendar/CalendarController.php @@ -3,12 +3,16 @@ namespace Controllers\Calendar; +use BusinessLogic\Calendar\CalendarEvent; use BusinessLogic\Calendar\CalendarHandler; +use BusinessLogic\Calendar\ReminderUnit; use BusinessLogic\Calendar\SearchEventsFilter; use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\Helpers; use BusinessLogic\Security\UserContext; use BusinessLogic\Security\UserPrivilege; use BusinessLogic\ValidationModel; +use Controllers\JsonRetriever; class CalendarController extends \BaseClass { function get() { @@ -38,4 +42,36 @@ class CalendarController extends \BaseClass { return output($events); } + + function put($id) { + /* @var $userContext UserContext */ + global $applicationContext, $hesk_settings, $userContext; + + $json = JsonRetriever::getJsonData(); + + $event = $this->transformJson($json); + + /* @var $calendarHandler CalendarHandler */ + $calendarHandler = $applicationContext->get(CalendarHandler::clazz()); + + return output($calendarHandler->updateEvent($event, $userContext, $hesk_settings)); + } + + private function transformJson($json, $creating = false) { + $event = new CalendarEvent(); + + if ($creating) { + $event->id = Helpers::safeArrayGet($json, 'id'); + } + + $event->startTime = date('Y-m-d H:i:s', Helpers::safeArrayGet($json, 'startTime')); + $event->endTime = date('Y-m-d H:i:s', Helpers::safeArrayGet($json, 'endTime')); + $event->allDay = Helpers::safeArrayGet($json, 'allDay') === 'true'; + $event->title = Helpers::safeArrayGet($json, 'title'); + $event->location = Helpers::safeArrayGet($json, 'location'); + $event->comments = Helpers::safeArrayGet($json, 'comments'); + $event->categoryId = Helpers::safeArrayGet($json, 'categoryId'); + $event->reminderValue = Helpers::safeArrayGet($json, 'reminderValue'); + $event->reminderUnits = ReminderUnit::getByName(Helpers::safeArrayGet($json, 'reminderUnits')); + } } \ No newline at end of file diff --git a/api/DataAccess/Calendar/CalendarGateway.php b/api/DataAccess/Calendar/CalendarGateway.php index 558e4113..9367dbb7 100644 --- a/api/DataAccess/Calendar/CalendarGateway.php +++ b/api/DataAccess/Calendar/CalendarGateway.php @@ -8,6 +8,7 @@ use BusinessLogic\Calendar\CalendarEvent; use BusinessLogic\Calendar\ReminderUnit; use BusinessLogic\Calendar\SearchEventsFilter; use BusinessLogic\Calendar\TicketEvent; +use BusinessLogic\Security\UserContext; use Core\Constants\Priority; use DataAccess\CommonDao; @@ -128,4 +129,32 @@ class CalendarGateway extends CommonDao { return $events; } + + /** + * @param $event CalendarEvent + * @param $userContext UserContext + * @param $heskSettings array + */ + public function updateEvent($event, $userContext, $heskSettings) { + $this->init(); + + $sql = "UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "calendar_event` SET `start` = '" . hesk_dbEscape($event->startTime) + . "', `end` = '" . hesk_dbEscape($event->endTime) . "', `all_day` = '" . ($event->allDay ? 1 : 0) . "', `name` = '" + . hesk_dbEscape(addslashes($event->title)) . "', `location` = '" . hesk_dbEscape(addslashes($event->location)) . "', `comments` = '" + . hesk_dbEscape(addslashes($event->comments)) . "', `category` = " . intval($event->categoryId) . " WHERE `id` = " . intval($event->id); + + if ($event->reminderValue != null) { + $delete_sql = "DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "calendar_event_reminder` WHERE `event_id` = " . intval($event->id) + . " AND `user_id` = " . intval($userContext->id); + hesk_dbQuery($delete_sql); + $insert_sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "calendar_event_reminder` (`user_id`, `event_id`, + `amount`, `unit`) VALUES (" . intval($userContext->id) . ", " . intval($event->id) . ", " . intval($event->reminderValue) . ", + " . intval($event->reminderUnits) . ")"; + hesk_dbQuery($insert_sql); + } + + hesk_dbQuery($sql); + + $this->close(); + } } \ No newline at end of file diff --git a/internal-api/dao/calendar_dao.php b/internal-api/dao/calendar_dao.php index a3f15d98..3ad28dbf 100644 --- a/internal-api/dao/calendar_dao.php +++ b/internal-api/dao/calendar_dao.php @@ -154,30 +154,7 @@ function update_event($event, $hesk_settings) { print_error('Access Denied', 'You cannot edit an event in this category'); } - $event['start'] = date('Y-m-d H:i:s', strtotime($event['start'])); - $event['end'] = date('Y-m-d H:i:s', strtotime($event['end'])); - if ($event['create_ticket_date'] != null) { - $event['create_ticket_date'] = date('Y-m-d H:i:s', strtotime($event['create_ticket_date'])); - } - $event['all_day'] = $event['all_day'] ? 1 : 0; - $event['assign_to'] = $event['assign_to'] != null ? intval($event['assign_to']) : 'NULL'; - - $sql = "UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event` SET `start` = '" . hesk_dbEscape($event['start']) - . "', `end` = '" . hesk_dbEscape($event['end']) . "', `all_day` = '" . hesk_dbEscape($event['all_day']) . "', `name` = '" - . hesk_dbEscape(addslashes($event['title'])) . "', `location` = '" . hesk_dbEscape(addslashes($event['location'])) . "', `comments` = '" - . hesk_dbEscape(addslashes($event['comments'])) . "', `category` = " . intval($event['category']) . " WHERE `id` = " . intval($event['id']); - if ($event['reminder_amount'] != null) { - $delete_sql = "DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` WHERE `event_id` = " . intval($event['id']) - . " AND `user_id` = " . intval($event['reminder_user']); - hesk_dbQuery($delete_sql); - $insert_sql = "INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "calendar_event_reminder` (`user_id`, `event_id`, - `amount`, `unit`) VALUES (" . intval($event['reminder_user']) . ", " . intval($event['id']) . ", " . intval($event['reminder_amount']) . ", - " . intval($event['reminder_units']) . ")"; - hesk_dbQuery($insert_sql); - } - - hesk_dbQuery($sql); } function delete_event($id, $hesk_settings) {