PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/triggers/interface_50_modNotification_Notification.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 261 lines | 151 code | 39 blank | 71 comment | 42 complexity | 94f89b4219a5a1eddfb1ebe77ef7583b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011 Regis Houssin <regis.houssin@capnetworks.com>
  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 3 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/core/triggers/interface_50_modNotification_Notification.class.php
  20. * \ingroup notification
  21. * \brief File of class of triggers for notification module
  22. */
  23. /**
  24. * Class of triggers for notification module
  25. */
  26. class InterfaceNotification
  27. {
  28. var $db;
  29. var $listofmanagedevents=array('BILL_VALIDATE','ORDER_VALIDATE','PROPAL_VALIDATE',
  30. 'FICHEINTER_VALIDATE','ORDER_SUPPLIER_APPROVE','ORDER_SUPPLIER_REFUSE');
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function __construct($db = '')
  37. {
  38. $this->db = $db;
  39. $this->name = preg_replace('/^Interface/i','',get_class($this));
  40. $this->family = "notification";
  41. $this->description = "Triggers of this module send email notifications according to Notification module setup.";
  42. $this->version = 'dolibarr'; // 'experimental' or 'dolibarr' or version
  43. $this->picto = 'email';
  44. }
  45. /**
  46. * Return name of trigger file
  47. *
  48. * @return string Name of trigger file
  49. */
  50. function getName()
  51. {
  52. return $this->name;
  53. }
  54. /**
  55. * Return description of trigger file
  56. *
  57. * @return string Description of trigger file
  58. */
  59. function getDesc()
  60. {
  61. return $this->description;
  62. }
  63. /**
  64. * Return version of trigger file
  65. *
  66. * @return string Version of trigger file
  67. */
  68. function getVersion()
  69. {
  70. global $langs;
  71. $langs->load("admin");
  72. if ($this->version == 'experimental') return $langs->trans("Experimental");
  73. elseif ($this->version == 'dolibarr') return DOL_VERSION;
  74. elseif ($this->version) return $this->version;
  75. else return $langs->trans("Unknown");
  76. }
  77. /**
  78. * Function called when a Dolibarrr business event is done.
  79. * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
  80. *
  81. * @param string $action Event action code
  82. * @param Object $object Object
  83. * @param User $user Object user
  84. * @param Translate $langs Object langs
  85. * @param conf $conf Object conf
  86. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  87. */
  88. function run_trigger($action,$object,$user,$langs,$conf)
  89. {
  90. if (empty($conf->notification->enabled)) return 0; // Module not active, we do nothing
  91. require_once DOL_DOCUMENT_ROOT .'/core/class/notify.class.php';
  92. if ($action == 'BILL_VALIDATE')
  93. {
  94. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  95. $ref = dol_sanitizeFileName($object->ref);
  96. $filepdf = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  97. if (! file_exists($filepdf)) $filepdf='';
  98. $filepdf=''; // We can't add PDF as it is not generated yet.
  99. $langs->load("other");
  100. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$object->ref);
  101. $notify = new Notify($this->db);
  102. $notify->send($action, $object->socid, $mesg, 'facture', $object->id, $filepdf);
  103. }
  104. elseif ($action == 'ORDER_VALIDATE')
  105. {
  106. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  107. $ref = dol_sanitizeFileName($object->ref);
  108. $filepdf = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  109. if (! file_exists($filepdf)) $filepdf='';
  110. $filepdf=''; // We can't add PDF as it is not generated yet.
  111. $langs->load("other");
  112. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$object->ref);
  113. $notify = new Notify($this->db);
  114. $notify->send($action, $object->socid, $mesg, 'order', $object->id, $filepdf);
  115. }
  116. elseif ($action == 'PROPAL_VALIDATE')
  117. {
  118. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  119. $ref = dol_sanitizeFileName($object->ref);
  120. $filepdf = $conf->propal->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  121. if (! file_exists($filepdf)) $filepdf='';
  122. $filepdf=''; // We can't add PDF as it is not generated yet.
  123. $langs->load("other");
  124. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$object->ref);
  125. $notify = new Notify($this->db);
  126. $notify->send($action, $object->socid, $mesg, 'propal', $object->id, $filepdf);
  127. }
  128. elseif ($action == 'FICHEINTER_VALIDATE')
  129. {
  130. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  131. $ref = dol_sanitizeFileName($object->ref);
  132. $filepdf = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  133. if (! file_exists($filepdf)) $filepdf='';
  134. $filepdf=''; // We can't add PDF as it is not generated yet.
  135. $langs->load("other");
  136. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$object->ref);
  137. $notify = new Notify($this->db);
  138. $notify->send($action, $object->socid, $mesg, 'ficheinter', $object->id, $filepdf);
  139. }
  140. elseif ($action == 'ORDER_SUPPLIER_APPROVE')
  141. {
  142. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  143. $ref = dol_sanitizeFileName($object->ref);
  144. $filepdf = $conf->fournisseur->dir_output . '/commande/' . $ref . '/' . $ref . '.pdf';
  145. if (! file_exists($filepdf)) $filepdf='';
  146. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  147. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$object->ref,$user->getFullName($langs));
  148. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  149. $notify = new Notify($this->db);
  150. $notify->send($action, $object->socid, $mesg, 'order_supplier', $object->id, $filepdf);
  151. }
  152. elseif ($action == 'ORDER_SUPPLIER_REFUSE')
  153. {
  154. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  155. $ref = dol_sanitizeFileName($object->ref);
  156. $filepdf = $conf->fournisseur->dir_output . '/commande/' . $ref . '/' . $ref . '.pdf';
  157. if (! file_exists($filepdf)) $filepdf='';
  158. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  159. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$object->ref,$user->getFullName($langs));
  160. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  161. $notify = new Notify($this->db);
  162. $notify->send($action, $object->socid, $mesg, 'order_supplier', $object->id, $filepdf);
  163. }
  164. // If not found
  165. /*
  166. else
  167. {
  168. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  169. return -1;
  170. }
  171. */
  172. return 0;
  173. }
  174. /**
  175. * Return list of events managed by notification module
  176. *
  177. * @return array Array of events managed by notification module
  178. */
  179. function getListOfManagedEvents()
  180. {
  181. global $conf,$langs;
  182. $ret=array();
  183. $sql = "SELECT rowid, code, label, description, elementtype";
  184. $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
  185. $sql.= $this->db->order("elementtype, code");
  186. dol_syslog("Get list of notifications sql=".$sql);
  187. $resql=$this->db->query($sql);
  188. if ($resql)
  189. {
  190. $num=$this->db->num_rows($resql);
  191. $i=0;
  192. while ($i < $num)
  193. {
  194. $obj=$this->db->fetch_object($resql);
  195. $qualified=0;
  196. // Check is this event is supported by notification module
  197. if (in_array($obj->code,$this->listofmanagedevents)) $qualified=1;
  198. // Check if module for this event is active
  199. if ($qualified)
  200. {
  201. //print 'xx'.$obj->code;
  202. $element=$obj->elementtype;
  203. if ($element == 'order_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  204. elseif ($element == 'invoice_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  205. elseif ($element == 'withdraw' && empty($conf->prelevement->enabled)) $qualified=0;
  206. elseif ($element == 'shipping' && empty($conf->expedition->enabled)) $qualified=0;
  207. elseif ($element == 'member' && empty($conf->adherent->enabled)) $qualified=0;
  208. elseif (! in_array($element,array('order_supplier','invoice_supplier','withdraw','shipping','member'))
  209. && empty($conf->$element->enabled)) $qualified=0;
  210. }
  211. if ($qualified)
  212. {
  213. $ret[]=array('rowid'=>$obj->rowid,'code'=>$obj->code,'label'=>$obj->label,'description'=>$obj->description,'elementtype'=>$obj->elementtype);
  214. }
  215. $i++;
  216. }
  217. }
  218. else dol_print_error($this->db);
  219. return $ret;
  220. }
  221. }
  222. ?>