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