dolibarr 23.0.3
controller.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023-2024 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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
30{
35 public $accessNeedLoggedUser = true;
36
41 public $accessRight = false;
42
47 public $controllerStatus = true;
48
52 public $db;
53
57 public $tplPath;
58
62 public $formList;
63
64
70 public function __construct()
71 {
72 global $db, $hookmanager;
73
74 $this->db = $db;
75
76 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
77 $hookmanager->initHooks(array('webportalpage', 'webportal'));
78 }
79
86 public function action()
87 {
88 $resHook = $this->hookDoAction();
89
90 return ($resHook < 0 ? -1 : 1);
91 }
92
98 public function checkAccess()
99 {
100 global $hookmanager;
102
103 if ($this->accessNeedLoggedUser) {
104 if (!$context->userIslog()) {
105 return false;
106 }
107 }
108
109 // Hooks for security access
110 $hookmanager->initHooks(array('webportaldao'));
111 $parameters = array('controller' => $context->controller);
112 $reshook = $hookmanager->executeHooks('checkAccess', $parameters, $context, $context->action);
113 if ($reshook > 0) {
114 $this->accessRight = !empty($hookmanager->resArray['accessRight']);
115 }
116
117 if (!$this->accessRight) {
118 return false;
119 }
120
121 return true;
122 }
123
129 public function display()
130 {
132
133 $this->loadTemplate('header');
134
135 $this->hookPrintPageView();
136
137 if (!$context->controller_found) {
138 $this->loadTemplate('404');
139 }
140
141 $this->loadTemplate('footer');
142 }
143
149 public function display404()
150 {
151 $this->loadTemplate('header');
152 $this->loadTemplate('404');
153 $this->loadTemplate('footer');
154 }
155
162 public function hookDoAction($parameters = array())
163 {
164 global $hookmanager;
165
167
168 /* Use $context singleton to modify menu, */
169 $parameters['controller'] = $context->controller;
170
171 $reshook = $hookmanager->executeHooks('doActions', $parameters, $context, $context->action); // Note that $action and $object may have been modified by hook
172 if ($reshook < 0) {
173 $context->setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
174 }
175
176 return $reshook;
177 }
178
185 public function hookPrintPageView($parameters = array())
186 {
187 global $hookmanager;
188
190
191 /* Use $context singleton to modify menu, */
192 $parameters['controller'] = $context->controller;
193
194 $reshook = $hookmanager->executeHooks('PrintPageView', $parameters, $context, $context->action); // Note that $action and $object may have been modified by hook
195 if ($reshook < 0) {
196 $context->setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
197 }
198
199 return $reshook;
200 }
201
209 public function loadTemplate($templateName, $vars = false)
210 {
211 global $conf, $langs, $hookmanager, $db; // may be used into the tpl
212
213 // load for tpl
214 // This set $context->rootUrl
216
217 if (!preg_match('/^[0-9\.A-ZaZ_\-]*$/ui', $templateName)) {
218 return false;
219 }
220
221 if (!empty($this->tplPath)) {
222 $tplPath = $this->tplPath . '/' . $templateName . '.tpl.php';
223 if (file_exists($tplPath)) {
224 include $tplPath;
225 return true;
226 }
227 }
228
229 $tplPath = $context->tplPath . '/' . $templateName . '.tpl.php';
230
231 if (!file_exists($tplPath)) {
232 print 'ERROR TPL NOT FOUND : ' . $templateName;
233 return false;
234 }
235
236 $controller = $this; // transmit controller to tpl
237
238 include $tplPath;
239
240 return true;
241 }
242}
static getInstance()
Singleton method to create one instance of this object.
Class to manage pages.
display()
Display.
hookPrintPageView($parameters=array())
Execute hook PrintPageView.
action()
Action method is called before html output can be used to manage security and change context.
display404()
Display error template.
__construct()
Constructor.
hookDoAction($parameters=array())
Execute hook doActions.
loadTemplate($templateName, $vars=false)
Load a template .tpl file.
checkAccess()
Check current access to controller.
$context
@method int call_trigger(string $triggerName, ?User $user)
Definition logout.php:42