canvas.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /* Copyright (C) 2010-2018 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/canvas.class.php
  20. * \ingroup core
  21. * \brief File of class to manage canvas
  22. */
  23. /**
  24. * Class to manage canvas
  25. */
  26. class Canvas
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * @var string[] Error codes (or messages)
  38. */
  39. public $errors = array();
  40. public $actiontype;
  41. public $dirmodule; // Module directory
  42. public $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
  43. public $canvas; // Name of canvas (ex: company, individual, product, service, ...)
  44. public $card; // Tab (sub-canvas)
  45. public $template_dir; // Initialized by getCanvas with templates directory
  46. public $control; // Initialized by getCanvas with controller instance
  47. /**
  48. * Constructor
  49. *
  50. * @param DoliDB $db Database handler
  51. * @param string $actiontype Action type ('create', 'view', 'edit', 'list')
  52. */
  53. public function __construct($db, $actiontype = 'view')
  54. {
  55. $this->db = $db;
  56. $this->actiontype = $this->_cleanaction($actiontype);
  57. }
  58. /**
  59. * Return action code cleaned
  60. *
  61. * @param string $action Action type ('create', 'view', 'edit', 'list', 'add', 'update')
  62. * @return string Cleaned action type ('create', 'view', 'edit', 'list')
  63. */
  64. private function _cleanaction($action)
  65. {
  66. $newaction = $action;
  67. if ($newaction == 'add') {
  68. $newaction = 'create';
  69. }
  70. if ($newaction == 'update') {
  71. $newaction = 'edit';
  72. }
  73. if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') {
  74. $newaction = 'view';
  75. }
  76. return $newaction;
  77. }
  78. /**
  79. * Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
  80. *
  81. * @param string $module Name of target module (thirdparty, contact, ...)
  82. * @param string $card Tab name of card (ex: 'card', 'info', 'contactcard', ...) or '' for a list page
  83. * @param string $canvas Name of canvas (ex: mycanvas, default, or mycanvas@myexternalmodule)
  84. * @return void
  85. */
  86. public function getCanvas($module, $card, $canvas)
  87. {
  88. global $conf, $langs;
  89. // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
  90. $this->targetmodule = $module;
  91. $this->canvas = $canvas;
  92. $this->card = $card;
  93. $this->dirmodule = $module;
  94. // Correct values if canvas is into an external module
  95. $regs = array();
  96. if (preg_match('/^([^@]+)@([^@]+)$/i', $canvas, $regs)) {
  97. $this->canvas = $regs[1];
  98. $this->dirmodule = $regs[2];
  99. }
  100. // For compatibility
  101. if ($this->dirmodule == 'thirdparty') {
  102. $this->dirmodule = 'societe';
  103. }
  104. // Control file
  105. $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
  106. if (file_exists($controlclassfile)) {
  107. // Include actions class (controller)
  108. $this->control_file = $controlclassfile;
  109. require_once $controlclassfile;
  110. // Instantiate actions class (controller)
  111. $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
  112. $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
  113. }
  114. // Template dir
  115. $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
  116. if (!is_dir($this->template_dir)) {
  117. $this->template_dir = '';
  118. }
  119. //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
  120. //print ' => template_dir='.$this->template_dir.'<br>';
  121. }
  122. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  123. /**
  124. * Shared method for canvas to assign values for templates
  125. *
  126. * @param string $action Action string
  127. * @param int $id Object id (if ref not provided)
  128. * @param string $ref Object ref (if id not provided)
  129. * @return void
  130. */
  131. public function assign_values(&$action = 'view', $id = 0, $ref = '')
  132. {
  133. // phpcs:enable
  134. if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
  135. $this->control->assign_values($action, $id, $ref);
  136. }
  137. }
  138. /**
  139. * Return the template to display canvas (if it exists)
  140. *
  141. * @param string $action Action code
  142. * @return int 0=Canvas template file does not exist, 1=Canvas template file exists
  143. */
  144. public function displayCanvasExists($action)
  145. {
  146. // template_dir should be '/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/', for example '/mymodule/canvas/product/tpl'
  147. if (empty($this->template_dir)) {
  148. return 0;
  149. }
  150. if (file_exists($this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php')) {
  151. return 1;
  152. } else {
  153. return 0;
  154. }
  155. }
  156. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  157. /**
  158. * Display a canvas page. This will include the template for output.
  159. * Variables used by templates may have been defined or loaded before into the assign_values function.
  160. *
  161. * @param string $action Action code
  162. * @return void
  163. */
  164. public function display_canvas($action)
  165. {
  166. // phpcs:enable
  167. global $db, $conf, $langs, $user, $canvas;
  168. global $form, $formfile;
  169. //var_dump($this->card.'-'.$action);
  170. include $this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
  171. }
  172. // This functions should not be used anymore because canvas should contains only templates.
  173. // https://wiki.dolibarr.org/index.php/Canvas_development
  174. /**
  175. * Return if a canvas contains an action controller
  176. *
  177. * @return boolean Return if canvas contains actions (old feature. now actions should be inside hooks)
  178. */
  179. public function hasActions()
  180. {
  181. return (is_object($this->control));
  182. }
  183. /**
  184. * Shared method for canvas to execute actions.
  185. * @deprecated Use the doActions of hooks instead of this.
  186. * This function is called if you add a doActions class inside your canvas. Try to not
  187. * do that and add action code into a hook instead.
  188. *
  189. * @param string $action Action string
  190. * @param int $id Object id
  191. * @return mixed Return return code of doActions of canvas
  192. * @see https://wiki.dolibarr.org/index.php/Canvas_development
  193. */
  194. public function doActions(&$action = 'view', $id = 0)
  195. {
  196. if (method_exists($this->control, 'doActions')) {
  197. $ret = $this->control->doActions($action, $id);
  198. return $ret;
  199. }
  200. }
  201. }