vendor\project-biz\portal-bundle\src\Service\NotificationsHelper.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace ProjectBiz\PortalBundle\Service;
  4. use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaComparison;
  5. use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaComposite;
  6. use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaConstant;
  7. use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaUnmappedColumn;
  8. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  9. use Symfony\Component\DependencyInjection\Container;
  10. use ProjectBiz\UserBundle\Service\SecurityContextWrapper;
  11. use ProjectBiz\DatabaseBundle\Database\GenericRepositoryFactoryInterface;
  12. class NotificationsHelper
  13. {
  14.     private $securityContextWrapper;
  15.     private $genericRepositoryFactory;
  16.     
  17.     public function __construct(
  18.         Container $container,
  19.         Router $router,
  20.         $dateTimeFormat,
  21.         SecurityContextWrapper $securityContextWrapper,
  22.         GenericRepositoryFactoryInterface $genericRepositoryFactory
  23.     )
  24.     {
  25.         $this->container $container;
  26.         $this->router $router;
  27.         $this->dateTimeFormat $dateTimeFormat;
  28.         $this->securityContextWrapper $securityContextWrapper;
  29.         $this->genericRepositoryFactory $genericRepositoryFactory;
  30.     }
  31.     /**
  32.      * Get the notifications with additional information of the current user.
  33.      *
  34.      * @return array
  35.      */
  36.     public function getNotifications()
  37.     {
  38.         $notifications = [];
  39.         if (array_key_exists('ProjectBizWatchDogBundle'$this->container->getParameter('kernel.bundles'))) {
  40.             $userId $this->securityContextWrapper->getUserId();
  41.             $qb $this->container->get('database_connection')->createQueryBuilder();
  42.             $qb->select('*')
  43.                 ->from('Tab_WatchDogNotification''wn')
  44.                 ->where('WatchDogNotification_LINK_User_ID = ' $qb->createNamedParameter($userId))
  45.                 ->andWhere('WatchDogNotification_Active = 1')
  46.                 ->leftJoin('wn''Tab_WatchDog''w''wn.WatchDogNotification_LINK_WatchDog_ID = w.WatchDog_ID')
  47.                 ->leftJoin('w''Tab_TableDefinition''t''w.WatchDog_Table = t.TableDefinition_Tablename')
  48.                 ->orderBy('WatchDogNotification_Current_Value''asc')
  49.                 ->setMaxResults(10);
  50.             $notifications $qb->execute()->fetchAll();
  51.             foreach ($notifications as &$notification) {
  52.                 if ($notification['TableDefinition_MainRoute']) {
  53.                     $notification['route'] = $this->router->generate($notification['TableDefinition_MainRoute'], ['id' => $notification['WatchDogNotification_LINK_Target_ID']]);
  54.                 } else {
  55.                     $notification['route'] = null;
  56.                 }
  57.             }
  58.         }
  59.         return $notifications;
  60.     }
  61.     /**
  62.      * Get custom notifications as JSON.
  63.      *
  64.      * @return array
  65.      */
  66.     public function getCustomNotifications() : string
  67.     {
  68.         $repositoryFactory $this->container->get('citibiz.generic_repository_factory');
  69.         $repo $repositoryFactory->createGenericRepository('CustomNotification');
  70.         /*
  71.          * Retrieve all notifications of the current user that were not shown, yet.
  72.          */
  73.         $notificationsRows $repo->fetchAll(
  74.             $repo->findBy(
  75.                 new CriteriaComposite(
  76.                     'and',
  77.                     [
  78.                         new CriteriaComparison(
  79.                             '=',
  80.                             new CriteriaUnmappedColumn('CustomNotification_Owner_Link_User_ID'),
  81.                             new CriteriaConstant($this->container->get('citibiz.security_context_wrapper')->getUserId())
  82.                         ),
  83.                         new CriteriaComparison(
  84.                             '<>',
  85.                             new CriteriaUnmappedColumn('CustomNotification_Shown'),
  86.                             new CriteriaConstant(1)
  87.                         ),
  88.                         /* enable this to not retrieve notifications in the past
  89.                         new CriteriaComparison(
  90.                             '>',
  91.                             new CriteriaUnmappedColumn('CustomNotification_Time'),
  92.                             new CriteriaConstant((new \DateTime())->format($repo->getConnection()->getDatabasePlatform()->getDateTimeFormatString()))
  93.                         ),
  94.                         */
  95.                     ]
  96.                 )
  97.             )
  98.         ) ?? [];
  99.         $notifications = [];
  100.         /*
  101.          * Loop over database result to convert notifications into neccessary target format.
  102.          */
  103.         foreach ($notificationsRows as $key => $notificationRow) {
  104.             $additionalInfo null;
  105.             if ($notificationRow['REF_CustomNotification_Link_CRM_ID']['display']) {
  106.                 $additionalInfo[] = $notificationRow['REF_CustomNotification_Link_CRM_ID']['display'];
  107.             }
  108.             if ($notificationRow['CustomNotification_Info_01']) {
  109.                 $additionalInfo[] .= $notificationRow['CustomNotification_Info_01'];
  110.             }
  111.             if ($notificationRow['CustomNotification_Info_02']) {
  112.                 $additionalInfo[] .= $notificationRow['CustomNotification_Info_02'];
  113.             }
  114.             if ($additionalInfo) {
  115.                 $additionalInfo "\n" implode("\n"$additionalInfo);
  116.             }
  117.             $notifications[$key]['id']     = $notificationRow['CustomNotification_ID'];
  118.             $notifications[$key]['output'] = "Erinnerung:\n– " $notificationRow['CustomNotification_Name'] . " –\nFällig: " $notificationRow['CustomNotification_Time'] . " Uhr" $additionalInfo;
  119.             $notifications[$key]['time']   = ($notificationRow['CustomNotification_Time']->format('U') - time() - ($notificationRow['CustomNotification_Remembertime'] * 60)) * 1000;
  120.         }
  121.         return json_encode($notifications);
  122.     }
  123. }