mailing.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/comm/mailing/class/mailing.class.php
  21. * \ingroup mailing
  22. * \brief File of class to manage emailings module
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. /**
  26. * Class to manage emailings module
  27. */
  28. class Mailing extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element = 'mailing';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element = 'mailing';
  38. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto = 'email';
  42. /**
  43. * @var string title
  44. */
  45. public $title;
  46. /**
  47. * @var string subject
  48. */
  49. public $sujet;
  50. /**
  51. * @var string body
  52. */
  53. public $body;
  54. /**
  55. * @var int 1=Email will be sent even to email that has opt-out
  56. */
  57. public $evenunsubscribe;
  58. /**
  59. * @var int number of email
  60. */
  61. public $nbemail;
  62. /**
  63. * @var string background color
  64. */
  65. public $bgcolor;
  66. /**
  67. * @var string background image
  68. */
  69. public $bgimage;
  70. /**
  71. * @var int status
  72. */
  73. public $statut; // Status 0=Draft, 1=Validated, 2=Sent partially, 3=Sent completely
  74. /**
  75. * @var string email from
  76. */
  77. public $email_from;
  78. /**
  79. * @var string email to
  80. */
  81. public $sendto;
  82. /**
  83. * @var string email reply to
  84. */
  85. public $email_replyto;
  86. /**
  87. * @var string email errors to
  88. */
  89. public $email_errorsto;
  90. /**
  91. * @var string first joined file
  92. */
  93. public $joined_file1;
  94. /**
  95. * @var string second joined file
  96. */
  97. public $joined_file2;
  98. /**
  99. * @var string third joined file
  100. */
  101. public $joined_file3;
  102. /**
  103. * @var string fourth joined file
  104. */
  105. public $joined_file4;
  106. /**
  107. * @var int id of user create
  108. * @deprecated
  109. */
  110. public $user_creation;
  111. /**
  112. * @var int id of user create
  113. * @deprecated
  114. */
  115. public $user_creat;
  116. /**
  117. * @var int id of user validate
  118. * @deprecated
  119. */
  120. public $user_validation;
  121. /**
  122. * @var int id of user validate
  123. * @deprecated
  124. */
  125. public $user_valid;
  126. /**
  127. * @var integer|string date_creation
  128. * @deprecated
  129. */
  130. public $date_creat;
  131. /**
  132. * @var integer|string date_creation
  133. */
  134. public $date_creation;
  135. /**
  136. * @var int date validate
  137. * @deprecated
  138. */
  139. public $date_valid;
  140. /**
  141. * @var int date validate
  142. */
  143. public $date_validation;
  144. /**
  145. * @var int date sending
  146. */
  147. public $date_envoi;
  148. /**
  149. * @var array extraparams
  150. */
  151. public $extraparams = array();
  152. /**
  153. * @var array statut dest
  154. */
  155. public $statut_dest = array();
  156. /**
  157. * @var array labelStatus
  158. */
  159. public $labelStatus = array();
  160. /**
  161. * @var array substitutionarray
  162. */
  163. public $substitutionarray;
  164. /**
  165. * @var array substitutionarrayfortest
  166. */
  167. public $substitutionarrayfortest;
  168. const STATUS_DRAFT = 0;
  169. const STATUS_VALIDATED = 1;
  170. const STATUS_SENTPARTIALY = 2;
  171. const STATUS_SENTCOMPLETELY = 3;
  172. /**
  173. * Constructor
  174. *
  175. * @param DoliDb $db Database handler
  176. */
  177. public function __construct($db)
  178. {
  179. global $langs;
  180. $this->db = $db;
  181. // List of language codes for status
  182. $this->labelStatus[0] = 'MailingStatusDraft';
  183. $this->labelStatus[1] = 'MailingStatusValidated';
  184. $this->labelStatus[2] = 'MailingStatusSentPartialy';
  185. $this->labelStatus[3] = 'MailingStatusSentCompletely';
  186. $this->statut_dest[0] = 'MailingStatusNotSent';
  187. $this->statut_dest[1] = 'MailingStatusSent';
  188. $this->statut_dest[2] = 'MailingStatusRead';
  189. $this->statut_dest[3] = 'MailingStatusReadAndUnsubscribe'; // Read but ask to not be contacted anymore
  190. $this->statut_dest[-1] = 'MailingStatusError';
  191. }
  192. /**
  193. * Create an EMailing
  194. *
  195. * @param User $user Object of user making creation
  196. * @param int $notrigger Disable triggers
  197. * @return int Return integer <0 if KO, Id of created object if OK
  198. */
  199. public function create($user, $notrigger = 0)
  200. {
  201. global $conf, $langs;
  202. // Check properties
  203. if (preg_match('/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
  204. $this->error = 'InvalidHTMLStringCantBeCleaned';
  205. return -1;
  206. }
  207. $this->title = trim($this->title);
  208. $this->email_from = trim($this->email_from);
  209. if (!$this->email_from) {
  210. $this->error = $langs->trans("ErrorMailFromRequired");
  211. return -1;
  212. }
  213. $error = 0;
  214. $now = dol_now();
  215. $this->db->begin();
  216. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing";
  217. $sql .= " (date_creat, fk_user_creat, entity)";
  218. $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $user->id).", ".((int) $conf->entity).")";
  219. if (!$this->title) {
  220. $this->title = $langs->trans("NoTitle");
  221. }
  222. dol_syslog(__METHOD__, LOG_DEBUG);
  223. $resql = $this->db->query($sql);
  224. if ($resql) {
  225. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing");
  226. $result = $this->update($user, 1);
  227. if ($result < 0) {
  228. $error++;
  229. }
  230. if (!$error && !$notrigger) {
  231. // Call trigger
  232. $result = $this->call_trigger('MAILING_CREATE', $user);
  233. if ($result < 0) {
  234. $error++;
  235. }
  236. // End call triggers
  237. }
  238. if (!$error) {
  239. $this->db->commit();
  240. return $this->id;
  241. } else {
  242. $this->db->rollback();
  243. dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
  244. return -2;
  245. }
  246. } else {
  247. $this->error = $this->db->lasterror();
  248. $this->db->rollback();
  249. return -1;
  250. }
  251. }
  252. /**
  253. * Update emailing record
  254. *
  255. * @param User $user Object of user making change
  256. * @param int $notrigger Disable triggers
  257. * @return int < 0 if KO, > 0 if OK
  258. */
  259. public function update($user, $notrigger = 0)
  260. {
  261. // Check properties
  262. if (preg_match('/^InvalidHTMLStringCantBeCleaned/', $this->body)) {
  263. $this->error = 'InvalidHTMLStringCantBeCleaned';
  264. return -1;
  265. }
  266. $error = 0;
  267. $this->db->begin();
  268. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
  269. $sql .= " SET titre = '".$this->db->escape($this->title)."'";
  270. $sql .= ", sujet = '".$this->db->escape($this->sujet)."'";
  271. $sql .= ", body = '".$this->db->escape($this->body)."'";
  272. $sql .= ", email_from = '".$this->db->escape($this->email_from)."'";
  273. $sql .= ", email_replyto = '".$this->db->escape($this->email_replyto)."'";
  274. $sql .= ", email_errorsto = '".$this->db->escape($this->email_errorsto)."'";
  275. $sql .= ", bgcolor = '".($this->bgcolor ? $this->db->escape($this->bgcolor) : null)."'";
  276. $sql .= ", bgimage = '".($this->bgimage ? $this->db->escape($this->bgimage) : null)."'";
  277. $sql .= ", evenunsubscribe = ".((int) $this->evenunsubscribe);
  278. $sql .= " WHERE rowid = ".(int) $this->id;
  279. dol_syslog(__METHOD__, LOG_DEBUG);
  280. $resql = $this->db->query($sql);
  281. if ($resql) {
  282. if (!$error && !$notrigger) {
  283. // Call trigger
  284. $result = $this->call_trigger('MAILING_MODIFY', $user);
  285. if ($result < 0) {
  286. $error++;
  287. }
  288. // End call triggers
  289. }
  290. if (!$error) {
  291. dol_syslog(__METHOD__ . ' success');
  292. $this->db->commit();
  293. return 1;
  294. } else {
  295. $this->db->rollback();
  296. dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
  297. return -2;
  298. }
  299. } else {
  300. $this->error = $this->db->lasterror();
  301. $this->db->rollback();
  302. return -1;
  303. }
  304. }
  305. /**
  306. * Get object from database
  307. *
  308. * @param int $rowid Id of emailing
  309. * @param string $ref Title to search from title
  310. * @return int Return integer <0 if KO, >0 if OK
  311. */
  312. public function fetch($rowid, $ref = '')
  313. {
  314. global $conf;
  315. $sql = "SELECT m.rowid, m.titre as title, m.sujet, m.body, m.bgcolor, m.bgimage, m.evenunsubscribe";
  316. $sql .= ", m.email_from, m.email_replyto, m.email_errorsto";
  317. $sql .= ", m.statut, m.nbemail";
  318. $sql .= ", m.fk_user_creat, m.fk_user_valid";
  319. $sql .= ", m.date_creat";
  320. $sql .= ", m.date_valid";
  321. $sql .= ", m.date_envoi";
  322. $sql .= ", m.extraparams";
  323. $sql .= " FROM ".MAIN_DB_PREFIX."mailing as m";
  324. $sql .= " WHERE entity IN (".getEntity('mailing').")";
  325. if ($ref) {
  326. $sql .= " AND m.titre = '".$this->db->escape($ref)."'";
  327. } else {
  328. $sql .= " AND m.rowid = ".(int) $rowid;
  329. }
  330. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  331. $result = $this->db->query($sql);
  332. if ($result) {
  333. if ($this->db->num_rows($result)) {
  334. $obj = $this->db->fetch_object($result);
  335. $this->id = $obj->rowid;
  336. $this->ref = $obj->rowid;
  337. $this->statut = $obj->statut;
  338. $this->nbemail = $obj->nbemail;
  339. $this->title = $obj->title;
  340. $this->sujet = $obj->sujet;
  341. if (getDolGlobalString('FCKEDITOR_ENABLE_MAILING') && dol_textishtml(dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5))) {
  342. $this->body = dol_html_entity_decode($obj->body, ENT_COMPAT | ENT_HTML5);
  343. } else {
  344. $this->body = $obj->body;
  345. }
  346. $this->bgcolor = $obj->bgcolor;
  347. $this->bgimage = $obj->bgimage;
  348. $this->evenunsubscribe = $obj->evenunsubscribe;
  349. $this->email_from = $obj->email_from;
  350. $this->email_replyto = $obj->email_replyto;
  351. $this->email_errorsto = $obj->email_errorsto;
  352. $this->user_creation_id = $obj->fk_user_creat;
  353. $this->user_validation_id = $obj->fk_user_valid;
  354. $this->date_creat = $this->db->jdate($obj->date_creat);
  355. $this->date_creation = $this->db->jdate($obj->date_creat);
  356. $this->date_valid = $this->db->jdate($obj->date_valid);
  357. $this->date_validation = $this->db->jdate($obj->date_valid);
  358. $this->date_envoi = $this->db->jdate($obj->date_envoi);
  359. $this->extraparams = (array) json_decode($obj->extraparams, true);
  360. return 1;
  361. } else {
  362. dol_syslog(get_class($this)."::fetch Erreur -1");
  363. return -1;
  364. }
  365. } else {
  366. dol_syslog(get_class($this)."::fetch Erreur -2");
  367. return -2;
  368. }
  369. }
  370. /**
  371. * Load an object from its id and create a new one in database
  372. *
  373. * @param User $user User making the clone
  374. * @param int $fromid Id of object to clone
  375. * @param int $option1 1=Clone content, 0=Forget content
  376. * @param int $option2 1=Clone recipients
  377. * @return int New id of clone
  378. */
  379. public function createFromClone(User $user, $fromid, $option1, $option2)
  380. {
  381. global $langs;
  382. $error = 0;
  383. $object = new Mailing($this->db);
  384. $this->db->begin();
  385. // Load source object
  386. $object->fetch($fromid);
  387. $object->id = 0;
  388. $object->statut = 0;
  389. // Clear fields
  390. $object->title = $langs->trans("CopyOf").' '.$object->title.' '.dol_print_date(dol_now());
  391. // If no option copy content
  392. if (empty($option1)) {
  393. // Clear values
  394. $object->nbemail = 0;
  395. $object->sujet = '';
  396. $object->body = '';
  397. $object->bgcolor = '';
  398. $object->bgimage = '';
  399. $object->evenunsubscribe = 0;
  400. //$object->email_from = ''; // We do not reset from email because it is a mandatory value
  401. $object->email_replyto = '';
  402. $object->email_errorsto = '';
  403. $object->user_creation_id = $user->id;
  404. $object->user_validation_id = '';
  405. $object->date_creat = '';
  406. $object->date_valid = '';
  407. $object->date_envoi = '';
  408. }
  409. // Create clone
  410. $object->context['createfromclone'] = 'createfromclone';
  411. $result = $object->create($user);
  412. // Other options
  413. if ($result < 0) {
  414. $this->error = $object->error;
  415. $this->errors = array_merge($this->errors, $object->errors);
  416. $error++;
  417. }
  418. if (!$error) {
  419. // Clone recipient targets
  420. if (!empty($option2)) {
  421. require_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  422. $mailing_target = new MailingTargets($this->db);
  423. $target_array = array();
  424. $sql = "SELECT fk_contact,";
  425. $sql .= " lastname,";
  426. $sql .= " firstname,";
  427. $sql .= " email,";
  428. $sql .= " other,";
  429. $sql .= " source_url,";
  430. $sql .= " source_id ,";
  431. $sql .= " source_type";
  432. $sql .= " FROM ".MAIN_DB_PREFIX."mailing_cibles";
  433. $sql .= " WHERE fk_mailing = ".((int) $fromid);
  434. $result = $this->db->query($sql);
  435. if ($result) {
  436. if ($this->db->num_rows($result)) {
  437. while ($obj = $this->db->fetch_object($result)) {
  438. $target_array[] = array(
  439. 'fk_contact'=>$obj->fk_contact,
  440. 'lastname'=>$obj->lastname,
  441. 'firstname'=>$obj->firstname,
  442. 'email'=>$obj->email,
  443. 'other'=>$obj->other,
  444. 'source_url'=>$obj->source_url,
  445. 'source_id'=>$obj->source_id,
  446. 'source_type'=>$obj->source_type
  447. );
  448. }
  449. }
  450. } else {
  451. $this->error = $this->db->lasterror();
  452. return -1;
  453. }
  454. $mailing_target->addTargetsToDatabase($object->id, $target_array);
  455. }
  456. }
  457. unset($object->context['createfromclone']);
  458. // End
  459. if (!$error) {
  460. $this->db->commit();
  461. return $object->id;
  462. } else {
  463. $this->db->rollback();
  464. return -1;
  465. }
  466. }
  467. /**
  468. * Validate emailing
  469. *
  470. * @param User $user Objet user qui valide
  471. * @return int Return integer <0 if KO, >0 if OK
  472. */
  473. public function valid($user)
  474. {
  475. $now = dol_now();
  476. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
  477. $sql .= " SET statut = 1, date_valid = '".$this->db->idate($now)."', fk_user_valid=".$user->id;
  478. $sql .= " WHERE rowid = ".((int) $this->id);
  479. dol_syslog("Mailing::valid", LOG_DEBUG);
  480. if ($this->db->query($sql)) {
  481. return 1;
  482. } else {
  483. $this->error = $this->db->lasterror();
  484. return -1;
  485. }
  486. }
  487. /**
  488. * Delete emailing
  489. *
  490. * @param User $user User that delete
  491. * @param int $notrigger Disable triggers
  492. * @return int >0 if OK, <0 if KO
  493. */
  494. public function delete($user, $notrigger = 0)
  495. {
  496. $error = 0;
  497. $this->db->begin();
  498. if (!$notrigger) {
  499. $result = $this->call_trigger('MAILING_DELETE', $user);
  500. if ($result < 0) {
  501. $error++;
  502. }
  503. }
  504. if (!$error) {
  505. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing";
  506. $sql .= " WHERE rowid = " . ((int) $this->id);
  507. dol_syslog(__METHOD__, LOG_DEBUG);
  508. $resql = $this->db->query($sql);
  509. if ($resql) {
  510. $res = $this->delete_targets();
  511. if ($res <= 0) {
  512. $error++;
  513. }
  514. if (!$error) {
  515. dol_syslog(__METHOD__ . ' success');
  516. $this->db->commit();
  517. return 1;
  518. } else {
  519. $this->db->rollback();
  520. dol_syslog(__METHOD__ . ' ' . $this->error, LOG_ERR);
  521. return -2;
  522. }
  523. } else {
  524. $this->db->rollback();
  525. $this->error = $this->db->lasterror();
  526. return -1;
  527. }
  528. } else {
  529. $this->db->rollback();
  530. return -1;
  531. }
  532. }
  533. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  534. /**
  535. * Delete targets emailing
  536. *
  537. * @return int 1 if OK, 0 if error
  538. */
  539. public function delete_targets()
  540. {
  541. // phpcs:enable
  542. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing_cibles";
  543. $sql .= " WHERE fk_mailing = ".((int) $this->id);
  544. dol_syslog("Mailing::delete_targets", LOG_DEBUG);
  545. $resql = $this->db->query($sql);
  546. if ($resql) {
  547. $this->refreshNbOfTargets();
  548. return 1;
  549. } else {
  550. $this->error = $this->db->lasterror();
  551. return 0;
  552. }
  553. }
  554. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  555. /**
  556. * Change status of each recipient
  557. *
  558. * @param User $user Objet user qui valide
  559. * @return int Return integer <0 if KO, >0 if OK
  560. */
  561. public function reset_targets_status($user)
  562. {
  563. // phpcs:enable
  564. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  565. $sql .= " SET statut = 0";
  566. $sql .= " WHERE fk_mailing = ".((int) $this->id);
  567. dol_syslog("Mailing::reset_targets_status", LOG_DEBUG);
  568. $resql = $this->db->query($sql);
  569. if ($resql) {
  570. return 1;
  571. } else {
  572. $this->error = $this->db->lasterror();
  573. return -1;
  574. }
  575. }
  576. /**
  577. * Count number of target with status
  578. *
  579. * @param string $mode Mode ('alreadysent' = Sent success or error, 'alreadysentok' = Sent success, 'alreadysentko' = Sent error)
  580. * @return int Nb of target with status
  581. */
  582. public function countNbOfTargets($mode)
  583. {
  584. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
  585. $sql .= " WHERE fk_mailing = ".((int) $this->id);
  586. if ($mode == 'alreadysent') {
  587. $sql .= " AND statut <> 0";
  588. } elseif ($mode == 'alreadysentok') {
  589. $sql .= " AND statut > 0";
  590. } elseif ($mode == 'alreadysentko') {
  591. $sql .= " AND statut = -1";
  592. } else {
  593. $this->error = 'BadValueForParameterMode';
  594. return -2;
  595. }
  596. $resql = $this->db->query($sql);
  597. if ($resql) {
  598. $obj = $this->db->fetch_object($resql);
  599. if ($obj) {
  600. return $obj->nb;
  601. }
  602. } else {
  603. $this->error = $this->db->lasterror();
  604. return -1;
  605. }
  606. return 0;
  607. }
  608. /**
  609. * Refresh denormalized value ->nbemail into emailing record
  610. * Note: There is also the method update_nb into modules_mailings that is used for this.
  611. *
  612. * @return int Return integer <0 if KO, >0 if OK
  613. */
  614. public function refreshNbOfTargets()
  615. {
  616. $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."mailing_cibles";
  617. $sql .= " WHERE fk_mailing = ".((int) $this->id);
  618. $resql = $this->db->query($sql);
  619. if ($resql) {
  620. $obj = $this->db->fetch_object($resql);
  621. if ($obj) {
  622. $nbforupdate = $obj->nb;
  623. $sql = 'UPDATE '.MAIN_DB_PREFIX.'mailing SET nbemail = '.((int) $nbforupdate);
  624. $sql .= ' WHERE rowid = '.((int) $this->id);
  625. $resqlupdate = $this->db->query($sql);
  626. if (! $resqlupdate) {
  627. $this->error = $this->db->lasterror();
  628. return -1;
  629. } else {
  630. $this->nbemail = (int) $nbforupdate;
  631. }
  632. }
  633. } else {
  634. $this->error = $this->db->lasterror();
  635. return -1;
  636. }
  637. return 1;
  638. }
  639. /**
  640. * getTooltipContentArray
  641. *
  642. * @param array $params ex option, infologin
  643. * @since v18
  644. * @return array
  645. */
  646. public function getTooltipContentArray($params)
  647. {
  648. global $conf, $langs;
  649. $nofetch = !empty($params['nofetch']);
  650. $langs->load('mails');
  651. $datas = array();
  652. $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ShowEMailing").'</u>';
  653. if (isset($this->statut)) {
  654. $datas['picto'] .= ' '.$this->getLibStatut(5);
  655. }
  656. $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  657. if (isset($this->title)) {
  658. $datas['title'] .= '<br><b>'.$langs->trans('MailTitle').':</b> '.$this->title;
  659. }
  660. if (isset($this->sujet)) {
  661. $datas['subject'] .= '<br><b>'.$langs->trans('MailTopic').':</b> '.$this->sujet;
  662. }
  663. return $datas;
  664. }
  665. /**
  666. * Return a link to the object card (with optionally the picto)
  667. *
  668. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  669. * @param string $option On what the link point to ('nolink', ...)
  670. * @param int $notooltip 1=Disable tooltip
  671. * @param string $morecss Add more css on link
  672. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  673. * @return string String with URL
  674. */
  675. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  676. {
  677. global $conf, $langs, $hookmanager;
  678. if (!empty($conf->dol_no_mouse_hover)) {
  679. $notooltip = 1; // Force disable tooltips
  680. }
  681. $result = '';
  682. $params = [
  683. 'id' => $this->id,
  684. 'objecttype' => $this->element,
  685. 'option' => $option,
  686. 'nofetch' => 1,
  687. ];
  688. $classfortooltip = 'classfortooltip';
  689. $dataparams = '';
  690. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  691. $classfortooltip = 'classforajaxtooltip';
  692. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  693. $label = '';
  694. } else {
  695. $label = implode($this->getTooltipContentArray($params));
  696. }
  697. $url = DOL_URL_ROOT.'/comm/mailing/card.php?id='.$this->id;
  698. if ($option != 'nolink') {
  699. // Add param to save lastsearch_values or not
  700. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  701. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  702. $add_save_lastsearch_values = 1;
  703. }
  704. if ($add_save_lastsearch_values) {
  705. $url .= '&save_lastsearch_values=1';
  706. }
  707. }
  708. $linkclose = '';
  709. if (empty($notooltip)) {
  710. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  711. $label = $langs->trans("ShowEMailing");
  712. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  713. }
  714. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  715. $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
  716. } else {
  717. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  718. }
  719. $linkstart = '<a href="'.$url.'"';
  720. $linkstart .= $linkclose.'>';
  721. $linkend = '</a>';
  722. $result .= $linkstart;
  723. if ($withpicto) {
  724. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
  725. }
  726. if ($withpicto != 2) {
  727. $result .= $this->ref;
  728. }
  729. $result .= $linkend;
  730. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  731. global $action;
  732. $hookmanager->initHooks(array('emailingdao'));
  733. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  734. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  735. if ($reshook > 0) {
  736. $result = $hookmanager->resPrint;
  737. } else {
  738. $result .= $hookmanager->resPrint;
  739. }
  740. return $result;
  741. }
  742. /**
  743. * Return label of status of emailing (draft, validated, ...)
  744. *
  745. * @param int $mode 0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Picto+Long label, 7=Very short label+Picto
  746. * @return string Label
  747. */
  748. public function getLibStatut($mode = 0)
  749. {
  750. return $this->LibStatut($this->statut, $mode);
  751. }
  752. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  753. /**
  754. * Return the label of a given status
  755. *
  756. * @param int $status Id status
  757. * @param int $mode 0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Picto+Long label, 7=Very short label+Picto
  758. * @return string Label
  759. */
  760. public function LibStatut($status, $mode = 0)
  761. {
  762. // phpcs:enable
  763. global $langs;
  764. $langs->load("mailing");
  765. $labelStatus = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
  766. $labelStatusShort = $langs->transnoentitiesnoconv($this->labelStatus[$status]);
  767. $statusType = 'status'.$status;
  768. if ($status == 2) {
  769. $statusType = 'status3';
  770. }
  771. if ($status == 3) {
  772. $statusType = 'status6';
  773. }
  774. return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
  775. }
  776. /**
  777. * Return the label of a given status of a recipient
  778. * TODO Add class mailin_target.class.php
  779. *
  780. * @param int $status Id status
  781. * @param int $mode 0=Long label, 1=Short label, 2=Picto+Short label, 3=Picto, 4=Picto+Short label, 5=Short label+Picto, 6=Picto+Long label, 7=Very short label+Picto
  782. * @param string $desc Desc error
  783. * @return string Label
  784. */
  785. public static function libStatutDest($status, $mode = 0, $desc = '')
  786. {
  787. global $langs;
  788. $langs->load("mails");
  789. $labelStatus = array();
  790. $labelStatusShort = array();
  791. $labelStatus[-1] = $langs->transnoentitiesnoconv('MailingStatusError');
  792. $labelStatus[0] = $langs->transnoentitiesnoconv('MailingStatusNotSent');
  793. $labelStatus[1] = $langs->transnoentitiesnoconv('MailingStatusSent');
  794. $labelStatus[2] = $langs->transnoentitiesnoconv('MailingStatusRead');
  795. $labelStatus[3] = $langs->transnoentitiesnoconv('MailingStatusNotContact');
  796. $labelStatusShort[-1] = $langs->transnoentitiesnoconv('MailingStatusError');
  797. $labelStatusShort[0] = $langs->transnoentitiesnoconv('MailingStatusNotSent');
  798. $labelStatusShort[1] = $langs->transnoentitiesnoconv('MailingStatusSent');
  799. $labelStatusShort[2] = $langs->transnoentitiesnoconv('MailingStatusRead');
  800. $labelStatusShort[3] = $langs->transnoentitiesnoconv('MailingStatusNotContact');
  801. $statusType = 'status'.$status;
  802. if ($status == -1) {
  803. $statusType = 'status8';
  804. }
  805. if ($status == 1) {
  806. $statusType = 'status6';
  807. }
  808. if ($status == 2) {
  809. $statusType = 'status4';
  810. }
  811. $param = array();
  812. if ($status == - 1) {
  813. $param = array('badgeParams'=>array('attr'=>array('title'=>$desc)));
  814. }
  815. return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode, '', $param);
  816. }
  817. }