<?php
declare(strict_types = 1);
namespace ProjectBiz\PortalBundle\Service;
use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaComparison;
use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaComposite;
use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaConstant;
use ProjectBiz\DatabaseBundle\Database\Criteria\CriteriaUnmappedColumn;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\Container;
use ProjectBiz\UserBundle\Service\SecurityContextWrapper;
use ProjectBiz\DatabaseBundle\Database\GenericRepositoryFactoryInterface;
class NotificationsHelper
{
private $securityContextWrapper;
private $genericRepositoryFactory;
public function __construct(
Container $container,
Router $router,
$dateTimeFormat,
SecurityContextWrapper $securityContextWrapper,
GenericRepositoryFactoryInterface $genericRepositoryFactory
)
{
$this->container = $container;
$this->router = $router;
$this->dateTimeFormat = $dateTimeFormat;
$this->securityContextWrapper = $securityContextWrapper;
$this->genericRepositoryFactory = $genericRepositoryFactory;
}
/**
* Get the notifications with additional information of the current user.
*
* @return array
*/
public function getNotifications()
{
$securityContextWrapper = $this->container->get('citibiz.security_context_wrapper');
$notifications = [];
if (array_key_exists('ProjectBizWatchDogBundle', $this->container->getParameter('kernel.bundles'))) {
$userId = $securityContextWrapper->getUserId();
$qb = $this->container->get('database_connection')->createQueryBuilder();
$qb->select('*')
->from('Tab_WatchDogNotification', 'wn')
->where('WatchDogNotification_LINK_User_ID = ' . $qb->createNamedParameter($userId))
->andWhere('WatchDogNotification_Active = 1')
->leftJoin('wn', 'Tab_WatchDog', 'w', 'wn.WatchDogNotification_LINK_WatchDog_ID = w.WatchDog_ID')
->leftJoin('w', 'Tab_TableDefinition', 't', 'w.WatchDog_Table = t.TableDefinition_Tablename')
->orderBy('WatchDogNotification_Current_Value', 'asc')
->setMaxResults(10);
$notifications = $qb->execute()->fetchAll();
foreach ($notifications as &$notification) {
if ($notification['TableDefinition_WatchDogRoute']) {
$notification['route'] = $this->router->generate($notification['TableDefinition_WatchDogRoute'], ['id' => $notification['WatchDogNotification_LINK_Target_ID']]);
} else {
$notification['route'] = null;
}
if ($notification['TableDefinition_TableRoute']) {
$notification['tableroute'] = $this->router->generate($notification['TableDefinition_TableRoute']);
} else {
$notification['tableroute'] = null;
}
$notification['routewatchdog'] =$this->router->generate('watchdog_edit', ['id' => $notification['WatchDog_ID'], 'watched_tablename'=> $notification['WatchDog_Table'],'watched_columnname' => $notification['WatchDog_Column']]);
$notification['routedone'] =$this->router->generate('bau_watchdognotificationlist_edit', ['id' => $notification['WatchDogNotification_ID'], ['tablename' => 'WatchDogNotification']]);
if (isset($notification['WatchDogNotification_Display_Columns_02'])){
$notification['WatchDogNotification_Display_Columns_02']=strip_tags(htmlspecialchars_decode($notification['WatchDogNotification_Display_Columns_02']));
}
}
}
return $notifications;
}
/**
* Get custom notifications as JSON.
*
* @return array
*/
public function getCustomNotifications() : string
{
$repositoryFactory = $this->container->get('citibiz.generic_repository_factory');
$repo = $repositoryFactory->createGenericRepository('CustomNotification');
/*
* Retrieve all notifications of the current user that were not shown, yet.
*/
$notificationsRows = $repo->fetchAll(
$repo->findBy(
new CriteriaComposite(
'and',
[
new CriteriaComparison(
'=',
new CriteriaUnmappedColumn('CustomNotification_Owner_Link_User_ID'),
new CriteriaConstant($this->container->get('citibiz.security_context_wrapper')->getUserId())
),
new CriteriaComparison(
'<>',
new CriteriaUnmappedColumn('CustomNotification_Shown'),
new CriteriaConstant(1)
),
/* enable this to not retrieve notifications in the past
new CriteriaComparison(
'>',
new CriteriaUnmappedColumn('CustomNotification_Time'),
new CriteriaConstant((new \DateTime())->format($repo->getConnection()->getDatabasePlatform()->getDateTimeFormatString()))
),
*/
]
)
)
) ?? [];
$notifications = [];
/*
* Loop over database result to convert notifications into neccessary target format.
*/
foreach ($notificationsRows as $key => $notificationRow) {
$additionalInfo = null;
if ($notificationRow['REF_CustomNotification_Link_CRM_ID']['display']) {
$additionalInfo[] = $notificationRow['REF_CustomNotification_Link_CRM_ID']['display'];
}
if ($notificationRow['CustomNotification_Info_01']) {
$additionalInfo[] .= $notificationRow['CustomNotification_Info_01'];
}
if ($notificationRow['CustomNotification_Info_02']) {
$additionalInfo[] .= $notificationRow['CustomNotification_Info_02'];
}
if ($additionalInfo) {
$additionalInfo = "\n" . implode("\n", $additionalInfo);
}
$notifications[$key]['id'] = $notificationRow['CustomNotification_ID'];
$notifications[$key]['output'] = "Erinnerung:\n– " . $notificationRow['CustomNotification_Name'] . " –\nFällig: " . $notificationRow['CustomNotification_Time'] . " Uhr" . $additionalInfo;
$notifications[$key]['time'] = ($notificationRow['CustomNotification_Time']->format('U') - time() - ($notificationRow['CustomNotification_Remembertime'] * 60)) * 1000;
}
return json_encode($notifications);
}
}