Skip to content
Snippets Groups Projects
Commit ce4c0670 authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Fusion TBS et scripts de datasource

parent 8124164c
No related branches found
No related tags found
No related merge requests found
......@@ -98,11 +98,11 @@ function Bt_exitBatch($returnCode, $message='')
*/
function Bt_logInDataBase($totalProcessed=0, $totalErrors=0, $info='')
{
$query = "insert into history_batch(module_name, batch_id, event_date, "
$query = "insert into history_batch (module_name, batch_id, event_date, "
. "total_processed, total_errors, info) values('"
. $GLOBALS['batchName'] . "', " . $GLOBALS['wb'] . ", "
. $GLOBALS['db']->current_datetime() . ", " . $totalProcessed . ", " . $totalErrors . ", '"
. $GLOBALS['func']->protect_string_db(substr(str_replace('\\', '\\\\', $info), 0, 999)) . "')";
. $GLOBALS['func']->protect_string_db(substr(str_replace('\\', '\\\\', str_replace("'", "`", $info)), 0, 999)) . "')";
//. $GLOBALS['func']->protect_string_db(substr($info, 0, 999)) . "')";
Bt_doQuery($GLOBALS['db'], $query);
}
......
......@@ -123,7 +123,7 @@ foreach (array_keys($options) as $key) {
}
$logger->write($txt, 'DEBUG');
$GLOBALS['configFile'] = $options['config'];
$ta_id = $options['notif'];
$notificationId = $options['notif'];
$logger->write('Load xml config file:' . $GLOBALS['configFile'], 'INFO');
......@@ -154,13 +154,12 @@ if ($xmlconfig == FALSE) {
$config = $xmlconfig->CONFIG;
$lang = (string)$config->Lang;
$maarchDirectory = (string)$config->MaarchDirectory;
chdir($maarchDirectory);
$maarchUrl = (string)$config->MaarchUrl;
$maarchApps = (string) $config->MaarchApps;
$GLOBALS['TmpDirectory'] = (string)$config->TmpDirectory;
$_SESSION['config']['tmppath'] = (string)$config->TmpDirectory;
$GLOBALS['batchDirectory'] = $maarchDirectory . 'modules'
. DIRECTORY_SEPARATOR . 'notifications'
. DIRECTORY_SEPARATOR . 'batch';
......@@ -178,51 +177,45 @@ if ((string) $log4phpParams->enabled == 'true') {
$logger->set_log4PhpConfigPath((string) $log4phpParams->Log4PhpConfigPath);
$logger->set_log4PhpBatchName('process_event_stack');
}
// Mailer
$mailerParams = $xmlconfig->MAILER;
$path_to_mailer = (string)$mailerParams->path_to_mailer;
$mailerParams = $xmlconfig->MAILER;
// INCLUDES
try {
Bt_myInclude(
$maarchDirectory . 'core' . DIRECTORY_SEPARATOR . 'class'
'core' . DIRECTORY_SEPARATOR . 'class'
. DIRECTORY_SEPARATOR . 'class_functions.php'
);
Bt_myInclude(
$maarchDirectory . 'core' . DIRECTORY_SEPARATOR . 'class'
'core' . DIRECTORY_SEPARATOR . 'class'
. DIRECTORY_SEPARATOR . 'class_db.php'
);
Bt_myInclude(
$maarchDirectory . 'core' . DIRECTORY_SEPARATOR . 'class'
'core' . DIRECTORY_SEPARATOR . 'class'
. DIRECTORY_SEPARATOR . 'class_core_tools.php'
);
// Notifications
Bt_myInclude(
$maarchDirectory . "modules" . DIRECTORY_SEPARATOR . "notifications"
"modules" . DIRECTORY_SEPARATOR . "notifications"
. DIRECTORY_SEPARATOR . "notifications_tables_definition.php"
);
Bt_myInclude(
$maarchDirectory . "modules" . DIRECTORY_SEPARATOR . "notifications"
"modules" . DIRECTORY_SEPARATOR . "notifications"
. DIRECTORY_SEPARATOR . "class" . DIRECTORY_SEPARATOR . "templates_association_controler.php"
);
Bt_myInclude(
$maarchDirectory . "modules" . DIRECTORY_SEPARATOR . "notifications"
"modules" . DIRECTORY_SEPARATOR . "notifications"
. DIRECTORY_SEPARATOR . "class" . DIRECTORY_SEPARATOR . "diffusion_type_controler.php"
);
Bt_myInclude(
$maarchDirectory . "modules" . DIRECTORY_SEPARATOR . "notifications"
. DIRECTORY_SEPARATOR . "class" . DIRECTORY_SEPARATOR . "diffusion_content_controler.php"
"modules" . DIRECTORY_SEPARATOR . "notifications"
. DIRECTORY_SEPARATOR . "class" . DIRECTORY_SEPARATOR . "event_controler.php"
);
// Templates
Bt_myInclude(
$maarchDirectory . 'modules' . DIRECTORY_SEPARATOR . 'templates'
. DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'template_merger.php'
'modules' . DIRECTORY_SEPARATOR . 'templates'
. DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'templates_controler.php'
);
/*Bt_myInclude(
$maarchDirectory . $path_to_mailer
); */
} catch (IncludeFileError $e) {
$logger->write(
......@@ -232,24 +225,34 @@ try {
exit();
}
// Controlers and objects
$dbConfig = $xmlconfig->CONFIG_BASE;
$_SESSION['config']['databaseserver'] = (string)$dbConfig->databaseserver;
$_SESSION['config']['databaseserverport'] = (string)$dbConfig->databaseserverport;
$_SESSION['config']['databaseuser'] = (string)$dbConfig->databaseuser;
$_SESSION['config']['databasepassword'] = (string)$dbConfig->databasepassword;
$_SESSION['config']['databasename'] = (string)$dbConfig->databasename;
$_SESSION['config']['databasetype'] = (string)$dbConfig->databasetype;
$coreTools = new core_tools();
$coreTools->load_lang($lang, $maarchDirectory, $maarchApps);
$func = new functions();
// Notifications
$templates_association_controler = new templates_association_controler();
$diffusion_type_controler = new diffusion_type_controler();
$diffusion_content_controler = new diffusion_content_controler();
// Templates
$template_merger = new template_merger();
$event_controler = new event_controler();
$templates_controler = new templates_controler();
$db = new dbquery($GLOBALS['configFile']);
$db = new dbquery();
$db->connect();
$databasetype = (string)$xmlconfig->CONFIG_BASE->databasetype;
// Collection for res
$collparams = $xmlconfig->COLLECTION;
$coll_id = $collparams->Id;
$coll_table = $collparams->Table;
$coll_view = $collparams->View;
$GLOBALS['errorLckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR
. $GLOBALS['batchName'] . '_error.lck';
......
cd C:\xampp\htdocs\maarch_entreprise_trunk\modules\notifications\batch
process_event_stack_VAL.bat
process_email_stack.bat
cd C:\xampp\htdocs\maarch_entreprise_trunk\modules\notifications\batch
process_event_stack_VAL.bat
process_email_stack.bat
pause
......@@ -25,9 +25,7 @@ while ($state <> 'END') {
if ($totalEmailsToProcess === 0) {
Bt_exitBatch(0, 'No e-mail to process');
}
$GLOBALS['logger']->write($totalEmailsToProcess . ' e-mails to proceed.', 'INFO');
$GLOBALS['emails'] = array();
while ($emailRecordset = $GLOBALS['db']->fetch_object()) {
$GLOBALS['emails'][] = $emailRecordset;
......@@ -62,19 +60,18 @@ while ($state <> 'END') {
if($email->attachments != '') {
$attachments = explode(',', $email->attachments);
foreach($attachments as $attachment) {
foreach($attachments as $num => $attachment) {
if(is_file($attachment)) {
$name = basename($attachment);
$ext = strrchr($attachment, '.');
$name = str_pad(($num + 1), 4, '0', STR_PAD_LEFT) . $ext;
$ctype = '';
switch($ext) {
case '.pdf':
$ctype = 'application/pdf';
break;
}
$file_content = $GLOBALS['mailer']->getFile($attachment);
$GLOBALS['mailer']->addAttachment($file_content, $name, $ctype);
$file_content = $GLOBALS['mailer']->getFile($attachment);
$GLOBALS['mailer']->addAttachment($file_content, $name, $ctype);
}
}
}
......@@ -83,6 +80,7 @@ while ($state <> 'END') {
if($return) {
$exec_result = 'SENT';
} else {
$GLOBALS['logger']->write("Errors when sending message through SMTP :" . implode(', ', $GLOBALS['mailer']->errors), 'ERROR');
$exec_result = 'FAILED';
}
$query = "UPDATE " . _NOTIF_EMAIL_STACK_TABLE_NAME
......
......@@ -5,30 +5,13 @@
Processes events from table event_stack
1 - Groups events by
* Type/template
* Recipient
* Event
Result =
$notifications
[ta_sid]
[mail]
[es_sid]
[]
2 - Uses data to prepare data source for template merging :
$templates_association
$events
$GLOBALS['xxxxxxx'] => all datasources that may be constructed by the specific script
2 - Merge template for each recipient / event
3 - Prepare e-mail and add to e-mail stack
Result =
$notifications
[ta_sid]
[user_id]
[notification] = current notification
[recipient] = current recipient
[events] = list of events
[xxxxxxx] = custom
******************************************************************************/
/* begin */
......@@ -44,66 +27,14 @@ while ($state <> 'END') {
/**********************************************************************/
/* LOAD_TEMPLATES_ASSOC */
/* Load parameters */
/* Load template association identified with notification id */
/**********************************************************************/
case 'LOAD_TEMPLATES_ASSOC' :
$query = "SELECT * FROM " . _TEMPLATES_ASSOCIATION_TABLE_NAME
. " WHERE notification_id ='" . $ta_id . "'";
Bt_doQuery($db, $query);
if ($db->nb_result() === 0) {
Bt_exitBatch(1, "Template association for notification '".$ta_id."' not found");
$logger->write("Loading configuration for notification id " . $notificationId, 'INFO');
$templateAssociation = $templates_association_controler->getByNotificationId($notificationId);
if ($templateAssociation === FALSE) {
Bt_exitBatch(1, "Template association for notification '".$notificationId."' not found");
}
$ta = $db->fetch_object();
$state = 'LOAD_TEMPLATE';
break;
/**********************************************************************/
/* LOAD_TEMPLATES */
/* Load parameters */
/**********************************************************************/
case 'LOAD_TEMPLATE' :
$logger->write('Loading template ' . $ta->template_id, 'INFO');
$query = "SELECT * FROM " . _TEMPLATES_TABLE_NAME
. " WHERE id = " . $ta->template_id;
Bt_doQuery($db, $query);
if ($db->nb_result() === 0) {
Bt_exitBatch(5, 'Could not load template '. $ta->template_id);
}
$template = $db->fetch_object();
$state = 'LOAD_DIFFUSION_TYPE';
break;
/**********************************************************************/
/* LOAD_DIFFUSION_TYPE */
/* Load parameters */
/**********************************************************************/
case 'LOAD_DIFFUSION_TYPE':
$dt = @$diffusion_type_controler->getDiffusionType($ta->diffusion_type);
if($dt == false) {
Bt_exitBatch(4, 'Unknown diffusion type '. $ta->diffusion_type);
}
$dscript = $maarchDirectory . $dt->script;
if(!is_file($dscript)) {
Bt_exitBatch(5, 'Could not find the diffusion script '. $dscript);
}
require_once($dscript);
$state = 'LOAD_CONTENT_TYPE';
break;
/**********************************************************************/
/* LOAD_CONTENT_TYPE */
/* Load parameters */
/**********************************************************************/
case 'LOAD_CONTENT_TYPE' :
$dc = @$diffusion_content_controler->getDiffusionContent($ta->diffusion_content);
if($dc == false) {
Bt_exitBatch(4, 'Unknown diffusion content '. $ta->diffusion_content);
}
$dcscript = $maarchDirectory . $dc->script;
if(!is_file($dcscript)) {
Bt_exitBatch(5, 'Could not find the diffusion content script '. $dcscript);
}
require_once($dcscript);
$state = 'LOAD_EVENTS';
break;
......@@ -112,20 +43,14 @@ while ($state <> 'END') {
/* Checking if the stack has notifications to proceed */
/**********************************************************************/
case 'LOAD_EVENTS' :
$query = "SELECT * FROM " . _NOTIF_EVENT_STACK_TABLE_NAME
. " WHERE exec_date is NULL "
. " AND ta_sid = " . $ta->system_id ;
Bt_doQuery($db, $query);
$totalEventsToProcess = $db->nb_result();
$logger->write("Loading events for template association id " . $templateAssociation->system_id, 'INFO');
$events = $event_controler->getEventsByTemplateAssociationId($templateAssociation->system_id);
$totalEventsToProcess = count($events);
$currentEvent = 0;
if ($totalEventsToProcess === 0) {
Bt_exitBatch(0, 'No event to process');
}
$logger->write($totalEventsToProcess . ' events to process', 'INFO');
$events = array();
while ($eventRecordset = $db->fetch_object()) {
$events[] = $eventRecordset;
}
$notifications = array();
$state = 'MERGE_EVENT';
break;
......@@ -136,40 +61,29 @@ while ($state <> 'END') {
/**********************************************************************/
case 'MERGE_EVENT' :
foreach($events as $event) {
$logger->write("Getting recipients using diffusion type '" .$ta->diffusion_type . "'", 'INFO');
$query = '';
$query = getRecipients($ta, $event);
if(!$query) {
Bt_exitBatch(4, 'Could not retrieve the query to select recipients');
}
Bt_doQuery($db, $query);
$nbRecipients = $db->nb_result();
$logger->write("Getting recipients using diffusion type '" .$templateAssociation->diffusion_type . "'", 'INFO');
// Diffusion type specific recipients
$recipients = $diffusion_type_controler->getRecipients($templateAssociation, $event);
$nbRecipients = count($recipients);
if ($nbRecipients === 0) {
$logger->write('No recipient found' , 'WARNING');
$exec_result = "FAILED: no recipient found";
} else {
$exec_result = 'SUCCESS';
$logger->write('Found ' . $nbRecipients .' recipients', 'INFO');
while ($userRecordset = $db->fetch_object()) {
$user_id = $userRecordset->user_id;
$logger->write($nbRecipients .' recipients found', 'INFO');
foreach ($recipients as $recipient) {
$user_id = $recipient->user_id;
if(!isset($notifications[$user_id])) {
$notifications[$user_id]['maarchUrl'] = $maarchUrl;
$notifications[$user_id]['maarchApps'] = $maarchApps;
$notifications[$user_id]['template_association'] = $ta;
$notifications[$user_id]['template'] = $template;
$notifications[$user_id]['recipient'] = $userRecordset;
$notifications[$user_id]['attachments'] = array();
$notifications[$user_id]['recipient'] = $recipient;
}
$notifications[$user_id]['events'][] = $event;
}
}
$query = "UPDATE " . _NOTIF_EVENT_STACK_TABLE_NAME
. " SET exec_date = ".$db->current_datetime().", exec_result = '".$exec_result."'"
. " WHERE system_id = ".$event->system_id;
Bt_doQuery($db, $query);
$event_controler->commitEvent($event->system_id, $exec_result);
}
$totalNotificationsToProcess = count($notifications);
$logger->write('There are ' . $totalNotificationsToProcess .' notifications to process', 'INFO');
$logger->write($totalNotificationsToProcess .' notifications to process', 'INFO');
$state = 'MERGE_TEMPLATE';
break;
......@@ -179,54 +93,68 @@ while ($state <> 'END') {
/**********************************************************************/
case 'MERGE_TEMPLATE' :
foreach($notifications as $user_id => $notification) {
// Get additional data from content script
$notification = getAdditionalContent($notification);
$logger->write('Data for notification is "' . implode(', ', array_keys($notification)) .'"', 'INFO');
$logger->write('Data for notification is "' . print_r($notification,true) .'"', 'INFO');
// Load language file
require_once($maarchDirectory
. 'modules'
. DIRECTORY_SEPARATOR . 'notifications'
. DIRECTORY_SEPARATOR . 'lang'
. DIRECTORY_SEPARATOR . 'fr.php'
);
// Load CSS
$style = file_get_contents($maarchDirectory
. 'modules'
. DIRECTORY_SEPARATOR . 'notifications'
. DIRECTORY_SEPARATOR . 'css'
. DIRECTORY_SEPARATOR . 'template.css'
);
// Merge template with data and style
$logger->write('Merging template with data', 'INFO');
$html = $template_merger->merge($template->content, $notification, $style);
$logger->write('Merging template #' . $templateAssociation->template_id
. ' ('.count($notification['events']).' events) for user ' . $user_id, 'INFO');
$params = array(
'recipient' => $notification['recipient'],
'events' => $notification['events'],
'maarchUrl' => $maarchUrl,
'maarchApps' => $maarchApps,
'coll_id' => $coll_id,
'res_table' => $coll_table,
'res_view' => $coll_view
);
$html = $templates_controler->merge($templateAssociation->template_id, $params, 'content');
if(strlen($html) === 0) {
Bt_exitBatch(8, "Could not merge template with the data");
}
// Prepare e-mail for stack
$sender = $func->protect_string_db((string)$mailerParams->mailfrom, $databasetype);
$recipient = $notification['recipient']->mail;
$subject = $func->protect_string_db($ta->description, $databasetype);
$html = $func->protect_string_db($html, $databasetype);
$sender = $func->protect_string_db((string)$mailerParams->mailfrom);
$recipient_mail = $notification['recipient']->mail;
$subject = $func->protect_string_db($templateAssociation->description);
$html = $func->protect_string_db($html);
$html = str_replace('&amp;', '&', $html);
if($ta->is_attached == 'Y') {
$attachments = $func->protect_string_db(implode(',', $notification['attachments']), $databasetype);
// Attachments
$logger->write('Checking if attachment required for ' . $user_id, 'INFO');
$attachments = array();
$indAttach = $diffusion_type_controler->getAttachFor($templateAssociation, $user_id);
if($indAttach) {
foreach($notification['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');
}
$logger->write('Adding e-mail to email stack', 'INFO');
$query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME
. " (sender, recipient, subject, html_body, charset, attachments, module) "
. "VALUES ('".$sender."', "
. "'".$recipient."', "
. "'".$recipient_mail."', "
. "'".$subject."', "
. "'".$html."', "
. "'".(string)$mailerParams->charset."', "
. "'".$attachments."', "
. "'".implode(',', $attachments)."', "
. "'notifications')";
$logger->write('SQL query:' . $query, 'DEBUG');
$db->query($query, false, true);
$db2 = new dbquery();
$db2->connect();
$db2->query($query, false, true);
$currentNotification++;
}
$state = 'END';
......@@ -238,7 +166,7 @@ while ($state <> 'END') {
$logger->write('End of process', 'INFO');
Bt_logInDataBase(
$totalEventsToProcess, 0, 'process without error'
);
);
$db->disconnect();
unlink($GLOBALS['lckFile']);
exit($GLOBALS['exitCode']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment