40 if (empty($objectclass) || empty($uploaddir)) {
41 dol_print_error(
null,
'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
46 if (!empty($permtoread) && empty($permissiontoread)) {
47 $permissiontoread = $permtoread;
49 if (!empty($permtocreate) && empty($permissiontoadd)) {
50 $permissiontoadd = $permtocreate;
52 if (!empty($permtodelete) && empty($permissiontodelete)) {
53 $permissiontodelete = $permtodelete;
58 $maxformassaction = (empty($conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS) ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
59 if (!empty($massaction) && is_array($toselect) && count($toselect) < 1) {
63 if (!$error && is_array($toselect) && count($toselect) > $maxformassaction) {
64 setEventMessages($langs->trans(
'TooManyRecordForMassAction', $maxformassaction),
null,
'errors');
68 if (!$error && $massaction ==
'confirm_presend' && !
GETPOST(
'sendmail')) {
69 $massaction =
'presend';
71 if (!$error && $massaction ==
'confirm_presend') {
75 $langs->load(
"mails");
76 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
77 include_once DOL_DOCUMENT_ROOT.
'/core/lib/signature.lib.php';
79 $listofobjectid = array();
80 $listofobjectthirdparties = array();
81 $listofobjectcontacts = array();
82 $listofobjectref = array();
83 $contactidtosend = array();
84 $attachedfilesThirdpartyObj = array();
85 $oneemailperrecipient = (
GETPOST(
'oneemailperrecipient',
'int') ? 1 : 0);
90 $objecttmp =
new $objectclass($db);
91 if ($objecttmp->element ==
'expensereport') {
92 $thirdparty =
new User($db);
94 if ($objecttmp->element ==
'partnership' &&
getDolGlobalString(
'PARTNERSHIP_IS_MANAGED_FOR') ==
'member') {
97 if ($objecttmp->element ==
'holiday') {
98 $thirdparty =
new User($db);
101 foreach ($toselect as $toselectid) {
102 $objecttmp =
new $objectclass($db);
103 $result = $objecttmp->fetch($toselectid);
105 $listofobjectid[$toselectid] = $toselectid;
107 $thirdpartyid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid);
108 if ($objecttmp->element ==
'societe') {
109 $thirdpartyid = $objecttmp->id;
111 if ($objecttmp->element ==
'expensereport') {
112 $thirdpartyid = $objecttmp->fk_user_author;
114 if ($objecttmp->element ==
'partnership' &&
getDolGlobalString(
'PARTNERSHIP_IS_MANAGED_FOR') ==
'member') {
115 $thirdpartyid = $objecttmp->fk_member;
117 if ($objecttmp->element ==
'holiday') {
118 $thirdpartyid = $objecttmp->fk_user;
120 if (empty($thirdpartyid)) {
124 if ($objectclass ==
'Facture') {
125 $tmparraycontact = array();
126 $tmparraycontact = $objecttmp->liste_contact(-1,
'external', 0,
'BILLING');
127 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
128 foreach ($tmparraycontact as $data_email) {
129 $listofobjectcontacts[$toselectid][$data_email[
'id']] = $data_email[
'email'];
132 } elseif ($objectclass ==
'CommandeFournisseur') {
133 $tmparraycontact = array();
134 $tmparraycontact = $objecttmp->liste_contact(-1,
'external', 0,
'CUSTOMER');
135 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
136 foreach ($tmparraycontact as $data_email) {
137 $listofobjectcontacts[$toselectid][$data_email[
'id']] = $data_email[
'email'];
142 $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
143 $listofobjectref[$thirdpartyid][$toselectid] = $objecttmp;
149 if (
GETPOST(
'fromtype',
'alpha') ===
'user' && empty($user->email)) {
152 $massaction =
'presend';
155 $receiver =
GETPOST(
'receiver',
'alphawithlgt');
156 if (!is_array($receiver)) {
157 if (empty($receiver) || $receiver ==
'-1') {
160 $receiver = array($receiver);
163 if (!trim($_POST[
'sendto']) && count($receiver) == 0 && count($listofobjectthirdparties) == 1) {
165 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Recipient")),
null,
'warnings');
166 $massaction =
'presend';
169 if (!
GETPOST(
'subject',
'restricthtml')) {
171 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"MailTopic")),
null,
'warnings');
172 $massaction =
'presend';
177 foreach ($listofobjectthirdparties as $thirdpartyid) {
178 $result = $thirdparty->fetch($thirdpartyid);
191 if (trim($_POST[
'sendto'])) {
193 $tmparray[] = trim(
GETPOST(
'sendto',
'alphawithlgt'));
195 if (count($receiver) > 0) {
196 foreach ($receiver as $key => $val) {
198 if ($val ==
'thirdparty') {
199 $tmparray[] = $thirdparty->name.
' <'.$thirdparty->email.
'>';
200 } elseif ($val && method_exists($thirdparty,
'contact_get_property')) {
201 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
206 $sendto = implode(
',', $tmparray);
209 $receivercc =
GETPOST(
'receivercc',
'alphawithlgt');
210 if (!is_array($receivercc)) {
211 if ($receivercc ==
'-1') {
212 $receivercc = array();
214 $receivercc = array($receivercc);
218 if (trim($_POST[
'sendtocc'])) {
219 $tmparray[] = trim(
GETPOST(
'sendtocc',
'alphawithlgt'));
221 if (count($receivercc) > 0) {
222 foreach ($receivercc as $key => $val) {
224 if ($val ==
'thirdparty') {
225 $tmparray[] = $thirdparty->name.
' <'.$thirdparty->email.
'>';
227 $tmparray[] = $thirdparty->contact_get_property((
int) $val,
'email');
232 $sendtocc = implode(
',', $tmparray);
235 $listofqualifiedobj = array();
236 $listofqualifiedref = array();
237 $thirdpartywithoutemail = array();
239 foreach ($listofobjectref[$thirdpartyid] as $objectid => $objectobj) {
242 $langs->load(
"errors");
244 $resaction .=
'<div class="error">'.$langs->trans(
'ErrorOnlyProposalNotDraftCanBeSentInMassAction', $objectobj->ref).
'</div><br>';
248 $langs->load(
"errors");
250 $resaction .=
'<div class="error">'.$langs->trans(
'ErrorOnlyOrderNotDraftCanBeSentInMassAction', $objectobj->ref).
'</div><br>';
254 $langs->load(
"errors");
256 $resaction .=
'<div class="error">'.$langs->trans(
'ErrorOnlyInvoiceValidatedCanBeSentInMassAction', $objectobj->ref).
'</div><br>';
261 if (empty($sendto)) {
262 if ($objectobj->element ==
'societe') {
263 $sendto = $objectobj->email;
264 } elseif ($objectobj->element ==
'expensereport') {
265 $fuser =
new User($db);
266 $fuser->fetch($objectobj->fk_user_author);
267 $sendto = $fuser->email;
268 } elseif ($objectobj->element ==
'partnership' &&
getDolGlobalString(
'PARTNERSHIP_IS_MANAGED_FOR') ==
'member') {
270 $fadherent->fetch($objectobj->fk_member);
271 $sendto = $fadherent->email;
272 } elseif ($objectobj->element ==
'holiday') {
273 $fuser =
new User($db);
274 $fuser->fetch($objectobj->fk_user);
275 $sendto = $fuser->email;
276 } elseif ($objectobj->element ==
'facture' && !empty($listofobjectcontacts[$objectid])) {
277 $emails_to_sends = array();
278 $objectobj->fetch_thirdparty();
279 $contactidtosend = array();
280 foreach ($listofobjectcontacts[$objectid] as $contactemailid => $contactemailemail) {
281 $emails_to_sends[] = $objectobj->thirdparty->contact_get_property($contactemailid,
'email');
282 if (!in_array($contactemailid, $contactidtosend)) {
283 $contactidtosend[] = $contactemailid;
286 if (count($emails_to_sends) > 0) {
287 $sendto = implode(
',', $emails_to_sends);
289 } elseif ($objectobj->element ==
'order_supplier' && !empty($listofobjectcontacts[$objectid])) {
290 $emails_to_sends = array();
291 $objectobj->fetch_thirdparty();
292 $contactidtosend = array();
293 foreach ($listofobjectcontacts[$objectid] as $contactemailid => $contactemailemail) {
294 $emails_to_sends[] = $objectobj->thirdparty->contact_get_property($contactemailid,
'email');
295 if (!in_array($contactemailid, $contactidtosend)) {
296 $contactidtosend[] = $contactemailid;
299 if (count($emails_to_sends) > 0) {
300 $sendto = implode(
',', $emails_to_sends);
303 $objectobj->fetch_thirdparty();
304 $sendto = $objectobj->thirdparty->email;
308 if (empty($sendto)) {
309 if ($objectobj->element ==
'societe') {
310 $objectobj->thirdparty = $objectobj;
315 if (empty($thirdpartywithoutemail[$objectobj->thirdparty->id])) {
316 $resaction .=
'<div class="error">'.$langs->trans(
'NoRecipientEmail', $objectobj->thirdparty->name).
'</div><br>';
318 dol_syslog(
'No recipient for thirdparty: '.$objectobj->thirdparty->name, LOG_WARNING);
319 $thirdpartywithoutemail[$objectobj->thirdparty->id] = 1;
323 if (
GETPOST(
'addmaindocfile')) {
330 $filedir = $uploaddir.
'/'.$subdir.dol_sanitizeFileName($objectobj->ref);
331 $filepath = $filedir.
'/'.$filename;
334 if ($objectobj->element ==
'invoice_supplier') {
335 $fileparams =
dol_most_recent_file($uploaddir.
'/'.
get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element).$objectobj->ref, preg_quote($objectobj->ref,
'/').
'([^\-])+');
336 $filepath = $fileparams[
'fullname'];
340 $filename_found =
'';
341 $filepath_found =
'';
342 $file_check_list = array();
343 $file_check_list[] = array(
347 if (!empty($conf->global->MAIL_MASS_ACTION_ADD_LAST_IF_MAIN_DOC_NOT_FOUND) && !empty($objectobj->last_main_doc)) {
348 $file_check_list[] = array(
349 'name' => basename($objectobj->last_main_doc),
350 'path' => DOL_DATA_ROOT .
'/' . $objectobj->last_main_doc,
353 foreach ($file_check_list as $file_check_arr) {
355 $filename_found = $file_check_arr[
'name'];
356 $filepath_found = $file_check_arr[
'path'];
361 if ($filepath_found) {
363 $attachedfilesThirdpartyObj[$thirdpartyid][$objectid] = array(
364 'paths'=>array($filepath_found),
365 'names'=>array($filename_found),
370 $langs->load(
"errors");
371 foreach ($file_check_list as $file_check_arr) {
372 $resaction .=
'<div class="error">'.$langs->trans(
'ErrorCantReadFile', $file_check_arr[
'path']).
'</div><br>';
373 dol_syslog(
'Failed to read file: '.$file_check_arr[
'path'], LOG_WARNING);
380 $listofqualifiedobj[$objectid] = $objectobj;
381 $listofqualifiedref[$objectid] = $objectobj->ref;
388 if (count($listofqualifiedobj) > 0) {
389 $langs->load(
"commercial");
392 $fromtype =
GETPOST(
'fromtype');
393 if ($fromtype ===
'user') {
394 $from =
dol_string_nospecial($user->getFullName($langs),
' ', array(
",")).
' <'.$user->email.
'>';
395 } elseif ($fromtype ===
'company') {
396 $from = $conf->global->MAIN_INFO_SOCIETE_NOM.
' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
397 } elseif (preg_match(
'/user_aliases_(\d+)/', $fromtype, $reg)) {
398 $tmp = explode(
',', $user->email_aliases);
399 $from = trim($tmp[($reg[1] - 1)]);
400 } elseif (preg_match(
'/global_aliases_(\d+)/', $fromtype, $reg)) {
401 $tmp = explode(
',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
402 $from = trim($tmp[($reg[1] - 1)]);
403 } elseif (preg_match(
'/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
404 $sql =
"SELECT rowid, label, email FROM ".MAIN_DB_PREFIX.
"c_email_senderprofile WHERE rowid = ".(int) $reg[1];
405 $resql = $db->query($sql);
406 $obj = $db->fetch_object(
$resql);
415 $subject =
GETPOST(
'subject',
'restricthtml');
416 $message =
GETPOST(
'message',
'restricthtml');
418 $sendtobcc =
GETPOST(
'sendtoccc');
419 if ($objectclass ==
'Propal') {
420 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO));
422 if ($objectclass ==
'Commande') {
423 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO));
425 if ($objectclass ==
'Facture') {
426 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO));
428 if ($objectclass ==
'Supplier_Proposal') {
429 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO));
431 if ($objectclass ==
'CommandeFournisseur') {
432 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO));
434 if ($objectclass ==
'FactureFournisseur') {
435 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO));
437 if ($objectclass ==
'Project') {
438 $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_PROJECT_TO) ?
'' : (($sendtobcc ?
", " :
"").$conf->global->MAIN_MAIL_AUTOCOPY_PROJECT_TO));
443 $looparray = array();
444 if (!$oneemailperrecipient) {
445 $looparray = $listofqualifiedobj;
446 foreach ($looparray as $key => $objecttmp) {
447 $looparray[$key]->thirdparty = $thirdparty;
450 $objectforloop =
new $objectclass($db);
451 $objectforloop->thirdparty = $thirdparty;
452 $looparray[0] = $objectforloop;
455 dol_syslog(
"We have set an array of ".count($looparray).
" emails to send. oneemailperrecipient=".$oneemailperrecipient);
457 foreach ($looparray as $objectid => $objecttmp) {
459 if (!empty($conf->project->enabled) && method_exists($objecttmp,
'fetch_projet') && is_null($objecttmp->project)) {
460 $objecttmp->fetch_projet();
463 $substitutionarray[
'__ID__'] = ($oneemailperrecipient ? join(
', ', array_keys($listofqualifiedobj)) : $objecttmp->id);
464 $substitutionarray[
'__REF__'] = ($oneemailperrecipient ? join(
', ', $listofqualifiedref) : $objecttmp->
ref);
465 $substitutionarray[
'__EMAIL__'] = $thirdparty->email;
466 $substitutionarray[
'__CHECK_READ__'] =
'<img src="'.DOL_MAIN_URL_ROOT.
'/public/emailing/mailing-read.php?tag='.urlencode($thirdparty->tag).
'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).
'" width="1" height="1" style="width:1px;height:1px" border="0"/>';
468 $parameters = array(
'mode'=>
'formemail');
470 if (!empty($listofobjectthirdparties)) {
471 $parameters[
'listofobjectthirdparties'] = $listofobjectthirdparties;
473 if (!empty($listofobjectref)) {
474 $parameters[
'listofobjectref'] = $listofobjectref;
482 $attachedfiles = array(
'paths'=>array(),
'names'=>array(),
'mimes'=>array());
483 if ($oneemailperrecipient) {
485 if (is_array($attachedfilesThirdpartyObj[$thirdparty->id]) && count($attachedfilesThirdpartyObj[$thirdparty->id])) {
486 foreach ($attachedfilesThirdpartyObj[$thirdparty->id] as $keyObjId => $objAttachedFiles) {
488 $attachedfiles = array(
489 'paths'=>array_merge($attachedfiles[
'paths'], $objAttachedFiles[
'paths']),
490 'names'=>array_merge($attachedfiles[
'names'], $objAttachedFiles[
'names']),
491 'mimes'=>array_merge($attachedfiles[
'mimes'], $objAttachedFiles[
'mimes'])
495 } elseif (!empty($attachedfilesThirdpartyObj[$thirdparty->id][$objectid])) {
498 $attachedfiles = $attachedfilesThirdpartyObj[$thirdparty->id][$objectid];
501 $filepath = $attachedfiles[
'paths'];
502 $filename = $attachedfiles[
'names'];
503 $mimetype = $attachedfiles[
'mimes'];
506 if ($oneemailperrecipient) {
507 $trackid =
'thi'.$thirdparty->id;
508 if ($objecttmp->element ==
'expensereport') {
509 $trackid =
'use'.$thirdparty->id;
511 if ($objecttmp->element ==
'holiday') {
512 $trackid =
'use'.$thirdparty->id;
515 $trackid = strtolower(get_class($objecttmp));
516 if (get_class($objecttmp) ==
'Contrat') {
519 if (get_class($objecttmp) ==
'Propal') {
522 if (get_class($objecttmp) ==
'Commande') {
525 if (get_class($objecttmp) ==
'Facture') {
528 if (get_class($objecttmp) ==
'Supplier_Proposal') {
531 if (get_class($objecttmp) ==
'CommandeFournisseur') {
534 if (get_class($objecttmp) ==
'FactureFournisseur') {
538 $trackid .= $objecttmp->id;
544 if (empty($sendcontext)) {
545 $sendcontext =
'standard';
549 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
550 $mailfile =
new CMailFile($subjectreplaced, $sendto, $from, $messagereplaced, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1,
'',
'', $trackid,
'', $sendcontext);
551 if ($mailfile->error) {
552 $resaction .=
'<div class="error">'.$mailfile->error.
'</div>';
554 $result = $mailfile->sendfile();
556 $resaction .= $langs->trans(
'MailSuccessfulySent', $mailfile->getValidAddress($mailfile->addr_from, 2), $mailfile->getValidAddress($mailfile->addr_to, 2)).
'<br>';
561 foreach ($listofqualifiedobj as $objid2 => $objectobj2) {
562 if ((!$oneemailperrecipient) && $objid2 != $objectid) {
566 dol_syslog(
"Try to insert email event into agenda for objid=".$objid2.
" => objectobj=".get_class($objectobj2));
575 $actionmsg = $langs->transnoentities(
'MailSentBy').
' '.$from.
' '.$langs->transnoentities(
'To').
' '.$sendto;
578 $actionmsg =
dol_concatdesc($actionmsg, $langs->transnoentities(
'Bcc').
": ".$sendtocc);
580 $actionmsg =
dol_concatdesc($actionmsg, $langs->transnoentities(
'MailTopic').
": ".$subjectreplaced);
581 $actionmsg =
dol_concatdesc($actionmsg, $langs->transnoentities(
'TextUsedInTheMessageBody').
":");
587 $objectobj2->sendtoid = (empty($contactidtosend) ? 0 : $contactidtosend);
588 $objectobj2->actionmsg = $actionmsg;
589 $objectobj2->actionmsg2 = $actionmsg2;
590 $objectobj2->fk_element = $objid2;
591 $objectobj2->elementtype = $objectobj2->element;
593 $triggername = strtoupper(get_class($objectobj2)).
'_SENTBYMAIL';
594 if ($triggername ==
'SOCIETE_SENTBYMAIL') {
595 $triggername =
'COMPANY_SENTBYMAIL';
597 if ($triggername ==
'CONTRAT_SENTBYMAIL') {
598 $triggername =
'CONTRACT_SENTBYMAIL';
600 if ($triggername ==
'COMMANDE_SENTBYMAIL') {
601 $triggername =
'ORDER_SENTBYMAIL';
603 if ($triggername ==
'FACTURE_SENTBYMAIL') {
604 $triggername =
'BILL_SENTBYMAIL';
606 if ($triggername ==
'EXPEDITION_SENTBYMAIL') {
607 $triggername =
'SHIPPING_SENTBYMAIL';
609 if ($triggername ==
'COMMANDEFOURNISSEUR_SENTBYMAIL') {
610 $triggername =
'ORDER_SUPPLIER_SENTBYMAIL';
612 if ($triggername ==
'FACTUREFOURNISSEUR_SENTBYMAIL') {
613 $triggername =
'BILL_SUPPLIER_SENTBYMAIL';
615 if ($triggername ==
'SUPPLIERPROPOSAL_SENTBYMAIL') {
616 $triggername =
'PROPOSAL_SUPPLIER_SENTBYMAIL';
619 if (!empty($triggername)) {
621 $result = $objectobj2->call_trigger($triggername, $user);
629 dol_syslog(
"Error in trigger ".$triggername.
' '.$db->lasterror(), LOG_ERR);
636 $langs->load(
"other");
637 if ($mailfile->error) {
638 $resaction .= $langs->trans(
'ErrorFailedToSendMail', $from, $sendto);
639 $resaction .=
'<br><div class="error">'.$mailfile->error.
'</div>';
640 } elseif (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
641 $resaction .=
'<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
643 $resaction .= $langs->trans(
'ErrorFailedToSendMail', $from, $sendto) .
'<br><div class="error">(unhandled error)</div>';
651 $resaction .= ($resaction ?
'<br>' : $resaction);
652 $resaction .=
'<strong>'.$langs->trans(
"ResultOfMailSending").
':</strong><br>'.
"\n";
653 $resaction .= $langs->trans(
"NbSelected").
': '.count($toselect).
"\n<br>";
654 $resaction .= $langs->trans(
"NbIgnored").
': '.($nbignored ? $nbignored : 0).
"\n<br>";
655 $resaction .= $langs->trans(
"NbSent").
': '.($nbsent ? $nbsent : 0).
"\n<br>";
660 setEventMessages($langs->trans(
"EMailSentForNElements", $nbsent.
'/'.count($toselect)),
null,
'mesgs');
673 if (!$error && $massaction ==
'cancelorders') {
679 $orders =
GETPOST(
'toselect',
'array');
680 foreach ($orders as $id_order) {
682 if ($cmd->fetch($id_order) <= 0) {
687 $langs->load(
'errors');
688 setEventMessages($langs->trans(
"ErrorObjectMustHaveStatusValidToBeCanceled", $cmd->ref),
null,
'errors');
693 $result = $cmd->cancel();
717 if (!$error && $massaction ==
"builddoc" && $permissiontoread && !
GETPOST(
'button_search')) {
718 if (empty($diroutputmassaction)) {
719 dol_print_error(
null,
'include of actions_massactions.inc.php is done but var $diroutputmassaction was not defined');
723 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
724 require_once DOL_DOCUMENT_ROOT.
'/core/lib/pdf.lib.php';
725 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
727 $objecttmp =
new $objectclass($db);
728 $listofobjectid = array();
729 $listofobjectthirdparties = array();
730 $listofobjectref = array();
731 foreach ($toselect as $toselectid) {
732 $objecttmp =
new $objectclass($db);
733 $result = $objecttmp->fetch($toselectid);
735 $listofobjectid[$toselectid] = $toselectid;
736 $thirdpartyid = $objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid;
737 $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
738 $listofobjectref[$toselectid] = $objecttmp->ref;
742 $arrayofinclusion = array();
743 foreach ($listofobjectref as $tmppdf) {
746 foreach ($listofobjectref as $tmppdf) {
747 $arrayofinclusion[] =
'^'.preg_quote(
dol_sanitizeFileName($tmppdf),
'/').
'_[a-zA-Z0-9\-\_\'\&\.]+\.pdf$';
749 $listoffiles =
dol_dir_list($uploaddir,
'all', 1, implode(
'|', $arrayofinclusion),
'\.meta$|\.png',
'date', SORT_DESC, 0,
true);
753 foreach ($listofobjectref as $basename) {
755 foreach ($listoffiles as $filefound) {
756 if (strstr($filefound[
"name"], $basename)) {
757 $files[] = $uploaddir.
'/'.$basename.
'/'.$filefound[
"name"];
764 $outputlangs = $langs;
766 if ($conf->global->MAIN_MULTILANGS && empty($newlang) &&
GETPOST(
'lang_id',
'aZ09')) {
767 $newlang =
GETPOST(
'lang_id',
'aZ09');
772 if (!empty($newlang)) {
774 $outputlangs->setDefaultLang($newlang);
777 if (!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT)) {
783 $filename = preg_replace(
'/\s/',
'_', $filename);
786 if (in_array($objecttmp->element, array(
'facture',
'facture_fournisseur')) && $search_status ==
Facture::STATUS_VALIDATED) {
787 if ($option ==
'late') {
794 $filename .=
'_'.$year;
797 $filename .=
'_'.$month;
800 if (count($files) > 0) {
802 $file = $diroutputmassaction.
'/'.$filename.
'_'.
dol_print_date($now,
'dayhourlog').
'.pdf';
805 foreach ($files as $f) {
806 $input_files .=
' '.escapeshellarg($f);
809 $cmd =
'pdftk '.$input_files.
' cat output '.escapeshellarg($file);
813 if (file_exists($file)) {
814 if (!empty($conf->global->MAIN_UMASK)) {
815 @chmod($file, octdec($conf->global->MAIN_UMASK));
818 $langs->load(
"exports");
821 setEventMessages($langs->trans(
'ErrorPDFTkOutputFileNotFound'),
null,
'errors');
824 setEventMessages($langs->trans(
'NoPDFAvailableForDocGenAmongChecked'),
null,
'errors');
829 $page_largeur = $formatarray[
'width'];
830 $page_hauteur = $formatarray[
'height'];
831 $format = array($page_largeur, $page_hauteur);
835 if (class_exists(
'TCPDF')) {
836 $pdf->setPrintHeader(
false);
837 $pdf->setPrintFooter(
false);
841 if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) {
842 $pdf->SetCompression(
false);
846 foreach ($files as $file) {
848 $pagecount = $pdf->setSourceFile($file);
849 for ($i = 1; $i <= $pagecount; $i++) {
850 $tplidx = $pdf->importPage($i);
851 $s = $pdf->getTemplatesize($tplidx);
852 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
853 $pdf->useTemplate($tplidx);
862 $filename = preg_replace(
'/\s/',
'_', $filename);
865 if (in_array($objecttmp->element, array(
'facture',
'facture_fournisseur')) && $search_status ==
Facture::STATUS_VALIDATED) {
866 if ($option ==
'late') {
873 $filename .=
'_'.$year;
876 $filename .=
'_'.$month;
880 $file = $diroutputmassaction.
'/'.$filename.
'_'.
dol_print_date($now,
'dayhourlog').
'.pdf';
881 $pdf->Output($file,
'F');
882 if (!empty($conf->global->MAIN_UMASK)) {
883 @chmod($file, octdec($conf->global->MAIN_UMASK));
886 $langs->load(
"exports");
889 setEventMessages($langs->trans(
'NoPDFAvailableForDocGenAmongChecked'),
null,
'errors');
895 if ($action ==
'remove_file') {
896 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
898 $langs->load(
"other");
899 $upload_dir = $diroutputmassaction;
900 $file = $upload_dir.
'/'.
GETPOST(
'file');
912 if (!$error && $massaction ==
'validate' && $permissiontoadd) {
913 $objecttmp =
new $objectclass($db);
915 if (($objecttmp->element ==
'facture' || $objecttmp->element ==
'invoice') && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
916 $langs->load(
"errors");
917 setEventMessages($langs->trans(
'ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'),
null,
'errors');
920 if ($objecttmp->element ==
'invoice_supplier' && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
921 $langs->load(
"errors");
922 setEventMessages($langs->trans(
'ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'),
null,
'errors');
925 if ($objecttmp->element ==
'facture') {
926 if (!empty($toselect) && !empty($conf->global->INVOICE_CHECK_POSTERIOR_DATE)) {
928 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"facture";
929 $sql .=
" WHERE rowid IN (".$db->sanitize(implode(
",", $toselect)).
")";
930 $sql .=
" ORDER BY datef";
932 $resql = $db->query($sql);
935 while ( !empty($arr = $db->fetch_row(
$resql))) {
936 $toselectnew[] = $arr[0];
938 $toselect = (empty($toselectnew)) ? $toselect : $toselectnew;
949 foreach ($toselect as $toselectid) {
950 $result = $objecttmp->fetch($toselectid);
952 $result = $objecttmp->validate($user);
954 $langs->load(
"errors");
955 setEventMessages($langs->trans(
"ErrorObjectMustHaveStatusDraftToBeValidated", $objecttmp->ref),
null,
'errors');
958 } elseif ($result < 0) {
965 if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
966 $outputlangs = $langs;
968 if ($conf->global->MAIN_MULTILANGS && empty($newlang) &&
GETPOST(
'lang_id',
'aZ09')) {
969 $newlang =
GETPOST(
'lang_id',
'aZ09');
971 if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
972 $newlang = $objecttmp->thirdparty->default_lang;
974 if (!empty($newlang)) {
976 $outputlangs->setDefaultLang($newlang);
977 $outputlangs->load(
'products');
979 $model = $objecttmp->model_pdf;
980 $ret = $objecttmp->fetch($objecttmp->id);
982 $hidedetails = !empty($hidedetails) ? $hidedetails : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
983 $hidedesc = !empty($hidedesc) ? $hidedesc : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
984 $hideref = !empty($hideref) ? $hideref : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
985 $moreparams = !empty($moreparams) ? $moreparams :
null;
987 $result = $objecttmp->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
1005 setEventMessages($langs->trans(
"RecordModifiedSuccessfully"),
null,
'mesgs');
1018 if (!$error && ($massaction ==
'delete' || ($action ==
'delete' && $confirm ==
'yes')) && $permissiontodelete) {
1021 $objecttmp =
new $objectclass($db);
1026 $unique_arr = array_unique($toselect);
1027 foreach ($unique_arr as $toselectid) {
1028 $result = $objecttmp->fetch($toselectid);
1031 if ($objectclass ==
'Facture' && empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $objecttmp->status !=
Facture::STATUS_DRAFT) {
1032 $langs->load(
"errors");
1034 $TMsg[] =
'<div class="error">'.$langs->trans(
'ErrorOnlyDraftStatusCanBeDeletedInMassAction', $objecttmp->ref).
'</div><br>';
1038 if (method_exists($objecttmp,
'is_erasable') && $objecttmp->is_erasable() <= 0) {
1039 $langs->load(
"errors");
1041 $TMsg[] =
'<div class="error">'.$langs->trans(
'ErrorRecordHasChildren').
' '.$objecttmp->ref.
'</div><br>';
1046 $langs->load(
"errors");
1048 $TMsg[] =
'<div class="error">'.$langs->trans(
'ErrorLeaveRequestMustBeDraftCanceledOrRefusedToBeDeleted', $objecttmp->ref).
'</div><br>';
1052 if ($objectclass ==
"Task" && $objecttmp->hasChildren() > 0) {
1053 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"projet_task SET fk_task_parent = 0 WHERE fk_task_parent = ".((int) $objecttmp->id);
1054 $res = $db->query($sql);
1062 if (in_array($objecttmp->element, array(
'societe',
'member'))) {
1063 $result = $objecttmp->delete($objecttmp->id, $user, 1);
1064 } elseif (in_array($objecttmp->element, array(
'action'))) {
1065 $result = $objecttmp->delete();
1067 $result = $objecttmp->delete($user);
1070 if (empty($result)) {
1071 $TMsg = array_merge($objecttmp->errors, $TMsg);
1072 } elseif ($result < 0) {
1086 if (empty($error)) {
1090 } elseif ($nbok > 0) {
1097 if (!empty($TMsg)) {
1112 if (!$error && $massaction ==
'generate_doc' && $permissiontoread) {
1114 $objecttmp =
new $objectclass($db);
1116 foreach ($toselect as $toselectid) {
1117 $result = $objecttmp->fetch($toselectid);
1119 $outputlangs = $langs;
1121 if ($conf->global->MAIN_MULTILANGS && empty($newlang) &&
GETPOST(
'lang_id',
'aZ09')) {
1122 $newlang =
GETPOST(
'lang_id',
'aZ09');
1124 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($objecttmp->thirdparty->default_lang)) {
1125 $newlang = $objecttmp->thirdparty->default_lang;
1127 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($objecttmp->default_lang)) {
1128 $newlang = $objecttmp->default_lang;
1130 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && empty($objecttmp->thirdparty)) {
1131 $objecttmp->fetch_thirdparty();
1132 $newlang = $objecttmp->thirdparty->default_lang;
1134 if (!empty($newlang)) {
1135 $outputlangs =
new Translate(
"", $conf);
1136 $outputlangs->setDefaultLang($newlang);
1140 if (empty($hidedetails)) {
1141 $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
1143 if (empty($hidedesc)) {
1144 $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
1146 if (empty($hideref)) {
1147 $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
1149 if (empty($moreparams)) {
1153 $result = $objecttmp->generateDocument($objecttmp->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1181 if (!$error && ($action ==
'affecttag' && $confirm ==
'yes') && $permissiontoadd) {
1184 $affecttag_type=
GETPOST(
'affecttag_type',
'alpha');
1185 if (!empty($affecttag_type)) {
1186 $affecttag_type_array=explode(
',', $affecttag_type);
1190 if (!empty($affecttag_type_array)) {
1192 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
1194 $to_affecttag_type_array=array();
1195 $categ_type_array=$categ->getMapList();
1196 foreach ($categ_type_array as $categdef) {
1197 if (in_array($categdef[
'code'], $affecttag_type_array)) {
1198 $to_affecttag_type_array[] = $categdef[
'code'];
1204 if (!empty($to_affecttag_type_array)) {
1205 foreach ($to_affecttag_type_array as $categ_type) {
1206 $contcats =
GETPOST(
'contcats_' . $categ_type,
'array');
1208 foreach ($toselect as $toselectid) {
1209 $result = $object->fetch($toselectid);
1212 $result = $object->setCategoriesCommon($contcats, $categ_type,
false);
1241 if (!$error && ($massaction ==
'enable' || ($action ==
'enable' && $confirm ==
'yes')) && $permissiontoadd) {
1244 $objecttmp =
new $objectclass($db);
1246 foreach ($toselect as $toselectid) {
1247 $result = $objecttmp->fetch($toselectid);
1249 if (in_array($objecttmp->element, array(
'societe'))) {
1250 $result =$objecttmp->setStatut(1);
1278 if (!$error && ($massaction ==
'disable' || ($action ==
'disable' && $confirm ==
'yes')) && $permissiontoadd) {
1281 $objecttmp =
new $objectclass($db);
1283 foreach ($toselect as $toselectid) {
1284 $result = $objecttmp->fetch($toselectid);
1286 if (in_array($objecttmp->element, array(
'societe'))) {
1287 $result =$objecttmp->setStatut(0);
1315 if (!$error && $action ==
'confirm_edit_value_extrafields' && $confirm ==
'yes' && $permissiontoadd) {
1318 $objecttmp =
new $objectclass($db);
1320 $e->fetch_name_optionals_label($objecttmp->table_element);
1323 $extrafieldKeyToUpdate =
GETPOST(
'extrafield-key-to-update');
1326 foreach ($toselect as $toselectid) {
1328 $objecttmp =
new $objectclass($db);
1329 $result = $objecttmp->fetch($toselectid);
1332 $ret = $e->setOptionalsFromPost(
null, $objecttmp, $extrafieldKeyToUpdate);
1334 $objecttmp->insertExtraFields();
1358 if (!$error && ($massaction ==
'affectcommercial' || ($action ==
'affectcommercial' && $confirm ==
'yes')) && $permissiontoadd) {
1361 $objecttmp =
new $objectclass($db);
1364 foreach ($toselect as $toselectid) {
1365 $result = $objecttmp->fetch($toselectid);
1367 if (in_array($objecttmp->element, array(
'societe'))) {
1368 $result = $objecttmp->setSalesRep(
GETPOST(
"commercial",
"alpha"));
1386 setEventMessages($langs->trans(
"CommercialsAffected", $nbok),
null,
'mesgs');
1397 if (!$error && ($massaction ==
'approveleave' || ($action ==
'approveleave' && $confirm ==
'yes')) && $permissiontoapprove) {
1400 $objecttmp =
new $objectclass($db);
1402 foreach ($toselect as $toselectid) {
1403 $result = $objecttmp->fetch($toselectid);
1406 setEventMessages($langs->trans(
'StatusOfRefMustBe', $objecttmp->ref, $langs->transnoentitiesnoconv(
'Validated')),
null,
'warnings');
1409 if ($user->id == $objecttmp->fk_validator) {
1410 $objecttmp->oldcopy =
dol_clone($objecttmp);
1412 $objecttmp->date_valid =
dol_now();
1413 $objecttmp->fk_user_valid = $user->id;
1416 $verif = $objecttmp->approve($user);
1426 $nbopenedday =
num_open_day($objecttmp->date_debut_gmt, $objecttmp->date_fin_gmt, 0, 1, $objecttmp->halfday);
1427 $soldeActuel = $objecttmp->getCpforUser($objecttmp->fk_user, $objecttmp->fk_type);
1428 $newSolde = ($soldeActuel - $nbopenedday);
1431 $result = $objecttmp->addLogCP($user->id, $objecttmp->fk_user, $langs->transnoentitiesnoconv(
"Holidays"), $newSolde, $objecttmp->fk_type);
1438 $result = $objecttmp->updateSoldeCP($objecttmp->fk_user, $newSolde, $objecttmp->fk_type);
1447 $destinataire =
new User($db);
1448 $destinataire->fetch($objecttmp->fk_user);
1449 $emailTo = $destinataire->email;
1452 dol_syslog(
"User that request leave has no email, so we redirect directly to finished page without sending email");
1455 $expediteur =
new User($db);
1456 $expediteur->fetch($objecttmp->fk_validator);
1458 $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM;
1461 $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
1462 if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
1463 $societeName = $conf->global->MAIN_APPLICATION_TITLE;
1466 $subject = $societeName.
" - ".$langs->transnoentitiesnoconv(
"HolidaysValidated");
1469 $message = $langs->transnoentitiesnoconv(
"Hello").
" ".$destinataire->firstname.
",\n";
1472 $message .= $langs->transnoentities(
"HolidaysValidatedBody",
dol_print_date($objecttmp->date_debut,
'day'),
dol_print_date($objecttmp->date_fin,
'day')).
"\n";
1474 $message .=
"- ".$langs->transnoentitiesnoconv(
"ValidatedBy").
" : ".
dolGetFirstLastname($expediteur->firstname, $expediteur->lastname).
"\n";
1476 $message .=
"- ".$langs->transnoentitiesnoconv(
"Link").
" : ".$dolibarr_main_url_root.
"/holiday/card.php?id=".$objecttmp->id.
"\n\n";
1479 $trackid =
'leav'.$objecttmp->id;
1481 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1482 $mail =
new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(),
'',
'', 0, 0,
'',
'', $trackid);
1485 $result = $mail->sendfile();
1494 $langs->load(
"errors");
1495 setEventMessages($langs->trans(
'ErrorNotApproverForHoliday', $objecttmp->ref),
null,
'errors');
1507 } elseif ($nbok == 1) {
1516 $parameters[
'toselect'] = $toselect;
1517 $parameters[
'uploaddir'] = $uploaddir;
1518 $parameters[
'massaction'] = $massaction;
1519 $parameters[
'diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction :
null;
1521 $reshook = $hookmanager->executeHooks(
'doMassActions', $parameters, $object, $action);