card_presend.tpl.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <http://www.gnu.org/licenses/>.
  16. * or see http://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. {
  30. print "Error, template page can't be called as URL";
  31. exit;
  32. }
  33. if ($action == 'presend')
  34. {
  35. $langs->load("mails");
  36. $titreform='SendMail';
  37. $object->fetch_projet();
  38. if (! in_array($object->element, array('societe', 'user', 'member')))
  39. {
  40. // TODO get also the main_lastdoc field of $object. If not found, try to guess with following code
  41. $ref = dol_sanitizeFileName($object->ref);
  42. include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  43. // Special case
  44. if ($object->element == 'invoice_supplier')
  45. {
  46. $fileparams = dol_most_recent_file($diroutput . '/' . get_exdir($object->id,2,0,0,$object,$object->element).$ref, preg_quote($ref,'/').'([^\-])+');
  47. }
  48. else
  49. {
  50. $fileparams = dol_most_recent_file($diroutput . '/' . $ref, preg_quote($ref, '/').'[^\-]+');
  51. }
  52. $file = $fileparams['fullname'];
  53. }
  54. // Define output language
  55. $outputlangs = $langs;
  56. $newlang = '';
  57. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
  58. {
  59. $newlang = $_REQUEST['lang_id'];
  60. }
  61. if ($conf->global->MAIN_MULTILANGS && empty($newlang))
  62. {
  63. $newlang = $object->thirdparty->default_lang;
  64. }
  65. if (!empty($newlang))
  66. {
  67. $outputlangs = new Translate('', $conf);
  68. $outputlangs->setDefaultLang($newlang);
  69. // Load traductions files requiredby by page
  70. $outputlangs->loadLangs(array('commercial','bills','orders','contracts','members','propal','products','supplier_proposal','interventions'));
  71. }
  72. $topicmail='';
  73. if (empty($object->ref_client)) {
  74. $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
  75. } else if (! empty($object->ref_client)) {
  76. $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REFCLIENT__)');
  77. }
  78. // Build document if it not exists
  79. $forcebuilddoc=true;
  80. if (in_array($object->element, array('societe', 'user', 'member'))) $forcebuilddoc=false;
  81. if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) $forcebuilddoc=false;
  82. 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
  83. {
  84. if ((! $file || ! is_readable($file)) && method_exists($object, 'generateDocument'))
  85. {
  86. $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  87. if ($result < 0) {
  88. dol_print_error($db, $object->error, $object->errors);
  89. exit();
  90. }
  91. if ($object->element == 'invoice_supplier')
  92. {
  93. $fileparams = dol_most_recent_file($diroutput . '/' . get_exdir($object->id,2,0,0,$object,$object->element).$ref, preg_quote($ref,'/').'([^\-])+');
  94. }
  95. else
  96. {
  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. dol_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. {
  114. $formmail->fromid = $user->id;
  115. }
  116. $formmail->trackid=$trackid;
  117. if (! empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
  118. {
  119. include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  120. $formmail->frommail=dolAddEmailTrackId($formmail->frommail, $trackid);
  121. }
  122. $formmail->withfrom = 1;
  123. // Fill list of recipient with email inside <>.
  124. $liste = array();
  125. if ($object->element == 'expensereport')
  126. {
  127. $fuser = new User($db);
  128. $fuser->fetch($object->fk_user_author);
  129. $liste['thirdparty'] = $fuser->getFullName($langs)." <".$fuser->email.">";
  130. }
  131. elseif ($object->element == 'societe')
  132. {
  133. foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
  134. $liste[$key] = $value;
  135. }
  136. }
  137. elseif ($object->element == 'user' || $object->element == 'member')
  138. {
  139. $liste['thirdparty'] = $object->getFullName($langs)." <".$object->email.">";
  140. }
  141. else
  142. {
  143. if (is_object($object->thirdparty))
  144. {
  145. foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
  146. $liste[$key] = $value;
  147. }
  148. }
  149. }
  150. if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
  151. $listeuser=array();
  152. $fuserdest = new User($db);
  153. $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<>\'\''));
  154. if ($result>0 && is_array($fuserdest->users) && count($fuserdest->users)>0) {
  155. foreach($fuserdest->users as $uuserdest) {
  156. $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id,'email');
  157. }
  158. } elseif ($result<0) {
  159. setEventMessages(null, $fuserdest->errors,'errors');
  160. }
  161. if (count($listeuser)>0) {
  162. $formmail->withtouser = $listeuser;
  163. $formmail->withtoccuser = $listeuser;
  164. }
  165. }
  166. $formmail->withto = GETPOST('sendto') ? GETPOST('sendto') : $liste;
  167. $formmail->withtocc = $liste;
  168. $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
  169. $formmail->withtopic = $topicmail;
  170. $formmail->withfile = 2;
  171. $formmail->withbody = 1;
  172. $formmail->withdeliveryreceipt = 1;
  173. $formmail->withcancel = 1;
  174. //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
  175. if (! isset($arrayoffamiliestoexclude)) $arrayoffamiliestoexclude=null;
  176. // Make substitution in email content
  177. $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
  178. $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"/>' : '';
  179. $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
  180. $substitutionarray['__CONTACTCIVNAME__'] = '';
  181. $parameters = array(
  182. 'mode' => 'formemail'
  183. );
  184. complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
  185. // Find the good contact adress
  186. $custcontact = '';
  187. $contactarr = array();
  188. $contactarr = $object->liste_contact(- 1, 'external');
  189. if (is_array($contactarr) && count($contactarr) > 0) {
  190. require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  191. $contactstatic = new Contact($db);
  192. foreach ($contactarr as $contact) {
  193. $contactstatic->fetch($contact['id']);
  194. $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($langs, 1);
  195. }
  196. }
  197. // Tableau des substitutions
  198. $formmail->substit = $substitutionarray;
  199. // Tableau des parametres complementaires
  200. $formmail->param['action'] = 'send';
  201. $formmail->param['models'] = $modelmail;
  202. $formmail->param['models_id']=GETPOST('modelmailselected','int');
  203. $formmail->param['id'] = $object->id;
  204. $formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
  205. $formmail->param['fileinit'] = array($file);
  206. // Show form
  207. print $formmail->get_form();
  208. dol_fiche_end();
  209. }