dolibarr 21.0.0-alpha
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 *
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{
34 public $accessNeedLoggedUser = true;
35
40 public $accessRight = false;
41
46 public $controllerStatus = true;
47
51 public $db;
52
56 public $tplPath;
57
58
64 public function __construct()
65 {
66 global $db, $hookmanager;
67
68 $this->db = $db;
69
70 // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
71 $hookmanager->initHooks(array('webportalpage', 'webportal'));
72 }
73
80 public function action()
81 {
82 $resHook = $this->hookDoAction();
83
84 return ($resHook < 0 ? -1 : 1);
85 }
86
92 public function checkAccess()
93 {
95
96 if ($this->accessNeedLoggedUser) {
97 if (!$context->userIslog()) {
98 return false;
99 }
100 }
101
102 if (!$this->accessRight) {
103 return false;
104 }
105
106 return true;
107 }
108
114 public function display()
115 {
117
118 $this->loadTemplate('header');
119
120 $this->hookPrintPageView();
121
122 if (!$context->controller_found) {
123 $this->loadTemplate('404');
124 }
125
126 $this->loadTemplate('footer');
127 }
128
134 public function display404()
135 {
136 $this->loadTemplate('header');
137 $this->loadTemplate('404');
138 $this->loadTemplate('footer');
139 }
140
147 public function hookDoAction($parameters = array())
148 {
149 global $hookmanager;
150
152
153 /* Use $context singleton to modify menu, */
154 $parameters['controller'] = $context->controller;
155
156 $reshook = $hookmanager->executeHooks('doActions', $parameters, $context, $context->action); // Note that $action and $object may have been modified by hook
157 if ($reshook < 0) {
158 $context->setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
159 }
160
161 return $reshook;
162 }
163
170 public function hookPrintPageView($parameters = array())
171 {
172 global $hookmanager;
173
175
176 /* Use $context singleton to modify menu, */
177 $parameters['controller'] = $context->controller;
178
179 $reshook = $hookmanager->executeHooks('PrintPageView', $parameters, $context, $context->action); // Note that $action and $object may have been modified by hook
180 if ($reshook < 0) {
181 $context->setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
182 }
183
184 return $reshook;
185 }
186
194 public function loadTemplate($templateName, $vars = false)
195 {
196 global $conf, $langs, $hookmanager, $db; // may be used into the tpl
197
198 $context = Context::getInstance(); // load for tpl
199
200 if (!preg_match('/^[0-9\.A-ZaZ_\-]*$/ui', $templateName)) {
201 return false;
202 }
203
204 if (!empty($this->tplPath)) {
205 $tplPath = $this->tplPath . '/' . $templateName . '.tpl.php';
206 if (file_exists($tplPath)) {
207 include $tplPath;
208 return true;
209 }
210 }
211
212 $tplPath = $context->tplPath . '/' . $templateName . '.tpl.php';
213
214 if (!file_exists($tplPath)) {
215 print 'ERROR TPL NOT FOUND : ' . $templateName;
216 return false;
217 }
218
219 $controller = $this; // transmit controller to tpl
220
221 include $tplPath;
222
223 return true;
224 }
225}
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