dolibarr 21.0.0-beta
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-2024 Frédéric France <frederic.france@free.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
51// Protection
52if (empty($objectclass) || empty($uploaddir)) {
53 dol_print_error(null, 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
54 exit;
55}
56
57'
58@phan-var-force string $massaction
59@phan-var-force string $objectclass
60@phan-var-force ?string $diroutputmassaction
61@phan-var-force ?string $uploaddir
62@phan-var-force string[] $toselect
63@phan-var-force array<string,mixed> $parameters
64';
65
66$error = 0;
67
68// Mass actions. Controls on number of lines checked.
69$maxformassaction = (!getDolGlobalString('MAIN_LIMIT_FOR_MASS_ACTIONS') ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
70if (!empty($massaction) && is_array($toselect) && count($toselect) < 1) {
71 $error++;
72 setEventMessages($langs->trans("NoRecordSelected"), null, "warnings");
73}
74if (!$error && is_array($toselect) && count($toselect) > $maxformassaction) {
75 setEventMessages($langs->trans('TooManyRecordForMassAction', $maxformassaction), null, 'errors');
76 $error++;
77}
78
79if (!$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
80 $massaction = 'presend_attendees';
81}
82
83if (!$error && $massaction == 'confirm_presend_attendees') {
84 $resaction = '';
85 $nbsent = 0;
86 $nbignored = 0;
87 $langs->load("mails");
88 include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
89
90 $listofobjectid = array();
91
92 $listofobjectref = array();
93 $oneemailperrecipient = (GETPOSTINT('oneemailperrecipient') ? 1 : 0);
94
95 $listofselectedid = array();
96 $listofselectedref = array();
97 if (!$error) {
98 require_once DOL_DOCUMENT_ROOT . '/eventorganization/class/conferenceorboothattendee.class.php';
99 $attendee = new ConferenceOrBoothAttendee($db);
100 $objecttmp = new $objectclass($db);
101 '@phan-var-force CommonObject $objecttmp';
102
103 foreach ($toselect as $toselectid) {
104 $result = $objecttmp->fetch($toselectid);
105 if ($result > 0) {
106 $attendees = $attendee->fetchAll();
107 if (is_array($attendees) && count($attendees) > 0) {
108 foreach ($attendees as $attmail) {
109 if (!empty($attmail->email)) {
110 $attmail->fetch_thirdparty();
111 $listofselectedid[$attmail->email] = $attmail;
112 $listofselectedref[$attmail->email] = $objecttmp;
113 }
114 }
115 }
116 }
117 }
118 }
119 '@phan-var-force CommonObject $objecttmp';
120 '@phan-var-force array<string,CommonObject> $listofselectedref';
121
122 // Check mandatory parameters
123 if (GETPOST('fromtype', 'alpha') === 'user' && empty($user->email)) {
124 $error++;
125 setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings');
126 $massaction = 'presend_attendees';
127 }
128
129 $receiver = GETPOST('receiver', 'alphawithlgt');
130 if (!is_array($receiver)) {
131 if (empty($receiver) || $receiver == '-1') {
132 $receiver = array();
133 } else {
134 $receiver = array($receiver);
135 }
136 }
137 if (!trim(GETPOST('sendto', 'alphawithlgt')) && count($receiver) == 0 && count($listofselectedid) == 0) { // if only one recipient, receiver is mandatory
138 $error++;
139 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Recipient")), null, 'warnings');
140 $massaction = 'presend_attendees';
141 }
142
143 if (!GETPOST('subject', 'restricthtml')) {
144 $error++;
145 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTopic")), null, 'warnings');
146 $massaction = 'presend_attendees';
147 }
148
149 if (!$error && !empty($listofselectedid)) {
150 $objecttmp->fetch_thirdparty();
151 foreach ($listofselectedid as $email => $attendees) {
152 $sendto = '';
153 $sendtocc = '';
154 $sendtobcc = '';
155 $sendtoid = array();
156
157 // Define $sendto
158 $sendto = $attendees->thirdparty->name . '<' . trim($attendees->email) . '>';
159
160 // Define $sendtocc
161 $receivercc = GETPOST('receivercc', 'alphawithlgt');
162 if (!is_array($receivercc)) {
163 if ($receivercc == '-1') {
164 $receivercc = array();
165 } else {
166 $receivercc = array($receivercc);
167 }
168 }
169 $tmparray = array();
170 if (trim(GETPOST('sendtocc', 'alphawithlgt'))) {
171 $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
172 }
173 $sendtocc = implode(',', $tmparray);
174
175
176 $langs->load("commercial");
177
178 $reg = array();
179 $fromtype = GETPOST('fromtype');
180 if ($fromtype === 'user') {
181 $from = $user->getFullName($langs) . ' <' . $user->email . '>';
182 } elseif ($fromtype === 'company') {
183 $from = getDolGlobalString('MAIN_INFO_SOCIETE_NOM') . ' <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '>';
184 } elseif (preg_match('/user_aliases_(\d+)/', $fromtype, $reg)) {
185 $tmp = explode(',', $user->email_aliases);
186 $from = trim($tmp[((int) $reg[1] - 1)]);
187 } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
188 $tmp = explode(',', getDolGlobalString('MAIN_INFO_SOCIETE_MAIL_ALIASES'));
189 $from = trim($tmp[((int) $reg[1] - 1)]);
190 } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
191 $sql = "SELECT rowid, label, email FROM " . MAIN_DB_PREFIX . "c_email_senderprofile WHERE rowid = " . (int) $reg[1];
192 $resql = $db->query($sql);
193 $obj = $db->fetch_object($resql);
194 if ($obj) {
195 $from = dol_string_nospecial($obj->label, ' ', array(",")) . ' <' . $obj->email . '>';
196 }
197 } else {
198 $from = dol_string_nospecial(GETPOST('fromname'), ' ', array(",")) . ' <' . GETPOST('frommail') . '>';
199 }
200
201 $replyto = $from;
202 $subject = GETPOST('subject', 'restricthtml');
203 $message = GETPOST('message', 'restricthtml');
204
205 $sendtobcc = GETPOST('sendtoccc', 'alphawithlgt');
206
207 // $objecttmp is a real object or an empty object if we choose to send one email per thirdparty instead of one per object
208 // Make substitution in email content
209 $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $attendees);
210 $url_link = null;
211 $html_link = null;
212
213 if (getDolGlobalString('MAIN_AGENDA_XCAL_EXPORTKEY')) {
214 $urlwithouturlroot = preg_replace('/' . preg_quote(DOL_URL_ROOT, '/') . '$/i', '', trim($dolibarr_main_url_root));
215 $urlwithroot = $urlwithouturlroot . DOL_URL_ROOT;
216 $url_link = $urlwithroot . '/public/agenda/agendaexport.php?format=ical' . ($conf->entity > 1 ? "&entity=" . $conf->entity : "");
217 $url_link .= '&exportkey=' . ($conf->global->MAIN_AGENDA_XCAL_EXPORTKEY ? urlencode(getDolGlobalString('MAIN_AGENDA_XCAL_EXPORTKEY')) : '...');
218 $url_link .= "&project=" . $listofselectedref[$email]->fk_project . '&module=' . urlencode('@eventorganization') . '&status=' . ConferenceOrBooth::STATUS_CONFIRMED;
219 $html_link = '<a href="' . $url_link . '">' . $langs->trans('DownloadICSLink') . '</a>';
220 }
221 $substitutionarray['__EVENTORGANIZATION_ICS_LINK__'] = $html_link;
222 $substitutionarray['__EVENTORGANIZATION_URL_LINK__'] = $url_link;
223
224 $parameters = array('mode' => 'formemail');
225
226 if (!empty($listofobjectref)) {
227 $parameters['listofobjectref'] = $listofobjectref;
228 }
229
230 complete_substitutions_array($substitutionarray, $langs, $attendees, $parameters);
231
232 $subjectreplaced = make_substitutions($subject, $substitutionarray);
233 $messagereplaced = make_substitutions($message, $substitutionarray);
234
235
236 if (empty($sendcontext)) {
237 $sendcontext = 'standard';
238 }
239
240 // Send mail (substitutionarray must be done just before this)
241 require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
242 $mailfile = new CMailFile($subjectreplaced, $sendto, $from, $messagereplaced, array(), array(), array(), $sendtocc, $sendtobcc, $deliveryreceipt, -1, '', '', "attendees_".$attendees->id, '', $sendcontext);
243 if ($mailfile->error) {
244 $resaction .= '<div class="error">' . $mailfile->error . '</div>';
245 } else {
246 $result = $mailfile->sendfile();
247 if ($result) {
248 $resaction .= $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2)) . '<br>'; // Must not contain "
249 $error = 0;
250
251 dol_syslog("Try to insert email event into agenda for objid=" . $attendees->id . " => objectobj=" . get_class($attendees));
252
253 $actionmsg = $langs->transnoentities('MailSentByTo', $from, $sendto);
254 if ($message) {
255 if ($sendtocc) {
256 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
257 }
258 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subjectreplaced);
259 $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
260 $actionmsg = dol_concatdesc($actionmsg, $messagereplaced);
261 }
262 $actionmsg2 = '';
263
264 $objectobj2 = $listofselectedref[$email];
265 // Initialisation donnees
266 $objectobj2->actionmsg = $actionmsg; // Long text
267 $objectobj2->actionmsg2 = $actionmsg2; // Short text
268 $objectobj2->fk_element = $objectobj2->id;
269 $objectobj2->elementtype = $objectobj2->element;
270
271 $triggername = 'CONFERENCEORBOOTHATTENDEE_SENTBYMAIL';
272 if (!empty($triggername)) {
273 // Call trigger
274 $result = $objectobj2->call_trigger($triggername, $user);
275 if ($result < 0) {
276 $error++;
277 }
278 // End call triggers
279
280 if ($error) {
281 setEventMessages($db->lasterror(), $objectobj2->errors, 'errors');
282 dol_syslog("Error in trigger " . $triggername . ' ' . $db->lasterror(), LOG_ERR);
283 }
284 }
285
286 $nbsent++; // Nb of object sent
287 } else {
288 $langs->load("other");
289 if ($mailfile->error) {
290 $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto);
291 $resaction .= '<br><div class="error">' . $mailfile->error . '</div>';
292 } elseif (getDolGlobalString('MAIN_DISABLE_ALL_MAILS')) {
293 $resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
294 } else {
295 $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto) . '<br><div class="error">(unhandled error)</div>';
296 }
297 }
298 }
299 } // foreach ($listofselectedid as $email => $attendees)
300 }
301 $resaction .= ($resaction ? '<br>' : $resaction);
302 $resaction .= '<strong>' . $langs->trans("ResultOfMailSending") . ':</strong><br>' . "\n";
303 $resaction .= $langs->trans("NbSelected") . ': ' . count($toselect) . "\n<br>";
304 $resaction .= $langs->trans("NbIgnored") . ': ' . ($nbignored ? $nbignored : 0) . "\n<br>";
305 $resaction .= $langs->trans("NbSent") . ': ' . ($nbsent ? $nbsent : 0) . "\n<br>";
306
307 if ($nbsent) {
308 $action = ''; // Do not show form post if there was at least one successful sent
309 //setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
310 setEventMessages($langs->trans("EMailSentForNElements", $nbsent . '/' . count($toselect)), null, 'mesgs');
311 setEventMessages($resaction, null, 'mesgs');
312 } else {
313 //setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
314 setEventMessages($resaction, null, 'warnings');
315 }
316
317 $action = 'list';
318 $massaction = '';
319}
320
321
322
323$parameters['toselect'] = $toselect;
324$parameters['uploaddir'] = $uploaddir;
325$parameters['massaction'] = $massaction;
326$parameters['diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction : null;
327
328$reshook = $hookmanager->executeHooks('doMassActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
329if ($reshook < 0) {
330 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
331}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class for ConferenceOrBoothAttendee.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79