interface_50_modNotification_Notification.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/triggers/interface_50_modNotification_Notification.class.php
  22. * \ingroup notification
  23. * \brief File of class of triggers for notification module
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
  27. /**
  28. * Class of triggers for notification module
  29. */
  30. class InterfaceNotification extends DolibarrTriggers
  31. {
  32. public $listofmanagedevents = array();
  33. /**
  34. * Constructor
  35. *
  36. * @param DoliDB $db Database handler
  37. */
  38. public function __construct($db)
  39. {
  40. $this->db = $db;
  41. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  42. $this->family = "notification";
  43. $this->description = "Triggers of this module send Email notifications according to Notification module setup.";
  44. // 'development', 'experimental', 'dolibarr' or version
  45. $this->version = self::VERSION_DOLIBARR;
  46. $this->picto = 'email';
  47. $this->listofmanagedevents = Notify::$arrayofnotifsupported;
  48. }
  49. /**
  50. * Function called when a Dolibarrr business event is done.
  51. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  52. *
  53. * @param string $action Event action code
  54. * @param Object $object Object
  55. * @param User $user Object user
  56. * @param Translate $langs Object langs
  57. * @param conf $conf Object conf
  58. * @return int Return integer <0 if KO, 0 if no triggered ran, >0 if OK
  59. */
  60. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  61. {
  62. global $hookmanager;
  63. if (empty($conf->notification) || !isModEnabled('notification')) {
  64. return 0; // Module not active, we do nothing
  65. }
  66. if (!is_object($hookmanager)) {
  67. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  68. $hookmanager = new HookManager($this->db);
  69. }
  70. $hookmanager->initHooks(array('notification'));
  71. $parameters = array();
  72. $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
  73. if (empty($reshook)) {
  74. if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
  75. $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
  76. }
  77. }
  78. // If the trigger code is not managed by the Notification module
  79. if (!in_array($action, $this->listofmanagedevents)) {
  80. return 0;
  81. }
  82. dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
  83. $notify = new Notify($this->db);
  84. $notify->send($action, $object);
  85. return 1;
  86. }
  87. /**
  88. * Return list of events managed by notification module
  89. *
  90. * @return array Array of events managed by notification module
  91. */
  92. public function getListOfManagedEvents()
  93. {
  94. global $conf, $action;
  95. global $hookmanager;
  96. if (!is_object($hookmanager)) {
  97. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  98. $hookmanager = new HookManager($this->db);
  99. }
  100. $hookmanager->initHooks(array('notification'));
  101. $parameters = array();
  102. $object = new stdClass();
  103. $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
  104. if (empty($reshook)) {
  105. if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
  106. $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
  107. }
  108. }
  109. $ret = array();
  110. $sql = "SELECT rowid, code, contexts, label, description, elementtype";
  111. $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
  112. $sql .= $this->db->order("rang, elementtype, code");
  113. dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
  114. $resql = $this->db->query($sql);
  115. if ($resql) {
  116. $num = $this->db->num_rows($resql);
  117. $i = 0;
  118. while ($i < $num) {
  119. $obj = $this->db->fetch_object($resql);
  120. $qualified = 0;
  121. // Check is this event is supported by notification module
  122. if (in_array($obj->code, $this->listofmanagedevents)) {
  123. $qualified = 1;
  124. }
  125. // Check if module for this event is active
  126. if ($qualified) {
  127. //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
  128. $element = $obj->elementtype;
  129. // Exclude events if related module is disabled
  130. if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
  131. $qualified = 0;
  132. } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
  133. $qualified = 0;
  134. } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
  135. $qualified = 0;
  136. } elseif ($element == 'shipping' && !isModEnabled('expedition')) {
  137. $qualified = 0;
  138. } elseif ($element == 'member' && !isModEnabled('adherent')) {
  139. $qualified = 0;
  140. } elseif (($element == 'expense_report' || $element == 'expensereport') && !isModEnabled('expensereport')) {
  141. $qualified = 0;
  142. } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
  143. $qualified = 0;
  144. }
  145. }
  146. if ($qualified) {
  147. $ret[] = array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'contexts'=>$obj->contexts, 'label'=>$obj->label, 'description'=>$obj->description, 'elementtype'=>$obj->elementtype);
  148. }
  149. $i++;
  150. }
  151. } else {
  152. dol_print_error($this->db);
  153. }
  154. return $ret;
  155. }
  156. }