interface_50_modNotification_Notification.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <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. if (empty($conf->notification) || !isModEnabled('notification')) {
  63. return 0; // Module not active, we do nothing
  64. }
  65. // If the trigger code is not managed by the Notification module
  66. if (!in_array($action, $this->listofmanagedevents)) {
  67. return 0;
  68. }
  69. dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
  70. $notify = new Notify($this->db);
  71. $notify->send($action, $object);
  72. return 1;
  73. }
  74. /**
  75. * Return list of events managed by notification module
  76. *
  77. * @return array Array of events managed by notification module
  78. */
  79. public function getListOfManagedEvents()
  80. {
  81. global $conf, $action;
  82. global $hookmanager;
  83. if (!is_object($hookmanager)) {
  84. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  85. $hookmanager = new HookManager($this->db);
  86. }
  87. $hookmanager->initHooks(array('notification'));
  88. $parameters = array();
  89. $object = new stdClass();
  90. $reshook = $hookmanager->executeHooks('notifsupported', $parameters, $object, $action);
  91. if (empty($reshook)) {
  92. if (!empty($hookmanager->resArray['arrayofnotifsupported'])) {
  93. $this->listofmanagedevents = array_merge($this->listofmanagedevents, $hookmanager->resArray['arrayofnotifsupported']);
  94. }
  95. }
  96. $ret = array();
  97. $sql = "SELECT rowid, code, contexts, label, description, elementtype";
  98. $sql .= " FROM ".MAIN_DB_PREFIX."c_action_trigger";
  99. $sql .= $this->db->order("rang, elementtype, code");
  100. dol_syslog("getListOfManagedEvents Get list of notifications", LOG_DEBUG);
  101. $resql = $this->db->query($sql);
  102. if ($resql) {
  103. $num = $this->db->num_rows($resql);
  104. $i = 0;
  105. while ($i < $num) {
  106. $obj = $this->db->fetch_object($resql);
  107. $qualified = 0;
  108. // Check is this event is supported by notification module
  109. if (in_array($obj->code, $this->listofmanagedevents)) {
  110. $qualified = 1;
  111. }
  112. // Check if module for this event is active
  113. if ($qualified) {
  114. //print 'xx'.$obj->code.' '.$obj->elementtype.'<br>';
  115. $element = $obj->elementtype;
  116. // Exclude events if related module is disabled
  117. if ($element == 'order_supplier' && !isModEnabled('supplier_order')) {
  118. $qualified = 0;
  119. } elseif ($element == 'invoice_supplier' && !isModEnabled('supplier_invoice')) {
  120. $qualified = 0;
  121. } elseif ($element == 'withdraw' && !isModEnabled('prelevement')) {
  122. $qualified = 0;
  123. } elseif ($element == 'shipping' && !isModEnabled('expedition')) {
  124. $qualified = 0;
  125. } elseif ($element == 'member' && !isModEnabled('adherent')) {
  126. $qualified = 0;
  127. } elseif (($element == 'expense_report' || $element == 'expensereport') && empty($conf->expensereport->enabled)) {
  128. $qualified = 0;
  129. } elseif (!in_array($element, array('order_supplier', 'invoice_supplier', 'withdraw', 'shipping', 'member', 'expense_report', 'expensereport')) && empty($conf->$element->enabled)) {
  130. $qualified = 0;
  131. }
  132. }
  133. if ($qualified) {
  134. $ret[] = array('rowid'=>$obj->rowid, 'code'=>$obj->code, 'contexts'=>$obj->contexts, 'label'=>$obj->label, 'description'=>$obj->description, 'elementtype'=>$obj->elementtype);
  135. }
  136. $i++;
  137. }
  138. } else {
  139. dol_print_error($this->db);
  140. }
  141. return $ret;
  142. }
  143. }