card_presend.tpl.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /* Copyright (C) 2017-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /*
  20. * Code to ouput content when action is presend
  21. *
  22. * $trackid must be defined
  23. * $modelmail
  24. * $defaulttopic
  25. * $diroutput
  26. * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
  27. */
  28. // Protection to avoid direct call of template
  29. if (empty($conf) || !is_object($conf)) {
  30. print "Error, template page can't be called as URL";
  31. exit;
  32. }
  33. if ($action == 'presend') {
  34. $langs->load("mails");
  35. $titreform = 'SendMail';
  36. $object->fetch_projet();
  37. if (!isset($file)) $file = null;
  38. $ref = dol_sanitizeFileName($object->ref);
  39. if (!in_array($object->element, array('user', 'member'))) {
  40. //$fileparams['fullname'] can be filled from the card
  41. //Get also the main_lastdoc field of $object. If not found, try to guess with following code
  42. if (!empty($object->last_main_doc) && is_readable(DOL_DATA_ROOT.'/'.$object->last_main_doc) && is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) {
  43. $fileparams['fullname'] = DOL_DATA_ROOT.'/'.$object->last_main_doc;
  44. } else {
  45. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  46. // Special case
  47. if ($object->element == 'invoice_supplier') {
  48. $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
  49. } else {
  50. $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
  51. }
  52. }
  53. $file = isset($fileparams['fullname'])?$fileparams['fullname']:null;
  54. }
  55. // Define output language
  56. $outputlangs = $langs;
  57. $newlang = '';
  58. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  59. $newlang = $object->thirdparty->default_lang;
  60. if (GETPOST('lang_id', 'aZ09')) {
  61. $newlang = GETPOST('lang_id', 'aZ09');
  62. }
  63. }
  64. if (!empty($newlang)) {
  65. $outputlangs = new Translate('', $conf);
  66. $outputlangs->setDefaultLang($newlang);
  67. // Load traductions files required by page
  68. $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
  69. }
  70. $topicmail = '';
  71. if (empty($object->ref_client)) {
  72. $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
  73. } elseif (!empty($object->ref_client)) {
  74. $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
  75. }
  76. // Build document if it not exists
  77. $forcebuilddoc = true;
  78. if (in_array($object->element, array('user', 'member'))) {
  79. $forcebuilddoc = false;
  80. }
  81. if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
  82. $forcebuilddoc = false;
  83. }
  84. if ($object->element == 'societe' && empty($conf->global->COMPANY_ADDON_PDF)) {
  85. $forcebuilddoc = false;
  86. }
  87. if ($forcebuilddoc) { // If there is no default value for supplier invoice, we do not generate file, even if modelpdf was set by a manual generation
  88. if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
  89. $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  90. if ($result < 0) {
  91. dol_print_error($db, $object->error, $object->errors);
  92. exit();
  93. }
  94. if ($object->element == 'invoice_supplier') {
  95. $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
  96. } else {
  97. $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
  98. }
  99. $file = $fileparams['fullname'];
  100. }
  101. }
  102. print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
  103. print '<div class="clearboth"></div>';
  104. print '<br>';
  105. print load_fiche_titre($langs->trans($titreform));
  106. print dol_get_fiche_head('');
  107. // Create form for email
  108. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  109. $formmail = new FormMail($db);
  110. $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
  111. $formmail->fromtype = (GETPOST('fromtype') ?GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
  112. if ($formmail->fromtype === 'user') {
  113. $formmail->fromid = $user->id;
  114. }
  115. if ($object->element === 'facture' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
  116. $formmail->frommail = $conf->global->INVOICE_EMAIL_SENDER;
  117. $formmail->fromname = (!empty($conf->global->INVOICE_EMAIL_SENDER_NAME) ? $conf->global->INVOICE_EMAIL_SENDER_NAME : '');
  118. $formmail->fromtype = 'special';
  119. }
  120. if ($object->element === 'shipping' && !empty($conf->global->SHIPPING_EMAIL_SENDER)) {
  121. $formmail->frommail = $conf->global->SHIPPING_EMAIL_SENDER;
  122. $formmail->fromname = (!empty($conf->global->SHIPPING_EMAIL_SENDER_NAME) ? $conf->global->SHIPPING_EMAIL_SENDER_NAME : '');
  123. $formmail->fromtype = 'special';
  124. }
  125. if ($object->element === 'commande' && !empty($conf->global->COMMANDE_EMAIL_SENDER)) {
  126. $formmail->frommail = $conf->global->COMMANDE_EMAIL_SENDER;
  127. $formmail->fromname = (!empty($conf->global->COMMANDE_EMAIL_SENDER_NAME) ? $conf->global->COMMANDE_EMAIL_SENDER_NAME : '');
  128. $formmail->fromtype = 'special';
  129. }
  130. if ($object->element === 'order_supplier' && !empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER)) {
  131. $formmail->frommail = $conf->global->ORDER_SUPPLIER_EMAIL_SENDER;
  132. $formmail->fromname = (!empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME) ? $conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME : '');
  133. $formmail->fromtype = 'special';
  134. }
  135. if ($object->element === 'recruitmentcandidature' ) {
  136. $formmail->frommail = (!empty($conf->global->RECRUITMENT_EMAIL_SENDER) ? $conf->global->RECRUITMENT_EMAIL_SENDER : $recruitermail);
  137. $formmail->fromname = (!empty($conf->global->RECRUITMENT_EMAIL_SENDER_NAME) ? $conf->global->RECRUITMENT_EMAIL_SENDER_NAME : (!empty($recruitername) ? $recruitername : ''));
  138. $formmail->fromtype = 'special';
  139. }
  140. // Set the default "From"
  141. $defaultfrom = '';
  142. if (GETPOSTISSET('fromtype')) {
  143. $defaultfrom = GETPOST('fromtype');
  144. } else {
  145. $parameters = array();
  146. $reshook = $hookmanager->executeHooks('getDefaultFromEmail', $parameters, $formmail);
  147. if (empty($reshook)) {
  148. $defaultfrom = $formmail->fromtype;
  149. }
  150. if (!empty($hookmanager->resArray['defaultfrom'])) {
  151. $defaultfrom = $hookmanager->resArray['defaultfrom'];
  152. }
  153. }
  154. $formmail->fromtype = $defaultfrom;
  155. $formmail->trackid = empty($trackid) ? '' : $trackid;
  156. $formmail->inreplyto = empty($inreplyto) ? '' : $inreplyto;
  157. $formmail->withfrom = 1;
  158. // Define $liste, a list of recipients with email inside <>.
  159. $liste = array();
  160. if ($object->element == 'expensereport') {
  161. $fuser = new User($db);
  162. $fuser->fetch($object->fk_user_author);
  163. $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
  164. } elseif ($object->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
  165. $fadherent = new Adherent($db);
  166. $fadherent->fetch($object->fk_member);
  167. $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
  168. } elseif ($object->element == 'societe') {
  169. foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
  170. $liste[$key] = $value;
  171. }
  172. } elseif ($object->element == 'contact') {
  173. $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
  174. } elseif ($object->element == 'user' || $object->element == 'member') {
  175. $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
  176. } else {
  177. if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
  178. $object->fetch_thirdparty();
  179. }
  180. if (is_object($object->thirdparty)) {
  181. foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
  182. $liste[$key] = $value;
  183. }
  184. }
  185. }
  186. if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
  187. $listeuser = array();
  188. $fuserdest = new User($db);
  189. $result = $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, array('customsql'=>"t.statut=1 AND t.employee=1 AND t.email IS NOT NULL AND t.email <> ''"), 'AND', true);
  190. if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
  191. foreach ($fuserdest->users as $uuserdest) {
  192. $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
  193. }
  194. } elseif ($result < 0) {
  195. setEventMessages(null, $fuserdest->errors, 'errors');
  196. }
  197. if (count($listeuser) > 0) {
  198. $formmail->withtouser = $listeuser;
  199. $formmail->withtoccuser = $listeuser;
  200. }
  201. }
  202. //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
  203. if (!isset($arrayoffamiliestoexclude)) {
  204. $arrayoffamiliestoexclude = null;
  205. }
  206. // Make substitution in email content
  207. if (!empty($object)) {
  208. // First we set ->substit (useless, it will be erased later) and ->substit_lines
  209. $formmail->setSubstitFromObject($object, $langs);
  210. }
  211. $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
  212. // Overwrite __SENDEREMAIL_SIGNATURE__ with value select into form
  213. if ($formmail->fromtype) {
  214. $reg = array();
  215. if (preg_match('/user/', $formmail->fromtype, $reg)) {
  216. $emailsendersignature = $user->signature;
  217. } elseif (preg_match('/company/', $formmail->fromtype, $reg)) {
  218. $emailsendersignature = '';
  219. } elseif (preg_match('/senderprofile_(\d+)/', $formmail->fromtype, $reg)) {
  220. $sql = "SELECT rowid, label, email, signature FROM ".$db->prefix()."c_email_senderprofile";
  221. $sql .= " WHERE rowid = ".((int) $reg[1]);
  222. $resql = $db->query($sql);
  223. if ($resql) {
  224. $obj = $db->fetch_object($resql);
  225. if ($obj) {
  226. $emailsendersignature = $obj->signature;
  227. }
  228. }
  229. }
  230. }
  231. $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = $emailsendersignature;
  232. $substitutionarray['__CHECK_READ__'] = "";
  233. if (is_object($object) && is_object($object->thirdparty)) {
  234. $checkRead= '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php';
  235. $checkRead.='?tag='.(!empty($object->thirdparty->tag)?urlencode($object->thirdparty->tag):"");
  236. $checkRead.='&securitykey='.(!empty($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY)?urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY):"");
  237. $checkRead.='" width="1" height="1" style="width:1px;height:1px" border="0"/>';
  238. $substitutionarray['__CHECK_READ__'] = $checkRead;
  239. }
  240. $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
  241. $substitutionarray['__CONTACTCIVNAME__'] = '';
  242. $parameters = array(
  243. 'mode' => 'formemail'
  244. );
  245. complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
  246. // Find all external contact addresses
  247. $tmpobject = $object;
  248. if (($object->element == 'shipping' || $object->element == 'reception')) {
  249. $origin = $object->origin;
  250. $origin_id = $object->origin_id;
  251. if (!empty($origin) && !empty($origin_id)) {
  252. $element = $subelement = $origin;
  253. $regs = array();
  254. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  255. $element = $regs[1];
  256. $subelement = $regs[2];
  257. }
  258. // For compatibility
  259. if ($element == 'order') {
  260. $element = $subelement = 'commande';
  261. }
  262. if ($element == 'propal') {
  263. $element = 'comm/propal';
  264. $subelement = 'propal';
  265. }
  266. if ($element == 'contract') {
  267. $element = $subelement = 'contrat';
  268. }
  269. if ($element == 'inter') {
  270. $element = $subelement = 'ficheinter';
  271. }
  272. if ($element == 'shipping') {
  273. $element = $subelement = 'expedition';
  274. }
  275. if ($element == 'order_supplier') {
  276. $element = 'fourn';
  277. $subelement = 'fournisseur.commande';
  278. }
  279. if ($element == 'project') {
  280. $element = 'projet';
  281. }
  282. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  283. $classname = ucfirst($origin);
  284. $objectsrc = new $classname($db);
  285. $objectsrc->fetch($origin_id);
  286. $tmpobject = $objectsrc;
  287. }
  288. }
  289. $contactarr = array();
  290. $contactarr = $tmpobject->liste_contact(-1, 'external');
  291. if (is_array($contactarr) && count($contactarr) > 0) {
  292. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  293. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  294. $contactstatic = new Contact($db);
  295. $tmpcompany = new Societe($db);
  296. foreach ($contactarr as $contact) {
  297. $contactstatic->fetch($contact['id']);
  298. // Complete substitution array
  299. $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
  300. $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
  301. $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
  302. $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
  303. // Complete $liste with the $contact
  304. if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
  305. $contacttoshow = '';
  306. if (isset($object->thirdparty) && is_object($object->thirdparty)) {
  307. if ($contactstatic->fk_soc != $object->thirdparty->id) {
  308. $tmpcompany->fetch($contactstatic->fk_soc);
  309. if ($tmpcompany->id > 0) {
  310. $contacttoshow .= $tmpcompany->name.': ';
  311. }
  312. }
  313. }
  314. $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
  315. $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
  316. $liste[$contact['id']] = $contacttoshow;
  317. }
  318. }
  319. }
  320. $formmail->withto = $liste;
  321. $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
  322. $formmail->withtocc = $liste;
  323. $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
  324. $formmail->withtopic = $topicmail;
  325. $formmail->withfile = 2;
  326. $formmail->withbody = 1;
  327. $formmail->withdeliveryreceipt = 1;
  328. $formmail->withcancel = 1;
  329. // Array of substitutions
  330. $formmail->substit = $substitutionarray;
  331. // Array of other parameters
  332. $formmail->param['action'] = 'send';
  333. $formmail->param['models'] = $modelmail;
  334. $formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
  335. $formmail->param['id'] = $object->id;
  336. $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
  337. $formmail->param['fileinit'] = array($file);
  338. // Show form
  339. print $formmail->get_form();
  340. print dol_get_fiche_end();
  341. }