interface_50_modNotification_Notification.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /* Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2013-2014 Marcos García <marcosgdf@gmail.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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/triggers/interface_50_modNotification_Notification.class.php
  21. * \ingroup notification
  22. * \brief File of class of triggers for notification module
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  25. /**
  26. * Class of triggers for notification module
  27. */
  28. class InterfaceNotification extends DolibarrTriggers
  29. {
  30. public $family = 'notification';
  31. public $description = "Triggers of this module send email notifications according to Notification module setup.";
  32. /**
  33. * Version of the trigger
  34. * @var string
  35. */
  36. public $version = self::VERSION_DOLIBARR;
  37. /**
  38. * @var string Image of the trigger
  39. */
  40. public $picto = 'email';
  41. // @TODO Defined also into notify.class.php)
  42. public $listofmanagedevents=array(
  43. 'BILL_VALIDATE',
  44. 'BILL_PAYED',
  45. 'ORDER_VALIDATE',
  46. 'PROPAL_VALIDATE',
  47. 'PROPAL_CLOSE_SIGNED',
  48. 'FICHINTER_VALIDATE',
  49. 'FICHINTER_ADD_CONTACT',
  50. 'ORDER_SUPPLIER_VALIDATE',
  51. 'ORDER_SUPPLIER_APPROVE',
  52. 'ORDER_SUPPLIER_REFUSE',
  53. 'SHIPPING_VALIDATE',
  54. 'EXPENSE_REPORT_VALIDATE',
  55. 'EXPENSE_REPORT_APPROVE',
  56. 'HOLIDAY_VALIDATE',
  57. 'HOLIDAY_APPROVE'
  58. );
  59. /**
  60. * Function called when a Dolibarrr business event is done.
  61. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  62. *
  63. * @param string $action Event action code
  64. * @param Object $object Object
  65. * @param User $user Object user
  66. * @param Translate $langs Object langs
  67. * @param conf $conf Object conf
  68. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  69. */
  70. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  71. {
  72. if (empty($conf->notification->enabled)) return 0; // Module not active, we do nothing
  73. require_once DOL_DOCUMENT_ROOT .'/core/class/notify.class.php';
  74. $notify = new Notify($this->db);
  75. if (! in_array($action, $notify->arrayofnotifsupported)) return 0;
  76. dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
  77. $notify->send($action, $object);
  78. return 1;
  79. }
  80. /**
  81. * Return list of events managed by notification module
  82. *
  83. * @return array Array of events managed by notification module
  84. */
  85. function getListOfManagedEvents()
  86. {
  87. global $conf;
  88. $ret=array();
  89. $sql = "SELECT rowid, code, label, description, elementtype";
  90. $sql.= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
  91. $sql.= $this->db->order("rang, elementtype, code");
  92. dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
  93. $resql=$this->db->query($sql);
  94. if ($resql)
  95. {
  96. $num=$this->db->num_rows($resql);
  97. $i=0;
  98. while ($i < $num)
  99. {
  100. $obj=$this->db->fetch_object($resql);
  101. $qualified=0;
  102. // Check is this event is supported by notification module
  103. if (in_array($obj->code, $this->listofmanagedevents)) $qualified=1;
  104. // Check if module for this event is active
  105. if ($qualified)
  106. {
  107. //print 'xx'.$obj->code;
  108. $element=$obj->elementtype;
  109. // Exclude events if related module is disabled
  110. if ($element == 'order_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  111. elseif ($element == 'invoice_supplier' && empty($conf->fournisseur->enabled)) $qualified=0;
  112. elseif ($element == 'withdraw' && empty($conf->prelevement->enabled)) $qualified=0;
  113. elseif ($element == 'shipping' && empty($conf->expedition->enabled)) $qualified=0;
  114. elseif ($element == 'member' && empty($conf->adherent->enabled)) $qualified=0;
  115. elseif (! in_array($element,array('order_supplier','invoice_supplier','withdraw','shipping','member','expensereport')) && empty($conf->$element->enabled)) $qualified=0;
  116. }
  117. if ($qualified)
  118. {
  119. $ret[]=array('rowid'=>$obj->rowid,'code'=>$obj->code,'label'=>$obj->label,'description'=>$obj->description,'elementtype'=>$obj->elementtype);
  120. }
  121. $i++;
  122. }
  123. }
  124. else dol_print_error($this->db);
  125. return $ret;
  126. }
  127. }