diff --git a/view/preventionplan/preventionplan_card.php b/view/preventionplan/preventionplan_card.php index 99839bfc6..ef1038fde 100644 --- a/view/preventionplan/preventionplan_card.php +++ b/view/preventionplan/preventionplan_card.php @@ -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']); @@ -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"]); @@ -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)) ? '' : ''; - $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)) ? '' : ''; + $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