Skip to content

Commit

Permalink
Evarisk#3937 [PreventionPlan] add: contact signatories in recipient f…
Browse files Browse the repository at this point in the history
…or mail
  • Loading branch information
evarisk-micka committed Jun 5, 2024
1 parent f56410c commit e05fa2b
Showing 1 changed file with 129 additions and 130 deletions.
259 changes: 129 additions & 130 deletions view/preventionplan/preventionplan_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
require_once __DIR__ . '/../../lib/digiriskdolibarr_preventionplan.lib.php';

// Global variables definitions
global $conf, $db, $hookmanager, $langs, $user;
global $conf, $db, $hookmanager, $langs, $moduleNameLowerCase, $user;

// Load translation files required by the page
saturne_load_langs(['other', 'mails']);
Expand Down Expand Up @@ -383,8 +383,11 @@

if ($result < 0) {
// Delete accident KO
if (!empty($accident->errors)) setEventMessages(null, $accident->errors, 'errors');
else setEventMessages($accident->error, null, 'errors');
if (!empty($object->errors)) {
setEventMessages(null, $object->errors, 'errors');
} else {
setEventMessages($object->error, [], 'errors');
}
}
// Delete accident OK
$urltogo = str_replace('preventionplan_card.php', 'preventionplan_list.php', $_SERVER["PHP_SELF"]);
Expand Down Expand Up @@ -1503,153 +1506,149 @@ class="attachment-thumbail size-thumbnail photo photowithmargin" alt="">
if (GETPOST('modelselected', 'alpha')) {
$action = 'presend';
}
if ($action == 'presend') {
$langs->load("mails");

$titreform = 'SendMail';
// Presend form
if ($action == 'presend') {
$langs->load('mails');

$object->fetch_projet();
$object->fetch_projet();
if (!in_array($object->element, ['societe', 'user', 'member'])) {
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';

if ( ! in_array($object->element, array('societe', 'user', 'member'))) {
$ref = dol_sanitizeFileName($object->ref);
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
$fileparams = dol_most_recent_file($diroutput . '/' . $ref, preg_quote($ref, '/') . '[^\-]+');
$file = $fileparams['fullname'];
}
$ref = dol_sanitizeFileName($object->ref);
$fileparams = dol_most_recent_file($diroutput . '/' . $ref, preg_quote($ref, '/') . '[^\-]+');
$file = $fileparams['fullname'];
}

// Define output language
$outputlangs = $langs;
$newlang = '';
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) {
$newlang = $_REQUEST['lang_id'];
}
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
$newlang = $object->thirdparty->default_lang;
}
// Define output language
$outputlangs = $langs;
$newlang = '';
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id'])) {
$newlang = $_REQUEST['lang_id'];
}
if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
$newlang = $object->thirdparty->default_lang;
}

if ( ! empty($newlang)) {
$outputlangs = new Translate('', $conf);
$outputlangs->setDefaultLang($newlang);
// Load traductions files required by page
$outputlangs->loadLangs(array('digiriskdolibarr'));
}
if (!empty($newlang)) {
$outputlangs = new Translate('', $conf);
$outputlangs->setDefaultLang($newlang);
// Load traductions files required by page
$outputlangs->loadLangs(['digiriskdolibarr']);
}

$topicmail = '';
if (empty($object->ref_client)) {
$topicmail = $outputlangs->trans($defaulttopic, '__REF__');
} elseif ( ! empty($object->ref_client)) {
$topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REFCLIENT__)');
}
$topicmail = '';
if (empty($object->ref_client)) {
$topicmail = $outputlangs->trans($defaulttopic, '__REF__');
} else {
$topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REFCLIENT__)');
}

print load_fiche_titre($langs->trans($titreform), '', $object->picto, '', 'formmailbeforetitle');
print load_fiche_titre($langs->trans('SendMail'), '', $object->picto, '', 'formmailbeforetitle');

print dol_get_fiche_head('');
print dol_get_fiche_head('');

// Create form for email
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
$formmail = new FormMail($db);
$masterWorker = $signatory->fetchSignatory('MasterWorker', $object->id, 'preventionplan');
$masterWorker = array_shift($masterWorker);
// Create form for email
include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
$formmail = new FormMail($db);
$masterWorker = $signatory->fetchSignatory('MasterWorker', $object->id, 'preventionplan');
$masterWorker = array_shift($masterWorker);

$formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
$formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : ( ! empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
$formmail->fromid = $masterWorker->id;
$formmail->trackid = $trackid;
$formmail->fromname = $masterWorker->firstname . ' ' . $masterWorker->lastname;
$formmail->frommail = $masterWorker->email;
$formmail->fromalsorobot = 1;
$formmail->withfrom = 1;
$formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
$formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
$formmail->fromid = $masterWorker->id;
$formmail->trackid = $trackid;
$formmail->fromname = $masterWorker->firstname . ' ' . $masterWorker->lastname;
$formmail->frommail = $masterWorker->email;
$formmail->fromalsorobot = 1;
$formmail->withfrom = 1;

// Fill list of recipient with email inside <>.
$liste = array();
// Fill list of recipient with email inside <>.
$liste = [];

$labourInspectorContact = $digiriskresources->fetchResourcesFromObject('LabourInspectorAssigned', $object);
$labourInspectorContact = $digiriskresources->fetchResourcesFromObject('LabourInspectorAssigned', $object);

if ( ! empty($object->socid) && $object->socid > 0 && ! is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
$object->fetch_thirdparty();
}
if (is_object($object->thirdparty)) {
foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
$liste[$key] = $value;
}
}
if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
$listeuser = [];
$fuserdest = new User($db);

if ( ! empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
$listeuser = array();
$fuserdest = new User($db);
$result = $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, ['customsql' => 't.statut=1 AND t.employee=1 AND t.email IS NOT NULL AND t.email<>\'\''], 'AND', true);
if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
foreach ($fuserdest->users as $uuserdest) {
$listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
}
} elseif ($result < 0) {
setEventMessages(null, $fuserdest->errors, 'errors');
}
if (count($listeuser) > 0) {
$formmail->withtouser = $listeuser;
$formmail->withtoccuser = $listeuser;
}
}

$result = $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, array('customsql' => 't.statut=1 AND t.employee=1 AND t.email IS NOT NULL AND t.email<>\'\''), 'AND', true);
if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
foreach ($fuserdest->users as $uuserdest) {
$listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
}
} elseif ($result < 0) {
setEventMessages(null, $fuserdest->errors, 'errors');
}
if (count($listeuser) > 0) {
$formmail->withtouser = $listeuser;
$formmail->withtoccuser = $listeuser;
}
}
$signatoriesArray = $signatory->fetchSignatories($object->id, 'preventionplan');
if (is_array($signatoriesArray) && !empty($signatoriesArray)) {
foreach ($signatoriesArray as $objectSignatory) {
$liste[$objectSignatory->id] = dol_strtoupper($objectSignatory->lastname) . ' ' . ucfirst($objectSignatory->firstname) . (!empty($objectSignatory->email) ? ' <' . $objectSignatory->email . '>' : '');
}
}

$formmail->withto = $liste;
$formmail->withtofree = (GETPOSTISSET('sendto') ? (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1') : '1');
$formmail->withtocc = $liste;
$formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
$formmail->withtopic = $topicmail;
$formmail->withfile = 2;
$formmail->withbody = 1;
$formmail->withdeliveryreceipt = 1;
$formmail->withcancel = 1;

//$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
if (!isset($arrayoffamiliestoexclude)) {
$arrayoffamiliestoexclude = null;
}

$withto = array($labourInspectorContact->id => $labourInspectorContact->firstname . ' ' . $labourInspectorContact->lastname . " <" . $labourInspectorContact->email . ">");

$formmail->withto = $withto;
$formmail->withtofree = (GETPOSTISSET('sendto') ? (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1') : '1');
$formmail->withtocc = $liste;
$formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
$formmail->withtopic = $topicmail;
$formmail->withfile = 2;
$formmail->withbody = 1;
$formmail->withdeliveryreceipt = 1;
$formmail->withcancel = 1;

//$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
if ( ! isset($arrayoffamiliestoexclude)) $arrayoffamiliestoexclude = null;

// Make substitution in email content
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
$substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ? '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . $object->thirdparty->tag . '&securitykey=' . urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '';
$substitutionarray['__PERSONALIZED__'] = ''; // deprecated
$substitutionarray['__CONTACTCIVNAME__'] = '';
$parameters = array(
'mode' => 'formemail'
);
complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);

// Find the good contact address
$tmpobject = $object;

$contactarr = array();
$contactarr = $tmpobject->liste_contact(-1, 'external');

if (is_array($contactarr) && count($contactarr) > 0) {
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
$contactstatic = new Contact($db);

foreach ($contactarr as $contact) {
$contactstatic->fetch($contact['id']);
$substitutionarray['__CONTACT_NAME_' . $contact['code'] . '__'] = $contactstatic->getFullName($outputlangs, 1);
}
}
// Make substitution in email content
$substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
$substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ? '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . $object->thirdparty->tag . '&securitykey=' . urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '';
$substitutionarray['__PERSONALIZED__'] = ''; // deprecated
$substitutionarray['__CONTACTCIVNAME__'] = '';
$parameters = [
'mode' => 'formemail'
];
complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);

// Find the good contact address
$tmpobject = $object;

$contactarr = [];
$contactarr = $tmpobject->liste_contact(-1, 'external');

if (is_array($contactarr) && count($contactarr) > 0) {
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
$contactstatic = new Contact($db);

foreach ($contactarr as $contact) {
$contactstatic->fetch($contact['id']);
$substitutionarray['__CONTACT_NAME_' . $contact['code'] . '__'] = $contactstatic->getFullName($outputlangs, 1);
}
}

// Array of substitutions
$formmail->substit = $substitutionarray;
// Array of substitutions
$formmail->substit = $substitutionarray;

// Array of other parameters
$formmail->param['action'] = 'send';
$formmail->param['models'] = $modelmail;
$formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
$formmail->param['id'] = $object->id;
$formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
$formmail->param['fileinit'] = array($file);
// Array of other parameters
$formmail->param['action'] = 'send';
$formmail->param['models'] = $modelmail;
$formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
$formmail->param['id'] = $object->id;
$formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
$formmail->param['fileinit'] = [$file];

// Show form
print $formmail->get_form();
// Show form
print $formmail->get_form();

print dol_get_fiche_end();
}
print dol_get_fiche_end();
}
}

// End of page
Expand Down

0 comments on commit e05fa2b

Please sign in to comment.