dolibarrtriggers.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <http://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. const VERSION_DEVELOPMENT = 'development';
  65. const VERSION_EXPERIMENTAL = 'experimental';
  66. const VERSION_DOLIBARR = 'dolibarr';
  67. /**
  68. * Constructor
  69. *
  70. * @param DoliDB $db Database handler
  71. */
  72. public function __construct(DoliDB $db) {
  73. $this->db = $db;
  74. if (empty($this->name))
  75. {
  76. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  77. }
  78. }
  79. /**
  80. * Returns the name of the trigger file
  81. *
  82. * @return string
  83. */
  84. public function getName()
  85. {
  86. return $this->name;
  87. }
  88. /**
  89. * Returns the description of trigger file
  90. *
  91. * @return string
  92. */
  93. public function getDesc()
  94. {
  95. return $this->description;
  96. }
  97. /**
  98. * Returns the version of the trigger file
  99. *
  100. * @return string Version of trigger file
  101. */
  102. public function getVersion()
  103. {
  104. global $langs;
  105. $langs->load("admin");
  106. if ($this->version == self::VERSION_DEVELOPMENT) {
  107. return $langs->trans("VersionDevelopment");
  108. } elseif ($this->version == self::VERSION_EXPERIMENTAL) {
  109. return $langs->trans("VersionExperimental");
  110. } elseif ($this->version == self::VERSION_DOLIBARR) {
  111. return DOL_VERSION;
  112. } elseif ($this->version) {
  113. return $this->version;
  114. } else {
  115. return $langs->trans("Unknown");
  116. }
  117. }
  118. /**
  119. * Function called when a Dolibarrr business event is done.
  120. * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared)
  121. *
  122. * @param string $action Event action code
  123. * @param Object $object Object
  124. * @param User $user Object user
  125. * @param Translate $langs Object langs
  126. * @param conf $conf Object conf
  127. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  128. */
  129. abstract function runTrigger($action, $object, User $user, Translate $langs, Conf $conf);
  130. }