dolibarrtriggers.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * Class that all the triggers must extend
  19. */
  20. abstract class DolibarrTriggers
  21. {
  22. /**
  23. * Database handler
  24. * @var DoliDB
  25. */
  26. protected $db;
  27. /**
  28. * Name of the trigger
  29. * @var mixed|string
  30. */
  31. public $name = '';
  32. /**
  33. * Description of the trigger
  34. * @var string
  35. */
  36. public $description = '';
  37. /**
  38. * Version of the trigger
  39. * @var string
  40. */
  41. public $version = self::VERSION_DEVELOPMENT;
  42. /**
  43. * Image of the trigger
  44. * @var string
  45. */
  46. public $picto = 'technic';
  47. /**
  48. * Category of the trigger
  49. * @var string
  50. */
  51. public $family = '';
  52. /**
  53. * Error reported by the trigger
  54. * @var string
  55. * @deprecated Use $this->errors
  56. * @see $errors
  57. */
  58. public $error = '';
  59. /**
  60. * Errors reported by the trigger
  61. * @var array
  62. */
  63. public $errors = array();
  64. /**
  65. * @var string module is in development
  66. */
  67. const VERSION_DEVELOPMENT = 'development';
  68. /**
  69. * @var string module is experimental
  70. */
  71. const VERSION_EXPERIMENTAL = 'experimental';
  72. /**
  73. * @var string module is dolibarr ready
  74. */
  75. const VERSION_DOLIBARR = 'dolibarr';
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. public function __construct(DoliDB $db)
  82. {
  83. $this->db = $db;
  84. if (empty($this->name)) {
  85. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  86. }
  87. }
  88. /**
  89. * Returns the name of the trigger file
  90. *
  91. * @return string
  92. */
  93. public function getName()
  94. {
  95. return $this->name;
  96. }
  97. /**
  98. * Returns the description of trigger file
  99. *
  100. * @return string
  101. */
  102. public function getDesc()
  103. {
  104. return $this->description;
  105. }
  106. /**
  107. * Returns the version of the trigger file
  108. *
  109. * @return string Version of trigger file
  110. */
  111. public function getVersion()
  112. {
  113. global $langs;
  114. $langs->load("admin");
  115. if ($this->version == self::VERSION_DEVELOPMENT) {
  116. return $langs->trans("VersionDevelopment");
  117. } elseif ($this->version == self::VERSION_EXPERIMENTAL) {
  118. return $langs->trans("VersionExperimental");
  119. } elseif ($this->version == self::VERSION_DOLIBARR) {
  120. return DOL_VERSION;
  121. } elseif ($this->version) {
  122. return $this->version;
  123. } else {
  124. return $langs->trans("Unknown");
  125. }
  126. }
  127. /**
  128. * Function called when a Dolibarrr business event is done.
  129. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  130. *
  131. * @param string $action Event action code
  132. * @param Object $object Object
  133. * @param User $user Object user
  134. * @param Translate $langs Object langs
  135. * @param conf $conf Object conf
  136. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  137. */
  138. abstract public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf);
  139. }