Skip to content
Snippets Groups Projects
process_event_stack.php 6.7 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
     
     1 - Groups events by 
    	* Recipient
    	* Event
    
    
     2 - Merge template for each recipient / event
     
     3 - Prepare e-mail and add to e-mail stack
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
     
    ******************************************************************************/
    
    /* begin */
    // load the config and prepare to process
    include('load_process_event_stack.php');
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    while ($state <> 'END') {
        if (isset($logger)) {
            $logger->write('STATE:' . $state, 'INFO');
        }
        switch ($state) {
     
    	/**********************************************************************/
    
        /*                          LOAD_NOTIFICATIONS           	          */
        /* Load notification defsidentified with notification id              */
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        /**********************************************************************/
    
    		$logger->write("Loading configuration for notification id " . $notificationId, 'INFO');
    
    		$notification = $notifications_controler->getByNotificationId($notificationId);
    		if ($notification === FALSE) {
    
    			Bt_exitBatch(1, "Template association for notification '".$notificationId."' not found");
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            }
    		$state = 'LOAD_EVENTS';
            break;
    		
    	/**********************************************************************/
        /*                          LOAD_EVENTS                               */
        /* Checking if the stack has notifications to proceed                 */
        /**********************************************************************/
        case 'LOAD_EVENTS' :
    
    		$logger->write("Loading events for notification sid " . $notification->notification_sid, 'INFO');
    		$events = $events_controler->getEventsByNotificationSid($notification->notification_sid);
    
    		$totalEventsToProcess = count($events);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    		$currentEvent = 0;
    		if ($totalEventsToProcess === 0) {
    			Bt_exitBatch(0, 'No event to process');
            }
    		$logger->write($totalEventsToProcess . ' events to process', 'INFO');
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    		$state = 'MERGE_EVENT';
            break;
    		
    	/**********************************************************************/
        /*                  MERGE_EVENT	    	                              */
        /* Process event stack to get recipients 				              */
        /**********************************************************************/
    	case 'MERGE_EVENT' :
    		foreach($events as $event) {
    
    			$logger->write("Getting recipients using diffusion type '" .$notification->diffusion_type . "'", 'INFO');
    
    			// Diffusion type specific recipients
    
    			$recipients = $diffusion_type_controler->getRecipients($notification, $event);
    
    			$nbRecipients = count($recipients);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			if ($nbRecipients === 0) {
    				$logger->write('No recipient found' , 'WARNING');
    
    				$events_controler->commitEvent($event->event_stack_sid, "FAILED: no recipient found");
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			} else {
    
    				$logger->write($nbRecipients .' recipients found', 'INFO');
    				foreach ($recipients as $recipient) {
    					$user_id = $recipient->user_id;
    
    					if(!isset($tmpNotifs[$user_id])) {
    						$tmpNotifs[$user_id]['recipient'] = $recipient;
    						$logger->write('Checking if attachment required for ' . $user_id, 'INFO');
    						$tmpNotifs[$user_id]['attach'] = $diffusion_type_controler->getAttachFor($notification, $user_id);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    					}
    
    					$tmpNotifs[$user_id]['events'][] = $event;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    				}
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    		} 
    
    		$totalNotificationsToProcess = count($tmpNotifs);
    
    		$logger->write($totalNotificationsToProcess .' notifications to process', 'INFO');
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    		$state = 'MERGE_TEMPLATE';
    		break;
    		
    	/**********************************************************************/
        /*                    	MERGE_TEMPLATE			           	          */
        /* Load parameters				                                      */
        /**********************************************************************/
        case 'MERGE_TEMPLATE' :
    
    		foreach($tmpNotifs as $user_id => $tmpNotif) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			// Merge template with data and style
    
    			$logger->write('Merging template #' . $notification->template_id 
    				. ' ('.count($tmpNotif['events']).' events) for user ' . $user_id, 'INFO');
    
    			
    			$params = array(
    
    				'recipient' => $tmpNotif['recipient'],
    				'events' => $tmpNotif['events'],
    
    				'maarchUrl' => $maarchUrl,
    				'maarchApps' => $maarchApps,
    				'coll_id' => $coll_id,
                    'res_table' => $coll_table,
                    'res_view' => $coll_view
    			);
    
    			$html = $templates_controler->merge($notification->template_id, $params, 'content');
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			if(strlen($html) === 0) {
    
    				foreach($tmpNotif['events'] as $event) {
    					$events_controler->commitEvent($event->event_stack_sid, "FAILED: Error when merging template");
    				}
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    				Bt_exitBatch(8, "Could not merge template with the data");
    			}
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			// Prepare e-mail for stack
    
    			$sender = $func->protect_string_db((string)$mailerParams->mailfrom);
    
    			$recipient_mail = $tmpNotif['recipient']->mail;
    			$subject = $func->protect_string_db($notification->description);
    
    			$html = $func->protect_string_db($html);
    			$html = str_replace('&amp;', '&', $html);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			
    
    			// Attachments
    			$attachments = array();
    
    			if($tmpNotif['attach']) {	
    				$logger->write('Adding attachments', 'INFO');
    				foreach($tmpNotif['events'] as $event) {
    
    					$query = "SELECT "
    						. "ds.path_template ,"
    						. "mlb.path, "
    						. "mlb.filename " 
    						. "FROM ".$coll_view." mlb LEFT JOIN docservers ds ON mlb.docserver_id = ds.docserver_id "
    						. "WHERE mlb.res_id = " . $event->record_id;
    					Bt_doQuery($db, $query);
    					$path_parts = $db->fetch_object();
    					$path = $path_parts->path_template . str_replace('#', '/', $path_parts->path) . $path_parts->filename;
    					$path = str_replace('//', '/', $path);
    					$path = str_replace('\\', '/', $path);
    					$attachments[] = $path;
    				}
    				$logger->write(count($attachments) . ' attachment(s) added', 'INFO');
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			}
    			
    			$logger->write('Adding e-mail to email stack', 'INFO');
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
    			$query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME 
    				. " (sender, recipient, subject, html_body, charset, attachments, module) "
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    				. "VALUES ('".$sender."', "
    
    				. "'".$recipient_mail."', "
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    				. "'".$subject."', "
    				. "'".$html."', " 
    				. "'".(string)$mailerParams->charset."', "
    
    				. "'".implode(',', $attachments)."', "
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    				. "'notifications')";
    			$logger->write('SQL query:' . $query, 'DEBUG');
    
    			$db2 = new dbquery();
    			$db2->connect();
    			$db2->query($query, false, true);
    
    			
    			foreach($tmpNotif['events'] as $event) {
    				$events_controler->commitEvent($event->event_stack_sid, "SUCCESS");
    			}
    			
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    			$currentNotification++;
    		} 
    		$state = 'END';
    		break;
    	}
    
    }
    
    $logger->write('End of process', 'INFO');
    Bt_logInDataBase(
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
        $totalEventsToProcess, 0, 'process without error'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    $db->disconnect();
    unlink($GLOBALS['lckFile']);
    exit($GLOBALS['exitCode']);
    ?>