PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/triggers/interface_modNotification_Notification.class.php

https://github.com/asterix14/dolibarr
PHP | 256 lines | 151 code | 39 blank | 66 comment | 42 complexity | 54fb09226e951a997d43bfda8f69273e MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011 Regis Houssin <regis@dolibarr.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/triggers/interface_modNotification_Notification.class.php
  20. * \ingroup notification
  21. * \brief File of class of triggers for notification module
  22. */
  23. /**
  24. * \class InterfaceNotification
  25. * \brief Class of triggers for notification module
  26. */
  27. class InterfaceNotification
  28. {
  29. var $db;
  30. var $listofmanagedevents=array('BILL_VALIDATE','ORDER_VALIDATE','PROPAL_VALIDATE',
  31. 'FICHEINTER_VALIDATE','ORDER_SUPPLIER_APPROVE','ORDER_SUPPLIER_REFUSE');
  32. /**
  33. * Constructor.
  34. * @param DB Database handler
  35. */
  36. function InterfaceNotification($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. * @return string Name of trigger file
  48. */
  49. function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * Return description of trigger file
  55. * @return string Description of trigger file
  56. */
  57. function getDesc()
  58. {
  59. return $this->description;
  60. }
  61. /**
  62. * Return version of trigger file
  63. * @return string Version of trigger file
  64. */
  65. function getVersion()
  66. {
  67. global $langs;
  68. $langs->load("admin");
  69. if ($this->version == 'experimental') return $langs->trans("Experimental");
  70. elseif ($this->version == 'dolibarr') return DOL_VERSION;
  71. elseif ($this->version) return $this->version;
  72. else return $langs->trans("Unknown");
  73. }
  74. /**
  75. * Function called when a Dolibarrr business event is done.
  76. * All functions "run_trigger" are triggered if file is inside directory htdocs/core/triggers
  77. * @param action Event code (COMPANY_CREATE, PROPAL_VALIDATE, ...)
  78. * @param object Object action is done on
  79. * @param user Object user
  80. * @param langs Object langs
  81. * @param conf Object conf
  82. * @return int <0 if KO, 0 if no action are done, >0 if OK
  83. */
  84. function run_trigger($action,$object,$user,$langs,$conf)
  85. {
  86. if (empty($conf->notification->enabled)) return 0; // Module not active, we do nothing
  87. require_once(DOL_DOCUMENT_ROOT .'/core/class/notify.class.php');
  88. if ($action == 'BILL_VALIDATE')
  89. {
  90. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  91. $ref = dol_sanitizeFileName($object->ref);
  92. $filepdf = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  93. if (! file_exists($filepdf)) $filepdf='';
  94. $filepdf=''; // We can't add PDF as it is not generated yet.
  95. $langs->load("other");
  96. $mesg = $langs->transnoentitiesnoconv("EMailTextInvoiceValidated",$object->ref);
  97. $notify = new Notify($this->db);
  98. $notify->send($action, $object->socid, $mesg, 'facture', $object->id, $filepdf);
  99. }
  100. elseif ($action == 'ORDER_VALIDATE')
  101. {
  102. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  103. $ref = dol_sanitizeFileName($object->ref);
  104. $filepdf = $conf->commande->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  105. if (! file_exists($filepdf)) $filepdf='';
  106. $filepdf=''; // We can't add PDF as it is not generated yet.
  107. $langs->load("other");
  108. $mesg = $langs->transnoentitiesnoconv("EMailTextOrderValidated",$object->ref);
  109. $notify = new Notify($this->db);
  110. $notify->send($action, $object->socid, $mesg, 'order', $object->id, $filepdf);
  111. }
  112. elseif ($action == 'PROPAL_VALIDATE')
  113. {
  114. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  115. $ref = dol_sanitizeFileName($object->ref);
  116. $filepdf = $conf->propale->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  117. if (! file_exists($filepdf)) $filepdf='';
  118. $filepdf=''; // We can't add PDF as it is not generated yet.
  119. $langs->load("other");
  120. $mesg = $langs->transnoentitiesnoconv("EMailTextProposalValidated",$object->ref);
  121. $notify = new Notify($this->db);
  122. $notify->send($action, $object->socid, $mesg, 'propal', $object->id, $filepdf);
  123. }
  124. elseif ($action == 'FICHEINTER_VALIDATE')
  125. {
  126. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  127. $ref = dol_sanitizeFileName($object->ref);
  128. $filepdf = $conf->facture->dir_output . '/' . $ref . '/' . $ref . '.pdf';
  129. if (! file_exists($filepdf)) $filepdf='';
  130. $filepdf=''; // We can't add PDF as it is not generated yet.
  131. $langs->load("other");
  132. $mesg = $langs->transnoentitiesnoconv("EMailTextInterventionValidated",$object->ref);
  133. $notify = new Notify($this->db);
  134. $notify->send($action, $object->socid, $mesg, 'ficheinter', $object->id, $filepdf);
  135. }
  136. elseif ($action == 'ORDER_SUPPLIER_APPROVE')
  137. {
  138. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  139. $ref = dol_sanitizeFileName($object->ref);
  140. $filepdf = $conf->fournisseur->dir_output . '/commande/' . $ref . '/' . $ref . '.pdf';
  141. if (! file_exists($filepdf)) $filepdf='';
  142. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  143. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderApprovedBy",$object->ref,$user->getFullName($langs));
  144. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  145. $notify = new Notify($this->db);
  146. $notify->send($action, $object->socid, $mesg, 'order_supplier', $object->id, $filepdf);
  147. }
  148. elseif ($action == 'ORDER_SUPPLIER_REFUSE')
  149. {
  150. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  151. $ref = dol_sanitizeFileName($object->ref);
  152. $filepdf = $conf->fournisseur->dir_output . '/commande/' . $ref . '/' . $ref . '.pdf';
  153. if (! file_exists($filepdf)) $filepdf='';
  154. $mesg = $langs->transnoentitiesnoconv("Hello").",\n\n";
  155. $mesg.= $langs->transnoentitiesnoconv("EMailTextOrderRefusedBy",$object->ref,$user->getFullName($langs));
  156. $mesg.= "\n\n".$langs->transnoentitiesnoconv("Sincerely").".\n\n";
  157. $notify = new Notify($this->db);
  158. $notify->send($action, $object->socid, $mesg, 'order_supplier', $object->id, $filepdf);
  159. }
  160. // If not found
  161. /*
  162. else
  163. {
  164. dol_syslog("Trigger '".$this->name."' for action '$action' was ran by ".__FILE__." but no handler found for this action.");
  165. return -1;
  166. }
  167. */
  168. return 0;
  169. }
  170. /**
  171. * Return list of events managed by notification module
  172. * @return array Array of events managed by notification module
  173. */
  174. function getListOfManagedEvents()
  175. {
  176. global $conf,$langs;
  177. $ret=array();
  178. $sql = "SELECT rowid, code, label, description, elementtype";
  179. $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
  180. $sql.= $this->db->order("elementtype, code");
  181. dol_syslog("Get list of notifications sql=".$sql);
  182. $resql=$this->db->query($sql);
  183. if ($resql)
  184. {
  185. $num=$this->db->num_rows($resql);
  186. $i=0;
  187. while ($i < $num)
  188. {
  189. $obj=$this->db->fetch_object($resql);
  190. $qualified=0;
  191. // Check is this event is supported by notification module
  192. if (in_array($obj->code,$this->listofmanagedevents)) $qualified=1;
  193. // Check if module for this event is active
  194. if ($qualified)
  195. {
  196. //print 'xx'.$obj->code;
  197. $element=$obj->elementtype;
  198. if ($element == 'order_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  199. elseif ($element == 'invoice_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  200. elseif ($element == 'withdraw' && empty($conf->prelevement->enabled)) $qualified=0;
  201. elseif ($element == 'shipping' && empty($conf->expedition->enabled)) $qualified=0;
  202. elseif ($element == 'member' && empty($conf->adherent->enabled)) $qualified=0;
  203. elseif (! in_array($element,array('order_supplier','invoice_supplier','withdraw','shipping','member'))
  204. && empty($conf->$element->enabled)) $qualified=0;
  205. }
  206. if ($qualified)
  207. {
  208. $ret[]=array('rowid'=>$obj->rowid,'code'=>$obj->code,'label'=>$obj->label,'description'=>$obj->description,'elementtype'=>$obj->elementtype);
  209. }
  210. $i++;
  211. }
  212. }
  213. else dol_print_error($this->db);
  214. return $ret;
  215. }
  216. }
  217. ?>