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	print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDraftDeletion"), $langs->trans("ConfirmMassDeletionQuestion", count($toselect)), "delete", null, '', 0, 200, 500, 1);
32}
33
34if ($massaction == 'predelete') {
35	print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassDeletion"), $langs->trans("ConfirmMassDeletionQuestion", count($toselect)), "delete", null, '', 0, 200, 500, 1);
36}
37
38if ($massaction == 'preaffecttag') {
39	require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
40	$categ = new Categorie($db);
41	$categ_types=array();
42	$categ_type_array=$categ->getMapList();
43	foreach ($categ_type_array as $categdef) {
44		if (isset($object) && $categdef['obj_table']==$object->table_element) {
45			if (!array_key_exists($categdef['code'], $categ_types)) {
46				$categ_types[$categdef['code']] = array('code'=>$categdef['code'],'label'=>$langs->trans($categdef['obj_class']));
47			}
48		}
49		if (isset($objecttmp) && $categdef['obj_table']==$objecttmp->table_element) {
50			if (!array_key_exists($categdef['code'], $categ_types)) {
51				$categ_types[$categdef['code']] = array('code'=>$categdef['code'],'label'=>$langs->trans($categdef['obj_class']));
52			}
53		}
54	}
55
56	$formquestion = array();
57	if (!empty($categ_types)) {
58		foreach ($categ_types as $categ_type) {
59			$cate_arbo = $form->select_all_categories($categ_type['code'], null, 'parent', null, null, 1);
60			$formquestion[]=array('type' => 'other',
61					'name' => 'affecttag_'.$categ_type['code'],
62					'label' => $langs->trans("Tag").' '.$categ_type['label'],
63					'value' => $form->multiselectarray('contcats_'.$categ_type['code'], $cate_arbo, GETPOST('contcats_'.$categ_type['code'], 'array'), null, null, null, null, '60%'));
64		}
65		$formquestion[]=array('type' => 'other',
66				'name' => 'affecttag_type',
67				'label' => '',
68				'value' => '<input type="hidden" name="affecttag_type"  id="affecttag_type" value="'.implode(",", array_keys($categ_types)).'"/>');
69		print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmAffectTag"), $langs->trans("ConfirmAffectTagQuestion", count($toselect)), "affecttag", $formquestion, 1, 0, 200, 500, 1);
70	} else {
71		setEventMessage('CategTypeNotFound');
72	}
73}
74
75if ($massaction == 'presend') {
76	$langs->load("mails");
77
78	$listofselectedid = array();
79	$listofselectedthirdparties = array();
80	$listofselectedref = array();
81
82	if (!GETPOST('cancel', 'alpha')) {
83		foreach ($arrayofselected as $toselectid) {
84			$result = $objecttmp->fetch($toselectid);
85			if ($result > 0) {
86				$listofselectedid[$toselectid] = $toselectid;
87				$thirdpartyid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid);
88				if ($objecttmp->element == 'societe') {
89					$thirdpartyid = $objecttmp->id;
90				}
91				if ($objecttmp->element == 'expensereport') {
92					$thirdpartyid = $objecttmp->fk_user_author;
93				}
94				$listofselectedthirdparties[$thirdpartyid] = $thirdpartyid;
95				$listofselectedref[$thirdpartyid][$toselectid] = $objecttmp->ref;
96			}
97		}
98	}
99
100	print '<input type="hidden" name="massaction" value="confirm_presend">';
101
102	print dol_get_fiche_head(null, '', '');
103
104	// Create mail form
105	include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
106	$formmail = new FormMail($db);
107	$formmail->withform = -1;
108	$formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
109
110	if ($formmail->fromtype === 'user') {
111		$formmail->fromid = $user->id;
112	}
113	$formmail->trackid = $trackid;
114	if (!empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) { // If bit 2 is set
115		include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
116		$formmail->frommail = dolAddEmailTrackId($formmail->frommail, $trackid);
117	}
118	$formmail->withfrom = 1;
119	$liste = $langs->trans("AllRecipientSelected", count($arrayofselected));
120	if (count($listofselectedthirdparties) == 1) { // Only 1 different recipient selected, we can suggest contacts
121		$liste = array();
122		$thirdpartyid = array_shift($listofselectedthirdparties);
123		if ($objecttmp->element == 'expensereport') {
124			$fuser = new User($db);
125			$fuser->fetch($thirdpartyid);
126			$liste['thirdparty'] = $fuser->getFullName($langs)." &lt;".$fuser->email."&gt;";
127		} elseif ($objecttmp->element == 'partnership' && $conf->global->PARTNERSHIP_IS_MANAGED_FOR == 'member') {
128			$fadherent = new Adherent($db);
129			$fadherent->fetch($objecttmp->fk_member);
130			$liste['member'] = $fadherent->getFullName($langs)." &lt;".$fadherent->email."&gt;";
131		} else {
132			$soc = new Societe($db);
133			$soc->fetch($thirdpartyid);
134			foreach ($soc->thirdparty_and_contact_email_array(1) as $key => $value) {
135				$liste[$key] = $value;
136			}
137		}
138		$formmail->withtoreadonly = 0;
139	} else {
140		$formmail->withtoreadonly = 1;
141	}
142
143	$formmail->withoptiononeemailperrecipient = ((count($listofselectedref) == 1 && count(reset($listofselectedref)) == 1) || empty($liste)) ? 0 : ((GETPOST('oneemailperrecipient') == 'on') ? 1 : -1);
144
145	$formmail->withto = empty($liste) ? (GETPOST('sendto', 'alpha') ?GETPOST('sendto', 'alpha') : array()) : $liste;
146	$formmail->withtofree = empty($liste) ? 1 : 0;
147	$formmail->withtocc = 1;
148	$formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
149	$formmail->withtopic = $langs->transnoentities($topicmail, '__REF__', '__REF_CLIENT__');
150	$formmail->withfile = 1;
151	// $formmail->withfile = 2; Not yet supported in mass action
152	$formmail->withmaindocfile = 1; // Add a checkbox "Attach also main document"
153	if ($objecttmp->element != 'societe') {
154		$formmail->withfile = '<span class="hideonsmartphone">'.$langs->trans("OnlyPDFattachmentSupported").'</span>';
155		$formmail->withmaindocfile = - 1; // Add a checkbox "Attach also main document" but not checked by default
156	}
157	$formmail->withbody = 1;
158	$formmail->withdeliveryreceipt = 1;
159	$formmail->withcancel = 1;
160
161	// Make substitution in email content
162	$substitutionarray = getCommonSubstitutionArray($langs, 0, null, $object);
163
164	$substitutionarray['__EMAIL__'] = $sendto;
165	$substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ? '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.urlencode($object->thirdparty->tag).'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '';
166	$substitutionarray['__PERSONALIZED__'] = ''; // deprecated
167	$substitutionarray['__CONTACTCIVNAME__'] = '';
168
169	$parameters = array(
170		'mode' => 'formemail'
171	);
172	complete_substitutions_array($substitutionarray, $langs, $object, $parameters);
173
174	// Tableau des substitutions
175	$formmail->substit = $substitutionarray;
176
177	// Tableau des parametres complementaires du post
178	$formmail->param['action'] = $action;
179	$formmail->param['models'] = $modelmail;
180	$formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
181	$formmail->param['id'] = join(',', $arrayofselected);
182	// $formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?id='.$object->id;
183	if (!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && count($listofselectedthirdparties) > $conf->global->MAILING_LIMIT_SENDBYWEB) {
184		$langs->load("errors");
185		print img_warning().' '.$langs->trans('WarningNumberOfRecipientIsRestrictedInMassAction', $conf->global->MAILING_LIMIT_SENDBYWEB);
186		print ' - <a href="javascript: window.history.go(-1)">'.$langs->trans("GoBack").'</a>';
187		$arrayofmassactions = array();
188	} else {
189		print $formmail->get_form();
190	}
191
192	print dol_get_fiche_end();
193}
194// Allow Pre-Mass-Action hook (eg for confirmation dialog)
195$parameters = array(
196	'toselect' => $toselect,
197	'uploaddir' => isset($uploaddir) ? $uploaddir : null
198);
199
200$reshook = $hookmanager->executeHooks('doPreMassActions', $parameters, $object, $action);
201if ($reshook < 0) {
202	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
203} else {
204	print $hookmanager->resPrint;
205}
206