dolibarr 21.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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
30class Canvas
31{
35 public $db;
36
40 public $error = '';
41
45 public $errors = array();
46
50 public $actiontype;
51
55 public $dirmodule; // Module directory
59 public $targetmodule; // Module concerned by canvas (ex: thirdparty, contact, ...)
63 public $canvas; // Name of canvas (ex: company, individual, product, service, ...)
67 public $card; // Tab (sub-canvas)
68
72 public $template_dir;
76 public $control; // Initialized by getCanvas with controller instance
77
78
85 public function __construct($db, $actiontype = 'view')
86 {
87 $this->db = $db;
88
89 $this->actiontype = $this->_cleanaction($actiontype);
90 }
91
98 private function _cleanaction($action)
99 {
100 $newaction = $action;
101 if ($newaction == 'add') {
102 $newaction = 'create';
103 }
104 if ($newaction == 'update') {
105 $newaction = 'edit';
106 }
107 if (empty($newaction) || $newaction == 'delete' || $newaction == 'create_user' || $newaction == 'presend' || $newaction == 'send') {
108 $newaction = 'view';
109 }
110 return $newaction;
111 }
112
113
122 public function getCanvas($module, $card, $canvas)
123 {
124 global $conf, $langs;
125
126 // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
127 $this->targetmodule = $module;
128 $this->canvas = $canvas;
129 $this->card = $card;
130 $this->dirmodule = $module;
131 // Correct values if canvas is into an external module
132 $regs = array();
133 if (preg_match('/^([^@]+)@([^@]+)$/i', $canvas, $regs)) {
134 $this->canvas = $regs[1];
135 $this->dirmodule = $regs[2];
136 }
137 // For compatibility
138 if ($this->dirmodule == 'thirdparty') {
139 $this->dirmodule = 'societe';
140 }
141
142 // Control file
143 $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
144 if (file_exists($controlclassfile)) {
145 // Include actions class (controller)
146 require_once $controlclassfile;
147
148 // Instantiate actions class (controller)
149 $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
150 $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
151 }
152
153 // Template dir
154 $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
155 if (!is_dir($this->template_dir)) {
156 $this->template_dir = '';
157 }
158
159 //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
160 //print ' => template_dir='.$this->template_dir.'<br>';
161 }
162
163 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
172 public function assign_values(&$action = 'view', $id = 0, $ref = '')
173 {
174 // phpcs:enable
175 if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
176 $this->control->assign_values($action, $id, $ref);
177 }
178 }
179
186 public function displayCanvasExists($action)
187 {
188 // template_dir should be '/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/', for example '/mymodule/canvas/product/tpl'
189 if (empty($this->template_dir)) {
190 return 0;
191 }
192
193 if (file_exists($this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php')) {
194 return 1;
195 } else {
196 return 0;
197 }
198 }
199
200 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
208 public function display_canvas($action)
209 {
210 // phpcs:enable
211 global $db, $conf, $langs, $user, $canvas;
212 global $form, $formfile;
213
214 //var_dump($this->card.'-'.$action);
215 include $this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($action).'.tpl.php'; // Include native PHP template
216 }
217
218
219 // This functions should not be used anymore because canvas should contains only templates.
220 // https://wiki.dolibarr.org/index.php/Canvas_development
221
227 public function hasActions()
228 {
229 return (is_object($this->control));
230 }
231
243 public function doActions(&$action = 'view', $id = 0)
244 {
245 if (method_exists($this->control, 'doActions')) {
246 $ret = $this->control->doActions($action, $id);
247 return $ret;
248 }
249 return null;
250 }
251}
$id
Definition account.php:39
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.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.