* Copyright (C) 2021 Chl * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ // Initially copied from modulebuilder/template/myobject_card.php // Load Dolibarr environment $res=0; // Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined) if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php"; // Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME $tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1; while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; } if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include substr($tmp, 0, ($i+1))."/main.inc.php"; if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include dirname(substr($tmp, 0, ($i+1)))."/main.inc.php"; // Try main.inc.php using relative path if (! $res && file_exists("../main.inc.php")) $res=@include "../main.inc.php"; if (! $res && file_exists("../../main.inc.php")) $res=@include "../../main.inc.php"; if (! $res && file_exists("../../../main.inc.php")) $res=@include "../../../main.inc.php"; if (! $res) die("Include of main fails"); // Load needed classes/lib require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/invoice.lib.php'; require_once 'class/sribmcustommailinfo.class.php'; // Get parameters $id = GETPOST('id', 'int'); $output = ''; // Check security (take care of everything, even page layout, if something goes wrong) restrictedArea($user, 'facture', $id, 'facture_rec'); // Load translation files required by the page $langs->loadLangs(array('bills', 'compta', 'other', 'mails', 'products', 'companies', 'sendrecurringinvoicebymail@sendrecurringinvoicebymail')); // Wrap everything in a do-while(false) as a try-catch mecanisme // in order to print llxFooter whatever happens. do { /** * Part 0 : Preparations */ // Load necessary data $object = new FactureRec($db); $mailObject = new SRIBMCustomMailInfo($db); $form = new Form($db); if ($object->fetch($id) <= 0 or !$object->id) { setEventMessages($langs->trans("ErrorRecordNotFound"), null, 'errors'); break; } if ($mailObject->fetch(null, $object->id, true) <= 0) { setEventMessages("Weird stuff, shouldn't happen : " . $mailObject->error); break; } // List of senders (user, company, robot, ...) $listFrom = array(); $listFrom['robot'] = $conf->global->MAIN_MAIL_EMAIL_FROM; $listFrom['company'] = $conf->global->MAIN_INFO_SOCIETE_NOM .' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; if (!empty($user->email)) { $listFrom['user'] = $user->getFullName($langs) . ' <' . $user->email . '>'; } // If the address in the SRIBM object is not the same as the user (basically, // the user has changed its email address or the address has been set by another // user), we add an 'old' = 'no modification' option. if ($mailObject->id && $mailObject->fromtype == 'user' && isset($listFrom['user']) && $listFrom['user'] != $mailObject->frommail ) { $listFrom['old'] = $mailObject->frommail; } // List of contacts of the third party $listContacts = $mailObject->fac_rec_object->thirdparty->thirdparty_and_contact_email_array(1); // Substitution array/string $helpforsubstitution = $langs->trans('AvailableVariables').' :
'."\n"; $tmparray = getCommonSubstitutionArray($langs, 0, null, $object); complete_substitutions_array($tmparray, $langs); foreach($tmparray as $key => $val) { $helpforsubstitution .= $key . ' -> ' . $langs->trans(dol_string_nohtmltag($val)) . '
'; } /** * Part 1 : Treatment of form submission */ // If the mail form has been submitted, check and record the data if (GETPOST('save')) { do { // Validate input data if (! array_key_exists(GETPOST('fromtype', 'alpha'), $listFrom)) { setEventMessages('Unexpected from value', null, 'errors'); break; } if (GETPOST('sendto_socpeople', 'array') != array_intersect(GETPOST('sendto_socpeople', 'array'), array_keys($listContacts))) { setEventMessages("Unexpected contact value in 'to'", null, 'errors'); break; } if (GETPOST('sendcc_socpeople', 'array') != array_intersect(GETPOST('sendcc_socpeople', 'array'), array_keys($listContacts))) { setEventMessages("Unexpected contact value in 'cc'", null, 'errors'); break; } // Validate some non-breaking stuff after feeding if (empty(GETPOST('sendto_free', 'alpha')) && empty(GETPOST('sendto_socpeople', 'array'))) { // Kinda weird behaviour from CMailFile but better alert the user beforehand // FIXME: check if there is a workaround ? setEventMessages("In some configuration, CMailFile doesn't allow empty 'to' recipient. You should set at least one.", null, 'warnings'); //break; } if (! strlen(GETPOST('subject', 'alpha'))) { // Kinda weird behaviour from CMailFile but better alert the user beforehand // FIXME: check if there is a workaround ? setEventMessages("In some configuration, CMailFile doesn't allow empty subject. You should set one.", null, 'warnings'); //break; } // Feed the input data to the model $mailObject->active = GETPOST('active', 'int') ? 1 : 0; $mailObject->addmaindocfile = GETPOST('addmaindocfile', 'int') ? 1 : 0; $mailObject->fromtype = GETPOST('fromtype', 'alpha'); $mailObject->frommail = $listFrom[$mailObject->fromtype]; $mailObject->sendto_free = GETPOST('sendto_free', 'alpha'); $mailObject->sendto_thirdparty = in_array('thirdparty', GETPOST('sendto_socpeople', 'array')); $mailObject->sendcc_free = GETPOST('sendcc_free', 'alpha'); $mailObject->sendcc_thirdparty = in_array('thirdparty', GETPOST('sendcc_socpeople', 'array')); $mailObject->subject = GETPOST('subject', 'alpha'); $mailObject->body_plaintext = GETPOST('body_plaintext', 'alpha'); // Save into database if ($mailObject->id) { if ($mailObject->update($user) != 1) { setEventMessages($langs->trans("ErrorSQL") . ' : ' . $mailObject->error, null, 'errors'); break; } } else { $mailObject->fk_facture_rec = $object->id; if ($mailObject->create($user) < 0) { setEventMessages($langs->trans("ErrorSQL") . ' : ' . $mailObject->error, null, 'errors'); break; } } // Update the linked contacts $data = array_merge( array_map( function ($id) { return array('id' => $id, 'sendtype' => 'to'); }, array_filter(GETPOST('sendto_socpeople', 'array'), 'is_numeric') ), array_map( function ($id) { return array('id' => $id, 'sendtype' => 'cc'); }, array_filter(GETPOST('sendcc_socpeople', 'array'), 'is_numeric') ) ); if ($mailObject->updateLinkSocPeople($data) != 1) { setEventMessages($langs->trans("ErrorSQL") . ' : ' . $mailObject->error, null, 'errors'); break; } // Everything seems ok setEventMessages($langs->trans("CorrectlyUpdated"), null, 'mesgs'); // ... + redirect to cleanly reload all data and avoid some F5 misbehaviour header("Location: fiche-rec-tab1.php?id=" . (int) $id, true, 302); return; } while(false); } else if (GETPOST('reset') && $mailObject->id) { if ($mailObject->delete() == 1) { // Success message... setEventMessages($langs->trans("ResetDone"), null, 'mesgs'); // ... + redirect to cleanly reload all data header("Location: fiche-rec-tab1.php?id=" . (int) $id, true, 302); return; } else { setEventMessages($langs->trans("ErrorSQL") . " : " . $mailObject->error , null, 'errors'); } } // Prepare the pre-selected id for To/Cc select inputs $preselected = array( 'to' => array(), 'cc' => array(), ); if (GETPOSTISSET('sendto_socpeople') || GETPOSTISSET('sendcc_socpeople')) { // Retrieve data from last form submission $preselected['to'] = GETPOST('sendto_socpeople'); $preselected['cc'] = GETPOST('sendcc_socpeople'); } else { // Retrieve data from model foreach ($mailObject->linkToSocPeoples as $contact) { $preselected[$contact->pivot->sendtype][] = $contact->id; } // add the third-party's email in case sendXX_thirdparty is true if ($mailObject->sendto_thirdparty) { $preselected['to'][] = 'thirdparty'; } if ($mailObject->sendcc_thirdparty) { $preselected['cc'][] = 'thirdparty'; } } /** * Part 2 : Display */ // Same tabs than the main page $head=invoice_rec_prepare_head($object); $output .= dol_get_fiche_head($head, 'sendrecurringinvoicebymail', $langs->trans("RepeatableInvoice"), -1, 'bill'); // Add a div $output .= '
' . $mailObject->fac_rec_object->ref . "
\n"; $output .= '
' . $langs->trans('ThirdParty') . ' : ' . $mailObject->fac_rec_object->thirdparty->getNomUrl(1) . "
\n"; $output .= "\n\n"; $output .= '
' . $langs->trans("Options") . "
\n"; $output .= '
'; $output .= ''; $output .= ''; $output .= ' \n"; $output .= ' \n"; $output .= "\n"; $output .= "
active || GETPOSTISSET('active')) ? ' checked="checked"' : '') . " />
\n"; // Little explanations $output .= '
' . $langs->trans("CustomizationTitle") . "
\n"; // TODO: translation $output .= "

" . $langs->trans('CustomizationIntro', $langs->trans('Reset')) . "
\n"; $output .= $langs->trans('CustomizationLinkToGlobalTemplate', DOL_URL_ROOT . '/admin/mails_templates.php?search_label=sendrecurring') . "

\n\n"; $output .= ''; $output .= '\n"; // Recipient(s) $output .= '\n\n"; // CC $output .= '\n\n"; // Subject $output .= '\n\n"; // addmaindocfile $output .= '\n"; $tmp_addmaindocfile = $mailObject->addmaindocfile; if (! empty($_POST)) { $tmp_addmaindocfile = GETPOSTISSET('addmaindocfile') ? 1 : 0; } $output .= '\n"; // body_plaintext $output .= '\n\n"; $output .= "
'.$langs->trans("MailFrom").''; $defaultFrom = 'robot'; if (isset($listFrom['old'])) { $defaultFrom = 'old'; } elseif ($mailObject->id) { $defaultFrom = $mailObject->fromtype; } $output .= $form->selectarray('fromtype', $listFrom, GETPOST('fromtype', 'alpha') ? GETPOST('fromtype', 'alpha') : $defaultFrom); $output .= "
'; $output .= $form->textwithpicto($langs->trans("MailTo"), $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients")); $output .= ""; $output .= 'sendto_free)) . '" />'; $output .= " " . $langs->trans("and") . "/" . $langs->trans("or") . " "; $output .= $form->multiselectarray('sendto_socpeople', $listContacts, $preselected['to']); $output .= "
'; $output .= $form->textwithpicto($langs->trans("MailCC"), $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients")); $output .= ""; $output .= 'sendcc_free)) . '" />'; $output .= " " . $langs->trans("and") . "/" . $langs->trans("or") . " "; $output .= $form->multiselectarray('sendcc_socpeople', $listContacts, $preselected['cc']); $output .= "
'; $output .= $form->textwithpicto($langs->trans("MailTopic"), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfromtopic'); $output .= ""; $output .= ''; $output .= "
' . $langs->trans('MailFile') . " ' . $langs->trans("JoinMainDoc") . "
'; $output .= $form->textwithpicto($langs->trans("MailText"), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfrombody'); $output .= ""; /* // doleditor does some weird stuff, adding
and newlines, I'll get more into it when I have time. // fallback to simple \n"; $output .= "
\n"; $output .= '
'; $output .= ''; $output .= 'id ? '' : ' title="' . $langs->trans('ResetTooltipNoCustomisationToReset') . '" disabled="disabled"') . ' />'; $output .= "
\n"; } while (false); // Print everything llxHeader('', $langs->trans('RepeatableInvoice') . ' - ' . $langs->trans('SendingByMail'), ''); print $output; llxFooter();