1<?php
2/* Copyright (C)    2013      Cédric Salvador     <csalvador@gpcsolutions.fr>
3 * Copyright (C)    2013-2014 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C)	2015	  Marcos García		  <marcosgdf@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
21// Following var must be set:
22// $arrayofselected = array of id selected
23// $object
24// $objecttmp=new Propal($db);
25// $topicmail="SendSupplierProposalRef";
26// $modelmail="supplier_proposal_send";
27// $trackid='ord'.$object->id;
28
29
30if ($massaction == 'predeletedraft')
31{
32	print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDraftDeletion"), $langs->trans("ConfirmMassDeletionQuestion", count($toselect)), "delete", null, '', 0, 200, 500, 1);
33}
34
35if ($massaction == 'predelete')
36{
37	print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDeletion"), $langs->trans("ConfirmMassDeletionQuestion", count($toselect)), "delete", null, '', 0, 200, 500, 1);
38}
39
40if ($massaction == 'presend')
41{
42	$langs->load("mails");
43
44	$listofselectedid = array();
45	$listofselectedthirdparties = array();
46	$listofselectedref = array();
47
48	if (!GETPOST('cancel', 'alpha'))
49	{
50		foreach ($arrayofselected as $toselectid)
51		{
52			$result = $objecttmp->fetch($toselectid);
53			if ($result > 0)
54			{
55				$listofselectedid[$toselectid] = $toselectid;
56				$thirdpartyid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid);
57				if ($objecttmp->element == 'societe')
58					$thirdpartyid = $objecttmp->id;
59				if ($objecttmp->element == 'expensereport')
60					$thirdpartyid = $objecttmp->fk_user_author;
61				$listofselectedthirdparties[$thirdpartyid] = $thirdpartyid;
62				$listofselectedref[$thirdpartyid][$toselectid] = $objecttmp->ref;
63			}
64		}
65	}
66
67	print '<input type="hidden" name="massaction" value="confirm_presend">';
68
69	include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
70	$formmail = new FormMail($db);
71
72	print dol_get_fiche_head(null, '', '');
73
74	// Cree l'objet formulaire mail
75	include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
76	$formmail = new FormMail($db);
77	$formmail->withform = -1;
78	$formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
79
80	if ($formmail->fromtype === 'user')
81	{
82		$formmail->fromid = $user->id;
83	}
84	$formmail->trackid = $trackid;
85	if (!empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
86	{
87		include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
88		$formmail->frommail = dolAddEmailTrackId($formmail->frommail, $trackid);
89	}
90	$formmail->withfrom = 1;
91	$liste = $langs->trans("AllRecipientSelected", count($arrayofselected));
92	if (count($listofselectedthirdparties) == 1) // Only 1 different recipient selected, we can suggest contacts
93	{
94		$liste = array();
95		$thirdpartyid = array_shift($listofselectedthirdparties);
96		if ($objecttmp->element == 'expensereport')
97		{
98			$fuser = new User($db);
99			$fuser->fetch($thirdpartyid);
100			$liste['thirdparty'] = $fuser->getFullName($langs)." &lt;".$fuser->email."&gt;";
101		} else {
102			$soc = new Societe($db);
103			$soc->fetch($thirdpartyid);
104			foreach ($soc->thirdparty_and_contact_email_array(1) as $key => $value) {
105				$liste[$key] = $value;
106			}
107		}
108		$formmail->withtoreadonly = 0;
109	} else {
110		$formmail->withtoreadonly = 1;
111	}
112
113	$formmail->withoptiononeemailperrecipient = ((count($listofselectedref) == 1 && count(reset($listofselectedref)) == 1) || empty($liste)) ? 0 : ((GETPOST('oneemailperrecipient') == 'on') ? 1 : -1);
114
115	$formmail->withto = empty($liste) ? (GETPOST('sendto', 'alpha') ?GETPOST('sendto', 'alpha') : array()) : $liste;
116	$formmail->withtofree = empty($liste) ? 1 : 0;
117	$formmail->withtocc = 1;
118	$formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
119	$formmail->withtopic = $langs->transnoentities($topicmail, '__REF__', '__REFCLIENT__');
120	$formmail->withfile = 1;
121	// $formmail->withfile = 2; Not yet supported in mass action
122	$formmail->withmaindocfile = 1; // Add a checkbox "Attach also main document"
123	if ($objecttmp->element != 'societe') {
124		$formmail->withfile = '<span class="hideonsmartphone">'.$langs->trans("OnlyPDFattachmentSupported").'</span>';
125		$formmail->withmaindocfile = - 1; // Add a checkbox "Attach also main document" but not checked by default
126	}
127	$formmail->withbody = 1;
128	$formmail->withdeliveryreceipt = 1;
129	$formmail->withcancel = 1;
130
131	// Make substitution in email content
132	$substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
133
134	$substitutionarray['__EMAIL__'] = $sendto;
135	$substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ? '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$object->thirdparty->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '';
136	$substitutionarray['__PERSONALIZED__'] = ''; // deprecated
137	$substitutionarray['__CONTACTCIVNAME__'] = '';
138
139	$parameters = array(
140		'mode' => 'formemail'
141	);
142	complete_substitutions_array($substitutionarray, $langs, $object, $parameters);
143
144	// Tableau des substitutions
145	$formmail->substit = $substitutionarray;
146
147	// Tableau des parametres complementaires du post
148	$formmail->param['action'] = $action;
149	$formmail->param['models'] = $modelmail;
150	$formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
151	$formmail->param['id'] = join(',', $arrayofselected);
152	// $formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?id='.$object->id;
153	if (!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && count($listofselectedthirdparties) > $conf->global->MAILING_LIMIT_SENDBYWEB)
154	{
155		$langs->load("errors");
156		print img_warning().' '.$langs->trans('WarningNumberOfRecipientIsRestrictedInMassAction', $conf->global->MAILING_LIMIT_SENDBYWEB);
157		print ' - <a href="javascript: window.history.go(-1)">'.$langs->trans("GoBack").'</a>';
158		$arrayofmassactions = array();
159	} else {
160		print $formmail->get_form();
161	}
162
163	print dol_get_fiche_end();
164}
165// Allow Pre-Mass-Action hook (eg for confirmation dialog)
166$parameters = array(
167	'toselect' => $toselect,
168	'uploaddir' => $uploaddir
169);
170
171$reshook = $hookmanager->executeHooks('doPreMassActions', $parameters, $object, $action);
172if ($reshook < 0) {
173	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
174} else {
175	print $hookmanager->resPrint;
176}
177