canvas.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <http://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. 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') $newaction='create';
  68. if ($newaction == 'update') $newaction='edit';
  69. if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') $newaction='view';
  70. return $newaction;
  71. }
  72. /**
  73. * Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
  74. *
  75. * @param string $module Name of target module (thirdparty, contact, ...)
  76. * @param string $card Tab name of card (ex: 'card', 'info', 'contactcard', ...) or '' for a list page
  77. * @param string $canvas Name of canvas (ex: mycanvas, default, or mycanvas@myexternalmodule)
  78. * @return void
  79. */
  80. function getCanvas($module, $card, $canvas)
  81. {
  82. global $conf, $langs;
  83. // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
  84. $this->targetmodule = $module;
  85. $this->canvas = $canvas;
  86. $this->card = $card;
  87. $this->dirmodule = $module;
  88. // Correct values if canvas is into an external module
  89. if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
  90. {
  91. $this->canvas = $regs[1];
  92. $this->dirmodule = $regs[2];
  93. }
  94. // For compatibility
  95. if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }
  96. // Control file
  97. $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
  98. if (file_exists($controlclassfile))
  99. {
  100. // Include actions class (controller)
  101. $this->control_file=$controlclassfile;
  102. require_once $controlclassfile;
  103. // Instantiate actions class (controller)
  104. $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
  105. $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
  106. }
  107. // Template dir
  108. $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
  109. if (! is_dir($this->template_dir))
  110. {
  111. $this->template_dir='';
  112. }
  113. //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
  114. //print ' => template_dir='.$this->template_dir.'<br>';
  115. }
  116. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  117. /**
  118. * Shared method for canvas to assign values for templates
  119. *
  120. * @param string $action Action string
  121. * @param int $id Object id (if ref not provided)
  122. * @param string $ref Object ref (if id not provided)
  123. * @return void
  124. */
  125. function assign_values(&$action='view', $id=0, $ref='')
  126. {
  127. // phpcs:enable
  128. if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
  129. }
  130. /**
  131. * Return the template to display canvas (if it exists)
  132. *
  133. * @param string $action Action code
  134. * @return int 0=Canvas template file does not exist, 1=Canvas template file exists
  135. */
  136. function displayCanvasExists($action)
  137. {
  138. if (empty($this->template_dir)) return 0;
  139. if (file_exists($this->template_dir.(!empty($this->card)?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php')) return 1;
  140. else return 0;
  141. }
  142. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  143. /**
  144. * Display a canvas page. This will include the template for output.
  145. * Variables used by templates may have been defined or loaded before into the assign_values function.
  146. *
  147. * @param string $action Action code
  148. * @return void
  149. */
  150. function display_canvas($action)
  151. {
  152. // phpcs:enable
  153. global $db, $conf, $langs, $user, $canvas;
  154. global $form, $formfile;
  155. include $this->template_dir.(!empty($this->card)?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
  156. }
  157. // This functions should not be used anymore because canvas should contains only templates.
  158. // http://wiki.dolibarr.org/index.php/Canvas_development
  159. /**
  160. * Return if a canvas contains an action controller
  161. *
  162. * @return boolean Return if canvas contains actions (old feature. now actions should be inside hooks)
  163. */
  164. function hasActions()
  165. {
  166. return (is_object($this->control));
  167. }
  168. /**
  169. * Shared method for canvas to execute actions.
  170. * @deprecated Use the doActions of hooks instead of this.
  171. * This function is called if you add a doActions class inside your canvas. Try to not
  172. * do that and add action code into a hook instead.
  173. *
  174. * @param string $action Action string
  175. * @param int $id Object id
  176. * @return mixed Return return code of doActions of canvas
  177. * @see http://wiki.dolibarr.org/index.php/Canvas_development
  178. */
  179. function doActions(&$action='view', $id=0)
  180. {
  181. if (method_exists($this->control,'doActions'))
  182. {
  183. $ret = $this->control->doActions($action, $id);
  184. return $ret;
  185. }
  186. }
  187. }