PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/tpl/card_presend.tpl.php

http://github.com/Dolibarr/dolibarr
PHP | 303 lines | 275 code | 2 blank | 26 comment | 2 complexity | f2c0adad0060045bb34523bc8b5610c8 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /* Copyright (C) 2017-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /*
  19. * Code to ouput content when action is presend
  20. *
  21. * $trackid must be defined
  22. * $modelmail
  23. * $defaulttopic
  24. * $diroutput
  25. * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
  26. */
  27. // Protection to avoid direct call of template
  28. if (empty($conf) || !is_object($conf)) {
  29. print "Error, template page can't be called as URL";
  30. exit;
  31. }
  32. if ($action == 'presend') {
  33. $langs->load("mails");
  34. $titreform = 'SendMail';
  35. $object->fetch_projet();
  36. if (!in_array($object->element, array('societe', 'user', 'member'))) {
  37. // TODO get also the main_lastdoc field of $object. If not found, try to guess with following code
  38. $ref = dol_sanitizeFileName($object->ref);
  39. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  40. // Special case
  41. if ($object->element == 'invoice_supplier') {
  42. $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
  43. } else {
  44. $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
  45. }
  46. $file = $fileparams['fullname'];
  47. }
  48. // Define output language
  49. $outputlangs = $langs;
  50. $newlang = '';
  51. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  52. $newlang = GETPOST('lang_id', 'aZ09');
  53. }
  54. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
  55. $newlang = $object->thirdparty->default_lang;
  56. }
  57. if (!empty($newlang)) {
  58. $outputlangs = new Translate('', $conf);
  59. $outputlangs->setDefaultLang($newlang);
  60. // Load traductions files required by page
  61. $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions'));
  62. }
  63. $topicmail = '';
  64. if (empty($object->ref_client)) {
  65. $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
  66. } elseif (!empty($object->ref_client)) {
  67. $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
  68. }
  69. // Build document if it not exists
  70. $forcebuilddoc = true;
  71. if (in_array($object->element, array('societe', 'user', 'member'))) {
  72. $forcebuilddoc = false;
  73. }
  74. if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
  75. $forcebuilddoc = false;
  76. }
  77. 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
  78. if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
  79. $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  80. if ($result < 0) {
  81. dol_print_error($db, $object->error, $object->errors);
  82. exit();
  83. }
  84. if ($object->element == 'invoice_supplier') {
  85. $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
  86. } else {
  87. $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
  88. }
  89. $file = $fileparams['fullname'];
  90. }
  91. }
  92. print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
  93. print '<div class="clearboth"></div>';
  94. print '<br>';
  95. print load_fiche_titre($langs->trans($titreform));
  96. print dol_get_fiche_head('');
  97. // Create form for email
  98. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  99. $formmail = new FormMail($db);
  100. $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
  101. $formmail->fromtype = (GETPOST('fromtype') ?GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
  102. if ($formmail->fromtype === 'user') {
  103. $formmail->fromid = $user->id;
  104. }
  105. if ($object->element === 'facture' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
  106. $formmail->frommail = $conf->global->INVOICE_EMAIL_SENDER;
  107. $formmail->fromname = '';
  108. $formmail->fromtype = 'special';
  109. }
  110. if ($object->element === 'shipping' && !empty($conf->global->SHIPPING_EMAIL_SENDER)) {
  111. $formmail->frommail = $conf->global->SHIPPING_EMAIL_SENDER;
  112. $formmail->fromname = '';
  113. $formmail->fromtype = 'special';
  114. }
  115. if ($object->element === 'commande' && !empty($conf->global->COMMANDE_EMAIL_SENDER)) {
  116. $formmail->frommail = $conf->global->COMMANDE_EMAIL_SENDER;
  117. $formmail->fromname = '';
  118. $formmail->fromtype = 'special';
  119. }
  120. if ($object->element === 'order_supplier' && !empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER)) {
  121. $formmail->frommail = $conf->global->ORDER_SUPPLIER_EMAIL_SENDER;
  122. $formmail->fromname = '';
  123. $formmail->fromtype = 'special';
  124. }
  125. $formmail->trackid = $trackid;
  126. $formmail->withfrom = 1;
  127. // Fill list of recipient with email inside <>.
  128. $liste = array();
  129. if ($object->element == 'expensereport') {
  130. $fuser = new User($db);
  131. $fuser->fetch($object->fk_user_author);
  132. $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
  133. } elseif ($object->element == 'partnership' && $conf->global->PARTNERSHIP_IS_MANAGED_FOR == 'member') {
  134. $fadherent = new Adherent($db);
  135. $fadherent->fetch($object->fk_member);
  136. $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
  137. } elseif ($object->element == 'societe') {
  138. foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
  139. $liste[$key] = $value;
  140. }
  141. } elseif ($object->element == 'contact') {
  142. $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
  143. } elseif ($object->element == 'user' || $object->element == 'member') {
  144. $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
  145. } else {
  146. if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
  147. $object->fetch_thirdparty();
  148. }
  149. if (is_object($object->thirdparty)) {
  150. foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
  151. $liste[$key] = $value;
  152. }
  153. }
  154. }
  155. if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
  156. $listeuser = array();
  157. $fuserdest = new User($db);
  158. $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);
  159. if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
  160. foreach ($fuserdest->users as $uuserdest) {
  161. $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
  162. }
  163. } elseif ($result < 0) {
  164. setEventMessages(null, $fuserdest->errors, 'errors');
  165. }
  166. if (count($listeuser) > 0) {
  167. $formmail->withtouser = $listeuser;
  168. $formmail->withtoccuser = $listeuser;
  169. }
  170. }
  171. $formmail->withto = $liste;
  172. $formmail->withtofree = (GETPOSTISSET('sendto') ? (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1') : '1');
  173. $formmail->withtocc = $liste;
  174. $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
  175. $formmail->withtopic = $topicmail;
  176. $formmail->withfile = 2;
  177. $formmail->withbody = 1;
  178. $formmail->withdeliveryreceipt = 1;
  179. $formmail->withcancel = 1;
  180. //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
  181. if (!isset($arrayoffamiliestoexclude)) {
  182. $arrayoffamiliestoexclude = null;
  183. }
  184. // Make substitution in email content
  185. if ($object) {
  186. $formmail->setSubstitFromObject($object, $langs);
  187. }
  188. $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
  189. $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"/>' : '';
  190. $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
  191. $substitutionarray['__CONTACTCIVNAME__'] = '';
  192. $parameters = array(
  193. 'mode' => 'formemail'
  194. );
  195. complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
  196. // Find the good contact address
  197. $tmpobject = $object;
  198. if (($object->element == 'shipping' || $object->element == 'reception')) {
  199. $origin = $object->origin;
  200. $origin_id = $object->origin_id;
  201. if (!empty($origin) && !empty($origin_id)) {
  202. $element = $subelement = $origin;
  203. $regs = array();
  204. if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
  205. $element = $regs[1];
  206. $subelement = $regs[2];
  207. }
  208. // For compatibility
  209. if ($element == 'order') {
  210. $element = $subelement = 'commande';
  211. }
  212. if ($element == 'propal') {
  213. $element = 'comm/propal';
  214. $subelement = 'propal';
  215. }
  216. if ($element == 'contract') {
  217. $element = $subelement = 'contrat';
  218. }
  219. if ($element == 'inter') {
  220. $element = $subelement = 'ficheinter';
  221. }
  222. if ($element == 'shipping') {
  223. $element = $subelement = 'expedition';
  224. }
  225. if ($element == 'order_supplier') {
  226. $element = 'fourn';
  227. $subelement = 'fournisseur.commande';
  228. }
  229. if ($element == 'project') {
  230. $element = 'projet';
  231. }
  232. dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
  233. $classname = ucfirst($origin);
  234. $objectsrc = new $classname($db);
  235. $objectsrc->fetch($origin_id);
  236. $tmpobject = $objectsrc;
  237. }
  238. }
  239. $contactarr = array();
  240. $contactarr = $tmpobject->liste_contact(-1, 'external');
  241. if (is_array($contactarr) && count($contactarr) > 0) {
  242. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  243. $contactstatic = new Contact($db);
  244. foreach ($contactarr as $contact) {
  245. $contactstatic->fetch($contact['id']);
  246. $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
  247. $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
  248. $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
  249. $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
  250. }
  251. }
  252. // Array of substitutions
  253. $formmail->substit = $substitutionarray;
  254. // Array of other parameters
  255. $formmail->param['action'] = 'send';
  256. $formmail->param['models'] = $modelmail;
  257. $formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
  258. $formmail->param['id'] = $object->id;
  259. $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
  260. $formmail->param['fileinit'] = array($file);
  261. // Show form
  262. print $formmail->get_form();
  263. print dol_get_fiche_end();
  264. }