diff --git a/src/Calendar/CalendarService.php b/src/Calendar/CalendarService.php new file mode 100644 index 00000000..2f6126b4 --- /dev/null +++ b/src/Calendar/CalendarService.php @@ -0,0 +1,102 @@ +configuration = $configuration; + $this->repository = $repository; + $this->dispatcher = $dispatcher; + } + + /** + * @param User $user + * @return DragAndDropSource[] + * @throws \Exception + */ + public function getDragAndDropResources(User $user): array + { + $maxAmount = $this->configuration->getCalendarDragAndDropMaxEntries(); + $event = new CalendarDragAndDropSourceEvent($user, $maxAmount); + + if ($maxAmount < 1) { + return []; + } + + $data = $this->repository->getRecentActivities( + $user, + DateTimeFactory::createByUser($user)->createDateTime('-1 year'), + $maxAmount + ); + + $recentActivity = new RecentActivityEvent($user, $data); + $this->dispatcher->dispatch($recentActivity); + + $entries = []; + $colorHelper = new Color(); + foreach ($recentActivity->getRecentActivities() as $timesheet) { + $entries[] = new TimesheetEntry($timesheet, $colorHelper->getTimesheetColor($timesheet)); + } + + $event->addSource(new RecentActivitiesSource($entries)); + + $this->dispatcher->dispatch($event); + + return $event->getSources(); + } + + public function getGoogleSources(User $user): ?Google + { + $apiKey = $this->configuration->getCalendarGoogleApiKey(); + if ($apiKey === null) { + return null; + } + + $sources = []; + + foreach ($this->configuration->getCalendarGoogleSources() as $name => $config) { + $sources[] = new GoogleSource($name, $config['id'], $config['color']); + } + + $event = new CalendarGoogleSourceEvent($user); + $this->dispatcher->dispatch($event); + + foreach ($event->getSources() as $source) { + $sources[] = $source; + } + + return new Google($apiKey, $sources); + } +} diff --git a/src/Configuration/SystemConfiguration.php b/src/Configuration/SystemConfiguration.php index 7fc1b8ac..a4597515 100644 --- a/src/Configuration/SystemConfiguration.php +++ b/src/Configuration/SystemConfiguration.php @@ -170,6 +170,11 @@ class SystemConfiguration implements SystemBundleConfiguration return (string) $this->find('calendar.slot_duration'); } + public function getCalendarDragAndDropMaxEntries(): int + { + return (int) $this->find('calendar.dragdrop_amount'); + } + // ========== Customer configurations ========== public function getCustomerDefaultTimezone(): ?string diff --git a/src/Controller/CalendarController.php b/src/Controller/CalendarController.php index 29b39eb9..5fa1bec5 100644 --- a/src/Controller/CalendarController.php +++ b/src/Controller/CalendarController.php @@ -9,21 +9,11 @@ namespace App\Controller; -use App\Calendar\DragAndDropSource; -use App\Calendar\Google; -use App\Calendar\GoogleSource; -use App\Calendar\RecentActivitiesSource; -use App\Calendar\TimesheetEntry; +use App\Calendar\CalendarService; use App\Configuration\SystemConfiguration; -use App\Event\CalendarDragAndDropSourceEvent; -use App\Event\CalendarGoogleSourceEvent; -use App\Event\RecentActivityEvent; -use App\Repository\TimesheetRepository; use App\Timesheet\TrackingModeService; -use App\Utils\Color; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** * Controller used to display calendars. @@ -33,20 +23,17 @@ use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; */ class CalendarController extends AbstractController { - /** - * @var EventDispatcherInterface - */ - private $dispatcher; + private $calendarService; - public function __construct(EventDispatcherInterface $dispatcher) + public function __construct(CalendarService $calendarService) { - $this->dispatcher = $dispatcher; + $this->calendarService = $calendarService; } /** * @Route(path="/", name="calendar", methods={"GET"}) */ - public function userCalendar(SystemConfiguration $configuration, TrackingModeService $service, TimesheetRepository $repository) + public function userCalendar(SystemConfiguration $configuration, TrackingModeService $service) { $mode = $service->getActiveMode(); $factory = $this->getDateTimeFactory(); @@ -62,19 +49,24 @@ class CalendarController extends AbstractController 'slotDuration' => $configuration->getCalendarSlotDuration(), 'timeframeBegin' => $configuration->getCalendarTimeframeBegin(), 'timeframeEnd' => $configuration->getCalendarTimeframeEnd(), + 'dragDropAmount' => $configuration->getCalendarDragAndDropMaxEntries(), ]; $isPunchMode = !$mode->canEditDuration() && !$mode->canEditBegin() && !$mode->canEditEnd(); $dragAndDrop = []; if ($mode->canEditBegin()) { - $dragAndDrop = $this->getDragAndDropResources($repository); + try { + $dragAndDrop = $this->calendarService->getDragAndDropResources($this->getUser()); + } catch (\Exception $ex) { + $this->logException($ex); + } } return $this->render('calendar/user.html.twig', [ 'config' => $config, 'dragAndDrop' => $dragAndDrop, - 'google' => $this->getGoogleSources($configuration), + 'google' => $this->calendarService->getGoogleSources($this->getUser()), 'now' => $factory->createDateTime(), 'defaultStartTime' => $defaultStart->format('h:i:s'), 'is_punch_mode' => $isPunchMode, @@ -83,60 +75,4 @@ class CalendarController extends AbstractController 'can_edit_duration' => $mode->canEditDuration(), ]); } - - /** - * @return DragAndDropSource[] - */ - private function getDragAndDropResources(TimesheetRepository $repository): array - { - $event = new CalendarDragAndDropSourceEvent($this->getUser()); - - try { - $data = $repository->getRecentActivities( - $this->getUser(), - $this->getDateTimeFactory()->createDateTime('-1 year'), - 10 - ); - - $recentActivity = new RecentActivityEvent($this->getUser(), $data); - $this->dispatcher->dispatch($recentActivity); - - $entries = []; - $colorHelper = new Color(); - foreach ($recentActivity->getRecentActivities() as $timesheet) { - $entries[] = new TimesheetEntry($timesheet, $colorHelper->getTimesheetColor($timesheet)); - } - - $event->addSource(new RecentActivitiesSource($entries)); - } catch (\Exception $ex) { - $this->logException($ex); - } - - $this->dispatcher->dispatch($event); - - return $event->getSources(); - } - - private function getGoogleSources(SystemConfiguration $configuration): ?Google - { - $apiKey = $configuration->getCalendarGoogleApiKey(); - if ($apiKey === null) { - return null; - } - - $sources = []; - - foreach ($configuration->getCalendarGoogleSources() as $name => $config) { - $sources[] = new GoogleSource($name, $config['id'], $config['color']); - } - - $event = new CalendarGoogleSourceEvent($this->getUser()); - $this->dispatcher->dispatch($event); - - foreach ($event->getSources() as $source) { - $sources[] = $source; - } - - return new Google($apiKey, $sources); - } } diff --git a/src/Controller/SystemConfigurationController.php b/src/Controller/SystemConfigurationController.php index d4634531..bbf48d19 100644 --- a/src/Controller/SystemConfigurationController.php +++ b/src/Controller/SystemConfigurationController.php @@ -44,6 +44,7 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotNull; +use Symfony\Component\Validator\Constraints\Range; use Symfony\Component\Validator\Constraints\Regex; /** @@ -582,6 +583,11 @@ final class SystemConfigurationController extends AbstractController ->setTranslationDomain('system-configuration') ->setType(TextType::class) ->setConstraints([new Regex(['pattern' => '/[0-2]{1}[0-9]{1}:[0-9]{2}:[0-9]{2}/']), new NotNull()]), + (new Configuration()) + ->setName('calendar.dragdrop_amount') + ->setTranslationDomain('system-configuration') + ->setType(IntegerType::class) + ->setConstraints([new Range(['min' => 0, 'max' => 20]), new NotNull()]), ]), (new SystemConfigurationModel()) ->setSection(SystemConfigurationModel::SECTION_BRANDING) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8223cef7..ebadf0b0 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -391,6 +391,19 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->booleanNode('weekends')->defaultTrue()->end() + ->integerNode('dragdrop_amount') + ->defaultValue(10) + ->validate() + ->ifTrue(static function ($v) { + if ($v === null || $v < 0 || $v > 20) { + return true; + } + + return false; + }) + ->thenInvalid('The dragdrop_amount must be between 0 and 20') + ->end() + ->end() ->end() ; diff --git a/src/Event/CalendarDragAndDropSourceEvent.php b/src/Event/CalendarDragAndDropSourceEvent.php index 91068a7d..19cd75a5 100644 --- a/src/Event/CalendarDragAndDropSourceEvent.php +++ b/src/Event/CalendarDragAndDropSourceEvent.php @@ -23,10 +23,15 @@ final class CalendarDragAndDropSourceEvent extends Event * @var DragAndDropSource[] */ private $sources = []; + /** + * @var int + */ + private $maxEntries = 0; - public function __construct(User $user) + public function __construct(User $user, int $maxEntries) { $this->user = $user; + $this->maxEntries = $maxEntries; } public function getUser(): User @@ -34,6 +39,11 @@ final class CalendarDragAndDropSourceEvent extends Event return $this->user; } + public function getMaxEntries(): int + { + return $this->maxEntries; + } + public function addSource(DragAndDropSource $source): CalendarDragAndDropSourceEvent { $this->sources[] = $source; diff --git a/src/Repository/TimesheetRepository.php b/src/Repository/TimesheetRepository.php index d207b2ff..b2aad746 100644 --- a/src/Repository/TimesheetRepository.php +++ b/src/Repository/TimesheetRepository.php @@ -972,7 +972,7 @@ class TimesheetRepository extends EntityRepository * @return array|mixed * @throws \Doctrine\ORM\Query\QueryException */ - public function getRecentActivities(User $user = null, DateTime $startFrom = null, $limit = 10) + public function getRecentActivities(User $user = null, DateTime $startFrom = null, int $limit = 10) { $qb = $this->getEntityManager()->createQueryBuilder(); diff --git a/templates/calendar/user.html.twig b/templates/calendar/user.html.twig index 3f63dc19..c7c72f41 100644 --- a/templates/calendar/user.html.twig +++ b/templates/calendar/user.html.twig @@ -10,7 +10,7 @@ {% block main %}
- {% set hasDragAndDrop = (dragAndDrop is not empty and (dragAndDrop|filter(s => s.entries|length > 0)|length > 0)) %} + {% set hasDragAndDrop = (config.dragDropAmount > 0 and dragAndDrop is not empty and (dragAndDrop|filter(s => s.entries|length > 0)|length > 0)) %} {% if hasDragAndDrop %}