dolibarr 23.0.3
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 // Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
125 $this->targetmodule = $module;
126 $this->canvas = $canvas;
127 $this->card = $card;
128 $this->dirmodule = $module;
129 // Correct values if canvas is into an external module
130 $regs = array();
131 if (preg_match('/^([^@]+)@([^@]+)$/i', $canvas, $regs)) {
132 $this->canvas = $regs[1];
133 $this->dirmodule = $regs[2];
134 }
135 // For compatibility
136 if ($this->dirmodule == 'thirdparty') {
137 $this->dirmodule = 'societe';
138 }
139
140 // Control file
141 $controlclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
142 if (file_exists($controlclassfile)) {
143 // Include actions class (controller)
144 require_once $controlclassfile;
145
146 // Instantiate actions class (controller)
147 $controlclassname = 'Actions'.ucfirst($this->card).ucfirst($this->canvas);
148 $this->control = new $controlclassname($this->db, $this->dirmodule, $this->targetmodule, $this->canvas, $this->card);
149 }
150
151 // Template dir
152 $this->template_dir = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/');
153 if (!is_dir($this->template_dir)) {
154 $this->template_dir = '';
155 }
156
157 //print 'dimodule='.$dirmodule.' canvas='.$this->canvas.'<br>';
158 //print ' => template_dir='.$this->template_dir.'<br>';
159 }
160
161 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
170 public function assign_values(&$action = 'view', $id = 0, $ref = '')
171 {
172 // phpcs:enable
173 if (is_object($this->control) && method_exists($this->control, 'assign_values')) {
174 $this->control->assign_values($action, $id, $ref);
175 }
176 }
177
184 public function displayCanvasExists($action)
185 {
186 // template_dir should be '/'.$this->dirmodule.'/canvas/'.$this->canvas.'/tpl/', for example '/mymodule/canvas/product/tpl'
187 if (empty($this->template_dir)) {
188 return 0;
189 }
190
191 $newaction = $action;
192 if ($action && !in_array($action, array('create', 'view', 'edit', 'list'))) {
193 $newaction = 'view';
194 }
195
196 if (file_exists($this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($newaction).'.tpl.php')) {
197 return 1;
198 } else {
199 return 0;
200 }
201 }
202
203 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
211 public function display_canvas($action)
212 {
213 // phpcs:enable
214 global $db, $conf, $langs, $user, $canvas; // used into include
215 global $form, $formfile; // used into include
216
217 $newaction = $action;
218 if ($action && !in_array($action, array('create', 'view', 'edit', 'list'))) {
219 $newaction = 'view';
220 }
221
222 //var_dump($this->card.'-'.$action);
223 include $this->template_dir.(!empty($this->card) ? $this->card.'_' : '').$this->_cleanaction($newaction).'.tpl.php'; // Include native PHP template
224 }
225
226
227 // This functions should not be used anymore because canvas should contains only templates.
228 // https://wiki.dolibarr.org/index.php/Canvas_development
229
235 public function hasActions()
236 {
237 return (is_object($this->control));
238 }
239
251 public function doActions(&$action = 'view', $id = 0)
252 {
253 $control = $this->control;
254 if (method_exists($control, 'doActions')) {
255 $ret = $control->doActions($action, $id);
256 return $ret;
257 }
258 return null;
259 }
260}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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.