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

/htdocs/comm/mailing/class/mailing.class.php

https://github.com/asterix14/dolibarr
PHP | 441 lines | 295 code | 62 blank | 84 comment | 58 complexity | a0567781dea445c21d10aaa42e20e55d MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
  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 2 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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/comm/mailing/class/mailing.class.php
  21. * \ingroup mailing
  22. * \brief Fichier de la classe de gestion des mailings
  23. */
  24. require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php");
  25. /**
  26. * \class Mailing
  27. * \brief Classe permettant la gestion des mailings
  28. */
  29. class Mailing extends CommonObject
  30. {
  31. public $element='mailing';
  32. public $table_element='mailing';
  33. var $id;
  34. var $statut;
  35. var $titre;
  36. var $sujet;
  37. var $body;
  38. var $nbemail;
  39. var $bgcolor;
  40. var $bgimage;
  41. var $email_from;
  42. var $email_replyto;
  43. var $email_errorsto;
  44. var $joined_file1;
  45. var $joined_file2;
  46. var $joined_file3;
  47. var $joined_file4;
  48. var $user_creat;
  49. var $user_valid;
  50. var $date_creat;
  51. var $date_valid;
  52. /**
  53. * \brief Constructeur de la classe
  54. * \param DB handler acces base de donnees
  55. */
  56. function Mailing($DB)
  57. {
  58. $this->db = $DB ;
  59. // List of language codes for status
  60. $this->statuts[0] = 'MailingStatusDraft';
  61. $this->statuts[1] = 'MailingStatusValidated';
  62. $this->statuts[2] = 'MailingStatusSentPartialy';
  63. $this->statuts[3] = 'MailingStatusSentCompletely';
  64. }
  65. /**
  66. * \brief Create an EMailing
  67. * \param user Object of user making creation
  68. * \return -1 if error, Id of created object if OK
  69. */
  70. function create($user)
  71. {
  72. global $conf, $langs;
  73. $this->db->begin();
  74. $this->titre=trim($this->titre);
  75. $this->email_from=trim($this->email_from);
  76. if (! $this->email_from)
  77. {
  78. $this->error = $langs->trans("ErrorMailFromRequired");
  79. return -1;
  80. }
  81. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mailing";
  82. $sql .= " (date_creat, fk_user_creat, entity)";
  83. $sql .= " VALUES (".$this->db->idate(mktime()).", ".$user->id.", ".$conf->entity.")";
  84. if (! $this->titre)
  85. {
  86. $this->titre = $langs->trans("NoTitle");
  87. }
  88. dol_syslog("Mailing::Create sql=".$sql);
  89. $result=$this->db->query($sql);
  90. if ($result)
  91. {
  92. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mailing");
  93. if ($this->update($user) > 0)
  94. {
  95. $this->db->commit();
  96. }
  97. else
  98. {
  99. $this->db->rollback();
  100. return -1;
  101. }
  102. return $this->id;
  103. }
  104. else
  105. {
  106. $this->error=$this->db->lasterror();
  107. dol_syslog("Mailing::Create ".$this->error, LOG_ERR);
  108. $this->db->rollback();
  109. return -1;
  110. }
  111. }
  112. /**
  113. * \brief Update emailing record
  114. * \param user Object of user making change
  115. * \return < 0 if KO, > 0 if OK
  116. */
  117. function update($user)
  118. {
  119. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
  120. $sql .= " SET titre = '".$this->db->escape($this->titre)."'";
  121. $sql .= ", sujet = '".$this->db->escape($this->sujet)."'";
  122. $sql .= ", body = '".$this->db->escape($this->body)."'";
  123. $sql .= ", email_from = '".$this->email_from."'";
  124. $sql .= ", email_replyto = '".$this->email_replyto."'";
  125. $sql .= ", email_errorsto = '".$this->email_errorsto."'";
  126. $sql .= ", bgcolor = '".($this->bgcolor?$this->bgcolor:null)."'";
  127. $sql .= ", bgimage = '".($this->bgimage?$this->bgimage:null)."'";
  128. $sql .= " WHERE rowid = ".$this->id;
  129. dol_syslog("Mailing::Update sql=".$sql);
  130. $result=$this->db->query($sql);
  131. if ($result)
  132. {
  133. return 1;
  134. }
  135. else
  136. {
  137. $this->error=$this->db->lasterror();
  138. dol_syslog("Mailing::Update ".$this->error, LOG_ERR);
  139. return -1;
  140. }
  141. }
  142. /**
  143. * \brief Get object from database
  144. * \param rowid id du mailing
  145. * \return int
  146. */
  147. function fetch($rowid)
  148. {
  149. $sql = "SELECT m.rowid, m.titre, m.sujet, m.body, m.bgcolor, m.bgimage";
  150. $sql .= ", m.email_from, m.email_replyto, m.email_errorsto";
  151. $sql .= ", m.statut, m.nbemail";
  152. $sql .= ", m.fk_user_creat, m.fk_user_valid";
  153. $sql .= ", m.date_creat";
  154. $sql .= ", m.date_valid";
  155. $sql .= ", m.date_envoi";
  156. $sql .= " FROM ".MAIN_DB_PREFIX."mailing as m";
  157. $sql .= " WHERE m.rowid = ".$rowid;
  158. dol_syslog("Mailing.class::fetch sql=".$sql);
  159. $result=$this->db->query($sql);
  160. if ($result)
  161. {
  162. if ($this->db->num_rows($result))
  163. {
  164. $obj = $this->db->fetch_object($result);
  165. $this->id = $obj->rowid;
  166. $this->ref = $obj->rowid;
  167. $this->statut = $obj->statut;
  168. $this->nbemail = $obj->nbemail;
  169. $this->titre = $obj->titre;
  170. $this->sujet = $obj->sujet;
  171. $this->body = $obj->body;
  172. $this->bgcolor = $obj->bgcolor;
  173. $this->bgimage = $obj->bgimage;
  174. $this->email_from = $obj->email_from;
  175. $this->email_replyto = $obj->email_replyto;
  176. $this->email_errorsto = $obj->email_errorsto;
  177. $this->user_creat = $obj->fk_user_creat;
  178. $this->user_valid = $obj->fk_user_valid;
  179. $this->date_creat = $this->db->jdate($obj->date_creat);
  180. $this->date_valid = $this->db->jdate($obj->date_valid);
  181. $this->date_envoi = $this->db->jdate($obj->date_envoi);
  182. return 1;
  183. }
  184. else
  185. {
  186. dol_syslog("Mailing::Fetch Erreur -1");
  187. return -1;
  188. }
  189. }
  190. else
  191. {
  192. dol_syslog("Mailing::Fetch Erreur -2");
  193. return -2;
  194. }
  195. }
  196. /**
  197. * \brief Load an object from its id and create a new one in database
  198. * \param fromid Id of object to clone
  199. * \return int New id of clone
  200. */
  201. function createFromClone($fromid,$option1,$option2)
  202. {
  203. global $user,$langs;
  204. $error=0;
  205. $object=new Mailing($this->db);
  206. $this->db->begin();
  207. // Load source object
  208. $object->fetch($fromid);
  209. $object->id=0;
  210. $object->statut=0;
  211. // Clear fields
  212. $object->titre=$langs->trans("CopyOf").' '.$object->titre;
  213. // If no option copy content
  214. if (empty($option1))
  215. {
  216. // Clear values
  217. $object->nbemail = 0;
  218. $object->titre = $langs->trans("Draft").' '.mktime();
  219. $object->sujet = '';
  220. $object->body = '';
  221. $object->bgcolor = '';
  222. $object->bgimage = '';
  223. $object->email_from = '';
  224. $object->email_replyto = '';
  225. $object->email_errorsto = '';
  226. $object->user_creat = $user->id;
  227. $object->user_valid = '';
  228. $object->date_creat = '';
  229. $object->date_valid = '';
  230. $object->date_envoi = '';
  231. }
  232. // Create clone
  233. $result=$object->create($user);
  234. // Other options
  235. if ($result < 0)
  236. {
  237. $this->error=$object->error;
  238. $error++;
  239. }
  240. if (! $error)
  241. {
  242. }
  243. // End
  244. if (! $error)
  245. {
  246. $this->db->commit();
  247. return $object->id;
  248. }
  249. else
  250. {
  251. $this->db->rollback();
  252. return -1;
  253. }
  254. }
  255. /**
  256. * \brief Validate emailing
  257. * \param user Objet user qui valide
  258. * \return int <0 if KO, >0 if OK
  259. */
  260. function valid($user)
  261. {
  262. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing ";
  263. $sql .= " SET statut = 1, date_valid = ".$this->db->idate(gmmktime()).", fk_user_valid=".$user->id;
  264. $sql .= " WHERE rowid = ".$this->id;
  265. dol_syslog("Mailing::valid sql=".$sql, LOG_DEBUG);
  266. if ($this->db->query($sql))
  267. {
  268. return 1;
  269. }
  270. else
  271. {
  272. $this->error=$this->db->lasterror();
  273. dol_syslog("Mailing::Valid ".$this->error, LOG_ERR);
  274. return -1;
  275. }
  276. }
  277. /**
  278. * \brief Delete emailing
  279. * \param rowid id du mailing a supprimer
  280. * \return int 1 en cas de succes
  281. */
  282. function delete($rowid)
  283. {
  284. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mailing";
  285. $sql.= " WHERE rowid = ".$rowid;
  286. dol_syslog("Mailing::delete sql=".$sql, LOG_DEBUG);
  287. $resql=$this->db->query($sql);
  288. if ($resql)
  289. {
  290. return 1;
  291. }
  292. else
  293. {
  294. $this->error=$this->db->lasterror();
  295. dol_syslog("Mailing::Valid ".$this->error, LOG_ERR);
  296. return -1;
  297. }
  298. }
  299. /**
  300. * \brief Change status of each recipient
  301. * \param user Objet user qui valide
  302. * \return int <0 if KO, >0 if OK
  303. */
  304. function reset_targets_status($user)
  305. {
  306. $sql = "UPDATE ".MAIN_DB_PREFIX."mailing_cibles";
  307. $sql.= " SET statut = 0";
  308. $sql.= " WHERE fk_mailing = ".$this->id;
  309. dol_syslog("Mailing::reset_targets_status sql=".$sql, LOG_DEBUG);
  310. $resql=$this->db->query($sql);
  311. if ($resql)
  312. {
  313. return 1;
  314. }
  315. else
  316. {
  317. $this->error=$this->db->lasterror();
  318. dol_syslog("Mailing::Valid ".$this->error, LOG_ERR);
  319. return -1;
  320. }
  321. }
  322. /**
  323. * \brief Retourne le libelle du statut d'un mailing (brouillon, validee, ...
  324. * \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  325. * \return string Libelle
  326. */
  327. function getLibStatut($mode=0)
  328. {
  329. return $this->LibStatut($this->statut,$mode);
  330. }
  331. /**
  332. * \brief Renvoi le libelle d'un statut donnďż˝
  333. * \param statut Id statut
  334. * \param mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  335. * \return string Libelle du statut
  336. */
  337. function LibStatut($statut,$mode=0)
  338. {
  339. global $langs;
  340. $langs->load('mails');
  341. if ($mode == 0)
  342. {
  343. return $langs->trans($this->statuts[$statut]);
  344. }
  345. if ($mode == 1)
  346. {
  347. return $langs->trans($this->statuts[$statut]);
  348. }
  349. if ($mode == 2)
  350. {
  351. if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  352. if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]);
  353. if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
  354. if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
  355. }
  356. if ($mode == 3)
  357. {
  358. if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
  359. if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1');
  360. if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
  361. if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6');
  362. }
  363. if ($mode == 4)
  364. {
  365. if ($statut == 0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  366. if ($statut == 1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]);
  367. if ($statut == 2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
  368. if ($statut == 3) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
  369. }
  370. if ($mode == 5)
  371. {
  372. if ($statut == 0) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut0');
  373. if ($statut == 1) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut1');
  374. if ($statut == 2) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut3');
  375. if ($statut == 3) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut6');
  376. }
  377. }
  378. }
  379. ?>