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

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.     public function __construct(
  17.         Container $container,
  18.         Router $router,
  19.         $dateTimeFormat,
  20.         SecurityContextWrapper $securityContextWrapper,
  21.         GenericRepositoryFactoryInterface $genericRepositoryFactory
  22.     )
  23.     {
  24.         $this->container $container;
  25.         $this->router $router;
  26.         $this->dateTimeFormat $dateTimeFormat;
  27.         $this->securityContextWrapper $securityContextWrapper;
  28.         $this->genericRepositoryFactory $genericRepositoryFactory;
  29.     }
  30.     /**
  31.      * Get the notifications with additional information of the current user.
  32.      *
  33.      * @return array
  34.      */
  35.      public function getNotifications()
  36.      {
  37.          $securityContextWrapper $this->container->get('citibiz.security_context_wrapper');
  38.          $notifications = [];
  39.          if (array_key_exists('ProjectBizWatchDogBundle'$this->container->getParameter('kernel.bundles'))) {
  40.              $userId $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_WatchDogRoute']) {
  53.                      $notification['route'] = $this->router->generate($notification['TableDefinition_WatchDogRoute'], ['id' => $notification['WatchDogNotification_LINK_Target_ID']]);
  54.                  } else {
  55.                      $notification['route'] = null;
  56.                  }
  57.                  if ($notification['TableDefinition_TableRoute']) {
  58.                      $notification['tableroute'] = $this->router->generate($notification['TableDefinition_TableRoute']);
  59.                  } else {
  60.                      $notification['tableroute'] = null;
  61.                  }
  62.                 $notification['routewatchdog'] =$this->router->generate('watchdog_edit', ['id' => $notification['WatchDog_ID'], 'watched_tablename'=> $notification['WatchDog_Table'],'watched_columnname' =>  $notification['WatchDog_Column']]);
  63.                 $notification['routedone'] =$this->router->generate('bau_watchdognotificationlist_edit', ['id' => $notification['WatchDogNotification_ID'], ['tablename' => 'WatchDogNotification']]);
  64.                if (isset($notification['WatchDogNotification_Display_Columns_02'])){
  65.                 $notification['WatchDogNotification_Display_Columns_02']=strip_tags(htmlspecialchars_decode($notification['WatchDogNotification_Display_Columns_02']));
  66.              }
  67.            }
  68.          }
  69.          return $notifications;
  70.      }
  71.     /**
  72.      * Get custom notifications as JSON.
  73.      *
  74.      * @return array
  75.      */
  76.     public function getCustomNotifications() : string
  77.     {
  78.         $repositoryFactory $this->container->get('citibiz.generic_repository_factory');
  79.         $repo $repositoryFactory->createGenericRepository('CustomNotification');
  80.         /*
  81.          * Retrieve all notifications of the current user that were not shown, yet.
  82.          */
  83.         $notificationsRows $repo->fetchAll(
  84.             $repo->findBy(
  85.                 new CriteriaComposite(
  86.                     'and',
  87.                     [
  88.                         new CriteriaComparison(
  89.                             '=',
  90.                             new CriteriaUnmappedColumn('CustomNotification_Owner_Link_User_ID'),
  91.                             new CriteriaConstant($this->container->get('citibiz.security_context_wrapper')->getUserId())
  92.                         ),
  93.                         new CriteriaComparison(
  94.                             '<>',
  95.                             new CriteriaUnmappedColumn('CustomNotification_Shown'),
  96.                             new CriteriaConstant(1)
  97.                         ),
  98.                         /* enable this to not retrieve notifications in the past
  99.                         new CriteriaComparison(
  100.                             '>',
  101.                             new CriteriaUnmappedColumn('CustomNotification_Time'),
  102.                             new CriteriaConstant((new \DateTime())->format($repo->getConnection()->getDatabasePlatform()->getDateTimeFormatString()))
  103.                         ),
  104.                         */
  105.                     ]
  106.                 )
  107.             )
  108.         ) ?? [];
  109.         $notifications = [];
  110.         /*
  111.          * Loop over database result to convert notifications into neccessary target format.
  112.          */
  113.         foreach ($notificationsRows as $key => $notificationRow) {
  114.             $additionalInfo null;
  115.             if ($notificationRow['REF_CustomNotification_Link_CRM_ID']['display']) {
  116.                 $additionalInfo[] = $notificationRow['REF_CustomNotification_Link_CRM_ID']['display'];
  117.             }
  118.             if ($notificationRow['CustomNotification_Info_01']) {
  119.                 $additionalInfo[] .= $notificationRow['CustomNotification_Info_01'];
  120.             }
  121.             if ($notificationRow['CustomNotification_Info_02']) {
  122.                 $additionalInfo[] .= $notificationRow['CustomNotification_Info_02'];
  123.             }
  124.             if ($additionalInfo) {
  125.                 $additionalInfo "\n" implode("\n"$additionalInfo);
  126.             }
  127.             $notifications[$key]['id']     = $notificationRow['CustomNotification_ID'];
  128.             $notifications[$key]['output'] = "Erinnerung:\n– " $notificationRow['CustomNotification_Name'] . " –\nFällig: " $notificationRow['CustomNotification_Time'] . " Uhr" $additionalInfo;
  129.             $notifications[$key]['time']   = ($notificationRow['CustomNotification_Time']->format('U') - time() - ($notificationRow['CustomNotification_Remembertime'] * 60)) * 1000;
  130.         }
  131.         return json_encode($notifications);
  132.     }
  133. }