dolibarr  20.0.0-alpha
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 <https://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  public 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') {
80  $newaction = 'create';
81  }
82  if ($newaction == 'update') {
83  $newaction = 'edit';
84  }
85  if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') {
86  $newaction = 'view';
87  }
88  return $newaction;
89  }
90 
91 
100  public function getCanvas($module, $card, $canvas)
101  {
102  global $conf, $langs;
103 
104  // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
105  $this->targetmodule = $module;
106  $this->canvas = $canvas;
107  $this->card = $card;
108  $this->dirmodule = $module;
109  // Correct values if canvas is into an external module
110  $regs = array();
111  if (preg_match('/^([^@]+)@([^@]+)$/i', $canvas, $regs)) {
112  $this->canvas = $regs[1];
113  $this->dirmodule = $regs[2];
114  }
115  // For compatibility
116  if ($this->dirmodule == 'thirdparty') {
117  $this->dirmodule = 'societe';
118  }
119 
120  // Control file
121  $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
122  if (file_exists($controlclassfile)) {
123  // Include actions class (controller)
124  require_once $controlclassfile;
125 
126  // Instantiate actions class (controller)
127  $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
128  $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
129  }
130 
131  // Template dir
132  $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
133  if (!is_dir($this->template_dir)) {
134  $this->template_dir = '';
135  }
136 
137  //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
138  //print ' => template_dir='.$this->template_dir.'<br>';
139  }
140 
141  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
150  public function assign_values(&$action = 'view', $id = 0, $ref = '')
151  {
152  // phpcs:enable
153  if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
154  $this->control->assign_values($action, $id, $ref);
155  }
156  }
157 
164  public function displayCanvasExists($action)
165  {
166  // template_dir should be '/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/', for example '/mymodule/canvas/product/tpl'
167  if (empty($this->template_dir)) {
168  return 0;
169  }
170 
171  if (file_exists($this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php')) {
172  return 1;
173  } else {
174  return 0;
175  }
176  }
177 
178  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
186  public function display_canvas($action)
187  {
188  // phpcs:enable
189  global $db, $conf, $langs, $user, $canvas;
190  global $form, $formfile;
191 
192  //var_dump($this->card.'-'.$action);
193  include $this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
194  }
195 
196 
197  // This functions should not be used anymore because canvas should contains only templates.
198  // https://wiki.dolibarr.org/index.php/Canvas_development
199 
205  public function hasActions()
206  {
207  return (is_object($this->control));
208  }
209 
221  public function doActions(&$action = 'view', $id = 0)
222  {
223  if (method_exists($this->control, 'doActions')) {
224  $ret = $this->control->doActions($action, $id);
225  return $ret;
226  }
227  return null;
228  }
229 }
Class to manage canvas.
hasActions()
Return if a canvas contains an action controller.
getCanvas($module, $card, $canvas)
Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir.
_cleanaction($action)
Return action code cleaned.
__construct($db, $actiontype='view')
Constructor.
display_canvas($action)
Display a canvas page.
doActions(&$action='view', $id=0)
Shared method for canvas to execute actions.
displayCanvasExists($action)
Return if a template exists to display as canvas (if it exists)
assign_values(&$action='view', $id=0, $ref='')
Shared method for canvas to assign values for templates.
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.