dolibarr 20.0.0
actions_massactions_mail.inc.php
1<?php
2/* Copyright (C) 2015-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2018-2021 Nicolas ZABOURI <info@inovea-conseil.com>
4 * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2019 Ferran Marcet <fmarcet@2byte.es>
6 * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30// $massaction must be defined
31// $objectclass and $objectlabel must be defined
32// $parameters, $object, $action must be defined for the hook.
33
34// $permissiontoread, $permissiontoadd, $permissiontodelete, $permissiontoclose may be defined
35// $uploaddir may be defined (example to $conf->project->dir_output."/";)
36// $toselect may be defined
37// $diroutputmassaction may be defined
38
39
40// Protection
41if (empty($objectclass) || empty($uploaddir)) {
42 dol_print_error(null, 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
43 exit;
44}
45
46// Mass actions. Controls on number of lines checked.
47$maxformassaction = (!getDolGlobalString('MAIN_LIMIT_FOR_MASS_ACTIONS') ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
48if (!empty($massaction) && is_array($toselect) && count($toselect) < 1) {
49 $error++;
50 setEventMessages($langs->trans("NoRecordSelected"), null, "warnings");
51}
52if (!$error && is_array($toselect) && count($toselect) > $maxformassaction) {
53 setEventMessages($langs->trans('TooManyRecordForMassAction', $maxformassaction), null, 'errors');
54 $error++;
55}
56
57if (!$error && $massaction == 'confirm_presend_attendees' && !GETPOST('sendmail')) { // If we do not choose button send (for example when we change template or limit), we must not send email, but keep on send email form
58 $massaction = 'presend_attendees';
59}
60if (!$error && $massaction == 'confirm_presend_attendees') {
61 $resaction = '';
62 $nbsent = 0;
63 $nbignored = 0;
64 $langs->load("mails");
65 include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
66
67 $listofobjectid = array();
68
69 $listofobjectref = array();
70 $oneemailperrecipient = (GETPOSTINT('oneemailperrecipient') ? 1 : 0);
71
72 if (!$error) {
73 require_once DOL_DOCUMENT_ROOT . '/eventorganization/class/conferenceorboothattendee.class.php';
74 $attendee = new ConferenceOrBoothAttendee($db);
75 $listofselectedid = array();
76 $listofselectedref = array();
77 $objecttmp = new $objectclass($db);
78
79 foreach ($toselect as $toselectid) {
80 $result = $objecttmp->fetch($toselectid);
81 if ($result > 0) {
82 $attendees = $attendee->fetchAll();
83 if (is_array($attendees) && count($attendees) > 0) {
84 foreach ($attendees as $attmail) {
85 if (!empty($attmail->email)) {
86 $attmail->fetch_thirdparty();
87 $listofselectedid[$attmail->email] = $attmail;
88 $listofselectedref[$attmail->email] = $objecttmp;
89 }
90 }
91 }
92 }
93 }
94 }
95 '@phan-var-force CommonObject $objecttmp';
96
97 // Check mandatory parameters
98 if (GETPOST('fromtype', 'alpha') === 'user' && empty($user->email)) {
99 $error++;
100 setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings');
101 $massaction = 'presend_attendees';
102 }
103
104 $receiver = GETPOST('receiver', 'alphawithlgt');
105 if (!is_array($receiver)) {
106 if (empty($receiver) || $receiver == '-1') {
107 $receiver = array();
108 } else {
109 $receiver = array($receiver);
110 }
111 }
112 if (!trim(GETPOST('sendto', 'alphawithlgt')) && count($receiver) == 0 && count($listofselectedid) == 0) { // if only one recipient, receiver is mandatory
113 $error++;
114 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Recipient")), null, 'warnings');
115 $massaction = 'presend_attendees';
116 }
117
118 if (!GETPOST('subject', 'restricthtml')) {
119 $error++;
120 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTopic")), null, 'warnings');
121 $massaction = 'presend_attendees';
122 }
123
124 if (!$error) {
125 $objecttmp->fetch_thirdparty();
126 foreach ($listofselectedid as $email => $attendees) {
127 $sendto = '';
128 $sendtocc = '';
129 $sendtobcc = '';
130 $sendtoid = array();
131
132 // Define $sendto
133 $sendto = $attendees->thirdparty->name . '<' . trim($attendees->email) . '>';
134
135 // Define $sendtocc
136 $receivercc = GETPOST('receivercc', 'alphawithlgt');
137 if (!is_array($receivercc)) {
138 if ($receivercc == '-1') {
139 $receivercc = array();
140 } else {
141 $receivercc = array($receivercc);
142 }
143 }
144 $tmparray = array();
145 if (trim(GETPOST('sendtocc', 'alphawithlgt'))) {
146 $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
147 }
148 $sendtocc = implode(',', $tmparray);
149
150
151 $langs->load("commercial");
152
153 $reg = array();
154 $fromtype = GETPOST('fromtype');
155 if ($fromtype === 'user') {
156 $from = $user->getFullName($langs) . ' <' . $user->email . '>';
157 } elseif ($fromtype === 'company') {
158 $from = getDolGlobalString('MAIN_INFO_SOCIETE_NOM') . ' <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '>';
159 } elseif (preg_match('/user_aliases_(\d+)/', $fromtype, $reg)) {
160 $tmp = explode(',', $user->email_aliases);
161 $from = trim($tmp[($reg[1] - 1)]);
162 } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
163 $tmp = explode(',', getDolGlobalString('MAIN_INFO_SOCIETE_MAIL_ALIASES'));
164 $from = trim($tmp[($reg[1] - 1)]);
165 } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
166 $sql = "SELECT rowid, label, email FROM " . MAIN_DB_PREFIX . "c_email_senderprofile WHERE rowid = " . (int) $reg[1];
167 $resql = $db->query($sql);
168 $obj = $db->fetch_object($resql);
169 if ($obj) {
170 $from = dol_string_nospecial($obj->label, ' ', array(",")) . ' <' . $obj->email . '>';
171 }
172 } else {
173 $from = dol_string_nospecial(GETPOST('fromname'), ' ', array(",")) . ' <' . GETPOST('frommail') . '>';
174 }
175
176 $replyto = $from;
177 $subject = GETPOST('subject', 'restricthtml');
178 $message = GETPOST('message', 'restricthtml');
179
180 $sendtobcc = GETPOST('sendtoccc', 'alphawithlgt');
181
182 // $objecttmp is a real object or an empty object if we choose to send one email per thirdparty instead of one per object
183 // Make substitution in email content
184 $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $attendees);
185
186 if (getDolGlobalString('MAIN_AGENDA_XCAL_EXPORTKEY')) {
187 $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root));
188 $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT;
189 $url_link = $urlwithroot . '/public/agenda/agendaexport.php?format=ical' . ($conf->entity > 1 ? "&entity=" . $conf->entity : "");
190 $url_link .= '&exportkey=' . ($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY ? urlencode(getDolGlobalString('MAIN_AGENDA_XCAL_EXPORTKEY')) : '...');
191 $url_link .= "&project=" . $listofselectedref[$email]->fk_project . '&module=' . urlencode('@eventorganization') . '&status=' . ConferenceOrBooth::STATUS_CONFIRMED;
192 $html_link = '<a href="' . $url_link . '">' . $langs->trans('DownloadICSLink') . '</a>';
193 }
194 $substitutionarray['__EVENTORGANIZATION_ICS_LINK__'] = $html_link;
195 $substitutionarray['__EVENTORGANIZATION_URL_LINK__'] = $url_link;
196
197 $parameters = array('mode' => 'formemail');
198
199 if (!empty($listofobjectref)) {
200 $parameters['listofobjectref'] = $listofobjectref;
201 }
202
203 complete_substitutions_array($substitutionarray, $langs, $attendees, $parameters);
204
205 $subjectreplaced = make_substitutions($subject, $substitutionarray);
206 $messagereplaced = make_substitutions($message, $substitutionarray);
207
208
209 if (empty($sendcontext)) {
210 $sendcontext = 'standard';
211 }
212
213 // Send mail (substitutionarray must be done just before this)
214 require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
215 $mailfile = new CMailFile($subjectreplaced, $sendto, $from, $messagereplaced, array(), array(), array(), $sendtocc, $sendtobcc, $deliveryreceipt, -1, '', '', "attendees_".$attendees->id, '', $sendcontext);
216 if ($mailfile->error) {
217 $resaction .= '<div class="error">' . $mailfile->error . '</div>';
218 } else {
219 $result = $mailfile->sendfile();
220 if ($result) {
221 $resaction .= $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2)) . '<br>'; // Must not contain "
222 $error = 0;
223
224 dol_syslog("Try to insert email event into agenda for objid=" . $attendees->id . " => objectobj=" . get_class($attendees));
225
226 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
227 if ($message) {
228 if ($sendtocc) {
229 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
230 }
231 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subjectreplaced);
232 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
233 $actionmsg = dol_concatdesc($actionmsg, $messagereplaced);
234 }
235 $actionmsg2 = '';
236
237 $objectobj2 = $listofselectedref[$email];
238 // Initialisation donnees
239 $objectobj2->actionmsg = $actionmsg; // Long text
240 $objectobj2->actionmsg2 = $actionmsg2; // Short text
241 $objectobj2->fk_element = $objectobj2->id;
242 $objectobj2->elementtype = $objectobj2->element;
243
244 $triggername = 'CONFERENCEORBOOTHATTENDEE_SENTBYMAIL';
245 if (!empty($triggername)) {
246 // Call trigger
247 $result = $objectobj2->call_trigger($triggername, $user);
248 if ($result < 0) {
249 $error++;
250 }
251 // End call triggers
252
253 if ($error) {
254 setEventMessages($db->lasterror(), $objectobj2->errors, 'errors');
255 dol_syslog("Error in trigger " . $triggername . ' ' . $db->lasterror(), LOG_ERR);
256 }
257 }
258
259 $nbsent++; // Nb of object sent
260 } else {
261 $langs->load("other");
262 if ($mailfile->error) {
263 $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto);
264 $resaction .= '<br><div class="error">' . $mailfile->error . '</div>';
265 } elseif (getDolGlobalString('MAIN_DISABLE_ALL_MAILS')) {
266 $resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
267 } else {
268 $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto) . '<br><div class="error">(unhandled error)</div>';
269 }
270 }
271 }
272 }
273 }
274 $resaction .= ($resaction ? '<br>' : $resaction);
275 $resaction .= '<strong>' . $langs->trans("ResultOfMailSending") . ':</strong><br>' . "\n";
276 $resaction .= $langs->trans("NbSelected") . ': ' . count($toselect) . "\n<br>";
277 $resaction .= $langs->trans("NbIgnored") . ': ' . ($nbignored ? $nbignored : 0) . "\n<br>";
278 $resaction .= $langs->trans("NbSent") . ': ' . ($nbsent ? $nbsent : 0) . "\n<br>";
279
280 if ($nbsent) {
281 $action = ''; // Do not show form post if there was at least one successful sent
282 //setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
283 setEventMessages($langs->trans("EMailSentForNElements", $nbsent . '/' . count($toselect)), null, 'mesgs');
284 setEventMessages($resaction, null, 'mesgs');
285 } else {
286 //setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
287 setEventMessages($resaction, null, 'warnings');
288 }
289
290 $action = 'list';
291 $massaction = '';
292}
293
294
295
296$parameters['toselect'] = $toselect;
297$parameters['uploaddir'] = $uploaddir;
298$parameters['massaction'] = $massaction;
299$parameters['diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction : null;
300
301$reshook = $hookmanager->executeHooks('doMassActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
302if ($reshook < 0) {
303 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
304}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class for ConferenceOrBoothAttendee.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_concatdesc($text1, $text2, $forxml=false, $invert=false)
Concat 2 descriptions with a new line between them (second operand after first one with appropriate n...
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.