Skip to content
Snippets Groups Projects
process_event_stack.php 12.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    <?php
    /******************************************************************************
     BATCH PROCESS EVENT STACK
    
     Processes events from table event_stack
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
     1 - Groups events by
    
        * Recipient
        * Event
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
     2 - Merge template for each recipient / event
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
    
     3 - Prepare e-mail and add to e-mail stack
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    ******************************************************************************/
    
    /* begin */
    // load the config and prepare to process
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    include 'load_process_event_stack.php';
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    while ($state != 'END') {
    
        Bt_writeLog(['level' => 'INFO', 'message' => 'STATE:'.$state]);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        switch ($state) {
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
            /**********************************************************************/
            /*                          LOAD_NOTIFICATIONS                        */
            /* Load notification defsidentified with notification id              */
            /**********************************************************************/
            case 'LOAD_NOTIFICATIONS':
    
                Bt_writeLog(['level' => 'INFO', 'message' => 'Loading configuration for notification id '.$notificationId]);
                $notification = \Notification\models\NotificationModel::getByNotificationId(['notificationId' => $notificationId, 'select' => ['*']]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                if ($notification === false) {
                    Bt_exitBatch(1, "Notification '".$notificationId."' not found");
                }
    
                if ($notification['is_enabled'] === 'N') {
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    Bt_exitBatch(100, "Notification '".$notificationId."' is disabled");
                }
                $state = 'LOAD_EVENTS';
                break;
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
            /**********************************************************************/
            /*                          LOAD_EVENTS                               */
            /* Checking if the stack has notifications to proceed                 */
            /**********************************************************************/
            case 'LOAD_EVENTS':
    
                Bt_writeLog(['level' => 'INFO', 'message' => 'Loading events for notification sid '.$notification['notification_sid']]);
                $events = \Notification\models\NotificationsEventsModel::get(['select' => ['*'], 'where' => ['notification_sid = ?', 'exec_date is NULL'], 'data' => [$notification['notification_sid']]]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                $totalEventsToProcess = count($events);
                $currentEvent = 0;
                if ($totalEventsToProcess === 0) {
                    Bt_exitBatch(0, 'No event to process');
                }
    
                Bt_writeLog(['level' => 'INFO', 'message' => $totalEventsToProcess.' events to process']);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                $tmpNotifs = array();
                $state = 'MERGE_EVENT';
                break;
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
            /**********************************************************************/
            /*                  MERGE_EVENT                                       */
            /* Process event stack to get recipients                              */
            /**********************************************************************/
            case 'MERGE_EVENT':
                foreach ($events as $event) {
    
                    Bt_writeLog(['level' => 'INFO', 'message' => "Getting recipients using diffusion type '".$notification['diffusion_type']."'"]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    // Diffusion type specific recipients
    
                    $recipients = \Notification\controllers\DiffusionTypesController::getItemsToNotify(['request' => 'recipients', 'notification' => $notification, 'event' => $event]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    // Diffusion type specific res_id
    
                    Bt_writeLog(['level' => 'INFO', 'message' => "Getting document ids using diffusion type '".$notification['diffusion_type']."'"]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    $res_id = false;
    
                    if ($event['table_name'] == $coll_table || $event['table_name'] == $coll_view) {
                        $res_id = $event['record_id'];
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    } else {
    
                        $res_id = \Notification\controllers\DiffusionTypesController::getItemsToNotify(['request' => 'res_id', 'notification' => $notification, 'event' => $event]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    }
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    //Attach Mode ?
    
                    if (!empty($notification['attachfor_type']) || $notification['attachfor_type'] != null) {
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                        $attachMode = true;
    
                        Bt_writeLog(['level' => 'INFO', 'message' => 'The document will be attached for each recipient']);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    } else {
                        $attachMode = false;
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
                    }
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    $nbRecipients = count($recipients);
    
                    Bt_writeLog(['level' => 'INFO', 'message' => $nbRecipients.' recipients found, checking active and absences']);
    
                    $parameter = \Parameter\models\ParameterModel::getById(['select' => ['param_value_int'], 'id' => 'user_quota']);
    
                    if ($notification['diffusion_type'] === 'dest_entity') {
    
                        foreach ($recipients as $key => $recipient) {
                            $entity_id = $recipient['entity_id'];
    
                            Bt_writeLog(['level' => 'INFO', 'message' => 'Recipient entity '.$entity_id]);
    
                            if (($recipient['enabled'] == 'N' && (empty($parameter) || $parameter['param_value_int'] == 0)) || $recipient['mail'] == '') {
    
                                Bt_writeLog(['level' => 'INFO', 'message' => $entity_id.' is disabled or mail is invalid, this notification will not be send']);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
                            if (!isset($tmpNotifs[$entity_id])) {
                                $tmpNotifs[$entity_id]['recipient'] = $recipient;
                            }
                            $tmpNotifs[$entity_id]['events'][] = $event;
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    } else {
    
                        foreach ($recipients as $key => $recipient) {
                            $user_id = $recipient['user_id'];
    
                            Bt_writeLog(['level' => 'INFO', 'message' => 'Recipient '.$user_id]);
    
                            if (($recipient['status'] == 'SPD' && (empty($parameter) || $parameter['param_value_int'] == 0)) || $recipient['status'] == 'DEL') {
    
                                Bt_writeLog(['level' => 'INFO', 'message' => $user_id.' is disabled or deleted, this notification will not be send']);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                                continue;
                            }
    
                            if (!isset($tmpNotifs[$user_id])) {
                                $tmpNotifs[$user_id]['recipient'] = $recipient;
                            }
                            $tmpNotifs[$user_id]['events'][] = $event;
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    if (count($recipients) === 0) {
    
                        Bt_writeLog(['level' => 'WARNING', 'message' => 'No recipient found']);
                        \Notification\models\NotificationsEventsModel::update([
                            'set'   => ['exec_date' => 'CURRENT_TIMESTAMP', 'exec_result' => 'INFO: no recipient found'],
                            'where' => ['event_stack_sid = ?'],
                            'data'  => [$event['event_stack_sid']]
                        ]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    }
                }
                $totalNotificationsToProcess = count($tmpNotifs);
    
                Bt_writeLog(['level' => 'INFO', 'message' => $totalNotificationsToProcess.' notifications to process']);
                switch ($notification['notification_mode']) {
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    case 'EMAIL':
                        $state = 'FILL_EMAIL_STACK';
                        break;
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
            /**********************************************************************/
            /*                      FILL_EMAIL_STACK                              */
            /* Merge template and fill notif_email_stack                          */
            /**********************************************************************/
            case 'FILL_EMAIL_STACK':
                foreach ($tmpNotifs as $user_id => $tmpNotif) {
                    // Merge template with data and style
    
                    Bt_writeLog(['level' => 'INFO', 'message' => 'Merging template #'.$notification['template_id']
                    .' ('.count($tmpNotif['events']).' events) for user '.$user_id]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
                    $params = array(
    
                        'recipient'    => $tmpNotif['recipient'],
                        'events'       => $tmpNotif['events'],
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                        'notification' => $notification,
    
                        'maarchUrl'    => $maarchUrl,
                        'coll_id'      => $coll_id,
                        'res_table'    => $coll_table,
                        'res_view'     => $coll_view,
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    );
    
                    $html = \ContentManagement\controllers\MergeController::mergeNotification(['templateId' => $notification['template_id'], 'params' => $params]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                    if (strlen($html) === 0) {
    
                        $notificationError = array_column($tmpNotif['events'], 'event_stack_sid');
                        if (!empty($notificationError)) {
                            \Notification\models\NotificationsEventsModel::update([
                                'set'   => ['exec_date' => 'CURRENT_TIMESTAMP', 'exec_result' => 'FAILED: Error when merging template'],
                                'where' => ['event_stack_sid IN (?)'],
                                'data'  => [$notificationError]
                            ]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                        }
                        Bt_exitBatch(8, 'Could not merge template with the data');
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
    
                    // Prepare e-mail for stack
    
                    $recipient_mail = $tmpNotif['recipient']['mail'];
    
                    $subject        = $notification['description'];
    
                    if (!empty($recipient_mail)) {
                        $html = str_replace("&#039;", "'", $html);
                        $html = pg_escape_string($html);
                        $html = str_replace('&amp;', '&', $html);
                        $html = str_replace('&', '#and#', $html);
    
    
                        $recipient_mail = $tmpNotif['recipient']['mail'];
    
    
                        // Attachments
                        $attachments = array();
                        if ($attachMode) {
                            Bt_writeLog(['level' => 'INFO', 'message' => 'Adding attachments']);
                            foreach ($tmpNotif['events'] as $event) {
                                if ($event['res_id'] != '') {
                                    $resourceToAttach = \Resource\models\ResModel::getById(['resId' => $event['res_id'], 'select' => ['path', 'filename', 'docserver_id']]);
                                    if (!empty($resourceToAttach['docserver_id'])) {
    
                                        $docserver     = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $resourceToAttach['docserver_id'], 'select' => ['path_template']]);
                                        $path          = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resourceToAttach['path']) . $resourceToAttach['filename'];
                                        $path          = str_replace('//', '/', $path);
                                        $path          = str_replace('\\', '/', $path);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                            }
    
                            Bt_writeLog(['level' => 'INFO', 'message' => count($attachments).' attachment(s) added']);
    
                        Bt_writeLog(['level' => 'INFO', 'message' => 'adding e-mail to email stack']);
    
                        $arrayPDO = [
                            'recipient' => $recipient_mail,
                            'subject'   => $subject,
                            'html_body' => $html
                        ];
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                        if (count($attachments) > 0) {
    
                            $arrayPDO['attachments'] = implode(',', $attachments);
                        }
                        \Notification\models\NotificationsEmailsModel::create($arrayPDO);
    
                        $notificationSuccess = array_column($tmpNotif['events'], 'event_stack_sid');
                        if (!empty($notificationSuccess)) {
                            \Notification\models\NotificationsEventsModel::update([
                                'set'   => ['exec_date' => 'CURRENT_TIMESTAMP', 'exec_result' => 'SUCCESS'],
                                'where' => ['event_stack_sid IN (?)'],
                                'data'  => [$notificationSuccess]
                            ]);
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                        }
                    }
    
    Alex ORLUC's avatar
     
    Alex ORLUC committed
                $state = 'END';
                break;
    
    Bt_writeLog(['level' => 'INFO', 'message' => 'End of process']);
    Bt_logInDataBase($totalEventsToProcess, 0, 'process without error');
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    exit($GLOBALS['exitCode']);