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