From ec1f7ec97008bf83a110bcb901947ec7acb7b7a1 Mon Sep 17 00:00:00 2001 From: Jesper Kristensen Date: Wed, 14 Feb 2024 22:16:57 +0100 Subject: [PATCH] Updated code style for calendar namespace --- app/Domain/Calendar/Controllers/AddEvent.php | 101 +- app/Domain/Calendar/Controllers/DelEvent.php | 104 +- .../Controllers/DelExternalCalendar.php | 103 +- app/Domain/Calendar/Controllers/EditEvent.php | 111 +- .../Calendar/Controllers/EditExternal.php | 21 +- app/Domain/Calendar/Controllers/Export.php | 104 +- .../Calendar/Controllers/ExternalCal.php | 150 ++- app/Domain/Calendar/Controllers/Ical.php | 78 +- .../Calendar/Controllers/ImportGCal.php | 83 +- .../Calendar/Controllers/ShowAllGCals.php | 63 +- .../Calendar/Controllers/ShowMyCalendar.php | 87 +- app/Domain/Calendar/Js/calendarController.js | 49 +- app/Domain/Calendar/Repositories/Calendar.php | 1168 ++++++++--------- app/Domain/Calendar/Services/Calendar.php | 333 ++--- app/Domain/Calendar/Templates/ical.tpl.php | 10 +- .../Calendar/Templates/showMyCalendar.tpl.php | 2 +- 16 files changed, 1297 insertions(+), 1270 deletions(-) diff --git a/app/Domain/Calendar/Controllers/AddEvent.php b/app/Domain/Calendar/Controllers/AddEvent.php index bf869316b..e1cd97904 100644 --- a/app/Domain/Calendar/Controllers/AddEvent.php +++ b/app/Domain/Calendar/Controllers/AddEvent.php @@ -5,65 +5,70 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Auth\Services\Auth; - use Leantime\Domain\Calendar\Services\Calendar; - use Symfony\Component\HttpFoundation\Response; - use Leantime\Core\Frontcontroller; +use Leantime\Core\Controller; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Auth\Services\Auth; +use Leantime\Domain\Calendar\Services\Calendar; +use Symfony\Component\HttpFoundation\Response; +use Leantime\Core\Frontcontroller; + +/** + * + */ +class AddEvent extends Controller +{ + private Calendar $calendarService; /** + * init - initialize private variables * + * @param Calendar $calendarService + * @return void */ - class AddEvent extends Controller + public function init(Calendar $calendarService): void { - private Calendar $calendarService; + $this->calendarService = $calendarService; + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + } - /** - * init - initialize private variables - */ - public function init(Calendar $calendarService) - { - $this->calendarService = $calendarService; - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - } + /** + * @param array $params + * + * @return Response + */ + public function get(array $params): Response + { + $values = array( + 'description' => '', + 'dateFrom' => '', + 'dateTo' => '', + 'allDay' => '', + ); - /** - * @return Response - * @throws \Exception - */ - public function get(): Response - { - $values = array( - 'description' => '', - 'dateFrom' => '', - 'dateTo' => '', - 'allDay' => '', - ); + $this->tpl->assign('values', $values); + return $this->tpl->displayPartial('calendar.addEvent'); + } - $this->tpl->assign('values', $values); - return $this->tpl->displayPartial('calendar.addEvent'); - } + /** + * @param array $params + * + * @return Response + */ + public function post(array $params): Response + { + $result = $this->calendarService->addEvent($params); - /** - * @param $params - * @return Response - * @throws \Exception - */ - public function post($params): Response - { - $result = $this->calendarService->addEvent($params); + if (is_numeric($result) === true) { + $this->tpl->setNotification('notification.event_created_successfully', 'success'); - if (is_numeric($result) === true) { - $this->tpl->setNotification('notification.event_created_successfully', 'success'); - return Frontcontroller::redirect(BASE_URL . "/calendar/editEvent/" . $result); - } else { - $this->tpl->setNotification('notification.please_enter_title', 'error'); - $this->tpl->assign('values', $params); - return $this->tpl->displayPartial('calendar.addEvent'); - } + return Frontcontroller::redirect(BASE_URL . "/calendar/editEvent/" . $result); + } else { + $this->tpl->setNotification('notification.please_enter_title', 'error'); + $this->tpl->assign('values', $params); + + return $this->tpl->displayPartial('calendar.addEvent'); } } } diff --git a/app/Domain/Calendar/Controllers/DelEvent.php b/app/Domain/Calendar/Controllers/DelEvent.php index 9c8b74d8d..c6909e853 100644 --- a/app/Domain/Calendar/Controllers/DelEvent.php +++ b/app/Domain/Calendar/Controllers/DelEvent.php @@ -5,66 +5,76 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Services\Calendar as CalendarService; - use Leantime\Domain\Auth\Services\Auth; - use Leantime\Core\Frontcontroller; +use Leantime\Core\Controller; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Calendar\Services\Calendar as CalendarService; +use Leantime\Domain\Auth\Services\Auth; +use Leantime\Core\Frontcontroller; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class DelEvent extends Controller +{ + private CalendarService $calendarService; /** + * init - initialize private variables * + * @param CalendarService $calendarService + * + * @return void */ - class DelEvent extends Controller + public function init(CalendarService $calendarService): void { - private CalendarService $calendarService; + $this->calendarService = $calendarService; + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + } - /** - * init - initialize private variables - */ - public function init(CalendarService $calendarService) - { - $this->calendarService = $calendarService; - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - } + /** + * retrieves delete calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function get(array $params): Response + { + return $this->tpl->displayPartial('calendar.delEvent'); + } - /** - * retrieves delete calendar event page data - * - * @access public - * - */ - public function get() - { - return $this->tpl->displayPartial('calendar.delEvent'); - } + /** + * sets, creates, and updates edit calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function post(array $params): Response + { - /** - * sets, creates, and updates edit calendar event page data - * - * @access public - * - */ - public function post($params) - { + if (isset($_GET['id']) === false) { + return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); + } - if (isset($_GET['id']) === false) { - return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); - } + $id = (int) $_GET['id']; + $result = $this->calendarService->delEvent($id); - $id = (int)$_GET['id']; + if (is_numeric($result) === true) { + $this->tpl->setNotification('notification.event_removed_successfully', 'success'); - $result = $this->calendarService->delEvent($id); + return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); + } else { + $this->tpl->setNotification('notification.could_not_delete_event', 'error'); - if (is_numeric($result) === true) { - $this->tpl->setNotification('notification.event_removed_successfully', 'success'); - return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); - } else { - $this->tpl->setNotification('notification.could_not_delete_event', 'error'); - return $this->tpl->displayPartial('calendar.delEvent'); - } + return $this->tpl->displayPartial('calendar.delEvent'); } } - } diff --git a/app/Domain/Calendar/Controllers/DelExternalCalendar.php b/app/Domain/Calendar/Controllers/DelExternalCalendar.php index e1bcac2a7..025e071f1 100644 --- a/app/Domain/Calendar/Controllers/DelExternalCalendar.php +++ b/app/Domain/Calendar/Controllers/DelExternalCalendar.php @@ -5,66 +5,77 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Services\Calendar as CalendarService; - use Leantime\Domain\Auth\Services\Auth; - use Leantime\Core\Frontcontroller; +use Leantime\Core\Controller; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Calendar\Services\Calendar as CalendarService; +use Leantime\Domain\Auth\Services\Auth; +use Leantime\Core\Frontcontroller; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class DelExternalCalendar extends Controller +{ + private CalendarService $calendarService; /** + * init - initialize private variables + * + * @param CalendarService $calendarService * + * @return void */ - class DelExternalCalendar extends Controller + public function init(CalendarService $calendarService): void { - private CalendarService $calendarService; + $this->calendarService = $calendarService; + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + } - /** - * init - initialize private variables - */ - public function init(CalendarService $calendarService) - { - $this->calendarService = $calendarService; - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - } + /** + * retrieves delete calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function get(array $params): Response + { + return $this->tpl->displayPartial('calendar.delExternalCal'); + } - /** - * retrieves delete calendar event page data - * - * @access public - * - */ - public function get() - { - return $this->tpl->displayPartial('calendar.delExternalCal'); + /** + * sets, creates, and updates edit calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function post(array $params): Response + { + + if (isset($_GET['id']) === false) { + return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); } - /** - * sets, creates, and updates edit calendar event page data - * - * @access public - * - */ - public function post($params) - { + $id = (int)$_GET['id']; - if (isset($_GET['id']) === false) { - return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); - } + $result = $this->calendarService->deleteGCal($id); - $id = (int)$_GET['id']; + if ($result === true) { + $this->tpl->setNotification('notification.calendar_removed_successfully', 'success'); - $result = $this->calendarService->deleteGCal($id); + return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); + } else { + $this->tpl->setNotification('notification.could_not_delete_calendar', 'error'); - if ($result === true) { - $this->tpl->setNotification('notification.calendar_removed_successfully', 'success'); - return Frontcontroller::redirect(BASE_URL . "/calendar/showMyCalendar/"); - } else { - $this->tpl->setNotification('notification.could_not_delete_calendar', 'error'); - return $this->tpl->displayPartial('calendar.delEvent'); - } + return $this->tpl->displayPartial('calendar.delEvent'); } } - } diff --git a/app/Domain/Calendar/Controllers/EditEvent.php b/app/Domain/Calendar/Controllers/EditEvent.php index b18958644..7b7ebd13d 100644 --- a/app/Domain/Calendar/Controllers/EditEvent.php +++ b/app/Domain/Calendar/Controllers/EditEvent.php @@ -5,76 +5,73 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; +use Leantime\Core\Controller; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Calendar\Services\Calendar as CalendarService; +use Leantime\Domain\Auth\Services\Auth; +use Leantime\Core\Frontcontroller; +use Symfony\Component\HttpFoundation\Response; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Services\Calendar as CalendarService; - use Leantime\Domain\Auth\Services\Auth; - use Leantime\Domain\Calendar\Services\Calendar; - use Leantime\Core\Frontcontroller; +/** + * + */ +class EditEvent extends Controller +{ + private CalendarService $calendarService; /** + * init - initialize private variables + * + * @param CalendarService $calendarService * + * @return void */ - class EditEvent extends Controller + public function init(CalendarService $calendarService): void { - private CalendarService $calendarService; - - /** - * init - initialize private variables - */ - public function init(CalendarService $calendarService) - { - - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - $this->calendarService = $calendarService; - } + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + $this->calendarService = $calendarService; + } + /** + * retrieves edit calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function get(array $params): Response + { + $values = $this->calendarService->getEvent($params['id']); - /** - * retrieves edit calendar event page data - * - * @access public - * - */ - public function get($params) - { + $this->tpl->assign('values', $values); - $values = array( - 'description' => '', - 'dateFrom' => '', - 'dateTo' => '', - 'allDay' => '', - 'id' => '', - ); + return $this->tpl->displayPartial('calendar.editEvent'); + } - $values = $this->calendarService->getEvent($params['id']); + /** + * sets, creates, and updates edit calendar event page data + * + * @access public + * + * @param array $params + * + * @return Response + */ + public function post(array $params): Response + { + $params['id'] = $_GET['id'] ?? null; + $result = $this->calendarService->editEvent($params); - $this->tpl->assign('values', $values); - return $this->tpl->displayPartial('calendar.editEvent'); + if ($result === true) { + $this->tpl->setNotification('notification.event_edited_successfully', 'success'); + } else { + $this->tpl->setNotification('notification.please_enter_title', 'error'); } - /** - * sets, creates, and updates edit calendar event page data - * - * @access public - * - */ - public function post($params) - { - - $params['id'] = $_GET['id'] ?? null; - $result = $this->calendarService->editEvent($params); - - if ($result === true) { - $this->tpl->setNotification('notification.event_edited_successfully', 'success'); - } else { - $this->tpl->setNotification('notification.please_enter_title', 'error'); - } - - return Frontcontroller::redirect(BASE_URL . '/calendar/editEvent/' . $params['id']); - } + return Frontcontroller::redirect(BASE_URL . '/calendar/editEvent/' . $params['id']); } } diff --git a/app/Domain/Calendar/Controllers/EditExternal.php b/app/Domain/Calendar/Controllers/EditExternal.php index 16357b01c..124d3e031 100644 --- a/app/Domain/Calendar/Controllers/EditExternal.php +++ b/app/Domain/Calendar/Controllers/EditExternal.php @@ -6,22 +6,34 @@ use Leantime\Domain\Auth\Models\Roles; use Leantime\Domain\Calendar\Services\Calendar; use Leantime\Domain\Auth\Services\Auth; +use Symfony\Component\HttpFoundation\Response; +/** + * + */ class EditExternal extends Controller { private Calendar $calendarService; - public function init(Calendar $calendarService) + /** + * @param Calendar $calendarService + * + * @return void + */ + public function init(Calendar $calendarService): void { $this->calendarService = $calendarService; } - public function run() + /** + * @return Response + * + * @throws \Exception + */ + public function run(): Response { Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - $msgKey = ''; - if (isset($_GET['id']) === true) { $id = ($_GET['id']); @@ -45,7 +57,6 @@ public function run() $this->tpl->assign('values', $values); return $this->tpl->displayPartial('calendar.editExternalCalendar'); - } else { return $this->tpl->display('errors.error403'); } diff --git a/app/Domain/Calendar/Controllers/Export.php b/app/Domain/Calendar/Controllers/Export.php index 25720a7ad..e1e7e4985 100644 --- a/app/Domain/Calendar/Controllers/Export.php +++ b/app/Domain/Calendar/Controllers/Export.php @@ -5,69 +5,75 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Core\Environment; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Setting\Repositories\Setting as SettingRepository; - use Leantime\Domain\Auth\Services\Auth; - use Ramsey\Uuid\Uuid; +use Leantime\Core\Controller; +use Leantime\Core\Environment; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Setting\Repositories\Setting as SettingRepository; +use Leantime\Domain\Auth\Services\Auth; +use Ramsey\Uuid\Uuid; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class Export extends Controller +{ + private Environment $config; + private SettingRepository $settingsRepo; /** + * init - initialize private variables + * + * @param Environment $config + * @param SettingRepository $settingsRepo * + * @return void */ - class Export extends Controller + public function init(Environment $config, SettingRepository $settingsRepo): void { - private Environment $config; - private SettingRepository $settingsRepo; - - /** - * init - initialize private variables - */ - public function init(Environment $config, SettingRepository $settingsRepo) - { - $this->config = $config; - $this->settingsRepo = $settingsRepo; - } + $this->config = $config; + $this->settingsRepo = $settingsRepo; + } - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { - if (isset($_GET['remove'])) { - $this->settingsRepo->deleteSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret"); + /** + * run - display template and edit data + * + * @access public + * + * @return Response + */ + public function run(): Response + { + if (isset($_GET['remove'])) { + $this->settingsRepo->deleteSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret"); - $this->tpl->setNotification("notifications.ical_removed_success", "success"); - } + $this->tpl->setNotification("notifications.ical_removed_success", "success"); + } - //Add Post handling - if (isset($_POST['generateUrl'])) { - $uuid = Uuid::uuid4(); - $icalHash = $uuid->toString(); + //Add Post handling + if (isset($_POST['generateUrl'])) { + $uuid = Uuid::uuid4(); + $icalHash = $uuid->toString(); - $this->settingsRepo->saveSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret", $icalHash); + $this->settingsRepo->saveSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret", $icalHash); - $this->tpl->setNotification("notifications.ical_success", "success"); - } + $this->tpl->setNotification("notifications.ical_success", "success"); + } - $icalHash = $this->settingsRepo->getSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret"); - $userHash = hash('sha1', $_SESSION['userdata']['id'] . $this->config->sessionpassword); + $icalHash = $this->settingsRepo->getSetting("usersettings." . $_SESSION['userdata']['id'] . ".icalSecret"); + $userHash = hash('sha1', $_SESSION['userdata']['id'] . $this->config->sessionpassword); - if (!$icalHash) { - $icalUrl = ""; - } else { - $icalUrl = BASE_URL . "/calendar/ical/" . $icalHash . "_" . $userHash; - } + if (!$icalHash) { + $icalUrl = ""; + } else { + $icalUrl = BASE_URL . "/calendar/ical/" . $icalHash . "_" . $userHash; + } - //Add delete handling - $this->tpl->assign("url", $icalUrl); + //Add delete handling + $this->tpl->assign("url", $icalUrl); - return $this->tpl->displayPartial("calendar.export"); - } + return $this->tpl->displayPartial("calendar.export"); } - } diff --git a/app/Domain/Calendar/Controllers/ExternalCal.php b/app/Domain/Calendar/Controllers/ExternalCal.php index 0e03ff71a..75d205325 100644 --- a/app/Domain/Calendar/Controllers/ExternalCal.php +++ b/app/Domain/Calendar/Controllers/ExternalCal.php @@ -5,90 +5,108 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use GuzzleHttp\Client; - use Leantime\Core\AppSettings; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; - use Leantime\Domain\Auth\Services\Auth; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; +use Illuminate\Contracts\Container\BindingResolutionException; +use JetBrains\PhpStorm\NoReturn; +use Leantime\Core\AppSettings; +use Leantime\Core\Controller; +use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class ExternalCal extends Controller +{ + private CalendarRepository $calendarRepo; + + private int $cacheTime = 60 * 30; // 30min /** + * init - initialize private variables * + * @param CalendarRepository $calendarRepo + * + * @return void */ - class ExternalCal extends Controller + public function init(CalendarRepository $calendarRepo): void { - private CalendarRepository $calendarRepo; - - private $cacheTime = 60 * 30; // 30min - - /** - * init - initialize private variables - */ - public function init(CalendarRepository $calendarRepo) - { - $this->calendarRepo = $calendarRepo; - } + $this->calendarRepo = $calendarRepo; + } - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { + /** + * run - display template and edit data + * + * @access public + * + * @return void + * + * @throws BindingResolutionException + */ + #[NoReturn] + public function run(): void + { - $calId = $_GET['id']; + $calId = $_GET['id']; - if (!isset($_SESSION['calendarCache'])) { - $_SESSION['calendarCache'] = []; - } + if (!isset($_SESSION['calendarCache'])) { + $_SESSION['calendarCache'] = []; + } - if (isset($_SESSION['calendarCache'][$calId]) && $_SESSION['calendarCache'][$calId]['lastUpdate'] > time() - $this->cacheTime) { - $content = $_SESSION['calendarCache'][$calId]["content"]; - } else { - $cal = $this->calendarRepo->getExternalCalendar($calId, $_SESSION['userdata']['id']); + $content = ''; + if (isset($_SESSION['calendarCache'][$calId]) && $_SESSION['calendarCache'][$calId]['lastUpdate'] > time() - $this->cacheTime) { + $content = $_SESSION['calendarCache'][$calId]["content"]; + } else { + $cal = $this->calendarRepo->getExternalCalendar($calId, $_SESSION['userdata']['id']); - if (isset($cal["url"])) { - $content = $this->loadIcalUrl($cal["url"]); - $_SESSION['calendarCache'][$calId]["lastUpdate"] = time(); - $_SESSION['calendarCache'][$calId]["content"] = $content; - } + if (isset($cal["url"])) { + $content = $this->loadIcalUrl($cal["url"]); + $_SESSION['calendarCache'][$calId]["lastUpdate"] = time(); + $_SESSION['calendarCache'][$calId]["content"] = $content; } - - header('Content-type: text/calendar; charset=utf-8'); - //header('Content-disposition: attachment;filename="external.ics"'); - - echo $content; - - exit(); } - private function loadIcalUrl($url) { + header('Content-type: text/calendar; charset=utf-8'); + //header('Content-disposition: attachment;filename="external.ics"'); - $guzzle = app()->make(Client::class); - - $appSettings = app()->make(AppSettings::class); - try { - $response = $guzzle->request('GET', $url, [ - 'headers' => [ - 'Accept' => 'text/calendar', - 'User-Agent' => 'Leantime Calendar Integration v'.$appSettings->appVersion, //<-- Github wants a user agent. - ] - ]); - } catch (ClientException $e) { - throw new \Exception('Guzzle problem: ', Psr7\Message::toString($e->getRequest()), Psr7\Message::toString($e->getResponse())); - } + echo $content; + exit(); + } - if ($response->getStatusCode() == '200') { - return (string) $response->getBody(); - } else { - throw new \Exception('Guzzle bad response code'); - } + /** + * Load an iCal URL. + * + * @param string $url The URL of the iCal to load. + * + * @return string The contents of the iCal. + * + * @throws BindingResolutionException + */ + private function loadIcalUrl(string $url): string + { + $guzzle = app()->make(Client::class); + + $appSettings = app()->make(AppSettings::class); + try { + $response = $guzzle->request('GET', $url, [ + 'headers' => [ + 'Accept' => 'text/calendar', + // GitHub needs a user agent. + 'User-Agent' => 'Leantime Calendar Integration v' . $appSettings->appVersion, + ], + ]); + } catch (ClientException $e) { + throw new \Exception('Guzzle problem: ' . $e->getMessage(), $e->getCode(), $e); } + if ($response->getStatusCode() == '200') { + return (string) $response->getBody(); + } else { + throw new \Exception('Guzzle bad response code'); + } } - } diff --git a/app/Domain/Calendar/Controllers/Ical.php b/app/Domain/Calendar/Controllers/Ical.php index 81891e3fb..c268d6dcd 100644 --- a/app/Domain/Calendar/Controllers/Ical.php +++ b/app/Domain/Calendar/Controllers/Ical.php @@ -5,52 +5,60 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; - use Leantime\Domain\Auth\Services\Auth; - use Leantime\Core\Frontcontroller; +use Illuminate\Contracts\Container\BindingResolutionException; +use Leantime\Core\Controller; +use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; +use Leantime\Core\Frontcontroller; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class Ical extends Controller +{ + private CalendarRepository $calendarRepo; /** + * init - initialize private variables + * + * @param CalendarRepository $calendarRepo * + * @return void */ - class Ical extends Controller + public function init(CalendarRepository $calendarRepo): void { - private CalendarRepository $calendarRepo; - - /** - * init - initialize private variables - */ - public function init(CalendarRepository $calendarRepo) - { - $this->calendarRepo = $calendarRepo; - } + $this->calendarRepo = $calendarRepo; + } - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { - $calId = $_GET['id']; + /** + * run - display template and edit data + * + * @access public + * + * @return RedirectResponse|Response + * + * @throws BindingResolutionException + */ + public function run(): RedirectResponse|Response + { + $calId = $_GET['id']; - $idParts = explode("_", $calId); + $idParts = explode("_", $calId); - if (count($idParts) != 2) { - return Frontcontroller::redirect(BASE_URL . "/errors/404"); - } + if (count($idParts) != 2) { + return Frontcontroller::redirect(BASE_URL . "/errors/404"); + } - $calendar = $this->calendarRepo->getCalendarBySecretHash($idParts[1], $idParts[0]); + $calendar = $this->calendarRepo->getCalendarBySecretHash($idParts[1], $idParts[0]); - $this->tpl->assign("calendar", $calendar); + $this->tpl->assign("calendar", $calendar); - header('Content-type: text/calendar; charset=utf-8'); - header('Content-disposition: attachment;filename="leantime.ics"'); - return $this->tpl->display("calendar.ical", "blank"); - } - } + header('Content-type: text/calendar; charset=utf-8'); + header('Content-disposition: attachment;filename="leantime.ics"'); + return $this->tpl->display("calendar.ical", "blank"); + } } diff --git a/app/Domain/Calendar/Controllers/ImportGCal.php b/app/Domain/Calendar/Controllers/ImportGCal.php index d25c5fe9c..fe5df60b6 100644 --- a/app/Domain/Calendar/Controllers/ImportGCal.php +++ b/app/Domain/Calendar/Controllers/ImportGCal.php @@ -1,61 +1,62 @@ calendarRepo = $calendarRepo; + } /** + * run - display template and edit data * + * @access public + * + * @return Response */ - class ImportGCal extends Controller + public function run(): Response { - private CalendarRepository $calendarRepo; - - /** - * init - initialize private variables - */ - public function init(CalendarRepository $calendarRepo) - { - $this->calendarRepo = $calendarRepo; - } + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + $values = array( + 'url' => '', + 'name' => '', + 'colorClass' => '', + ); + if (isset($_POST['name']) === true) { $values = array( - 'url' => '', - 'name' => '', - 'colorClass' => '', + 'url' => ($_POST['url']), + 'name' => ($_POST['name']), + 'colorClass' => ($_POST['colorClass']), ); - if (isset($_POST['name']) === true) { - $values = array( - 'url' => ($_POST['url']), - 'name' => ($_POST['name']), - 'colorClass' => ($_POST['colorClass']), - ); - - $this->calendarRepo->addGUrl($values); - $this->tpl->setNotification('notification.gcal_imported_successfully', 'success', 'externalcalendar_created'); - } + $this->calendarRepo->addGUrl($values); + $this->tpl->setNotification('notification.gcal_imported_successfully', 'success', 'externalcalendar_created'); + } - $this->tpl->assign('values', $values); + $this->tpl->assign('values', $values); - return $this->tpl->displayPartial('calendar.importGCal'); - } + return $this->tpl->displayPartial('calendar.importGCal'); } } diff --git a/app/Domain/Calendar/Controllers/ShowAllGCals.php b/app/Domain/Calendar/Controllers/ShowAllGCals.php index b6f70222b..4a8e8720a 100644 --- a/app/Domain/Calendar/Controllers/ShowAllGCals.php +++ b/app/Domain/Calendar/Controllers/ShowAllGCals.php @@ -1,45 +1,48 @@ calendarRepo = $calendarRepo; + } /** + * run - display template and edit data + * + * @access public + * + * @return Response * + * @throws \Exception */ - class ShowAllGCals extends Controller + public function run(): Response { - private CalendarRepository $calendarRepo; - - /** - * init - initialize private variables - */ - public function init(CalendarRepository $calendarRepo) - { - $this->calendarRepo = $calendarRepo; - } - - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - //Assign vars - $this->tpl->assign('allCalendars', $this->calendarRepo->getMyGoogleCalendars()); + // Assign vars + $this->tpl->assign('allCalendars', $this->calendarRepo->getMyGoogleCalendars()); - return $this->tpl->display('calendar.showAllGCals'); - } + return $this->tpl->display('calendar.showAllGCals'); } } diff --git a/app/Domain/Calendar/Controllers/ShowMyCalendar.php b/app/Domain/Calendar/Controllers/ShowMyCalendar.php index 5b1e5a94d..319924b61 100644 --- a/app/Domain/Calendar/Controllers/ShowMyCalendar.php +++ b/app/Domain/Calendar/Controllers/ShowMyCalendar.php @@ -5,50 +5,59 @@ * */ -namespace Leantime\Domain\Calendar\Controllers { +namespace Leantime\Domain\Calendar\Controllers; - use Leantime\Core\Controller; - use Leantime\Domain\Auth\Models\Roles; - use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; - use Leantime\Domain\Auth\Services\Auth; +use Illuminate\Contracts\Container\BindingResolutionException; +use Leantime\Core\Controller; +use Leantime\Domain\Auth\Models\Roles; +use Leantime\Domain\Calendar\Repositories\Calendar as CalendarRepository; +use Leantime\Domain\Auth\Services\Auth; +use Symfony\Component\HttpFoundation\Response; + +/** + * + */ +class ShowMyCalendar extends Controller +{ + private CalendarRepository $calendarRepo; + + /** + * init - initialize private variables + * + * @param CalendarRepository $calendarRepo + * + * @return void + */ + public function init(CalendarRepository $calendarRepo): void + { + $this->calendarRepo = $calendarRepo; + } /** + * run - display template and edit data + * + * @access public * + * @return Response + * + * @throws BindingResolutionException */ - class ShowMyCalendar extends Controller + public function run(): Response { - private CalendarRepository $calendarRepo; - - /** - * init - initialize private variables - */ - public function init(CalendarRepository $calendarRepo) - { - $this->calendarRepo = $calendarRepo; - } - - /** - * run - display template and edit data - * - * @access public - */ - public function run() - { - Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); - - $this->tpl->assign('calendar', $this->calendarRepo->getCalendar($_SESSION['userdata']['id'])); - //$this->tpl->assign('gCalLink', $this->calendarRepo->getMyGoogleCalendars()); - - $_SESSION['lastPage'] = BASE_URL . "/calendar/showMyCalendar/"; - - $this->tpl->assign('externalCalendars', $this->calendarRepo->getMyExternalCalendars($_SESSION['userdata']['id'])); - - //ToDO: This should come from the ticket repo... - //$this->tpl->assign('ticketEditDates', $this->calendarRepo->getTicketEditDates()); - //$this->tpl->assign('ticketWishDates', $this->calendarRepo->getTicketWishDates()); - //$this->tpl->assign('dates', $this->calendarRepo->getAllDates($dateFrom, $dateTo)); - - return $this->tpl->display('calendar.showMyCalendar'); - } + Auth::authOrRedirect([Roles::$owner, Roles::$admin, Roles::$manager, Roles::$editor]); + + $this->tpl->assign('calendar', $this->calendarRepo->getCalendar($_SESSION['userdata']['id'])); + //$this->tpl->assign('gCalLink', $this->calendarRepo->getMyGoogleCalendars()); + + $_SESSION['lastPage'] = BASE_URL . "/calendar/showMyCalendar/"; + + $this->tpl->assign('externalCalendars', $this->calendarRepo->getMyExternalCalendars($_SESSION['userdata']['id'])); + + // @TODO: This should come from the ticket repo... + //$this->tpl->assign('ticketEditDates', $this->calendarRepo->getTicketEditDates()); + //$this->tpl->assign('ticketWishDates', $this->calendarRepo->getTicketWishDates()); + //$this->tpl->assign('dates', $this->calendarRepo->getAllDates($dateFrom, $dateTo)); + + return $this->tpl->display('calendar.showMyCalendar'); } } diff --git a/app/Domain/Calendar/Js/calendarController.js b/app/Domain/Calendar/Js/calendarController.js index b97c377d3..b31bf54c2 100644 --- a/app/Domain/Calendar/Js/calendarController.js +++ b/app/Domain/Calendar/Js/calendarController.js @@ -223,8 +223,7 @@ leantime.calendarController = (function () { }, eventDrop: function (event) { - if(event.event.extendedProps.enitityType == "ticket") { - + if (event.event.extendedProps.enitityType == "ticket") { jQuery.ajax({ type : 'PATCH', url : leantime.appUrl + '/api/tickets', @@ -236,9 +235,7 @@ leantime.calendarController = (function () { timeTo: luxon.DateTime.fromJSDate(event.event.end).toFormat(userTimeFormat), } }); - - }else if(event.event.extendedProps.enitityType == "event") { - + } else if (event.event.extendedProps.enitityType == "event") { jQuery.ajax({ type : 'PATCH', url : leantime.appUrl + '/api/calendar', @@ -252,7 +249,7 @@ leantime.calendarController = (function () { }, eventResize: function (event) { - if(event.event.extendedProps.enitityType == "ticket") { + if (event.event.extendedProps.enitityType == "ticket") { jQuery.ajax({ type : 'PATCH', url : leantime.appUrl + '/api/tickets', @@ -264,8 +261,7 @@ leantime.calendarController = (function () { timeTo: luxon.DateTime.fromJSDate(event.event.end).toFormat(userTimeFormat), } }) - }else if(event.event.extendedProps.enitityType == "event") { - + } else if (event.event.extendedProps.enitityType == "event") { jQuery.ajax({ type : 'PATCH', url : leantime.appUrl + '/api/calendar', @@ -278,14 +274,13 @@ leantime.calendarController = (function () { } }, - eventMouseEnter: function() { + eventMouseEnter: function () { }, - dateClick: function(info) { - if(info.view.type == "timeGridDay") { - + dateClick: function (info) { + if (info.view.type == "timeGridDay") { } }, - eventReceive: function(event) { + eventReceive: function (event) { jQuery.ajax({ type : 'PATCH', @@ -301,12 +296,12 @@ leantime.calendarController = (function () { }) }, - eventDragStart: function(event) { + eventDragStart: function (event) { }, eventDidMount: function (info) { - if(info.isDraggable === false) { + if (info.isDraggable === false) { jQuery(info.el).addClass("locked"); } @@ -321,10 +316,10 @@ leantime.calendarController = (function () { } }); - jQuery(document).ready(function() { + jQuery(document).ready(function () { //let tickets = jQuery("#yourToDoContainer")[0]; - jQuery("#yourToDoContainer").find(".ticketBox").each(function(){ + jQuery("#yourToDoContainer").find(".ticketBox").each(function () { var currentTicket = jQuery(this); jQuery(this).data('event', { @@ -349,7 +344,7 @@ leantime.calendarController = (function () { }); var tickets = jQuery("#yourToDoContainer")[0]; - if(tickets) { + if (tickets) { new FullCalendar.ThirdPartyDraggable(tickets, { itemSelector: '.ticketBox', eventDragMinDistance: 10, @@ -366,11 +361,11 @@ leantime.calendarController = (function () { }); } - calendar.scrollToTime( Date.now() ); + calendar.scrollToTime(Date.now()); }); - htmx.onLoad(function(content) { + htmx.onLoad(function (content) { // look up all elements with the tomselect class on it within the element var allSelects = htmx.findAll(content, "#yourToDoContainer") @@ -385,7 +380,7 @@ leantime.calendarController = (function () { });*/ // make the event draggable using jQuery UI - jQuery(tickets).find(".ticketBox").each(function() { + jQuery(tickets).find(".ticketBox").each(function () { var currentTicket = jQuery(this); jQuery(this).data('event', { @@ -411,7 +406,7 @@ leantime.calendarController = (function () { new FullCalendar.ThirdPartyDraggable(tickets, { eventDragMinDistance: 10, itemSelector: '.ticketBox', - eventData: function(eventEl) { + eventData: function (eventEl) { return { id: jQuery(eventEl).attr("data-val"), title: jQuery(eventEl).find(".titleContainer strong").text(), @@ -423,7 +418,7 @@ leantime.calendarController = (function () { } }); - calendar.scrollToTime( Date.now() ); + calendar.scrollToTime(Date.now()); } }); @@ -433,20 +428,20 @@ leantime.calendarController = (function () { jQuery(".minCalendar .calendarTitle h2").text(calendar.getCurrentData().viewTitle); - jQuery('.minCalendar .fc-prev-button').click(function() { + jQuery('.minCalendar .fc-prev-button').click(function () { calendar.prev(); calendar.getCurrentData() jQuery(".minCalendar .calendarTitle h2").text(calendar.getCurrentData().viewTitle); }); - jQuery('.minCalendar .fc-next-button').click(function() { + jQuery('.minCalendar .fc-next-button').click(function () { calendar.next(); jQuery(".minCalendar .calendarTitle h2").text(calendar.getCurrentData().viewTitle); }); - jQuery('.minCalendar .fc-today-button').click(function() { + jQuery('.minCalendar .fc-today-button').click(function () { calendar.today(); jQuery(".minCalendar .calendarTitle h2").text(calendar.getCurrentData().viewTitle); }); - jQuery(".minCalendar .calendarViewSelect").on("change", function(e){ + jQuery(".minCalendar .calendarViewSelect").on("change", function (e) { calendar.changeView(jQuery(".minCalendar .calendarViewSelect option:selected").val()); diff --git a/app/Domain/Calendar/Repositories/Calendar.php b/app/Domain/Calendar/Repositories/Calendar.php index 1b98f6993..6606fd0b2 100644 --- a/app/Domain/Calendar/Repositories/Calendar.php +++ b/app/Domain/Calendar/Repositories/Calendar.php @@ -1,277 +1,373 @@ "var(--yellow)", + "label-purple" => "var(--purple)", + "label-pink" => "var(--pink)", + "label-darker-blue" => "var(--darker-blue)", + "label-info" => "var(--dark-blue)", + "label-blue" => "var(--blue)", + "label-dark-blue" => "var(--dark-blue)", + "label-success" => "var(--green)", + "label-brown" => "var(--brown)", + "label-danger" => "var(--dark-red)", + "label-important" => "var(--red)", + "label-green" => "var(--green)", + "label-default" => "var(--grey)", + "label-dark-green" => "var(--dark-green)", + "label-red" => "var(--red)", + "label-dark-red" => "var(--dark-red)", + "label-grey" => "var(--grey)", + ); /** + * Class constructor. * + * @param DbCore $db The DbCore object. + * @param LanguageCore $language The LanguageCore object. + * @param DateTimeHelper $dateTimeHelper The DateTimeHelper object. + * @param Environment $config The Environment object. + * + * @return void */ - class Calendar extends RepositoryCore + public function __construct( + private DbCore $db, + private LanguageCore $language, + private DateTimeHelper $dateTimeHelper, + private Environment $config + ) { + } + + /** + * @param string $dateFrom + * @param string $dateTo + * + * @return array|false + */ + public function getAllDates(string $dateFrom, string $dateTo): false|array { + $query = "SELECT * FROM zp_calendar WHERE userId = :userId ORDER BY zp_calendar.dateFrom"; - private array $classColorMap = array( - "label-warning" => "var(--yellow)", - "label-purple" => "var(--purple)", - "label-pink" => "var(--pink)", - "label-darker-blue" => "var(--darker-blue)", - "label-info" => "var(--dark-blue)", - "label-blue" => "var(--blue)", - "label-dark-blue" => "var(--dark-blue)", - "label-success" => "var(--green)", - "label-brown" => "var(--brown)", - "label-danger" => "var(--dark-red)", - "label-important" => "var(--red)", - "label-green" => "var(--green)", - "label-default" => "var(--grey)", - "label-dark-green" => "var(--dark-green)", - "label-red" => "var(--red)", - "label-dark-red" => "var(--dark-red)", - "label-grey" => "var(--grey)", - ); + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - /** - * Class constructor. - * - * @param DbCore $db The DbCore object. - * @param LanguageCore $language The LanguageCore object. - * @param DateTimeHelper $dateTimeHelper The DateTimeHelper object. - * @param Environment $config The Environment object. - * @return void - */ - - public function __construct( - private DbCore $db, - private LanguageCore $language, - private DateTimeHelper $dateTimeHelper, - private Environment $config) - {} - - /** - * @param $dateFrom - * @param $dateTo - * @return array|false - */ - public function getAllDates($dateFrom, $dateTo): false|array - { - $query = "SELECT * FROM zp_calendar WHERE - userId = :userId ORDER BY zp_calendar.dateFrom"; - - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - - $stmn->execute(); - $allDates = $stmn->fetchAll(); - - return $allDates; - } + $stmn->execute(); - /** - * @param $dateFrom - * @param $dateTo - * @return false|array - */ - public function getAll($dateFrom, $dateTo): false|array - { - return $this->getAllDates($dateFrom, $dateTo); - } + return $stmn->fetchAll(); + } - /** - * @param $userId - * @return array - * @throws BindingResolutionException - */ - public function getCalendar($userId): array - { + /** + * @param string $dateFrom + * @param string $dateTo + * + * @return false|array + */ + public function getAll(string $dateFrom, string $dateTo): false|array + { + return $this->getAllDates($dateFrom, $dateTo); + } - $ticketService = app()->make(Tickets::class); - $dbTickets = $ticketService->getOpenUserTicketsThisWeekAndLater($userId, "", true); + /** + * @param int $userId + * + * @return array + * + * @throws BindingResolutionException + */ + public function getCalendar(int $userId): array + { + $ticketService = app()->make(Tickets::class); + $dbTickets = $ticketService->getOpenUserTicketsThisWeekAndLater($userId, "", true); - $tickets = array(); - if (isset($dbTickets["thisWeek"]["tickets"])) { - $tickets = array_merge($tickets, $dbTickets["thisWeek"]["tickets"]); - } + $tickets = array(); + if (isset($dbTickets["thisWeek"]["tickets"])) { + $tickets = array_merge($tickets, $dbTickets["thisWeek"]["tickets"]); + } - if (isset($dbTickets["later"]["tickets"])) { - $tickets = array_merge($tickets, $dbTickets["later"]["tickets"]); - } + if (isset($dbTickets["later"]["tickets"])) { + $tickets = array_merge($tickets, $dbTickets["later"]["tickets"]); + } - if (isset($dbTickets["overdue"]["tickets"])) { - $tickets = array_merge($tickets, $dbTickets["overdue"]["tickets"]); - } + if (isset($dbTickets["overdue"]["tickets"])) { + $tickets = array_merge($tickets, $dbTickets["overdue"]["tickets"]); + } + $sql = "SELECT * FROM zp_calendar WHERE userId = :userId"; - $sql = "SELECT * FROM zp_calendar WHERE userId = :userId"; + $stmn = $this->db->database->prepare($sql); + $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); - $stmn = $this->db->database->prepare($sql); - $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); + $stmn->execute(); + $values = $stmn->fetchAll(); + $stmn->closeCursor(); - $stmn->execute(); - $values = $stmn->fetchAll(); - $stmn->closeCursor(); + $newValues = array(); + foreach ($values as $value) { + $dateFrom = $this->dateTimeHelper->getTimestamp($value['dateFrom']); + $dateTo = $this->dateTimeHelper->getTimestamp($value['dateTo']); - $newValues = array(); - foreach ($values as $value) { + $allDay = filter_var($value['allDay'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); - $dateFrom = $this->dateTimeHelper->getTimestamp($value['dateFrom']); - $dateTo = $this->dateTimeHelper->getTimestamp($value['dateTo']); + $newValues[] = array( + 'title' => $value['description'], + 'allDay' => $allDay, + 'dateFrom' => array( + 'y' => date('Y', $dateFrom), + 'm' => date('m', $dateFrom), + 'd' => date('d', $dateFrom), + 'h' => $allDay ? "00" : date('H', $dateFrom), + 'i' => $allDay ? "00" : date('i', $dateFrom), + 'ical' => date('Ymd\THis', $dateFrom), + ), + 'dateTo' => array( + 'y' => date('Y', $dateTo), + 'm' => date('m', $dateTo), + 'd' => $allDay ? date('d', ($dateFrom + (60 * 60 * 24))) : date('d', $dateTo), + 'h' => $allDay ? "00" : date('H', $dateTo), + 'i' => $allDay ? "00" : date('i', $dateTo), + 'ical' => date('Ymd\THis', $dateTo), + ), + 'id' => $value['id'], + 'projectId' => '', + 'eventType' => "calendar", + 'dateContext' => 'plan', + 'backgroundColor' => 'var(--accent1)', + 'borderColor' => 'var(--accent1)', + ); + } - $allDay = filter_var($value['allDay'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + if (count($tickets)) { + $statusLabelsArray = array(); - $newValues[] = array( - 'title' => $value['description'], - 'allDay' => $allDay, - 'dateFrom' => array( - 'y' => date('Y', $dateFrom), - 'm' => date('m', $dateFrom), - 'd' => date('d', $dateFrom), - 'h' => $allDay ? "00" : date('H', $dateFrom), - 'i' => $allDay ? "00" : date('i', $dateFrom), - 'ical' => date('Ymd\THis', $dateFrom), - ), - 'dateTo' => array( - 'y' => date('Y', $dateTo), - 'm' => date('m', $dateTo), - 'd' => $allDay ? date('d', ($dateFrom + (60 * 60 * 24))) : date('d', $dateTo), - 'h' => $allDay ? "00" : date('H', $dateTo), - 'i' => $allDay ? "00" : date('i', $dateTo), - 'ical' => date('Ymd\THis', $dateTo), - ), - 'id' => $value['id'], - 'projectId' => '', - 'eventType' => "calendar", - 'dateContext' => 'plan', - 'backgroundColor' => 'var(--accent1)', - 'borderColor' => 'var(--accent1)', - ); - } + foreach ($tickets as $ticket) { + if (!isset($statusLabelsArray[$ticket['projectId']])) { + $statusLabelsArray[$ticket['projectId']] = $ticketService->getStatusLabels( + $ticket['projectId'] + ); + } - if (count($tickets)) { - $statusLabelsArray = array(); + if (isset($statusLabelsArray[$ticket['projectId']][$ticket['status']])) { + $statusName = $statusLabelsArray[$ticket['projectId']][$ticket['status']]["name"]; + $statusColor = $this->classColorMap[$statusLabelsArray[$ticket['projectId']][$ticket['status']]["class"]]; + } else { + $statusName = ""; + $statusColor = "var(--grey)"; + } - foreach ($tickets as $ticket) { - if (!isset($statusLabelsArray[$ticket['projectId']])) { - $statusLabelsArray[$ticket['projectId']] = $ticketService->getStatusLabels( - $ticket['projectId'] - ); - } + $backgroundColor = "var(--accent2)"; + + if ($ticket['dateToFinish'] != "0000-00-00 00:00:00" && $ticket['dateToFinish'] != "1969-12-31 00:00:00") { + $dateFrom = $this->dateTimeHelper->getTimestamp($ticket['dateToFinish']); + $dateTo = $this->dateTimeHelper->getTimestamp($ticket['dateToFinish']); + $context = '❕ ' . $this->language->__("label.due_todo"); + + $newValues[] = $this->mapEventData( + title:$context . $ticket['headline'] . " (" . $statusName . ")", + allDay:false, + id: $ticket['id'], + projectId: $ticket['projectId'], + eventType: "ticket", + dateContext: "edit", + backgroundColor: $backgroundColor, + borderColor: $statusColor, + dateFrom: $dateFrom, + dateTo: $dateTo + ); + } + + if ($ticket['editFrom'] != "0000-00-00 00:00:00" && $ticket['editFrom'] != "1969-12-31 00:00:00") { + // Set ticket to all-day ticket when no time is set + $timeStart = format($ticket['editFrom'])->time24(); + $timeEnd = format($ticket['editTo'])->time24(); - if (isset($statusLabelsArray[$ticket['projectId']][$ticket['status']])) { - $statusName = $statusLabelsArray[$ticket['projectId']][$ticket['status']]["name"]; - $statusColor = $this->classColorMap[$statusLabelsArray[$ticket['projectId']][$ticket['status']]["class"]]; + if ($timeStart == '00:00' && ($timeEnd == '00:00' || $timeEnd == '23:59')) { + $allDay = true; } else { - $statusName = ""; - $statusColor = "var(--grey)"; + $allDay = false; } - $backgroundColor = "var(--accent2)"; + $dateFrom = $this->dateTimeHelper->getTimestamp($ticket['editFrom']); + $dateTo = $this->dateTimeHelper->getTimestamp($ticket['editTo']); + $context = $this->language->__("label.planned_edit"); + + $newValues[] = $this->mapEventData( + title: $context . $ticket['headline'] . " (" . $statusName . ")", + allDay:$allDay, + id: $ticket['id'], + projectId: $ticket['projectId'], + eventType: "ticket", + dateContext: "edit", + backgroundColor: $backgroundColor, + borderColor: $statusColor, + dateFrom: $dateFrom, + dateTo: $dateTo + ); + } + } + } - $context = ""; - if ($ticket['dateToFinish'] != "0000-00-00 00:00:00" && $ticket['dateToFinish'] != "1969-12-31 00:00:00") { + return $newValues; + } + /** + * Generates an event array for fullcalendar.io frontend. + * + * @param string $title + * @param bool $allDay + * @param int $id + * @param int $projectId + * @param string $eventType + * @param string $dateContext + * @param string $backgroundColor + * @param string $borderColor + * @param int|null $dateFrom + * @param int|null $dateTo + * + * @return array + */ + private function mapEventData( + string $title, + bool $allDay, + int $id, + int $projectId, + string $eventType, + string $dateContext, + string $backgroundColor, + string $borderColor, + ?int $dateFrom, + ?int $dateTo + ): array { + return array( + 'title' => $title, + 'allDay' => $allDay, + 'dateFrom' => array( + 'y' => date('Y', $dateFrom), + 'm' => date('m', $dateFrom), + 'd' => date('d', $dateFrom), + 'h' => date('H', $dateFrom), + 'i' => date('i', $dateFrom), + 'ical' => date('Ymd\THis', $dateFrom), + ), + 'dateTo' => array( + 'y' => date('Y', $dateTo), + 'm' => date('m', $dateTo), + 'd' => date('d', $dateTo), + 'h' => date('H', $dateTo), + 'i' => date('i', $dateTo), + 'ical' => date('Ymd\THis', $dateTo), + ), + 'id' => $id, + 'projectId' => $projectId, + 'eventType' => $eventType, + 'dateContext' => $dateContext, + 'backgroundColor' => $backgroundColor, + 'borderColor' => $borderColor, + ); + } - $dateFrom = $this->dateTimeHelper->getTimestamp($ticket['dateToFinish']); - $dateTo = $this->dateTimeHelper->getTimestamp($ticket['dateToFinish']); - $context = '❕ ' . $this->language->__("label.due_todo"); + /** + * @param string $userHash + * @param string $calHash + * + * @return array|false + * + * @throws BindingResolutionException + */ + public function getCalendarBySecretHash(string $userHash, string $calHash): false|array + { + // get user + $userRepo = app()->make(Users::class); + $user = $userRepo->getUserBySha($userHash); - $newValues[] = $this->mapEventData( - title:$context . $ticket['headline'] . " (" . $statusName . ")", - allDay:false, - id: $ticket['id'], - projectId: $ticket['projectId'], - eventType: "ticket", - dateContext: "edit", - backgroundColor: $backgroundColor, - borderColor: $statusColor, - dateFrom: $dateFrom, - dateTo: $dateTo - ); - } + if (!isset($user['id'])) { + return false; + } - if ($ticket['editFrom'] != "0000-00-00 00:00:00" && $ticket['editFrom'] != "1969-12-31 00:00:00") { - - //Set ticket to all day ticket when no time is set - $timeStart = format($ticket['editFrom'])->time24(); - $timeEnd = format($ticket['editTo'])->time24(); - - if($timeStart == '00:00' && - ($timeEnd == '00:00' || $timeEnd == '23:59')){ - $allDay = true; - }else{ - $allDay = false; - } - - $dateFrom = $this->dateTimeHelper->getTimestamp($ticket['editFrom']); - $dateTo = $this->dateTimeHelper->getTimestamp($ticket['editTo']); - $context = $this->language->__("label.planned_edit"); - - $newValues[] = $this->mapEventData( - title: $context . $ticket['headline'] . " (" . $statusName . ")", - allDay:$allDay, - id: $ticket['id'], - projectId: $ticket['projectId'], - eventType: "ticket", - dateContext: "edit", - backgroundColor: $backgroundColor, - borderColor: $statusColor, - dateFrom: $dateFrom, - dateTo: $dateTo - ); - } - } - } + // Check if setting exists + $settingService = app()->make(Setting::class); + $hash = $settingService->getSetting("usersettings." . $user['id'] . ".icalSecret"); - return $newValues; + $_SESSION['usersettings.timezone'] ??= $settingService->getSetting("usersettings." . $user['id'] . ".timezone") ?: $this->config->defaultTimezone; + date_default_timezone_set($_SESSION['usersettings.timezone']); + + if ($hash !== false && $calHash == $hash) { + return $this->getCalendar($user['id']); + } else { + return false; } + } - /** - * Generates event array for fullcalendar.io frontend. - * - * @param string $title - * @param bool $allDay - * @param int $id - * @param int $projectId - * @param string $eventType - * @param string $dateContext - * @param string $backgroundColor - * @param string $borderColor - * @param int|null $dateFrom - * @param int|null $dateTo - * @return array - */ - private function mapEventData( - string $title, - bool $allDay, - int $id, - int $projectId, - string $eventType, - string $dateContext, - string $backgroundColor, - string $borderColor, - ?int $dateFrom, - ?int $dateTo): array - { - - return array( - 'title' => $title, - 'allDay' => $allDay, + /** + * @param int $id + * + * @return array + */ + public function getCalendarEventsForToday(int $id): array + { + $userTickets = "SELECT + tickets.dateToFinish, + tickets.headline, + tickets.id, + tickets.editFrom, + tickets.editTo + FROM zp_tickets AS tickets + WHERE + (tickets.userId = :userId OR tickets.editorId = :userId) + AND + ( + TO_DAYS(tickets.dateToFinish) = TO_DAYS(CURDATE()) OR + (TO_DAYS(tickets.editFrom) <= TO_DAYS(CURDATE()) AND TO_DAYS(tickets.editTo) >= TO_DAYS(CURDATE()) ) + )"; + + $stmn = $this->db->database->prepare($userTickets); + $stmn->bindValue(':userId', $id, PDO::PARAM_INT); + + $stmn->execute(); + $tickets = $stmn->fetchAll(); + $stmn->closeCursor(); + + $sql = "SELECT * FROM zp_calendar WHERE userId = :userId AND TO_DAYS(dateFrom) = TO_DAYS(CURDATE())"; + + $stmn = $this->db->database->prepare($sql); + $stmn->bindValue(':userId', $id, PDO::PARAM_INT); + + $stmn->execute(); + $values = $stmn->fetchAll(); + $stmn->closeCursor(); + + $newValues = array(); + foreach ($values as $value) { + $dateFrom = strtotime($value['dateFrom']); + $dateTo = strtotime($value['dateTo']); + + $newValues[] = array( + 'title' => $value['description'], + 'allDay' => $value['allDay'], 'dateFrom' => array( 'y' => date('Y', $dateFrom), 'm' => date('m', $dateFrom), 'd' => date('d', $dateFrom), 'h' => date('H', $dateFrom), 'i' => date('i', $dateFrom), - 'ical' => date('Ymd\THis', $dateFrom), ), 'dateTo' => array( 'y' => date('Y', $dateTo), @@ -279,106 +375,35 @@ private function mapEventData( 'd' => date('d', $dateTo), 'h' => date('H', $dateTo), 'i' => date('i', $dateTo), - 'ical' => date('Ymd\THis', $dateTo), ), - 'id' => $id, - 'projectId' => $projectId, - 'eventType' => $eventType, - 'dateContext' => $dateContext, - 'backgroundColor' => $backgroundColor, - 'borderColor' => $borderColor, + 'id' => $value['id'], + 'eventType' => "calendar", ); } + if (count($tickets)) { + foreach ($tickets as $ticket) { + if ($ticket['dateToFinish'] != "0000-00-00 00:00:00") { + $current = strtotime(date("Y-m-d")); + $date = strtotime(date("Y-m-d", strtotime($ticket['dateToFinish']))); - /** - * @param $userHash - * @param $calHash - * @return array|false - * @throws BindingResolutionException - */ - /** - * @param $userHash - * @param $calHash - * @return array|false - * @throws BindingResolutionException - */ - public function getCalendarBySecretHash($userHash, $calHash): false|array - { - //get user - $userRepo = app()->make(Users::class); - $user = $userRepo->getUserBySha($userHash); - - - if (!isset($user['id'])) { - return false; - } - - //Check if setting exists - $settingService = app()->make(Setting::class); - $hash = $settingService->getSetting("usersettings." . $user['id'] . ".icalSecret"); - - $_SESSION['usersettings.timezone'] ??= $settingService->getSetting("usersettings.". $user['id'] .".timezone") ?: $this->config->defaultTimezone; - date_default_timezone_set($_SESSION['usersettings.timezone']); - - if ($hash !== false && $calHash == $hash) { - return $this->getCalendar($user['id']); - } else { - return false; - } - } - - /** - * @param $id - * @return array - */ - /** - * @param $id - * @return array - */ - public function getCalendarEventsForToday($id): array - { - - - $userTickets = "SELECT - tickets.dateToFinish, - tickets.headline, - tickets.id, - tickets.editFrom, - tickets.editTo - FROM zp_tickets AS tickets - WHERE - (tickets.userId = :userId OR tickets.editorId = :userId) - AND - ( - TO_DAYS(tickets.dateToFinish) = TO_DAYS(CURDATE()) OR - (TO_DAYS(tickets.editFrom) <= TO_DAYS(CURDATE()) AND TO_DAYS(tickets.editTo) >= TO_DAYS(CURDATE()) ) - )"; - - $stmn = $this->db->database->prepare($userTickets); - $stmn->bindValue(':userId', $id, PDO::PARAM_INT); - - $stmn->execute(); - $tickets = $stmn->fetchAll(); - $stmn->closeCursor(); - - $sql = "SELECT * FROM zp_calendar WHERE userId = :userId AND TO_DAYS(dateFrom) = TO_DAYS(CURDATE())"; - - $stmn = $this->db->database->prepare($sql); - $stmn->bindValue(':userId', $id, PDO::PARAM_INT); - - $stmn->execute(); - $values = $stmn->fetchAll(); - $stmn->closeCursor(); + $datediff = $date - $current; + $difference = floor($datediff / (60 * 60 * 24)); + } else { + $difference = 1; + } - $newValues = array(); - foreach ($values as $value) { - $dateFrom = strtotime($value['dateFrom']); - $dateTo = strtotime($value['dateTo']); + if ($difference == 0) { + $dateFrom = strtotime($ticket['dateToFinish']); + $dateTo = strtotime($ticket['dateToFinish']); + } else { + $dateFrom = strtotime($ticket['editFrom']); + $dateTo = strtotime($ticket['editTo']); + } $newValues[] = array( - 'title' => $value['description'], - 'allDay' => $value['allDay'], + 'title' => 'To-Do: ' . $ticket['headline'], + 'allDay' => false, 'dateFrom' => array( 'y' => date('Y', $dateFrom), 'm' => date('m', $dateFrom), @@ -393,348 +418,267 @@ public function getCalendarEventsForToday($id): array 'h' => date('H', $dateTo), 'i' => date('i', $dateTo), ), - 'id' => $value['id'], - 'eventType' => "calendar", + 'id' => $ticket['id'], + 'eventType' => "ticket", ); } + } - if (count($tickets)) { - foreach ($tickets as $ticket) { - if ($ticket['dateToFinish'] != "0000-00-00 00:00:00") { - $current = strtotime(date("Y-m-d")); - $date = strtotime(date("Y-m-d", strtotime($ticket['dateToFinish']))); - - $datediff = $date - $current; - $difference = floor($datediff / (60 * 60 * 24)); - } else { - $difference = 1; - } - - if ($difference == 0) { - $dateFrom = strtotime($ticket['dateToFinish']); - $dateTo = strtotime($ticket['dateToFinish']); - } else { - $dateFrom = strtotime($ticket['editFrom']); - $dateTo = strtotime($ticket['editTo']); - } + return $newValues; + } - $newValues[] = array( - 'title' => 'To-Do: ' . $ticket['headline'], - 'allDay' => false, - 'dateFrom' => array( - 'y' => date('Y', $dateFrom), - 'm' => date('m', $dateFrom), - 'd' => date('d', $dateFrom), - 'h' => date('H', $dateFrom), - 'i' => date('i', $dateFrom), - ), - 'dateTo' => array( - 'y' => date('Y', $dateTo), - 'm' => date('m', $dateTo), - 'd' => date('d', $dateTo), - 'h' => date('H', $dateTo), - 'i' => date('i', $dateTo), - ), - 'id' => $ticket['id'], - 'eventType' => "ticket", - ); - } - } + /** + * @return array|false + */ + public function getTicketWishDates(): false|array + { + $query = "SELECT id, headline, dateToFinish FROM zp_tickets WHERE (userId = :userId OR editorId = :userId) AND dateToFinish <> '000-00-00 00:00:00'"; - return $newValues; - } + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn->execute(); + $values = $stmn->fetchAll(); + $stmn->closeCursor(); - /** - * @return array|false - */ - /** - * @return array|false - */ - public function getTicketWishDates(): false|array - { + return $values; + } - $query = "SELECT id, headline, dateToFinish FROM zp_tickets WHERE (userId = :userId OR editorId = :userId) AND dateToFinish <> '000-00-00 00:00:00'"; + /** + * @return array|false + */ + public function getTicketEditDates(): false|array + { + $query = "SELECT id, headline, editFrom, editTo FROM zp_tickets WHERE (userId = :userId OR editorId = :userId) AND editFrom <> '000-00-00 00:00:00'"; - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $stmn->execute(); - $values = $stmn->fetchAll(); - $stmn->closeCursor(); - - return $values; - } + $stmn->execute(); + $values = $stmn->fetchAll(); + $stmn->closeCursor(); + return $values; + } - /** - * @return array|false - */ - /** - * @return array|false - */ - public function getTicketEditDates(): false|array - { + /** + * @param array $values + * + * @return false|string + */ + public function addEvent(array $values): false|string + { + $query = "INSERT INTO zp_calendar (userId, dateFrom, dateTo, description, allDay) VALUES (:userId, :dateFrom, :dateTo, :description, :allDay)"; - $query = "SELECT id, headline, editFrom, editTo FROM zp_tickets WHERE (userId = :userId OR editorId = :userId) AND editFrom <> '000-00-00 00:00:00'"; + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn->bindValue(':dateFrom', $values['dateFrom'], PDO::PARAM_STR); + $stmn->bindValue(':dateTo', $values['dateTo'], PDO::PARAM_STR); + $stmn->bindValue(':description', $values['description'], PDO::PARAM_STR); + $stmn->bindValue(':allDay', $values['allDay'], PDO::PARAM_STR); - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + if ($stmn->execute()) { + $id = $this->db->database->lastInsertId(); + $stmn->closeCursor(); - $stmn->execute(); - $values = $stmn->fetchAll(); + return $id; + } else { $stmn->closeCursor(); - return $values; + return false; } + } - /** - * @param $values - * @return false|string - */ - /** - * @param $values - * @return false|string - */ - public function addEvent($values): false|string - { - - $query = "INSERT INTO zp_calendar (userId, dateFrom, dateTo, description, allDay) - VALUES (:userId, :dateFrom, :dateTo, :description, :allDay)"; - - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $stmn->bindValue(':dateFrom', $values['dateFrom'], PDO::PARAM_STR); - $stmn->bindValue(':dateTo', $values['dateTo'], PDO::PARAM_STR); - $stmn->bindValue(':description', $values['description'], PDO::PARAM_STR); - $stmn->bindValue(':allDay', $values['allDay'], PDO::PARAM_STR); - - if ($stmn->execute()) { - $id = $this->db->database->lastInsertId(); - $stmn->closeCursor(); - return $id; - } else { - $stmn->closeCursor(); - return false; - } - } + /** + * @param int $id + * + * @return mixed + */ + public function getEvent(int $id): mixed + { + $query = "SELECT * FROM zp_calendar WHERE id = :id"; - /** - * @param $id - * @return mixed - */ - /** - * @param $id - * @return mixed - */ - public function getEvent($id): mixed - { + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':id', $id, PDO::PARAM_INT); - $query = "SELECT * FROM zp_calendar WHERE id = :id"; + $stmn->execute(); + $values = $stmn->fetch(); + $stmn->closeCursor(); - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':id', $id, PDO::PARAM_INT); + return $values; + } - $stmn->execute(); - $values = $stmn->fetch(); - $stmn->closeCursor(); + /** + * @param array $values + * @param int $id + * + * @return void + */ + public function editEvent(array $values, int $id): void + { - return $values; - } + $query = "UPDATE zp_calendar SET + dateFrom = :dateFrom, + dateTo = :dateTo, + description = :description, + allDay = :allDay + WHERE id = :id AND userId = :userId LIMIT 1"; - /** - * @param $values - * @param $id - * @return void - */ - /** - * @param $values - * @param $id - * @return void - */ - public function editEvent($values, $id): void - { - - $query = "UPDATE zp_calendar SET - dateFrom = :dateFrom, - dateTo = :dateTo, - description = :description, - allDay = :allDay - WHERE id = :id AND userId = :userId LIMIT 1"; - - - $stmn = $this->db->database->prepare($query); - - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $stmn->bindValue(':id', $id, PDO::PARAM_INT); - $stmn->bindValue(':dateFrom', $values['dateFrom'], PDO::PARAM_STR); - $stmn->bindValue(':dateTo', $values['dateTo'], PDO::PARAM_STR); - $stmn->bindValue(':description', $values['description'], PDO::PARAM_STR); - $stmn->bindValue(':allDay', $values['allDay'], PDO::PARAM_STR); - - $stmn->execute(); - $stmn->closeCursor(); - } + $stmn = $this->db->database->prepare($query); - /** - * @param $id - * @return bool - */ - /** - * @param $id - * @return bool - */ - public function delPersonalEvent($id): bool - { + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn->bindValue(':id', $id, PDO::PARAM_INT); + $stmn->bindValue(':dateFrom', $values['dateFrom'], PDO::PARAM_STR); + $stmn->bindValue(':dateTo', $values['dateTo'], PDO::PARAM_STR); + $stmn->bindValue(':description', $values['description'], PDO::PARAM_STR); + $stmn->bindValue(':allDay', $values['allDay'], PDO::PARAM_STR); - $query = "DELETE FROM zp_calendar WHERE id = :id AND userId = :userId LIMIT 1"; + $stmn->execute(); + $stmn->closeCursor(); + } - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':id', $id, PDO::PARAM_INT); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + /** + * @param int $id + * + * @return int|false + */ + public function delPersonalEvent(int $id): int|false + { + $query = "DELETE FROM zp_calendar WHERE id = :id AND userId = :userId LIMIT 1"; - $value = $stmn->execute(); - $stmn->closeCursor(); + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':id', $id, PDO::PARAM_INT); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - return $value; - } + $value = $stmn->execute(); + $stmn->closeCursor(); - /** - * @return array|false - */ - public function getMyExternalCalendars($userId): false|array - { + return $value; + } - $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId"; + /** + * @param int $userId + * + * @return array|false + */ + public function getMyExternalCalendars(int $userId): false|array + { + $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId"; - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); - $stmn->execute(); - $values = $stmn->fetchAll(); - $stmn->closeCursor(); + $stmn->execute(); + $values = $stmn->fetchAll(); + $stmn->closeCursor(); - return $values; - } + return $values; + } - /** - * @return array|false - */ - public function getExternalCalendar($calendarId, $userId): false|array - { + /** + * @param int $calendarId + * @param int $userId + * + * @return array|false + */ + public function getExternalCalendar(int $calendarId, int $userId): false|array + { + $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; - $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); + $stmn->bindValue(':id', $calendarId, PDO::PARAM_INT); - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $userId, PDO::PARAM_INT); - $stmn->bindValue(':id', $calendarId, PDO::PARAM_INT); + $stmn->execute(); + $values = $stmn->fetch(); + $stmn->closeCursor(); - $stmn->execute(); - $values = $stmn->fetch(); - $stmn->closeCursor(); + return $values; + } - return $values; - } + /** + * @param int $id + * + * @return mixed + */ + public function getGCal(int $id): mixed + { + $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn->bindValue(':id', $id, PDO::PARAM_INT); - /** - * @param $id - * @return mixed - */ - public function getGCal($id): mixed - { + $stmn->execute(); + $values = $stmn->fetch(); + $stmn->closeCursor(); - $query = "SELECT id, url, name, colorClass FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; + return $values; + } - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $stmn->bindValue(':id', $id, PDO::PARAM_INT); + /** + * @param array $values + * @param int $id + * + * @return void + */ + public function editGUrl(array $values, int $id): void + { - $stmn->execute(); - $values = $stmn->fetch(); - $stmn->closeCursor(); + $query = "UPDATE zp_gcallinks SET + url = :url, + name = :name, + colorClass = :colorClass + WHERE userId = :userId AND id = :id LIMIT 1"; + + $stmn = $this->db->database->prepare($query); + $stmn->bindValue(':name', $values['name'], PDO::PARAM_STR); + $stmn->bindValue(':url', $values['url'], PDO::PARAM_STR); + $stmn->bindValue(':colorClass', $values['colorClass'], PDO::PARAM_STR); + $stmn->bindValue(':id', $id, PDO::PARAM_INT); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + + $stmn->execute(); + $stmn->closeCursor(); + } - return $values; - } + /** + * @param int $id + * + * @return bool + */ + public function deleteGCal(int $id): bool + { + $query = "DELETE FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; - /** - * @param $values - * @param $id - * @return void - */ - /** - * @param $values - * @param $id - * @return void - */ - public function editGUrl($values, $id): void - { - - $query = "UPDATE zp_gcallinks SET - url = :url, - name = :name, - colorClass = :colorClass - WHERE userId = :userId AND id = :id LIMIT 1"; - - $stmn = $this->db->database->prepare($query); - $stmn->bindValue(':name', $values['name'], PDO::PARAM_STR); - $stmn->bindValue(':url', $values['url'], PDO::PARAM_STR); - $stmn->bindValue(':colorClass', $values['colorClass'], PDO::PARAM_STR); - $stmn->bindValue(':id', $id, PDO::PARAM_INT); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - - $stmn->execute(); - $stmn->closeCursor(); - } + $stmn = $this->db->database->prepare($query); - /** - * @param $id - * @return void - */ - /** - * @param $id - * @return void - */ - public function deleteGCal($id): bool - { + $stmn->bindValue(':id', $id, PDO::PARAM_INT); + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $query = "DELETE FROM zp_gcallinks WHERE userId = :userId AND id = :id LIMIT 1"; + $result = $stmn->execute(); + $stmn->closeCursor(); - $stmn = $this->db->database->prepare($query); + return $result; + } + + /** + * @param array $values + * @return void + */ + public function addGUrl(array $values): void + { - $stmn->bindValue(':id', $id, PDO::PARAM_INT); - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $query = "INSERT INTO zp_gcallinks (userId, name, url, colorClass) VALUES (:userId, :name, :url, :colorClass)"; - $result = $stmn->execute(); - $stmn->closeCursor(); + $stmn = $this->db->database->prepare($query); - return $result; - } + $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); + $stmn->bindValue(':name', $values['name'], PDO::PARAM_STR); + $stmn->bindValue(':url', $values['url'], PDO::PARAM_STR); + $stmn->bindValue(':colorClass', $values['colorClass'], PDO::PARAM_STR); - /** - * @param $values - * @return void - */ - /** - * @param $values - * @return void - */ - public function addGUrl($values): void - { - - $query = "INSERT INTO zp_gcallinks (userId, name, url, colorClass) - VALUES - (:userId, :name, :url, :colorClass)"; - - $stmn = $this->db->database->prepare($query); - - $stmn->bindValue(':userId', $_SESSION['userdata']['id'], PDO::PARAM_INT); - $stmn->bindValue(':name', $values['name'], PDO::PARAM_STR); - $stmn->bindValue(':url', $values['url'], PDO::PARAM_STR); - $stmn->bindValue(':colorClass', $values['colorClass'], PDO::PARAM_STR); - - $stmn->execute(); - $stmn->closeCursor(); - } + $stmn->execute(); + $stmn->closeCursor(); } } diff --git a/app/Domain/Calendar/Services/Calendar.php b/app/Domain/Calendar/Services/Calendar.php index d3d2f3257..c26980ab2 100644 --- a/app/Domain/Calendar/Services/Calendar.php +++ b/app/Domain/Calendar/Services/Calendar.php @@ -1,204 +1,215 @@ calendarRepo = $calendarRepo; + $this->language = $language; + } /** + * Deletes a Google Calendar. * + * @param int $id The ID of the Google Calendar to delete. + * + * @return bool Returns true if the Google Calendar was successfully deleted, false otherwise. */ - class Calendar + public function deleteGCal(int $id): bool { - private CalendarRepository $calendarRepo; - private LanguageCore $language; - - /** - * @param CalendarRepository $calendarRepo - * @param LanguageCore $language - */ - public function __construct(CalendarRepository $calendarRepo, LanguageCore $language) - { - $this->calendarRepo = $calendarRepo; - $this->language = $language; - } + return $this->calendarRepo->deleteGCal($id); + } - /** - * Deletes a Google Calendar. - * - * @param int $id The ID of the Google Calendar to delete. - * @return bool Returns true if the Google Calendar was successfully deleted, false otherwise. - */ - public function deleteGCal(int $id): bool - { - return $this->calendarRepo->deleteGCal($id); + /** + * Patches calendar event + * + * @access public + * + * @params $id id of event to be updated (only events can be updated. Tickets need to be updated via ticket api + * @params $params key value array of columns to be updated + * + * @return bool true on success, false on failure + */ + public function patch($id, $params): bool + { + // Admins can always change anything. + // Otherwise user has to own the event + if ($this->userIsAllowedToUpdate($id)) { + return $this->calendarRepo->patch($id, $params); } - /** - * Patches calendar event - * - * @access public - * @params $id id of event to be updated (only events can be updated. Tickets need to be updated via ticket api - * @params $params key value array of columns to be updated - * - * @return bool true on success, false on failure - */ - public function patch($id, $params): bool - { - - //Admins can always change anything. - //Otherwise user has to own the event - if ($this->userIsAllowedToUpdate($id)) { - return $this->calendarRepo->patch($id, $params); - } + return false; + } - return false; - } + /** + * Checks if user is allowed to make changes to event + * + * @access public + * + * @params int $eventId Id of event to be checked + * + * @return bool true on success, false on failure + */ + private function userIsAllowedToUpdate($eventId): bool + { - /** - * Checks if user is allowed to make changes to event - * - * @access public - * @params int $eventId Id of event to be checked - * - * @return bool true on success, false on failure - */ - private function userIsAllowedToUpdate($eventId): bool - { - - if (Auth::userIsAtLeast(Roles::$admin)) { + if (Auth::userIsAtLeast(Roles::$admin)) { + return true; + } else { + $event = $this->calendarRepo->getEvent($eventId); + if ($event && $event["userId"] == $_SESSION['userdata']['id']) { return true; - } else { - $event = $this->calendarRepo->getEvent($eventId); - if ($event && $event["userId"] == $_SESSION['userdata']['id']) { - return true; - } } + } + + return false; + } + + /** + * Adds a new event to the user's calendar + * + * @access public + * + * @params array $values array of event values + * + * @return int|false returns the id on success, false on failure + */ + public function addEvent(array $values): int|false + { + $values['allDay'] = $values['allDay'] ?? false; + + $dateFrom = null; + if (isset($values['dateFrom']) === true && isset($values['timeFrom']) === true) { + $dateFrom = format($values['dateFrom'], $values['timeFrom'])->isoDateTimeFrom24h(); + } + $values['dateFrom'] = $dateFrom; + + $dateTo = null; + if (isset($values['dateTo']) === true && isset($values['timeTo']) === true) { + $dateTo = format($values['dateTo'], $values['timeTo'])->isoDateTimeFrom24h(); + } + $values['dateTo'] = $dateTo; + + if ($values['description'] !== '') { + $result = $this->calendarRepo->addEvent($values); + + return $result; + } else { return false; } + } + /** + * @param int $eventId + * + * @return mixed + */ + public function getEvent(int $eventId): mixed + { + return $this->calendarRepo->getEvent($eventId); + } - /** - * Adds a new event to the users calendar - * - * @access public - * @params array $values array of event values - * - * @return int|false returns the id on success, false on failure - */ - public function addEvent(array $values): int|false - { + /** + * edits an event on the user's calendar + * + * @access public + * + * @params array $values array of event values + * + * @return bool returns true on success, false on failure + */ + public function editEvent(array $values): bool + { + if (isset($values['id']) === true) { + $id = $values['id']; + $row = $this->calendarRepo->getEvent($id); - $values['allDay'] = $values['allDay'] ?? false; + if ($row === false) { + return false; + } + + if (isset($values['allDay']) === true) { + $allDay = 'true'; + } else { + $allDay = 'false'; + } + + $values['allDay'] = $allDay; $dateFrom = null; if (isset($values['dateFrom']) === true && isset($values['timeFrom']) === true) { - $dateFrom = format($values['dateFrom'], $values['timeFrom'])->isoDateTimeFrom24h(); + $dateFrom = format($values['dateFrom'], $values['timeFrom'])->isoDateTime(); } $values['dateFrom'] = $dateFrom; $dateTo = null; if (isset($values['dateTo']) === true && isset($values['timeTo']) === true) { - $dateTo = format($values['dateTo'], $values['timeTo'])->isoDateTimeFrom24h(); + $dateTo = format($values['dateTo'], $values['timeTo'])->isoDateTime(); } $values['dateTo'] = $dateTo; if ($values['description'] !== '') { - $result = $this->calendarRepo->addEvent($values); + $this->calendarRepo->editEvent($values, $id); - return $result; - } else { - return false; + return true; } } + return false; + } - /** - * @param $eventId - * @return mixed - */ - /** - * @param $eventId - * @return mixed - */ - public function getEvent($eventId): mixed - { - return $this->calendarRepo->getEvent($eventId); - } - - /** - * edits an event on the users calendar - * - * @access public - * @params array $values array of event values - * - * @return bool returns true on success, false on failure - */ - public function editEvent(array $values): bool - { - $id = null; - if (isset($values['id']) === true) { - $id = $values['id']; - - $row = $this->calendarRepo->getEvent($id); - - if ($row === false) { - return false; - } - - if (isset($values['allDay']) === true) { - $allDay = 'true'; - } else { - $allDay = 'false'; - } - - $values['allDay'] = $allDay; - - $dateFrom = null; - if (isset($values['dateFrom']) === true && isset($values['timeFrom']) === true) { - $dateFrom = format($values['dateFrom'], $values['timeFrom'])->isoDateTime(); - } - $values['dateFrom'] = $dateFrom; - - $dateTo = null; - if (isset($values['dateTo']) === true && isset($values['timeTo']) === true) { - $dateTo = format($values['dateTo'], $values['timeTo'])->isoDateTime(); - } - $values['dateTo'] = $dateTo; - - if ($values['description'] !== '') { - $this->calendarRepo->editEvent($values, $id); - - return true; - } - } - return false; - } - - /** - * deletes an event on the users calendar - * - * @access public - * @params array $values array of event values - * - * @return int|false returns the id on success, false on failure - */ - public function delEvent($id): int|false - { - $result = $this->calendarRepo->delPersonalEvent($id); - return $result; - } + /** + * deletes an event on the user's calendar + * + * @access public + * + * @param int $id + * + * @return int|false returns the id on success, false on failure + */ + public function delEvent(int $id): int|false + { + return $this->calendarRepo->delPersonalEvent($id); + } - public function getExternalCalendar($id, $userId) { - return $this->calendarRepo->getExternalCalendar($id, $userId); - } + /** + * @param int $id + * @param int $userId + * + * @return array|false + */ + public function getExternalCalendar(int $id, int $userId): bool|array + { + return $this->calendarRepo->getExternalCalendar($id, $userId); + } - public function editExternalCalendar($values, $id) { - return $this->calendarRepo->editGUrl($values, $id); - } + /** + * @param array $values + * @param int $id + * + * @return void + */ + public function editExternalCalendar(array $values, int $id): void + { + $this->calendarRepo->editGUrl($values, $id); } } diff --git a/app/Domain/Calendar/Templates/ical.tpl.php b/app/Domain/Calendar/Templates/ical.tpl.php index 808ebe1a4..e51f326c1 100644 --- a/app/Domain/Calendar/Templates/ical.tpl.php +++ b/app/Domain/Calendar/Templates/ical.tpl.php @@ -44,9 +44,8 @@ if ($trans['isdst']) { $t_dst = $trans['ts']; echo "BEGIN:DAYLIGHT" . $eol; - } - // standard time definition - else { + } else { + // standard time definition $t_std = $trans['ts']; echo "BEGIN:STANDARD" . $eol; } @@ -73,9 +72,8 @@ if ($trans['isdst']) { $t_dst = $trans['ts']; echo "END:DAYLIGHT" . $eol; - } - // standard time definition - else { + } else { + // standard time definition $t_std = $trans['ts']; echo "END:STANDARD" . $eol; } diff --git a/app/Domain/Calendar/Templates/showMyCalendar.tpl.php b/app/Domain/Calendar/Templates/showMyCalendar.tpl.php index 86fd1db2b..f7170dd2f 100644 --- a/app/Domain/Calendar/Templates/showMyCalendar.tpl.php +++ b/app/Domain/Calendar/Templates/showMyCalendar.tpl.php @@ -36,7 +36,7 @@
  • Projects & Tasks
  • - get('externalCalendars') as $calendars) { ?> + get('externalCalendars') as $calendars) { ?>