dolibarr  16.0.5
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  $this->control_file = $controlclassfile;
125  require_once $controlclassfile;
126 
127  // Instantiate actions class (controller)
128  $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
129  $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
130  }
131 
132  // Template dir
133  $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
134  if (!is_dir($this->template_dir)) {
135  $this->template_dir = '';
136  }
137 
138  //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
139  //print ' => template_dir='.$this->template_dir.'<br>';
140  }
141 
142  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
151  public function assign_values(&$action = 'view', $id = 0, $ref = '')
152  {
153  // phpcs:enable
154  if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
155  $this->control->assign_values($action, $id, $ref);
156  }
157  }
158 
165  public function displayCanvasExists($action)
166  {
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  }
228 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Canvas\displayCanvasExists
displayCanvasExists($action)
Return the template to display canvas (if it exists)
Definition: canvas.class.php:165
Canvas\assign_values
assign_values(&$action='view', $id=0, $ref='')
Shared method for canvas to assign values for templates.
Definition: canvas.class.php:151
Canvas\hasActions
hasActions()
Return if a canvas contains an action controller.
Definition: canvas.class.php:205
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
Canvas\__construct
__construct($db, $actiontype='view')
Constructor.
Definition: canvas.class.php:63
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Canvas\display_canvas
display_canvas($action)
Display a canvas page.
Definition: canvas.class.php:186
Canvas\getCanvas
getCanvas($module, $card, $canvas)
Initialize properties: ->targetmodule, ->canvas, ->card, ->dirmodule, ->template_dir.
Definition: canvas.class.php:100
Canvas
Class to manage canvas.
Definition: canvas.class.php:29
Canvas\_cleanaction
_cleanaction($action)
Return action code cleaned.
Definition: canvas.class.php:76
Canvas\doActions
doActions(&$action='view', $id=0)
Shared method for canvas to execute actions.
Definition: canvas.class.php:221