PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/class/canvas.class.php

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