actions_mymodule.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /* Copyright (C) ---Put here your own copyright and developer email---
  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. * \file htdocs/modulebuilder/template/class/actions_mymodule.class.php
  19. * \ingroup mymodule
  20. * \brief Example hook overload.
  21. *
  22. * Put detailed description here.
  23. */
  24. /**
  25. * Class ActionsMyModule
  26. */
  27. class ActionsMyModule
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. /**
  38. * @var array Errors
  39. */
  40. public $errors = array();
  41. /**
  42. * @var array Hook results. Propagated to $hookmanager->resArray for later reuse
  43. */
  44. public $results = array();
  45. /**
  46. * @var string String displayed by executeHook() immediately after return
  47. */
  48. public $resprints;
  49. /**
  50. * @var int Priority of hook (50 is used if value is not defined)
  51. */
  52. public $priority;
  53. /**
  54. * Constructor
  55. *
  56. * @param DoliDB $db Database handler
  57. */
  58. public function __construct($db)
  59. {
  60. $this->db = $db;
  61. }
  62. /**
  63. * Execute action
  64. *
  65. * @param array $parameters Array of parameters
  66. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  67. * @param string $action 'add', 'update', 'view'
  68. * @return int <0 if KO,
  69. * =0 if OK but we want to process standard actions too,
  70. * >0 if OK and we want to replace standard actions.
  71. */
  72. public function getNomUrl($parameters, &$object, &$action)
  73. {
  74. global $db, $langs, $conf, $user;
  75. $this->resprints = '';
  76. return 0;
  77. }
  78. /**
  79. * Overloading the doActions function : replacing the parent's function with the one below
  80. *
  81. * @param array $parameters Hook metadatas (context, etc...)
  82. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  83. * @param string $action Current action (if set). Generally create or edit or null
  84. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  85. * @return int < 0 on error, 0 on success, 1 to replace standard code
  86. */
  87. public function doActions($parameters, &$object, &$action, $hookmanager)
  88. {
  89. global $conf, $user, $langs;
  90. $error = 0; // Error counter
  91. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  92. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  93. // Do what you want here...
  94. // You can for example call global vars like $fieldstosearchall to overwrite them, or update database depending on $action and $_POST values.
  95. }
  96. if (!$error) {
  97. $this->results = array('myreturn' => 999);
  98. $this->resprints = 'A text to show';
  99. return 0; // or return 1 to replace standard code
  100. } else {
  101. $this->errors[] = 'Error message';
  102. return -1;
  103. }
  104. }
  105. /**
  106. * Overloading the doMassActions function : replacing the parent's function with the one below
  107. *
  108. * @param array $parameters Hook metadatas (context, etc...)
  109. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  110. * @param string $action Current action (if set). Generally create or edit or null
  111. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  112. * @return int < 0 on error, 0 on success, 1 to replace standard code
  113. */
  114. public function doMassActions($parameters, &$object, &$action, $hookmanager)
  115. {
  116. global $conf, $user, $langs;
  117. $error = 0; // Error counter
  118. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  119. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  120. foreach ($parameters['toselect'] as $objectid) {
  121. // Do action on each object id
  122. }
  123. }
  124. if (!$error) {
  125. $this->results = array('myreturn' => 999);
  126. $this->resprints = 'A text to show';
  127. return 0; // or return 1 to replace standard code
  128. } else {
  129. $this->errors[] = 'Error message';
  130. return -1;
  131. }
  132. }
  133. /**
  134. * Overloading the addMoreMassActions function : replacing the parent's function with the one below
  135. *
  136. * @param array $parameters Hook metadatas (context, etc...)
  137. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  138. * @param string $action Current action (if set). Generally create or edit or null
  139. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  140. * @return int < 0 on error, 0 on success, 1 to replace standard code
  141. */
  142. public function addMoreMassActions($parameters, &$object, &$action, $hookmanager)
  143. {
  144. global $conf, $user, $langs;
  145. $error = 0; // Error counter
  146. $disabled = 1;
  147. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  148. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  149. $this->resprints = '<option value="0"'.($disabled ? ' disabled="disabled"' : '').'>'.$langs->trans("MyModuleMassAction").'</option>';
  150. }
  151. if (!$error) {
  152. return 0; // or return 1 to replace standard code
  153. } else {
  154. $this->errors[] = 'Error message';
  155. return -1;
  156. }
  157. }
  158. /**
  159. * Execute action
  160. *
  161. * @param array $parameters Array of parameters
  162. * @param Object $object Object output on PDF
  163. * @param string $action 'add', 'update', 'view'
  164. * @return int <0 if KO,
  165. * =0 if OK but we want to process standard actions too,
  166. * >0 if OK and we want to replace standard actions.
  167. */
  168. public function beforePDFCreation($parameters, &$object, &$action)
  169. {
  170. global $conf, $user, $langs;
  171. global $hookmanager;
  172. $outputlangs = $langs;
  173. $ret = 0; $deltemp = array();
  174. dol_syslog(get_class($this).'::executeHooks action='.$action);
  175. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  176. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) { // do something only for the context 'somecontext1' or 'somecontext2'
  177. }
  178. return $ret;
  179. }
  180. /**
  181. * Execute action
  182. *
  183. * @param array $parameters Array of parameters
  184. * @param Object $pdfhandler PDF builder handler
  185. * @param string $action 'add', 'update', 'view'
  186. * @return int <0 if KO,
  187. * =0 if OK but we want to process standard actions too,
  188. * >0 if OK and we want to replace standard actions.
  189. */
  190. public function afterPDFCreation($parameters, &$pdfhandler, &$action)
  191. {
  192. global $conf, $user, $langs;
  193. global $hookmanager;
  194. $outputlangs = $langs;
  195. $ret = 0; $deltemp = array();
  196. dol_syslog(get_class($this).'::executeHooks action='.$action);
  197. /* print_r($parameters); print_r($object); echo "action: " . $action; */
  198. if (in_array($parameters['currentcontext'], array('somecontext1', 'somecontext2'))) {
  199. // do something only for the context 'somecontext1' or 'somecontext2'
  200. }
  201. return $ret;
  202. }
  203. /**
  204. * Overloading the loadDataForCustomReports function : returns data to complete the customreport tool
  205. *
  206. * @param array $parameters Hook metadatas (context, etc...)
  207. * @param string $action Current action (if set). Generally create or edit or null
  208. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  209. * @return int < 0 on error, 0 on success, 1 to replace standard code
  210. */
  211. public function loadDataForCustomReports($parameters, &$action, $hookmanager)
  212. {
  213. global $conf, $user, $langs;
  214. $langs->load("mymodule@mymodule");
  215. $this->results = array();
  216. $head = array();
  217. $h = 0;
  218. if ($parameters['tabfamily'] == 'mymodule') {
  219. $head[$h][0] = dol_buildpath('/module/index.php', 1);
  220. $head[$h][1] = $langs->trans("Home");
  221. $head[$h][2] = 'home';
  222. $h++;
  223. $this->results['title'] = $langs->trans("MyModule");
  224. $this->results['picto'] = 'mymodule@mymodule';
  225. }
  226. $head[$h][0] = 'customreports.php?objecttype='.$parameters['objecttype'].(empty($parameters['tabfamily']) ? '' : '&tabfamily='.$parameters['tabfamily']);
  227. $head[$h][1] = $langs->trans("CustomReports");
  228. $head[$h][2] = 'customreports';
  229. $this->results['head'] = $head;
  230. return 1;
  231. }
  232. /**
  233. * Overloading the restrictedArea function : check permission on an object
  234. *
  235. * @param array $parameters Hook metadatas (context, etc...)
  236. * @param string $action Current action (if set). Generally create or edit or null
  237. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  238. * @return int <0 if KO,
  239. * =0 if OK but we want to process standard actions too,
  240. * >0 if OK and we want to replace standard actions.
  241. */
  242. public function restrictedArea($parameters, &$action, $hookmanager)
  243. {
  244. global $user;
  245. if ($parameters['features'] == 'myobject') {
  246. if ($user->rights->mymodule->myobject->read) {
  247. $this->results['result'] = 1;
  248. return 1;
  249. } else {
  250. $this->results['result'] = 0;
  251. return 1;
  252. }
  253. }
  254. return 0;
  255. }
  256. /**
  257. * Execute action completeTabsHead
  258. *
  259. * @param array $parameters Array of parameters
  260. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  261. * @param string $action 'add', 'update', 'view'
  262. * @param Hookmanager $hookmanager hookmanager
  263. * @return int <0 if KO,
  264. * =0 if OK but we want to process standard actions too,
  265. * >0 if OK and we want to replace standard actions.
  266. */
  267. public function completeTabsHead(&$parameters, &$object, &$action, $hookmanager)
  268. {
  269. global $langs, $conf, $user;
  270. if (!isset($parameters['object']->element)) {
  271. return 0;
  272. }
  273. if ($parameters['mode'] == 'remove') {
  274. // utilisé si on veut faire disparaitre des onglets.
  275. return 0;
  276. } elseif ($parameters['mode'] == 'add') {
  277. $langs->load('mymodule@mymodule');
  278. // utilisé si on veut ajouter des onglets.
  279. $counter = count($parameters['head']);
  280. $element = $parameters['object']->element;
  281. $id = $parameters['object']->id;
  282. // verifier le type d'onglet comme member_stats où ça ne doit pas apparaitre
  283. // if (in_array($element, ['societe', 'member', 'contrat', 'fichinter', 'project', 'propal', 'commande', 'facture', 'order_supplier', 'invoice_supplier'])) {
  284. if (in_array($element, ['context1', 'context2'])) {
  285. $datacount = 0;
  286. $parameters['head'][$counter][0] = dol_buildpath('/mymodule/mymodule_tab.php', 1) . '?id=' . $id . '&amp;module='.$element;
  287. $parameters['head'][$counter][1] = $langs->trans('MyModuleTab');
  288. if ($datacount > 0) {
  289. $parameters['head'][$counter][1] .= '<span class="badge marginleftonlyshort">' . $datacount . '</span>';
  290. }
  291. $parameters['head'][$counter][2] = 'mymoduleemails';
  292. $counter++;
  293. }
  294. if ($counter > 0 && (int) DOL_VERSION < 14) {
  295. $this->results = $parameters['head'];
  296. // return 1 to replace standard code
  297. return 1;
  298. } else {
  299. // en V14 et + $parameters['head'] est modifiable par référence
  300. return 0;
  301. }
  302. }
  303. }
  304. /* Add here any other hooked methods... */
  305. }