dolibarr  9.0.0
canvas.class.php
Go to the documentation of this file.
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 
29 class Canvas
30 {
34  public $db;
35 
39  public $error='';
40 
44  public $errors = array();
45 
46  public $actiontype;
47 
48  public $dirmodule; // Module directory
49  public $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
50  public $canvas; // Name of canvas (ex: company, individual, product, service, ...)
51  public $card; // Tab (sub-canvas)
52 
53  public $template_dir; // Initialized by getCanvas with templates directory
54  public $control; // Initialized by getCanvas with controller instance
55 
56 
63  function __construct($db, $actiontype='view')
64  {
65  $this->db = $db;
66 
67  $this->actiontype = $this->_cleanaction($actiontype);
68  }
69 
76  private function _cleanaction($action)
77  {
78  $newaction = $action;
79  if ($newaction == 'add') $newaction='create';
80  if ($newaction == 'update') $newaction='edit';
81  if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') $newaction='view';
82  return $newaction;
83  }
84 
85 
94  function getCanvas($module, $card, $canvas)
95  {
96  global $conf, $langs;
97 
98  // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
99  $this->targetmodule = $module;
100  $this->canvas = $canvas;
101  $this->card = $card;
102  $this->dirmodule = $module;
103  // Correct values if canvas is into an external module
104  if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
105  {
106  $this->canvas = $regs[1];
107  $this->dirmodule = $regs[2];
108  }
109  // For compatibility
110  if ($this->dirmodule == 'thirdparty') { $this->dirmodule = 'societe'; }
111 
112  // Control file
113  $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
114  if (file_exists($controlclassfile))
115  {
116  // Include actions class (controller)
117  $this->control_file=$controlclassfile;
118  require_once $controlclassfile;
119 
120  // Instantiate actions class (controller)
121  $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
122  $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
123  }
124 
125  // Template dir
126  $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
127  if (! is_dir($this->template_dir))
128  {
129  $this->template_dir='';
130  }
131 
132  //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
133  //print ' => template_dir='.$this->template_dir.'<br>';
134  }
135 
136  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
145  function assign_values(&$action='view', $id=0, $ref='')
146  {
147  // phpcs:enable
148  if (method_exists($this->control,'assign_values')) $this->control->assign_values($action, $id, $ref);
149  }
150 
157  function displayCanvasExists($action)
158  {
159  if (empty($this->template_dir)) return 0;
160 
161  if (file_exists($this->template_dir.(!empty($this->card)?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php')) return 1;
162  else return 0;
163  }
164 
165  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
173  function display_canvas($action)
174  {
175  // phpcs:enable
176  global $db, $conf, $langs, $user, $canvas;
177  global $form, $formfile;
178 
179  include $this->template_dir.(!empty($this->card)?$this->card.'_':'').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
180  }
181 
182 
183  // This functions should not be used anymore because canvas should contains only templates.
184  // http://wiki.dolibarr.org/index.php/Canvas_development
185 
191  function hasActions()
192  {
193  return (is_object($this->control));
194  }
195 
207  function doActions(&$action='view', $id=0)
208  {
209  if (method_exists($this->control,'doActions'))
210  {
211  $ret = $this->control->doActions($action, $id);
212  return $ret;
213  }
214  }
215 }
display_canvas($action)
Display a canvas page.
Class to manage canvas.
__construct($db, $actiontype='view')
Constructor.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
displayCanvasExists($action)
Return the template to display canvas (if it exists)
assign_values(&$action='view', $id=0, $ref='')
Shared method for canvas to assign values for templates.
doActions(&$action='view', $id=0)
Shared method for canvas to execute actions.
_cleanaction($action)
Return action code cleaned.
hasActions()
Return if a canvas contains an action controller.
getCanvas($module, $card, $canvas)
Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir.