cibles.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2023 Laurent Destailleur <eldy@uers.sourceforge.net>
  4. * Copyright (C) 2005-2010 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/comm/mailing/cibles.php
  22. * \ingroup mailing
  23. * \brief Page to define emailing targets
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  28. require_once DOL_DOCUMENT_ROOT.'/comm/mailing/class/mailing.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formmailing.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/emailing.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array("mails", "admin"));
  36. // Load variable for pagination
  37. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  38. $sortfield = GETPOST('sortfield', 'aZ09comma');
  39. $sortorder = GETPOST('sortorder', 'aZ09comma');
  40. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  41. if (empty($page) || $page == -1) {
  42. $page = 0;
  43. } // If $page is not defined, or '' or -1
  44. $offset = $limit * $page;
  45. $pageprev = $page - 1;
  46. $pagenext = $page + 1;
  47. if (!$sortfield) {
  48. $sortfield = "mc.statut,email";
  49. }
  50. if (!$sortorder) {
  51. $sortorder = "DESC,ASC";
  52. }
  53. $id = GETPOST('id', 'int');
  54. $rowid = GETPOST('rowid', 'int');
  55. $action = GETPOST('action', 'aZ09');
  56. $search_lastname = GETPOST("search_lastname", 'alphanohtml');
  57. $search_firstname = GETPOST("search_firstname", 'alphanohtml');
  58. $search_email = GETPOST("search_email", 'alphanohtml');
  59. $search_other = GETPOST("search_other", 'alphanohtml');
  60. $search_dest_status = GETPOST('search_dest_status', 'int');
  61. // Search modules dirs
  62. $modulesdir = dolGetModulesDirs('/mailings');
  63. $object = new Mailing($db);
  64. $result = $object->fetch($id);
  65. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  66. $hookmanager->initHooks(array('ciblescard', 'globalcard'));
  67. $sqlmessage = '';
  68. // List of sending methods
  69. $listofmethods = array();
  70. //$listofmethods['default'] = $langs->trans('DefaultOutgoingEmailSetup');
  71. $listofmethods['mail'] = 'PHP mail function';
  72. //$listofmethods['simplemail']='Simplemail class';
  73. $listofmethods['smtps'] = 'SMTP/SMTPS socket library';
  74. if (version_compare(phpversion(), '7.0', '>=')) {
  75. $listofmethods['swiftmailer'] = 'Swift Mailer socket library';
  76. }
  77. // Security check
  78. if (!$user->hasRight('mailing', 'lire') || (!getDolGlobalString('EXTERNAL_USERS_ARE_AUTHORIZED') && $user->socid > 0)) {
  79. accessforbidden();
  80. }
  81. //$result = restrictedArea($user, 'mailing');
  82. /*
  83. * Actions
  84. */
  85. if ($action == 'add' && $user->hasRight('mailing', 'creer')) { // Add recipients
  86. $module = GETPOST("module", 'alpha');
  87. $result = -1;
  88. foreach ($modulesdir as $dir) {
  89. // Load modules attributes in arrays (name, numero, orders) from dir directory
  90. //print $dir."\n<br>";
  91. dol_syslog("Scan directory ".$dir." for modules");
  92. // Loading Class
  93. $file = $dir."/".$module.".modules.php";
  94. $classname = "mailing_".$module;
  95. if (file_exists($file)) {
  96. include_once $file;
  97. // Add targets into database
  98. dol_syslog("Call add_to_target() on class ".$classname." evenunsubscribe=".$object->evenunsubscribe);
  99. if (class_exists($classname)) {
  100. $obj = new $classname($db);
  101. $obj->evenunsubscribe = $object->evenunsubscribe;
  102. $result = $obj->add_to_target($id);
  103. $sqlmessage = $obj->sql;
  104. } else {
  105. $result = -1;
  106. break;
  107. }
  108. }
  109. }
  110. if ($result > 0) {
  111. setEventMessages($langs->trans("XTargetsAdded", $result), null, 'mesgs');
  112. $action = '';
  113. }
  114. if ($result == 0) {
  115. setEventMessages($langs->trans("WarningNoEMailsAdded"), null, 'warnings');
  116. }
  117. if ($result < 0) {
  118. setEventMessages($langs->trans("Error").($obj->error ? ' '.$obj->error : ''), null, 'errors');
  119. }
  120. }
  121. if (GETPOST('clearlist', 'int') && $user->hasRight('mailing', 'creer')) {
  122. // Loading Class
  123. $obj = new MailingTargets($db);
  124. $obj->clear_target($id);
  125. /* Avoid this to allow reposition
  126. header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
  127. exit;
  128. */
  129. }
  130. if (GETPOST('exportcsv', 'int') && $user->hasRight('mailing', 'lire')) {
  131. $completefilename = 'targets_emailing'.$object->id.'_'.dol_print_date(dol_now(), 'dayhourlog').'.csv';
  132. header('Content-Type: text/csv');
  133. header('Content-Disposition: attachment;filename='.$completefilename);
  134. // List of selected targets
  135. $sql = "SELECT mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut as status, mc.date_envoi, mc.tms,";
  136. $sql .= " mc.source_id, mc.source_type, mc.error_text";
  137. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  138. $sql .= " WHERE mc.fk_mailing = ".((int) $object->id);
  139. $sql .= $db->order($sortfield, $sortorder);
  140. $resql = $db->query($sql);
  141. if ($resql) {
  142. $num = $db->num_rows($resql);
  143. $sep = ',';
  144. while ($obj = $db->fetch_object($resql)) {
  145. print $obj->rowid.$sep;
  146. print '"'.$obj->lastname.'"'.$sep;
  147. print '"'.$obj->firstname.'"'.$sep;
  148. print $obj->email.$sep;
  149. print $obj->other.$sep;
  150. print $obj->tms.$sep;
  151. print $obj->source_type.$sep;
  152. print $obj->source_id.$sep;
  153. print $obj->date_envoi.$sep;
  154. print $obj->status.$sep;
  155. print '"'.$obj->error_text.'"'.$sep;
  156. print "\n";
  157. }
  158. exit;
  159. } else {
  160. dol_print_error($db);
  161. }
  162. exit;
  163. }
  164. if ($action == 'delete' && $user->hasRight('mailing', 'creer')) {
  165. // Ici, rowid indique le destinataire et id le mailing
  166. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles WHERE rowid = ".((int) $rowid);
  167. $resql = $db->query($sql);
  168. if ($resql) {
  169. if (!empty($id)) {
  170. $obj = new MailingTargets($db);
  171. $obj->update_nb($id);
  172. setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
  173. } else {
  174. header("Location: list.php");
  175. exit;
  176. }
  177. } else {
  178. dol_print_error($db);
  179. }
  180. }
  181. // Purge search criteria
  182. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
  183. $search_lastname = '';
  184. $search_firstname = '';
  185. $search_email = '';
  186. $search_other = '';
  187. $search_dest_status = '';
  188. }
  189. // Action update description of emailing
  190. if ($action == 'settitle' || $action == 'setemail_from' || $action == 'setreplyto' || $action == 'setemail_errorsto' || $action == 'setevenunsubscribe') {
  191. $upload_dir = $conf->mailing->dir_output."/".get_exdir($object->id, 2, 0, 1, $object, 'mailing');
  192. if ($action == 'settitle') {
  193. $object->title = trim(GETPOST('title', 'alpha'));
  194. } elseif ($action == 'setemail_from') {
  195. $object->email_from = trim(GETPOST('email_from', 'alphawithlgt')); // Must allow 'name <email>'
  196. } elseif ($action == 'setemail_replyto') {
  197. $object->email_replyto = trim(GETPOST('email_replyto', 'alphawithlgt')); // Must allow 'name <email>'
  198. } elseif ($action == 'setemail_errorsto') {
  199. $object->email_errorsto = trim(GETPOST('email_errorsto', 'alphawithlgt')); // Must allow 'name <email>'
  200. } elseif ($action == 'settitle' && empty($object->title)) {
  201. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailTitle"));
  202. } elseif ($action == 'setfrom' && empty($object->email_from)) {
  203. $mesg = $langs->trans("ErrorFieldRequired", $langs->transnoentities("MailFrom"));
  204. } elseif ($action == 'setevenunsubscribe') {
  205. $object->evenunsubscribe = (GETPOST('evenunsubscribe') ? 1 : 0);
  206. }
  207. if (!$mesg) {
  208. $result = $object->update($user);
  209. if ($result >= 0) {
  210. header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
  211. exit;
  212. }
  213. $mesg = $object->error;
  214. }
  215. setEventMessages($mesg, null, 'errors');
  216. $action = "";
  217. }
  218. /*
  219. * View
  220. */
  221. llxHeader('', $langs->trans("Mailing"), 'EN:Module_EMailing|FR:Module_Mailing|ES:M&oacute;dulo_Mailing');
  222. $form = new Form($db);
  223. $formmailing = new FormMailing($db);
  224. if ($object->fetch($id) >= 0) {
  225. $head = emailing_prepare_head($object);
  226. print dol_get_fiche_head($head, 'targets', $langs->trans("Mailing"), -1, 'email');
  227. $linkback = '<a href="'.DOL_URL_ROOT.'/comm/mailing/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  228. $morehtmlref = '<div class="refidno">';
  229. // Ref customer
  230. $morehtmlref .= $form->editfieldkey("", 'title', $object->title, $object, 0, 'string', '', 0, 1);
  231. $morehtmlref .= $form->editfieldval("", 'title', $object->title, $object, 0, 'string', '', null, null, '', 1);
  232. $morehtmlref .= '</div>';
  233. $morehtmlright = '';
  234. $nbtry = $nbok = 0;
  235. if ($object->statut == $object::STATUS_SENTPARTIALY || $object->statut == $object::STATUS_SENTCOMPLETELY) {
  236. $nbtry = $object->countNbOfTargets('alreadysent');
  237. $nbko = $object->countNbOfTargets('alreadysentko');
  238. $nbok = ($nbtry - $nbko);
  239. $morehtmlright .= ' ('.$nbtry.'/'.$object->nbemail;
  240. if ($nbko) {
  241. $morehtmlright .= ' - '.$nbko.' '.$langs->trans("Error");
  242. }
  243. $morehtmlright .= ') &nbsp; ';
  244. }
  245. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  246. print '<div class="fichecenter">';
  247. print '<div class="fichehalfleft">';
  248. print '<div class="underbanner clearboth"></div>';
  249. print '<table class="border centpercent tableforfield">';
  250. print '<tr><td class="titlefield">'.$langs->trans("MailFrom").'</td><td>';
  251. $emailarray = CMailFile::getArrayAddress($object->email_from);
  252. foreach ($emailarray as $email => $name) {
  253. if ($name && $name != $email) {
  254. print dol_escape_htmltag($name).' &lt;'.$email;
  255. print '&gt;';
  256. if (!isValidEmail($email)) {
  257. $langs->load("errors");
  258. print img_warning($langs->trans("ErrorBadEMail", $email));
  259. }
  260. } else {
  261. print dol_print_email($object->email_from, 0, 0, 0, 0, 1);
  262. }
  263. }
  264. //print dol_print_email($object->email_from, 0, 0, 0, 0, 1);
  265. //var_dump($object->email_from);
  266. print '</td></tr>';
  267. // Errors to
  268. print '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>';
  269. $emailarray = CMailFile::getArrayAddress($object->email_errorsto);
  270. foreach ($emailarray as $email => $name) {
  271. if ($name != $email) {
  272. print dol_escape_htmltag($name).' &lt;'.$email;
  273. print '&gt;';
  274. if (!isValidEmail($email)) {
  275. $langs->load("errors");
  276. print img_warning($langs->trans("ErrorBadEMail", $email));
  277. }
  278. } else {
  279. print dol_print_email($object->email_errorsto, 0, 0, 0, 0, 1);
  280. }
  281. }
  282. print '</td></tr>';
  283. print '</table>';
  284. print '</div>';
  285. print '<div class="fichehalfright">';
  286. print '<div class="underbanner clearboth"></div>';
  287. print '<table class="border centpercent tableforfield">';
  288. // Number of distinct emails
  289. print '<tr><td class="titlefield">';
  290. print $langs->trans("TotalNbOfDistinctRecipients");
  291. print '</td><td>';
  292. $nbemail = ($object->nbemail ? $object->nbemail : 0);
  293. if (is_numeric($nbemail)) {
  294. $text = '';
  295. if ((getDolGlobalString('MAILING_LIMIT_SENDBYWEB') && $conf->global->MAILING_LIMIT_SENDBYWEB < $nbemail) && ($object->statut == 1 || ($object->statut == 2 && $nbtry < $nbemail))) {
  296. if (getDolGlobalInt('MAILING_LIMIT_SENDBYWEB') > 0) {
  297. $text .= $langs->trans('LimitSendingEmailing', $conf->global->MAILING_LIMIT_SENDBYWEB);
  298. } else {
  299. $text .= $langs->trans('SendingFromWebInterfaceIsNotAllowed');
  300. }
  301. }
  302. if (empty($nbemail)) {
  303. $nbemail .= ' '.img_warning($langs->trans('ToAddRecipientsChooseHere'));//.' <span class="warning">'.$langs->trans("NoTargetYet").'</span>';
  304. }
  305. if ($text) {
  306. print $form->textwithpicto($nbemail, $text, 1, 'warning');
  307. } else {
  308. print $nbemail;
  309. }
  310. }
  311. print '</td></tr>';
  312. print '<tr><td>';
  313. print $langs->trans("MAIN_MAIL_SENDMODE");
  314. print '</td><td>';
  315. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') && getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  316. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING')];
  317. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE')) {
  318. $text = $listofmethods[getDolGlobalString('MAIN_MAIL_SENDMODE')];
  319. } else {
  320. $text = $listofmethods['mail'];
  321. }
  322. print $text;
  323. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'default') {
  324. if (getDolGlobalString('MAIN_MAIL_SENDMODE_EMAILING') != 'mail') {
  325. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER_EMAILING').')</span>';
  326. }
  327. } elseif (getDolGlobalString('MAIN_MAIL_SENDMODE') != 'mail' && getDolGlobalString('MAIN_MAIL_SMTP_SERVER')) {
  328. print ' <span class="opacitymedium">('.getDolGlobalString('MAIN_MAIL_SMTP_SERVER').')</span>';
  329. }
  330. print '</td></tr>';
  331. // Other attributes. Fields from hook formObjectOptions and Extrafields.
  332. include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
  333. print '</table>';
  334. print '</div>';
  335. print '</div>';
  336. print '<div class="clearboth"></div>';
  337. print dol_get_fiche_end();
  338. print '<br>';
  339. $allowaddtarget = ($object->statut == $object::STATUS_DRAFT);
  340. // Show email selectors
  341. if ($allowaddtarget && $user->hasRight('mailing', 'creer')) {
  342. print load_fiche_titre($langs->trans("ToAddRecipientsChooseHere"), ($user->admin ? info_admin($langs->trans("YouCanAddYourOwnPredefindedListHere"), 1) : ''), 'generic');
  343. print '<div class="div-table-responsive">';
  344. print '<div class="tagtable centpercentwithout1imp liste_titre_bydiv borderbottom" id="tablelines">';
  345. print '<div class="tagtr liste_titre">';
  346. print '<div class="tagtd"></div>';
  347. print '<div class="tagtd">'.$langs->trans("RecipientSelectionModules").'</div>';
  348. print '<div class="tagtd center maxwidth150">'.$langs->trans("NbOfUniqueEMails").'</div>';
  349. print '<div class="tagtd left"><div class="inline-block">'.$langs->trans("Filters").'</div>';
  350. print ' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class=" inline-block">'.$langs->trans("EvenUnsubscribe").' ';
  351. print ajax_object_onoff($object, 'evenunsubscribe', 'evenunsubscribe', 'EvenUnsubscribe:switch_on:warning', 'EvenUnsubscribe', array(), 'small valignmiddle', '', 1);
  352. print '</div>';
  353. print '</div>';
  354. print '<div class="tagtd">&nbsp;</div>';
  355. print '</div>'; // End tr
  356. clearstatcache();
  357. foreach ($modulesdir as $dir) {
  358. $modulenames = array();
  359. // Load modules attributes in arrays (name, numero, orders) from dir directory
  360. //print $dir."\n<br>";
  361. dol_syslog("Scan directory ".$dir." for modules");
  362. $handle = @opendir($dir);
  363. if (is_resource($handle)) {
  364. while (($file = readdir($handle)) !== false) {
  365. if (substr($file, 0, 1) != '.' && substr($file, 0, 3) != 'CVS') {
  366. $reg = array();
  367. if (preg_match("/(.*)\.modules\.php$/i", $file, $reg)) {
  368. if ($reg[1] == 'example') {
  369. continue;
  370. }
  371. $modulenames[] = $reg[1];
  372. }
  373. }
  374. }
  375. closedir($handle);
  376. }
  377. // Sort $modulenames
  378. sort($modulenames);
  379. $var = true;
  380. // Loop on each submodule
  381. foreach ($modulenames as $modulename) {
  382. // Loading Class
  383. $file = $dir.$modulename.".modules.php";
  384. $classname = "mailing_".$modulename;
  385. require_once $file;
  386. $obj = new $classname($db);
  387. // Check if qualified
  388. $qualified = (is_null($obj->enabled) ? 1 : dol_eval($obj->enabled, 1));
  389. // Check dependencies
  390. foreach ($obj->require_module as $key) {
  391. if (empty($conf->$key->enabled) || (empty($user->admin) && $obj->require_admin)) {
  392. $qualified = 0;
  393. //print "Les prerequis d'activation du module mailing ne sont pas respectes. Il ne sera pas actif";
  394. break;
  395. }
  396. }
  397. // If module is qualified
  398. if ($qualified) {
  399. $var = !$var;
  400. if ($allowaddtarget) {
  401. print '<form '.$bctag[$var].' name="'.$modulename.'" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&module='.$modulename.'" method="POST" enctype="multipart/form-data">';
  402. print '<input type="hidden" name="token" value="'.newToken().'">';
  403. print '<input type="hidden" name="action" value="add">';
  404. print '<input type="hidden" name="page_y" value="'.newToken().'">';
  405. } else {
  406. print '<div '.$bctag[$var].'>';
  407. }
  408. print '<div class="tagtd paddingleftimp marginleftonly paddingrightimp marginrightonly valignmiddle center">';
  409. if (empty($obj->picto)) {
  410. $obj->picto = 'generic';
  411. }
  412. print img_object($langs->trans("EmailingTargetSelector").': '.get_class($obj), $obj->picto, 'class="valignmiddle width25 size15x"');
  413. print '</div>';
  414. print '<div class="tagtd valignmiddle">'; // style="height: 4em"
  415. print $obj->getDesc();
  416. print '</div>';
  417. try {
  418. $obj->evenunsubscribe = $object->evenunsubscribe; // Set flag to include/exclude email that has opt-out.
  419. $nbofrecipient = $obj->getNbOfRecipients('');
  420. } catch (Exception $e) {
  421. dol_syslog($e->getMessage(), LOG_ERR);
  422. }
  423. print '<div class="tagtd center valignmiddle">';
  424. if ($nbofrecipient === '' || $nbofrecipient >= 0) {
  425. print $nbofrecipient;
  426. } else {
  427. print $langs->trans("Error").' '.img_error($obj->error);
  428. }
  429. print '</div>';
  430. print '<div class="tagtd left valignmiddle">';
  431. if ($allowaddtarget) {
  432. try {
  433. $filter = $obj->formFilter();
  434. } catch (Exception $e) {
  435. dol_syslog($e->getMessage(), LOG_ERR);
  436. }
  437. if ($filter) {
  438. print $filter;
  439. } else {
  440. print $langs->trans("None");
  441. }
  442. }
  443. print '</div>';
  444. print '<div class="tagtd right valignmiddle">';
  445. if ($allowaddtarget) {
  446. print '<input type="submit" class="button button-add small reposition" name="button_'.$modulename.'" value="'.$langs->trans("Add").'">';
  447. } else {
  448. print '<input type="submit" class="button small disabled" disabled="disabled" name="button_'.$modulename.'" value="'.$langs->trans("Add").'">';
  449. //print $langs->trans("MailNoChangePossible");
  450. print "&nbsp;";
  451. }
  452. print '</div>';
  453. if ($allowaddtarget) {
  454. print '</form>';
  455. } else {
  456. print '</div>';
  457. }
  458. }
  459. }
  460. } // End foreach dir
  461. $parameters = array();
  462. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  463. print $hookmanager->resPrint;
  464. print '</div>'; // End table
  465. print '</div>';
  466. print '<br>';
  467. if ($sqlmessage && $user->admin) {
  468. print info_admin($langs->trans("SQLUsedForExport").':<br> '.$sqlmessage, 0, 0, 1, '', 'TechnicalInformation');
  469. print '<br>';
  470. }
  471. print '<br>';
  472. }
  473. // List of selected targets
  474. $sql = "SELECT mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut, mc.date_envoi, mc.tms,";
  475. $sql .= " mc.source_url, mc.source_id, mc.source_type, mc.error_text,";
  476. $sql .= " COUNT(mu.rowid) as nb";
  477. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc";
  478. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."mailing_unsubscribe as mu ON mu.email = mc.email";
  479. $sql .= " WHERE mc.fk_mailing=".((int) $object->id);
  480. $asearchcriteriahasbeenset = 0;
  481. if ($search_lastname) {
  482. $sql .= natural_search("mc.lastname", $search_lastname);
  483. $asearchcriteriahasbeenset++;
  484. }
  485. if ($search_firstname) {
  486. $sql .= natural_search("mc.firstname", $search_firstname);
  487. $asearchcriteriahasbeenset++;
  488. }
  489. if ($search_email) {
  490. $sql .= natural_search("mc.email", $search_email);
  491. $asearchcriteriahasbeenset++;
  492. }
  493. if ($search_other) {
  494. $sql .= natural_search("mc.other", $search_other);
  495. $asearchcriteriahasbeenset++;
  496. }
  497. if ($search_dest_status != '' && $search_dest_status >= -1) {
  498. $sql .= " AND mc.statut = ".((int) $search_dest_status);
  499. $asearchcriteriahasbeenset++;
  500. }
  501. $sql .= ' GROUP BY mc.rowid, mc.lastname, mc.firstname, mc.email, mc.other, mc.statut, mc.date_envoi, mc.tms, mc.source_url, mc.source_id, mc.source_type, mc.error_text';
  502. $sql .= $db->order($sortfield, $sortorder);
  503. // Count total nb of records
  504. $nbtotalofrecords = '';
  505. if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
  506. $result = $db->query($sql);
  507. $nbtotalofrecords = $db->num_rows($result);
  508. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  509. $page = 0;
  510. $offset = 0;
  511. }
  512. // Fix/update nbemail on emailing record if it differs (may happen if user edit lines from database directly)
  513. if (empty($asearchcriteriahasbeenset)) {
  514. if ($nbtotalofrecords != $object->nbemail) {
  515. dol_syslog("We found a difference in nb of record in target table and the property ->nbemail, we fix ->nbemail");
  516. //print "nbemail=".$object->nbemail." nbtotalofrecords=".$nbtotalofrecords;
  517. $resultrefresh = $object->refreshNbOfTargets();
  518. if ($resultrefresh < 0) {
  519. dol_print_error($db, $object->error, $object->errors);
  520. }
  521. }
  522. }
  523. }
  524. //$nbtotalofrecords=$object->nbemail; // nbemail is a denormalized field storing nb of targets
  525. $sql .= $db->plimit($limit + 1, $offset);
  526. $resql = $db->query($sql);
  527. if ($resql) {
  528. $num = $db->num_rows($resql);
  529. $param = "&id=".$object->id;
  530. //if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
  531. if ($limit > 0 && $limit != $conf->liste_limit) {
  532. $param .= '&limit='.((int) $limit);
  533. }
  534. if ($search_lastname) {
  535. $param .= "&search_lastname=".urlencode($search_lastname);
  536. }
  537. if ($search_firstname) {
  538. $param .= "&search_firstname=".urlencode($search_firstname);
  539. }
  540. if ($search_email) {
  541. $param .= "&search_email=".urlencode($search_email);
  542. }
  543. if ($search_other) {
  544. $param .= "&search_other=".urlencode($search_other);
  545. }
  546. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  547. print '<input type="hidden" name="token" value="'.newToken().'">';
  548. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  549. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  550. print '<input type="hidden" name="page" value="'.$page.'">';
  551. print '<input type="hidden" name="id" value="'.$object->id.'">';
  552. $morehtmlcenter = '';
  553. if ($allowaddtarget) {
  554. $morehtmlcenter = '<span class="opacitymedium hideonsmartphone">'.$langs->trans("ToClearAllRecipientsClickHere").'</span> <a href="'.$_SERVER["PHP_SELF"].'?clearlist=1&id='.$object->id.'" class="button reposition smallpaddingimp">'.$langs->trans("TargetsReset").'</a>';
  555. }
  556. $morehtmlcenter .= ' &nbsp; <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=exportcsv&token='.newToken().'&exportcsv=1&id='.$object->id.'">'.img_picto('', 'download', 'class="pictofixedwidth"').$langs->trans("Download").'</a>';
  557. $massactionbutton = '';
  558. print_barre_liste($langs->trans("MailSelectedRecipients"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $morehtmlcenter, $num, $nbtotalofrecords, 'generic', 0, '', '', $limit, 0, 0, 1);
  559. print '</form>';
  560. print "\n<!-- Liste destinataires selectionnes -->\n";
  561. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  562. print '<input type="hidden" name="token" value="'.newToken().'">';
  563. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  564. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  565. print '<input type="hidden" name="page" value="'.$page.'">';
  566. print '<input type="hidden" name="id" value="'.$object->id.'">';
  567. print '<input type="hidden" name="limit" value="'.$limit.'">';
  568. print '<input type="hidden" name="page_y" value="">';
  569. print '<div class="div-table-responsive">';
  570. print '<table class="noborder centpercent">';
  571. // Ligne des champs de filtres
  572. print '<tr class="liste_titre_filter">';
  573. // Action column
  574. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  575. print '<td class="liste_titre maxwidthsearch">';
  576. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  577. print $searchpicto;
  578. print '</td>';
  579. }
  580. // EMail
  581. print '<td class="liste_titre">';
  582. print '<input class="flat maxwidth75" type="text" name="search_email" value="'.dol_escape_htmltag($search_email).'">';
  583. print '</td>';
  584. // Name
  585. print '<td class="liste_titre">';
  586. print '<input class="flat maxwidth50" type="text" name="search_lastname" value="'.dol_escape_htmltag($search_lastname).'">';
  587. print '</td>';
  588. // Firstname
  589. print '<td class="liste_titre">';
  590. print '<input class="flat maxwidth50" type="text" name="search_firstname" value="'.dol_escape_htmltag($search_firstname).'">';
  591. print '</td>';
  592. // Other
  593. print '<td class="liste_titre">';
  594. print '<input class="flat maxwidth100" type="text" name="search_other" value="'.dol_escape_htmltag($search_other).'">';
  595. print '</td>';
  596. // Source
  597. print '<td class="liste_titre">';
  598. print '&nbsp;';
  599. print '</td>';
  600. // Date last update
  601. print '<td class="liste_titre">';
  602. print '&nbsp;';
  603. print '</td>';
  604. // Date sending
  605. print '<td class="liste_titre">';
  606. print '&nbsp;';
  607. print '</td>';
  608. //Statut
  609. print '<td class="liste_titre center parentonrightofpage">';
  610. print $formmailing->selectDestinariesStatus($search_dest_status, 'search_dest_status', 1, 'width100 onrightofpage');
  611. print '</td>';
  612. // Action column
  613. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  614. print '<td class="liste_titre maxwidthsearch">';
  615. $searchpicto = $form->showFilterAndCheckAddButtons($massactionbutton ? 1 : 0, 'checkforselect', 1);
  616. print $searchpicto;
  617. print '</td>';
  618. }
  619. print '</tr>';
  620. if ($page) {
  621. $param .= "&page=".urlencode($page);
  622. }
  623. print '<tr class="liste_titre">';
  624. // Action column
  625. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  626. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch ');
  627. }
  628. print_liste_field_titre("EMail", $_SERVER["PHP_SELF"], "mc.email", $param, "", "", $sortfield, $sortorder);
  629. print_liste_field_titre("Lastname", $_SERVER["PHP_SELF"], "mc.lastname", $param, "", "", $sortfield, $sortorder);
  630. print_liste_field_titre("Firstname", $_SERVER["PHP_SELF"], "mc.firstname", $param, "", "", $sortfield, $sortorder);
  631. print_liste_field_titre("OtherInformations", $_SERVER["PHP_SELF"], "", $param, "", "", $sortfield, $sortorder);
  632. print_liste_field_titre("Source", $_SERVER["PHP_SELF"], "", $param, "", '', $sortfield, $sortorder, 'center ');
  633. // Date last update
  634. print_liste_field_titre("DateLastModification", $_SERVER["PHP_SELF"], "mc.tms", $param, "", '', $sortfield, $sortorder, 'center ');
  635. // Date sending
  636. print_liste_field_titre("DateSending", $_SERVER["PHP_SELF"], "mc.date_envoi", $param, '', '', $sortfield, $sortorder, 'center ');
  637. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "mc.statut", $param, '', '', $sortfield, $sortorder, 'center ');
  638. // Action column
  639. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  640. print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'maxwidthsearch ');
  641. }
  642. print '</tr>';
  643. $i = 0;
  644. if ($num) {
  645. include_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  646. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  647. include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  648. include_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  649. include_once DOL_DOCUMENT_ROOT.'/eventorganization/class/conferenceorboothattendee.class.php';
  650. $objectstaticmember = new Adherent($db);
  651. $objectstaticuser = new User($db);
  652. $objectstaticcompany = new Societe($db);
  653. $objectstaticcontact = new Contact($db);
  654. $objectstaticeventorganization = new ConferenceOrBoothAttendee($db);
  655. while ($i < min($num, $limit)) {
  656. $obj = $db->fetch_object($resql);
  657. print '<tr class="oddeven">';
  658. // Action column
  659. if (getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  660. print '<td class="center">';
  661. print '<!-- ID mailing_cibles = '.$obj->rowid.' -->';
  662. if ($obj->statut == $object::STATUS_DRAFT) { // Not sent yet
  663. if ($user->hasRight('mailing', 'creer')) {
  664. print '<a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&rowid='.((int) $obj->rowid).$param.'">'.img_delete($langs->trans("RemoveRecipient")).'</a>';
  665. }
  666. }
  667. /*if ($obj->statut == -1) // Sent with error
  668. {
  669. print '<a href="'.$_SERVER['PHP_SELF'].'?action=retry&rowid='.$obj->rowid.$param.'">'.$langs->trans("Retry").'</a>';
  670. }*/
  671. print '</td>';
  672. }
  673. print '<td class="tdoverflowmax150">';
  674. print img_picto($obj->email, 'email', 'class="paddingright"');
  675. if ($obj->nb > 0) {
  676. print img_warning($langs->trans("EmailOptedOut"), 'warning', 'pictofixedwidth');
  677. }
  678. print dol_escape_htmltag($obj->email);
  679. print '</td>';
  680. print '<td class="tdoverflowmax150" title="'.dol_escape_htmltag($obj->lastname).'">'.dol_escape_htmltag($obj->lastname).'</td>';
  681. print '<td class="tdoverflowmax150" title="'.dol_escape_htmltag($obj->firstname).'">'.dol_escape_htmltag($obj->firstname).'</td>';
  682. print '<td><span class="small">'.dol_escape_htmltag($obj->other).'</small></td>';
  683. print '<td class="center tdoverflowmax150">';
  684. if (empty($obj->source_id) || empty($obj->source_type)) {
  685. print empty($obj->source_url) ? '' : $obj->source_url; // For backward compatibility
  686. } else {
  687. if ($obj->source_type == 'member') {
  688. $objectstaticmember->fetch($obj->source_id);
  689. print $objectstaticmember->getNomUrl(1);
  690. } elseif ($obj->source_type == 'user') {
  691. $objectstaticuser->fetch($obj->source_id);
  692. print $objectstaticuser->getNomUrl(1);
  693. } elseif ($obj->source_type == 'thirdparty') {
  694. $objectstaticcompany->fetch($obj->source_id);
  695. print $objectstaticcompany->getNomUrl(1);
  696. } elseif ($obj->source_type == 'contact') {
  697. $objectstaticcontact->fetch($obj->source_id);
  698. print $objectstaticcontact->getNomUrl(1);
  699. } elseif ($obj->source_type == 'eventorganizationattendee') {
  700. $objectstaticeventorganization->fetch($obj->source_id);
  701. print $objectstaticeventorganization->getNomUrl(1);
  702. } else {
  703. print $obj->source_url;
  704. }
  705. }
  706. print '</td>';
  707. // Date last update
  708. print '<td class="center nowraponall">';
  709. print dol_print_date(dol_stringtotime($obj->tms), 'dayhour');
  710. print '</td>';
  711. // Date sent
  712. print '<td class="center nowraponall">';
  713. if ($obj->statut != $object::STATUS_DRAFT) {
  714. // Date sent
  715. print $obj->date_envoi;
  716. }
  717. print '</td>';
  718. // Status of recipient sending email (Warning != status of emailing)
  719. print '<td class="nowrap center">';
  720. if ($obj->statut == $object::STATUS_DRAFT) {
  721. print $object::libStatutDest($obj->statut, 2, '');
  722. } else {
  723. print $object::libStatutDest($obj->statut, 2, $obj->error_text);
  724. }
  725. print '</td>';
  726. // Action column
  727. if (!getDolGlobalString('MAIN_CHECKBOX_LEFT_COLUMN')) {
  728. print '<td class="center">';
  729. print '<!-- ID mailing_cibles = '.$obj->rowid.' -->';
  730. if ($obj->statut == $object::STATUS_DRAFT) { // Not sent yet
  731. if ($user->hasRight('mailing', 'creer')) {
  732. print '<a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=delete&token='.newToken().'&rowid='.((int) $obj->rowid).$param.'">'.img_delete($langs->trans("RemoveRecipient")).'</a>';
  733. }
  734. }
  735. /*if ($obj->statut == -1) // Sent with error
  736. {
  737. print '<a href="'.$_SERVER['PHP_SELF'].'?action=retry&rowid='.$obj->rowid.$param.'">'.$langs->trans("Retry").'</a>';
  738. }*/
  739. print '</td>';
  740. }
  741. print '</tr>';
  742. $i++;
  743. }
  744. } else {
  745. if ($object->statut < $object::STATUS_SENTPARTIALY) {
  746. print '<tr><td colspan="9">';
  747. print '<span class="opacitymedium">'.$langs->trans("NoTargetYet").'</span>';
  748. print '</td></tr>';
  749. } else {
  750. print '<tr><td colspan="9">';
  751. print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
  752. print '</td></tr>';
  753. }
  754. }
  755. print "</table><br>";
  756. print '</div>';
  757. print '</form>';
  758. $db->free($resql);
  759. } else {
  760. dol_print_error($db);
  761. }
  762. print "\n<!-- Fin liste destinataires selectionnes -->\n";
  763. }
  764. // End of page
  765. llxFooter();
  766. $db->close();