canvas.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /* Copyright (C) 2010-2011 Regis Houssin <regis.houssin@capnetworks.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. var $db;
  29. var $error;
  30. var $errors=array();
  31. var $actiontype;
  32. var $dirmodule; // Module directory
  33. var $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
  34. var $canvas; // Name of canvas (ex: company, individual, product, service, ...)
  35. var $card; // Tab (sub-canvas)
  36. var $template_dir; // Initialized by getCanvas with templates directory
  37. var $control; // Initialized by getCanvas with controller instance
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. * @param string $actiontype Action type ('create', 'view', 'edit', 'list')
  43. */
  44. function __construct($db, $actiontype='view')
  45. {
  46. $this->db = $db;
  47. $this->actiontype = $this->_cleanaction($actiontype);
  48. }
  49. /**
  50. * Return action code cleaned
  51. *
  52. * @param string $action Action type ('create', 'view', 'edit', 'list', 'add', 'update')
  53. * @return string Cleaned action type ('create', 'view', 'edit', 'list')
  54. */
  55. private function _cleanaction($action)
  56. {
  57. $newaction = $action;
  58. if ($newaction == 'add') $newaction='create';
  59. if ($newaction == 'update') $newaction='edit';
  60. if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user') $newaction='view';
  61. return $newaction;
  62. }
  63. /**
  64. * Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir
  65. *
  66. * @param string $module Name of target module (thirdparty, contact, ...)
  67. * @param string $card Tab name of card (ex: 'card', 'info', 'contactcard', ...) or '' for a list page
  68. * @param string $canvas Name of canvas (ex: mycanvas, default, or mycanvas@myexternalmodule)
  69. * @return void
  70. */
  71. function getCanvas($module, $card, $canvas)
  72. {
  73. global $conf, $langs;
  74. // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
  75. $this->targetmodule = $module;
  76. $this->canvas = $canvas;
  77. $this->card = $card;
  78. $this->dirmodule = $module;
  79. // Correct values if canvas is into an external module
  80. if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
  81. {
  82. $this->canvas = $regs[1];
  83. $this->dirmodule = $regs[2];
  84. }
  85. // For compatibility
  86. if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }
  87. // Control file
  88. $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
  89. if (file_exists($controlclassfile))
  90. {
  91. // Include actions class (controller)
  92. $this->control_file=$controlclassfile;
  93. require_once $controlclassfile;
  94. // Instantiate actions class (controller)
  95. $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
  96. $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
  97. }
  98. // Template dir
  99. $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
  100. if (! is_dir($this->template_dir))
  101. {
  102. $this->template_dir='';
  103. }
  104. //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
  105. //print ' => template_dir='.$this->template_dir.'<br>';
  106. }
  107. /**
  108. * Shared method for canvas to assign values for templates
  109. *
  110. * @param string &$action Action string
  111. * @param int $id Object id (if ref not provided)
  112. * @param string $ref Object ref (if id not provided)
  113. * @return void
  114. */
  115. function assign_values(&$action='view', $id=0, $ref='')
  116. {
  117. if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
  118. }
  119. /**
  120. * Return the template to display canvas (if it exists)
  121. *
  122. * @param string $action Action code
  123. * @return int 0=Canvas template file does not exist, 1=Canvas template file exists
  124. */
  125. function displayCanvasExists($action)
  126. {
  127. if (empty($this->template_dir)) return 0;
  128. if (file_exists($this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php')) return 1;
  129. else return 0;
  130. }
  131. /**
  132. * Display a canvas page. This will include the template for output.
  133. * Variables used by templates may have been defined or loaded before into the assign_values function.
  134. *
  135. * @param string $action Action code
  136. * @return void
  137. */
  138. function display_canvas($action)
  139. {
  140. global $db, $conf, $langs, $user, $canvas;
  141. global $form, $formfile;
  142. include $this->template_dir.($this->card?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
  143. }
  144. // This functions should not be used anymore because canvas should contains only templates.
  145. // http://wiki.dolibarr.org/index.php/Canvas_development
  146. /**
  147. * Return if a canvas contains an action controller
  148. *
  149. * @return boolean Return if canvas contains actions (old feature. now actions should be inside hooks)
  150. */
  151. function hasActions()
  152. {
  153. return (is_object($this->control));
  154. }
  155. /**
  156. * Shared method for canvas to execute actions
  157. *
  158. * @param string &$action Action string
  159. * @param int $id Object id
  160. * @return mixed Return return code of doActions of canvas
  161. * @deprecated This function is called if you add a doActions class inside your canvas. Try to not
  162. * do that and add action code into a hook instead.
  163. * @see http://wiki.dolibarr.org/index.php/Canvas_development
  164. */
  165. function doActions(&$action='view', $id=0)
  166. {
  167. if (method_exists($this->control,'doActions'))
  168. {
  169. $ret = $this->control->doActions($action, $id);
  170. return $ret;
  171. }
  172. }
  173. }