PageRenderTime 71ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/comm/mailing/fiche.php

https://github.com/asterix14/dolibarr
PHP | 1070 lines | 790 code | 169 blank | 111 comment | 180 complexity | 7cdb3744c6939ac7aa90dcfe78fa75c9 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?PHP
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2011 Laurent Destailleur <eldy@uers.sourceforge.net>
  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 2 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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/comm/mailing/fiche.php
  20. * \ingroup mailing
  21. * \brief Fiche mailing, onglet general
  22. */
  23. require("../../main.inc.php");
  24. require_once(DOL_DOCUMENT_ROOT."/core/lib/emailing.lib.php");
  25. require_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
  26. require_once(DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php");
  28. require_once(DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php");
  29. require_once(DOL_DOCUMENT_ROOT."/core/class/html.formother.class.php");
  30. $langs->load("mails");
  31. if (! $user->rights->mailing->lire || $user->societe_id > 0)
  32. accessforbidden();
  33. $message = '';
  34. // Tableau des substitutions possibles
  35. $substitutionarray=array(
  36. '__ID__' => 'IdRecord',
  37. '__EMAIL__' => 'EMail',
  38. '__LASTNAME__' => 'Lastname',
  39. '__FIRSTNAME__' => 'Firstname',
  40. '__OTHER1__' => 'Other1',
  41. '__OTHER2__' => 'Other2',
  42. '__OTHER3__' => 'Other3',
  43. '__OTHER4__' => 'Other4',
  44. '__OTHER5__' => 'Other5'
  45. );
  46. $substitutionarrayfortest=array(
  47. '__ID__' => 'TESTIdRecord',
  48. '__EMAIL__' => 'TESTEMail',
  49. '__LASTNAME__' => 'TESTLastname',
  50. '__FIRSTNAME__' => 'TESTFirstname',
  51. '__OTHER1__' => 'TESTOther1',
  52. '__OTHER2__' => 'TESTOther2',
  53. '__OTHER3__' => 'TESTOther3',
  54. '__OTHER4__' => 'TESTOther4',
  55. '__OTHER5__' => 'TESTOther5'
  56. );
  57. // Action clone object
  58. if ($_POST["action"] == 'confirm_clone' && $_POST['confirm'] == 'yes')
  59. {
  60. if (empty($_REQUEST["clone_content"]) && empty($_REQUEST["clone_receivers"]))
  61. {
  62. $mesg='<div class="error">'.$langs->trans("NoCloneOptionsSpecified").'</div>';
  63. }
  64. else
  65. {
  66. $object=new Mailing($db);
  67. $result=$object->createFromClone($_REQUEST['id'],$_REQUEST["clone_content"],$_REQUEST["clone_receivers"]);
  68. if ($result > 0)
  69. {
  70. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  71. exit;
  72. }
  73. else
  74. {
  75. $mesg=$object->error;
  76. $_GET['action']='';
  77. $_GET['id']=$_REQUEST['id'];
  78. }
  79. }
  80. }
  81. // Action send emailing for everybody
  82. if ($_REQUEST["action"] == 'sendallconfirmed' && $_REQUEST['confirm'] == 'yes')
  83. {
  84. if (empty($conf->global->MAILING_LIMIT_SENDBYWEB))
  85. {
  86. // Pour des raisons de securite, on ne permet pas cette fonction via l'IHM,
  87. // on affiche donc juste un message
  88. $message='<div class="warning">'.$langs->trans("MailingNeedCommand").'</div>';
  89. $message.='<br><textarea cols="70" rows="'.ROWS_2.'" wrap="soft">php ./scripts/emailings/mailing-send.php '.$_GET["id"].'</textarea>';
  90. $message.='<br><br><div class="warning">'.$langs->trans("MailingNeedCommand2").'</div>';
  91. $_GET["action"]='';
  92. }
  93. else if ($conf->global->MAILING_LIMIT_SENDBYWEB < 0)
  94. {
  95. $message='<div class="warning">'.$langs->trans("NotEnoughPermissions").'</div>';
  96. $_GET["action"]='';
  97. }
  98. else
  99. {
  100. $mil=new Mailing($db);
  101. $result=$mil->fetch($_REQUEST['id']);
  102. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  103. if ($mil->statut == 0)
  104. {
  105. dol_print_error('','ErrorMailIsNotValidated');
  106. exit;
  107. }
  108. $id = $mil->id;
  109. $subject = $mil->sujet;
  110. $message = $mil->body;
  111. $from = $mil->email_from;
  112. $replyto = $mil->email_replyto;
  113. $errorsto = $mil->email_errorsto;
  114. // Le message est-il en html
  115. $msgishtml=-1; // Unknown by default
  116. if (preg_match('/[\s\t]*<html>/i',$message)) $msgishtml=1;
  117. // Warning, we must not use begin-commit transaction here
  118. // because we want to save update for each mail sent.
  119. $nbok=0; $nbko=0;
  120. // On choisit les mails non deja envoyes pour ce mailing (statut=0)
  121. // ou envoyes en erreur (statut=-1)
  122. $sql = "SELECT mc.rowid, mc.nom, mc.prenom, mc.email, mc.other, mc.source_url, mc.source_id, mc.source_type";
  123. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  124. $sql .= " WHERE mc.statut < 1 AND mc.fk_mailing = ".$id;
  125. dol_syslog("fiche.php: select targets sql=".$sql, LOG_DEBUG);
  126. $resql=$db->query($sql);
  127. if ($resql)
  128. {
  129. $num = $db->num_rows($resql); // nb of possible recipients
  130. if ($num)
  131. {
  132. dol_syslog("comm/mailing/fiche.php: nb of targets = ".$num, LOG_DEBUG);
  133. // Positionne date debut envoi
  134. $sql="UPDATE ".MAIN_DB_PREFIX."mailing SET date_envoi=".$db->idate(gmmktime())." WHERE rowid=".$id;
  135. $resql2=$db->query($sql);
  136. if (! $resql2)
  137. {
  138. dol_print_error($db);
  139. }
  140. // Loop on each email and send it
  141. $i = 0;
  142. while ($i < $num && $i < $conf->global->MAILING_LIMIT_SENDBYWEB)
  143. {
  144. $res=1;
  145. $obj = $db->fetch_object($resql);
  146. // sendto en RFC2822
  147. $sendto = str_replace(',',' ',$obj->prenom." ".$obj->nom)." <".$obj->email.">";
  148. // Make substitutions on topic and body. From (AA=YY;BB=CC;...) we keep YY, CC, ...
  149. $other=explode(';',$obj->other);
  150. $tmpfield=explode('=',$other[0],2); $other1=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  151. $tmpfield=explode('=',$other[1],2); $other2=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  152. $tmpfield=explode('=',$other[2],2); $other3=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  153. $tmpfield=explode('=',$other[3],2); $other4=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  154. $tmpfield=explode('=',$other[4],2); $other5=(isset($tmpfield[1])?$tmpfield[1]:$tmpfield[0]);
  155. $substitutionarray=array(
  156. '__ID__' => $obj->source_id,
  157. '__EMAIL__' => $obj->email,
  158. '__LASTNAME__' => $obj->nom,
  159. '__FIRSTNAME__' => $obj->prenom,
  160. '__OTHER1__' => $other1,
  161. '__OTHER2__' => $other2,
  162. '__OTHER3__' => $other3,
  163. '__OTHER4__' => $other4,
  164. '__OTHER5__' => $other5
  165. );
  166. $substitutionisok=true;
  167. complete_substitutions_array($substitutionarray, $langs);
  168. $newsubject=make_substitutions($subject,$substitutionarray);
  169. $newmessage=make_substitutions($message,$substitutionarray);
  170. $arr_file = array();
  171. $arr_mime = array();
  172. $arr_name = array();
  173. $arr_css = array();
  174. $listofpaths=dol_dir_list($upload_dir,'all',0,'','','name',SORT_ASC,0);
  175. if (count($listofpaths))
  176. {
  177. foreach($listofpaths as $key => $val)
  178. {
  179. $arr_file[]=$listofpaths[$key]['fullname'];
  180. $arr_mime[]=dol_mimetype($listofpaths[$key]['name']);
  181. $arr_name[]=$listofpaths[$key]['name'];
  182. }
  183. }
  184. // Fabrication du mail
  185. $mail = new CMailFile($newsubject, $sendto, $from, $newmessage,
  186. $arr_file, $arr_mime, $arr_name,
  187. '', '', 0, $msgishtml, $errorsto, $arr_css);
  188. if ($mail->error)
  189. {
  190. $res=0;
  191. }
  192. if (! $substitutionisok)
  193. {
  194. $mail->error='Some substitution failed';
  195. $res=0;
  196. }
  197. // Send mail
  198. if ($res)
  199. {
  200. $res=$mail->sendfile();
  201. }
  202. if ($res)
  203. {
  204. // Mail successful
  205. $nbok++;
  206. dol_syslog("comm/mailing/fiche.php: ok for #".$i.($mail->error?' - '.$mail->error:''), LOG_DEBUG);
  207. $sql="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  208. $sql.=" SET statut=1, date_envoi=".$db->idate(gmmktime())." WHERE rowid=".$obj->rowid;
  209. $resql2=$db->query($sql);
  210. if (! $resql2)
  211. {
  212. dol_print_error($db);
  213. }
  214. }
  215. else
  216. {
  217. // Mail failed
  218. $nbko++;
  219. dol_syslog("comm/mailing/fiche.php: error for #".$i.($mail->error?' - '.$mail->error:''), LOG_WARNING);
  220. $sql="UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  221. $sql.=" SET statut=-1, date_envoi=".$db->idate(gmmktime())." WHERE rowid=".$obj->rowid;
  222. $resql2=$db->query($sql);
  223. if (! $resql2)
  224. {
  225. dol_print_error($db);
  226. }
  227. }
  228. $i++;
  229. }
  230. }
  231. // Loop finished, set global statut of mail
  232. if ($nbko > 0)
  233. {
  234. $statut=2; // Status 'sent partially' (because at least one error)
  235. }
  236. else
  237. {
  238. if ($nbok >= $num) $statut=3; // Send to everybody
  239. else $statut=2; // Status 'sent partially' (because not send to everybody)
  240. }
  241. $sql="UPDATE ".MAIN_DB_PREFIX."mailing SET statut=".$statut." WHERE rowid=".$id;
  242. dol_syslog("comm/mailing/fiche.php: update global status sql=".$sql, LOG_DEBUG);
  243. $resql2=$db->query($sql);
  244. if (! $resql2)
  245. {
  246. dol_print_error($db);
  247. }
  248. }
  249. else
  250. {
  251. dol_syslog($db->error());
  252. dol_print_error($db);
  253. }
  254. $message='';
  255. $_GET["action"] = '';
  256. }
  257. }
  258. // Action send test emailing
  259. if ($_POST["action"] == 'send' && empty($_POST["cancel"]))
  260. {
  261. $mil = new Mailing($db);
  262. $result=$mil->fetch($_POST["mailid"]);
  263. $error=0;
  264. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  265. $mil->sendto = $_POST["sendto"];
  266. if (! $mil->sendto)
  267. {
  268. $message='<div class="error">'.$langs->trans("ErrorFieldRequired",$langs->trans("MailTo")).'</div>';
  269. $error++;
  270. }
  271. if (! $error)
  272. {
  273. // Le message est-il en html
  274. $msgishtml=-1; // Inconnu par defaut
  275. if (preg_match('/[\s\t]*<html>/i',$message)) $msgishtml=1;
  276. // Pratique les substitutions sur le sujet et message
  277. $mil->sujet=make_substitutions($mil->sujet,$substitutionarrayfortest,$langs);
  278. $mil->body=make_substitutions($mil->body,$substitutionarrayfortest,$langs);
  279. $arr_file = array();
  280. $arr_mime = array();
  281. $arr_name = array();
  282. $arr_css = array();
  283. // Ajout CSS
  284. if (!empty($mil->bgcolor)) $arr_css['bgcolor'] = (preg_match('/^#/',$mil->bgcolor)?'':'#').$mil->bgcolor;
  285. if (!empty($mil->bgimage)) $arr_css['bgimage'] = $mil->bgimage;
  286. // Attached files
  287. $listofpaths=dol_dir_list($upload_dir,'all',0,'','','name',SORT_ASC,0);
  288. if (count($listofpaths))
  289. {
  290. foreach($listofpaths as $key => $val)
  291. {
  292. $arr_file[]=$listofpaths[$key]['fullname'];
  293. $arr_mime[]=dol_mimetype($listofpaths[$key]['name']);
  294. $arr_name[]=$listofpaths[$key]['name'];
  295. }
  296. }
  297. $mailfile = new CMailFile($mil->sujet,$mil->sendto,$mil->email_from,$mil->body,
  298. $arr_file,$arr_mime,$arr_name,'', '', 0, $msgishtml,$mil->email_errorsto,$arr_css);
  299. $result=$mailfile->sendfile();
  300. if ($result)
  301. {
  302. $message='<div class="ok">'.$langs->trans("MailSuccessfulySent",$mailfile->getValidAddress($mil->email_from,2),$mailfile->getValidAddress($mil->sendto,2)).'</div>';
  303. }
  304. else
  305. {
  306. $message='<div class="error">'.$langs->trans("ResultKo").'<br>'.$mailfile->error.' '.$result.'</div>';
  307. }
  308. $_GET["action"]='';
  309. $_GET["id"]=$mil->id;
  310. }
  311. }
  312. // Action add emailing
  313. if ($_POST["action"] == 'add')
  314. {
  315. $message='';
  316. $mil = new Mailing($db);
  317. $mil->email_from = trim($_POST["from"]);
  318. $mil->email_replyto = trim($_POST["replyto"]);
  319. $mil->email_errorsto = trim($_POST["errorsto"]);
  320. $mil->titre = trim($_POST["titre"]);
  321. $mil->sujet = trim($_POST["sujet"]);
  322. $mil->body = trim($_POST["body"]);
  323. $mil->bgcolor = trim($_POST["bgcolor"]);
  324. $mil->bgimage = trim($_POST["bgimage"]);
  325. if (! $mil->titre) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->trans("MailTitle"));
  326. if (! $mil->sujet) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->trans("MailTopic"));
  327. if (! $mil->body) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->trans("MailBody"));
  328. if (! $message)
  329. {
  330. if ($mil->create($user) >= 0)
  331. {
  332. Header("Location: fiche.php?id=".$mil->id);
  333. exit;
  334. }
  335. $message=$mil->error;
  336. }
  337. $message='<div class="error">'.$message.'</div>';
  338. $_GET["action"]="create";
  339. }
  340. // Action update description of emailing
  341. if ($_REQUEST["action"] == 'setdesc' || $_REQUEST["action"] == 'setfrom' || $_REQUEST["action"] == 'setreplyto' || $_REQUEST["action"] == 'seterrorsto')
  342. {
  343. $mil = new Mailing($db);
  344. $mil->fetch($_POST["id"]);
  345. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  346. if ($_REQUEST["action"] == 'setdesc') $mil->titre = trim($_REQUEST["desc"]);
  347. if ($_REQUEST["action"] == 'setfrom') $mil->email_from = trim($_REQUEST["from"]);
  348. if ($_REQUEST["action"] == 'setreplyto') $mil->email_replyto = trim($_REQUEST["replyto"]);
  349. if ($_REQUEST["action"] == 'seterrorsto') $mil->email_errorsto = trim($_REQUEST["errorsto"]);
  350. if ($_REQUEST["action"] == 'setdesc' && empty($mil->titre)) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->transnoentities("MailTitle"));
  351. if ($_REQUEST["action"] == 'setfrom' && empty($mil->email_from)) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->transnoentities("MailFrom"));
  352. if (! $message)
  353. {
  354. if ($mil->update($user) >= 0)
  355. {
  356. Header("Location: fiche.php?id=".$mil->id);
  357. exit;
  358. }
  359. $message=$mil->error;
  360. }
  361. $message='<div class="error">'.$message.'</div>';
  362. $_GET["action"]="";
  363. $_GET["id"]=$_REQUEST["id"];
  364. }
  365. /*
  366. * Add file in email form
  367. */
  368. if (! empty($_POST['addfile']))
  369. {
  370. $mil = new Mailing($db);
  371. $mil->fetch($_POST["id"]);
  372. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  373. require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
  374. // Set tmp user directory
  375. $mesg=dol_add_file_process($upload_dir,0,0);
  376. $_POST["action"]=$_GET["action"]="edit";
  377. $_GET["id"]=$_POST["id"];
  378. }
  379. // Action update emailing
  380. if (! empty($_POST["removedfile"]))
  381. {
  382. $mil = new Mailing($db);
  383. $mil->fetch($_POST["id"]);
  384. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  385. require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
  386. $mesg=dol_remove_file_process($_POST['removedfile'],0);
  387. $_POST["action"]=$_GET["action"]="edit";
  388. $_GET["id"]=$_POST["id"];
  389. }
  390. // Action update emailing
  391. if ($_POST["action"] == 'update' && empty($_POST["removedfile"]) && empty($_POST["cancel"]))
  392. {
  393. require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
  394. $mil = new Mailing($db);
  395. $mil->fetch($_POST["id"]);
  396. $isupload=0;
  397. if (! $isupload)
  398. {
  399. $mil->sujet = trim($_POST["sujet"]);
  400. $mil->body = trim($_POST["body"]);
  401. $mil->bgcolor = trim($_POST["bgcolor"]);
  402. $mil->bgimage = trim($_POST["bgimage"]);
  403. if (! $mil->sujet) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->trans("MailTopic"));
  404. if (! $mil->body) $message.=($message?'<br>':'').$langs->trans("ErrorFieldRequired",$langs->trans("MailBody"));
  405. if (! $message)
  406. {
  407. if ($mil->update($user) >= 0)
  408. {
  409. Header("Location: fiche.php?id=".$mil->id);
  410. exit;
  411. }
  412. $message=$mil->error;
  413. }
  414. $message='<div class="error">'.$message.'</div>';
  415. $_GET["action"]="edit";
  416. $_GET["id"]=$_POST["id"];
  417. }
  418. else
  419. {
  420. $_GET["action"]="edit";
  421. $_GET["id"]=$_POST["id"];
  422. }
  423. }
  424. // Action confirmation validation
  425. if ($_REQUEST["action"] == 'confirm_valid')
  426. {
  427. if ($_REQUEST["confirm"] == 'yes')
  428. {
  429. $mil = new Mailing($db);
  430. if ($mil->fetch($_REQUEST["id"]) >= 0)
  431. {
  432. $mil->valid($user);
  433. Header("Location: fiche.php?id=".$mil->id);
  434. exit;
  435. }
  436. else
  437. {
  438. dol_print_error($db);
  439. }
  440. }
  441. else
  442. {
  443. Header("Location: fiche.php?id=".$_REQUEST["id"]);
  444. exit;
  445. }
  446. }
  447. // Resend
  448. if ($_REQUEST["action"] == 'confirm_reset')
  449. {
  450. if ($_REQUEST["confirm"] == 'yes')
  451. {
  452. $mil = new Mailing($db);
  453. if ($mil->fetch($_REQUEST["id"]) >= 0)
  454. {
  455. $db->begin();
  456. $result=$mil->valid($user);
  457. if ($result > 0)
  458. {
  459. $result=$mil->reset_targets_status($user);
  460. }
  461. if ($result > 0)
  462. {
  463. $db->commit();
  464. Header("Location: fiche.php?id=".$mil->id);
  465. exit;
  466. }
  467. else
  468. {
  469. $mesg=$mil->error;
  470. $db->rollback();
  471. }
  472. }
  473. else
  474. {
  475. dol_print_error($db);
  476. }
  477. }
  478. else
  479. {
  480. Header("Location: fiche.php?id=".$_REQUEST["id"]);
  481. exit;
  482. }
  483. }
  484. // Action confirmation suppression
  485. if ($_REQUEST["action"] == 'confirm_delete')
  486. {
  487. if ($_REQUEST["confirm"] == 'yes')
  488. {
  489. $mil = new Mailing($db);
  490. $mil->id = $_REQUEST["id"];
  491. if ($mil->delete($mil->id))
  492. {
  493. Header("Location: ".DOL_URL_ROOT."/comm/mailing/liste.php");
  494. exit;
  495. }
  496. }
  497. }
  498. if (! empty($_POST["cancel"]))
  499. {
  500. $action = '';
  501. $_GET["id"] = isset($_POST["mailid"])?$_POST["mailid"]:$_POST["id"];
  502. }
  503. /*
  504. * View
  505. */
  506. $help_url='EN:Module_EMailing|FR:Module_Mailing|ES:M&oacute;dulo_Mailing';
  507. llxHeader('',$langs->trans("Mailing"),$help_url);
  508. $form = new Form($db);
  509. $htmlother = new FormOther($db);
  510. $mil = new Mailing($db);
  511. if ($_GET["action"] == 'create')
  512. {
  513. // EMailing in creation mode
  514. print '<form name="new_mailing" action="fiche.php" method="post">'."\n";
  515. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  516. print '<input type="hidden" name="action" value="add">';
  517. print_fiche_titre($langs->trans("NewMailing"));
  518. dol_htmloutput_mesg($message);
  519. print '<table class="border" width="100%">';
  520. print '<tr><td width="25%" class="fieldrequired">'.$langs->trans("MailTitle").'</td><td><input class="flat" name="titre" size="40" value="'.$_POST['titre'].'"></td></tr>';
  521. print '<tr><td width="25%" class="fieldrequired">'.$langs->trans("MailFrom").'</td><td><input class="flat" name="from" size="40" value="'.$conf->global->MAILING_EMAIL_FROM.'"></td></tr>';
  522. print '<tr><td width="25%">'.$langs->trans("MailErrorsTo").'</td><td><input class="flat" name="errorsto" size="40" value="'.(!empty($conf->global->MAILING_EMAIL_ERRORSTO)?$conf->global->MAILING_EMAIL_ERRORSTO:$conf->global->MAIN_MAIL_ERRORS_TO).'"></td></tr>';
  523. print '</table>';
  524. print '</br><br>';
  525. print '<table class="border" width="100%">';
  526. print '<tr><td width="25%" class="fieldrequired">'.$langs->trans("MailTopic").'</td><td><input class="flat" name="sujet" size="60" value="'.$_POST['sujet'].'"></td></tr>';
  527. print '<tr><td width="25%">'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  528. $htmlother->select_color($_POST['bgcolor'],'bgcolor','new_mailing',0);
  529. print '</td></tr>';
  530. print '<tr><td width="25%" class="fieldrequired" valign="top">'.$langs->trans("MailMessage").'<br>';
  531. print '<br><i>'.$langs->trans("CommonSubstitutions").':<br>';
  532. foreach($substitutionarray as $key => $val)
  533. {
  534. print $key.' = '.$langs->trans($val).'<br>';
  535. }
  536. print '</i></td>';
  537. print '<td>';
  538. // Editeur wysiwyg
  539. require_once(DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php");
  540. $doleditor=new DolEditor('body',$_POST['body'],'',320,'dolibarr_mailings','',true,true,$conf->global->FCKEDITOR_ENABLE_MAILING,20,70);
  541. $doleditor->Create();
  542. print '</td></tr>';
  543. print '</table>';
  544. print '<br><center><input type="submit" class="button" value="'.$langs->trans("CreateMailing").'"></center>';
  545. print '</form>';
  546. }
  547. else
  548. {
  549. if ($mil->fetch($_GET["id"]) >= 0)
  550. {
  551. $upload_dir = $conf->mailing->dir_output . "/" . get_exdir($mil->id,2,0,1);
  552. $head = emailing_prepare_head($mil);
  553. dol_fiche_head($head, 'card', $langs->trans("Mailing"), 0, 'email');
  554. dol_htmloutput_mesg($message);
  555. // Confirmation de la validation du mailing
  556. if ($_GET["action"] == 'valid')
  557. {
  558. $ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$mil->id,$langs->trans("ValidMailing"),$langs->trans("ConfirmValidMailing"),"confirm_valid",'','',1);
  559. if ($ret == 'html') print '<br>';
  560. }
  561. // Confirm reset
  562. if ($_GET["action"] == 'reset')
  563. {
  564. $ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$mil->id,$langs->trans("ResetMailing"),$langs->trans("ConfirmResetMailing",$mil->ref),"confirm_reset",'','',2);
  565. if ($ret == 'html') print '<br>';
  566. }
  567. // Confirm delete
  568. if ($_GET["action"] == 'delete')
  569. {
  570. $ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$mil->id,$langs->trans("DeleteAMailing"),$langs->trans("ConfirmDeleteMailing"),"confirm_delete",'','',1);
  571. if ($ret == 'html') print '<br>';
  572. }
  573. if ($_GET["action"] != 'edit')
  574. {
  575. /*
  576. * Mailing en mode visu
  577. */
  578. if ($_GET["action"] == 'sendall')
  579. {
  580. // Define message to recommand from command line
  581. // Pour des raisons de securite, on ne permet pas cette fonction via l'IHM,
  582. // on affiche donc juste un message
  583. if (empty($conf->global->MAILING_LIMIT_SENDBYWEB))
  584. {
  585. // Pour des raisons de securite, on ne permet pas cette fonction via l'IHM,
  586. // on affiche donc juste un message
  587. $mesg.='<div class="warning">'.$langs->trans("MailingNeedCommand").'</div>';
  588. $mesg.='<br><textarea cols="60" rows="'.ROWS_2.'" wrap="soft">php ./scripts/emailings/mailing-send.php '.$_GET["id"].'</textarea>';
  589. $mesg.='<br><br><div class="warning">'.$langs->trans("MailingNeedCommand2").'</div>';
  590. $_GET["action"]='';
  591. }
  592. else
  593. {
  594. $text='';
  595. if ($conf->file->mailing_limit_sendbyweb == 0)
  596. {
  597. $text.=$langs->trans("MailingNeedCommand");
  598. $text.='<br><textarea cols="60" rows="'.ROWS_2.'" wrap="soft">php ./scripts/emailings/mailing-send.php '.$_GET["id"].'</textarea>';
  599. $text.='<br><br>';
  600. }
  601. $text.=$langs->trans('ConfirmSendingEmailing').'<br>';
  602. $text.=$langs->trans('LimitSendingEmailing',$conf->global->MAILING_LIMIT_SENDBYWEB);
  603. $ret=$form->form_confirm($_SERVER['PHP_SELF'].'?id='.$_REQUEST['id'],$langs->trans('SendMailing'),$text,'sendallconfirmed',$formquestion,'',1,260);
  604. if ($ret == 'html') print '<br>';
  605. }
  606. }
  607. print '<table class="border" width="100%">';
  608. print '<tr><td width="25%">'.$langs->trans("Ref").'</td>';
  609. print '<td colspan="3">';
  610. print $form->showrefnav($mil,'id');
  611. print '</td></tr>';
  612. // Description
  613. print '<tr><td>'.$form->editfieldkey("MailTitle",'desc',$mil->titre,$mil,$user->rights->mailing->creer).'</td><td colspan="3">';
  614. print $form->editfieldval("MailTitle",'desc',$mil->titre,$mil,$user->rights->mailing->creer);
  615. print '</td></tr>';
  616. // From
  617. print '<tr><td>'.$form->editfieldkey("MailFrom",'from',$mil->email_from,$mil,$user->rights->mailing->creer && $mil->statut < 3,'email').'</td><td colspan="3">';
  618. print $form->editfieldval("MailFrom",'from',$mil->email_from,$mil,$user->rights->mailing->creer && $mil->statut < 3,'email');
  619. print '</td></tr>';
  620. // Errors to
  621. print '<tr><td>'.$form->editfieldkey("MailErrorsTo",'errorsto',$mil->email_errorsto,$mil,$user->rights->mailing->creer && $mil->statut < 3,'email').'</td><td colspan="3">';
  622. print $form->editfieldval("MailErrorsTo",'errorsto',$mil->email_errorsto,$mil,$user->rights->mailing->creer && $mil->statut < 3,'email');
  623. print '</td></tr>';
  624. // Status
  625. print '<tr><td width="25%">'.$langs->trans("Status").'</td><td colspan="3">'.$mil->getLibStatut(4).'</td></tr>';
  626. // Nb of distinct emails
  627. print '<tr><td width="25%">';
  628. print $langs->trans("TotalNbOfDistinctRecipients");
  629. print '</td><td colspan="3">';
  630. $nbemail = ($mil->nbemail?$mil->nbemail:img_warning('').' <font class="warning">'.$langs->trans("NoTargetYet").'</font>');
  631. if (!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && is_numeric($nbemail) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail)
  632. {
  633. if ($conf->global->MAILING_LIMIT_SENDBYWEB > 0)
  634. {
  635. $text=$langs->trans('LimitSendingEmailing',$conf->global->MAILING_LIMIT_SENDBYWEB);
  636. print $form->textwithpicto($nbemail,$text,1,'warning');
  637. }
  638. else
  639. {
  640. $text=$langs->trans('NotEnoughPermissions');
  641. print $form->textwithpicto($nbemail,$text,1,'warning');
  642. }
  643. }
  644. else
  645. {
  646. print $nbemail;
  647. }
  648. print '</td></tr>';
  649. print '</table>';
  650. print "</div>";
  651. // Clone confirmation
  652. if ($_GET["action"] == 'clone')
  653. {
  654. // Create an array for form
  655. $formquestion=array(
  656. 'text' => $langs->trans("ConfirmClone"),
  657. array('type' => 'checkbox', 'name' => 'clone_content', 'label' => $langs->trans("CloneContent"), 'value' => 1),
  658. array('type' => 'checkbox', 'name' => 'clone_receivers', 'label' => $langs->trans("CloneReceivers").' ('.$langs->trans("FeatureNotYetAvailable").')', 'value' => 0, 'disabled' => true)
  659. );
  660. // Paiement incomplet. On demande si motif = escompte ou autre
  661. $form->form_confirm($_SERVER["PHP_SELF"].'?id='.$mil->id,$langs->trans('CloneEMailing'),$langs->trans('ConfirmCloneEMailing',$mil->ref),'confirm_clone',$formquestion,'yes');
  662. print '<br>';
  663. }
  664. if ($mesg) print $mesg;
  665. /*
  666. * Boutons d'action
  667. */
  668. if (GETPOST("cancel") || GETPOST("confirm")=='no' || GETPOST("action") == ''
  669. || in_array(GETPOST('action'),array('valid','delete','sendall')))
  670. {
  671. print "\n\n<div class=\"tabsAction\">\n";
  672. if ($mil->statut == 0 && $user->rights->mailing->creer)
  673. {
  674. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&amp;id='.$mil->id.'">'.$langs->trans("EditMailing").'</a>';
  675. }
  676. //print '<a class="butAction" href="fiche.php?action=test&amp;id='.$mil->id.'">'.$langs->trans("PreviewMailing").'</a>';
  677. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=test&amp;id='.$mil->id.'">'.$langs->trans("TestMailing").'</a>';
  678. if ($mil->statut == 0)
  679. {
  680. if ($mil->nbemail <= 0)
  681. {
  682. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NoTargetYet")).'">'.$langs->trans("ValidMailing").'</a>';
  683. }
  684. else if (empty($user->rights->mailing->valider))
  685. {
  686. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("ValidMailing").'</a>';
  687. }
  688. else
  689. {
  690. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=valid&amp;id='.$mil->id.'">'.$langs->trans("ValidMailing").'</a>';
  691. }
  692. }
  693. if (($mil->statut == 1 || $mil->statut == 2) && $mil->nbemail > 0 && $user->rights->mailing->valider)
  694. {
  695. if ($conf->global->MAILING_LIMIT_SENDBYWEB < 0)
  696. {
  697. print '<a class="butActionRefused" href="#" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("NotEnoughPermissions")).'">'.$langs->trans("SendMailing").'</a>';
  698. }
  699. else
  700. {
  701. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=sendall&amp;id='.$mil->id.'">'.$langs->trans("SendMailing").'</a>';
  702. }
  703. }
  704. if ($user->rights->mailing->creer)
  705. {
  706. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=clone&amp;object=emailing&amp;id='.$mil->id.'">'.$langs->trans("ToClone").'</a>';
  707. }
  708. if (($mil->statut == 2 || $mil->statut == 3) && $user->rights->mailing->valider)
  709. {
  710. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=reset&amp;id='.$mil->id.'">'.$langs->trans("ResetMailing").'</a>';
  711. }
  712. if (($mil->statut <= 1 && $user->rights->mailing->creer) || $user->rights->mailing->supprimer)
  713. {
  714. print '<a class="butActionDelete" href="'.$_SERVER['PHP_SELF'].'?action=delete&amp;id='.$mil->id.'">'.$langs->trans("DeleteMailing").'</a>';
  715. }
  716. print '<br><br></div>';
  717. }
  718. // Affichage formulaire de TEST
  719. if ($_GET["action"] == 'test')
  720. {
  721. print_titre($langs->trans("TestMailing"));
  722. // Create l'objet formulaire mail
  723. include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php');
  724. $formmail = new FormMail($db);
  725. $formmail->fromname = $mil->email_from;
  726. $formmail->frommail = $mil->email_from;
  727. $formmail->withsubstit=1;
  728. $formmail->withfrom=0;
  729. $formmail->withto=$user->email?$user->email:1;
  730. $formmail->withtocc=0;
  731. $formmail->withtoccc=$conf->global->MAIN_EMAIL_USECCC;
  732. $formmail->withtopic=0;
  733. $formmail->withtopicreadonly=1;
  734. $formmail->withfile=0;
  735. $formmail->withbody=0;
  736. $formmail->withbodyreadonly=1;
  737. $formmail->withcancel=1;
  738. $formmail->withdeliveryreceipt=0;
  739. // Tableau des substitutions
  740. $formmail->substit=$substitutionarrayfortest;
  741. // Tableau des parametres complementaires du post
  742. $formmail->param["action"]="send";
  743. $formmail->param["models"]="body";
  744. $formmail->param["mailid"]=$mil->id;
  745. $formmail->param["returnurl"]=DOL_URL_ROOT."/comm/mailing/fiche.php?id=".$mil->id;
  746. $formmail->show_form();
  747. print '<br>';
  748. }
  749. // Print mail content
  750. print_fiche_titre($langs->trans("EMail"),'','');
  751. print '<table class="border" width="100%">';
  752. // Subject
  753. print '<tr><td width="25%">'.$langs->trans("MailTopic").'</td><td colspan="3">'.$mil->sujet.'</td></tr>';
  754. // Joined files
  755. print '<tr><td>'.$langs->trans("MailFile").'</td><td colspan="3">';
  756. // List of files
  757. $listofpaths=dol_dir_list($upload_dir,'all',0,'','','name',SORT_ASC,0);
  758. if (count($listofpaths))
  759. {
  760. foreach($listofpaths as $key => $val)
  761. {
  762. print img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  763. print '<br>';
  764. }
  765. }
  766. else
  767. {
  768. print $langs->trans("NoAttachedFiles").'<br>';
  769. }
  770. print '</td></tr>';
  771. // Background color
  772. /*print '<tr><td width="25%">'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  773. $htmlother->select_color($mil->bgcolor,'bgcolor','edit_mailing',0);
  774. print '</td></tr>';*/
  775. // Message
  776. print '<tr><td valign="top">'.$langs->trans("MailMessage").'</td>';
  777. print '<td colspan="3" bgcolor="'.($mil->bgcolor?(preg_match('/^#/',$mil->bgcolor)?'':'#').$mil->bgcolor:'white').'">';
  778. print dol_htmlentitiesbr($mil->body);
  779. print '</td>';
  780. print '</tr>';
  781. print '</table>';
  782. print "<br>";
  783. }
  784. else
  785. {
  786. /*
  787. * Mailing en mode edition
  788. */
  789. if ($mesg) print $mesg."<br>";
  790. if ($message) print $message."<br>";
  791. print '<table class="border" width="100%">';
  792. print '<tr><td width="25%">'.$langs->trans("Ref").'</td><td colspan="3">'.$mil->id.'</td></tr>';
  793. print '<tr><td width="25%">'.$langs->trans("MailTitle").'</td><td colspan="3">'.$mil->titre.'</td></tr>';
  794. print '<tr><td width="25%">'.$langs->trans("MailFrom").'</td><td colspan="3">'.dol_print_email($mil->email_from,0,0,0,0,1).'</td></tr>';
  795. print '<tr><td width="25%">'.$langs->trans("MailErrorsTo").'</td><td colspan="3">'.dol_print_email($mil->email_errorsto,0,0,0,0,1).'</td></tr>';
  796. // Status
  797. print '<tr><td width="25%">'.$langs->trans("Status").'</td><td colspan="3">'.$mil->getLibStatut(4).'</td></tr>';
  798. // Nb of distinct emails
  799. print '<tr><td width="25%">';
  800. print $langs->trans("TotalNbOfDistinctRecipients");
  801. print '</td><td colspan="3">';
  802. $nbemail = ($mil->nbemail?$mil->nbemail:'<font class="error">'.$langs->trans("NoTargetYet").'</font>');
  803. if (!empty($conf->global->MAILING_LIMIT_SENDBYWEB) && is_numeric($nbemail) && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail)
  804. {
  805. $text=$langs->trans('LimitSendingEmailing',$conf->global->MAILING_LIMIT_SENDBYWEB);
  806. print $form->textwithpicto($nbemail,$text,1,'warning');
  807. }
  808. else
  809. {
  810. print $nbemail;
  811. }
  812. print '</td></tr>';
  813. print '</table>';
  814. print "</div>";
  815. print "\n";
  816. print '<form name="edit_mailing" action="fiche.php" method="post" enctype="multipart/form-data">'."\n";
  817. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  818. print '<input type="hidden" name="action" value="update">';
  819. print '<input type="hidden" name="id" value="'.$mil->id.'">';
  820. // Print mail content
  821. print_fiche_titre($langs->trans("EMail"),'','');
  822. print '<table class="border" width="100%">';
  823. // Subject
  824. print '<tr><td width="25%" class="fieldrequired">'.$langs->trans("MailTopic").'</td><td colspan="3"><input class="flat" type="text" size=60 name="sujet" value="'.$mil->sujet.'"></td></tr>';
  825. dol_init_file_process($upload_dir);
  826. // Joined files
  827. $addfileaction='addfile';
  828. print '<tr><td>'.$langs->trans("MailFile").'</td>';
  829. print '<td colspan="3">';
  830. // List of files
  831. $listofpaths=dol_dir_list($upload_dir,'all',0,'','','name',SORT_ASC,0);
  832. // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript
  833. $out.= '<input type="hidden" class="removedfilehidden" name="removedfile" value="">'."\n";
  834. $out.= '<script type="text/javascript" language="javascript">';
  835. $out.= 'jQuery(document).ready(function () {';
  836. $out.= ' jQuery(".removedfile").click(function() {';
  837. $out.= ' jQuery(".removedfilehidden").val(jQuery(this).val());';
  838. $out.= ' });';
  839. $out.= '})';
  840. $out.= '</script>'."\n";
  841. if (count($listofpaths))
  842. {
  843. foreach($listofpaths as $key => $val)
  844. {
  845. $out.= '<div id="attachfile_'.$key.'">';
  846. $out.= img_mime($listofpaths[$key]['name']).' '.$listofpaths[$key]['name'];
  847. $out.= ' <input type="image" style="border: 0px;" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/delete.png" value="'.($key+1).'" class="removedfile" id="removedfile_'.$key.'" name="removedfile_'.$key.'" />';
  848. $out.= '<br></div>';
  849. }
  850. }
  851. else
  852. {
  853. $out.= $langs->trans("NoAttachedFiles").'<br>';
  854. }
  855. // Add link to add file
  856. $out.= '<input type="file" class="flat" id="addedfile" name="addedfile" value="'.$langs->trans("Upload").'" />';
  857. $out.= ' ';
  858. $out.= '<input type="submit" class="button" id="'.$addfileaction.'" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'" />';
  859. print $out;
  860. print '</td></tr>';
  861. // Background color
  862. print '<tr><td width="25%">'.$langs->trans("BackgroundColorByDefault").'</td><td colspan="3">';
  863. $htmlother->select_color($mil->bgcolor,'bgcolor','edit_mailing',0);
  864. print '</td></tr>';
  865. // Message
  866. print '<tr><td width="25%" valign="top">'.$langs->trans("MailMessage").'<br>';
  867. print '<br><i>'.$langs->trans("CommonSubstitutions").':<br>';
  868. print '__ID__ = '.$langs->trans("IdRecord").'<br>';
  869. print '__EMAIL__ = '.$langs->trans("EMail").'<br>';
  870. print '__LASTNAME__ = '.$langs->trans("Lastname").'<br>';
  871. print '__FIRSTNAME__ = '.$langs->trans("Firstname").'<br>';
  872. print '__OTHER1__ = '.$langs->trans("Other").'1<br>';
  873. print '__OTHER2__ = '.$langs->trans("Other").'2<br>';
  874. print '__OTHER3__ = '.$langs->trans("Other").'3<br>';
  875. print '__OTHER4__ = '.$langs->trans("Other").'4<br>';
  876. print '__OTHER5__ = '.$langs->trans("Other").'5<br>';
  877. print '</i></td>';
  878. print '<td colspan="3">';
  879. // Editeur wysiwyg
  880. require_once(DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php");
  881. $doleditor=new DolEditor('body',$mil->body,'',320,'dolibarr_mailings','',true,true,$conf->global->FCKEDITOR_ENABLE_MAILING,20,70);
  882. $doleditor->Create();
  883. print '</td></tr>';
  884. print '<tr><td colspan="4" align="center">';
  885. print '<input type="submit" class="button" value="'.$langs->trans("Save").'" name="save">';
  886. print ' &nbsp; ';
  887. print '<input type="submit" class="button" value="'.$langs->trans("Cancel").'" name="cancel">';
  888. print '</td></tr>';
  889. print '</table>';
  890. print '</form>';
  891. print '<br>';
  892. }
  893. }
  894. else
  895. {
  896. dol_print_error($db,$mil->error);
  897. }
  898. }
  899. $db->close();
  900. llxFooter();
  901. ?>