2023-09-12 15:12:30 +02:00
< ? php
2024-03-03 16:00:13 +01:00
/* Copyright ( C ) 2023 - 2024 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2024-11-06 23:57:45 +01:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-03-03 16:00:13 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2024-11-06 23:57:45 +01:00
/**
* \file htdocs / webportal / class / controller . class . php
* \ingroup webportal
* \brief File of controller class for WebPortal
*/
2023-09-12 15:12:30 +02:00
/**
* Class to manage pages
*/
2023-09-12 15:26:12 +02:00
class Controller
{
/**
* if this controller need logged user or not
* @ var bool
*/
public $accessNeedLoggedUser = true ;
/**
* define current user access
* @ var bool
*/
public $accessRight = false ;
/**
* If controller is active
* @ var bool
*/
public $controllerStatus = true ;
2023-09-28 15:04:28 +02:00
/**
2024-01-24 10:28:38 +01:00
* @ var DoliDB Database handler
2023-09-28 15:04:28 +02:00
*/
public $db ;
2023-09-12 15:26:12 +02:00
/**
* @ var string Tpl path will use default context -> tplPath if empty
*/
public $tplPath ;
/**
2024-01-22 13:59:49 +01:00
* Constructor
2023-09-12 15:26:12 +02:00
*
2023-09-12 17:08:19 +02:00
* @ return void
2023-09-12 15:26:12 +02:00
*/
public function __construct ()
{
2023-09-28 15:04:28 +02:00
global $db , $hookmanager ;
$this -> db = $db ;
2023-09-12 15:26:12 +02:00
2024-06-30 21:02:50 +02:00
// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
2023-09-12 15:26:12 +02:00
$hookmanager -> initHooks ( array ( 'webportalpage' , 'webportal' ));
}
/**
* Action method is called before html output
* can be used to manage security and change context
*
2024-02-08 14:11:54 +01:00
* @ return int Return integer < 0 on error , > 0 on success
2023-09-12 15:26:12 +02:00
*/
public function action ()
{
2024-02-08 14:11:54 +01:00
$resHook = $this -> hookDoAction ();
return ( $resHook < 0 ? - 1 : 1 );
2023-09-12 15:26:12 +02:00
}
/**
* Check current access to controller
*
* @ return bool
*/
public function checkAccess ()
{
$context = Context :: getInstance ();
if ( $this -> accessNeedLoggedUser ) {
if ( ! $context -> userIslog ()) {
return false ;
}
}
if ( ! $this -> accessRight ) {
return false ;
}
return true ;
}
/**
* Display
*
* @ return void
*/
public function display ()
{
$context = Context :: getInstance ();
$this -> loadTemplate ( 'header' );
$this -> hookPrintPageView ();
if ( ! $context -> controller_found ) {
$this -> loadTemplate ( '404' );
}
$this -> loadTemplate ( 'footer' );
}
/**
* Display error template
*
* @ return void
*/
public function display404 ()
{
$this -> loadTemplate ( 'header' );
$this -> loadTemplate ( '404' );
$this -> loadTemplate ( 'footer' );
}
/**
* Execute hook doActions
*
2024-11-06 23:57:45 +01:00
* @ param array < string , mixed > $parameters Parameters
2024-02-08 14:11:54 +01:00
* @ return int Return integer < 0 on error , 0 on success , 1 to replace standard code
2023-09-12 15:26:12 +02:00
*/
public function hookDoAction ( $parameters = array ())
{
global $hookmanager ;
$context = Context :: getInstance ();
/* Use $context singleton to modify menu, */
$parameters [ 'controller' ] = $context -> controller ;
$reshook = $hookmanager -> executeHooks ( 'doActions' , $parameters , $context , $context -> action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
$context -> setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
}
return $reshook ;
}
/**
* Execute hook PrintPageView
*
2024-11-06 23:57:45 +01:00
* @ param array < string , mixed > $parameters Parameters
2024-02-08 14:11:54 +01:00
* @ return int Return integer < 0 on error , 0 on success , 1 to replace standard code
2023-09-12 15:26:12 +02:00
*/
public function hookPrintPageView ( $parameters = array ())
{
global $hookmanager ;
$context = Context :: getInstance ();
/* Use $context singleton to modify menu, */
$parameters [ 'controller' ] = $context -> controller ;
$reshook = $hookmanager -> executeHooks ( 'PrintPageView' , $parameters , $context , $context -> action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
$context -> setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
}
return $reshook ;
}
/**
2024-02-26 15:02:39 +01:00
* Load a template . tpl file
2023-09-12 15:26:12 +02:00
*
2024-02-26 15:02:39 +01:00
* @ param string $templateName Template file name ( without the . tpl . php )
2023-09-12 16:05:24 +02:00
* @ param mixed $vars Data to transmit to template
2024-02-26 15:02:39 +01:00
* @ return bool True if template found , else false
2023-09-12 15:26:12 +02:00
*/
public function loadTemplate ( $templateName , $vars = false )
{
2024-02-08 14:11:54 +01:00
global $conf , $langs , $hookmanager , $db ; // may be used into the tpl
2023-09-12 15:26:12 +02:00
$context = Context :: getInstance (); // load for tpl
if ( ! preg_match ( '/^[0-9\.A-ZaZ_\-]*$/ui' , $templateName )) {
return false ;
}
if ( ! empty ( $this -> tplPath )) {
$tplPath = $this -> tplPath . '/' . $templateName . '.tpl.php' ;
if ( file_exists ( $tplPath )) {
include $tplPath ;
return true ;
}
}
$tplPath = $context -> tplPath . '/' . $templateName . '.tpl.php' ;
if ( ! file_exists ( $tplPath )) {
print 'ERROR TPL NOT FOUND : ' . $templateName ;
return false ;
}
2023-09-28 10:44:22 +02:00
$controller = $this ; // transmit controller to tpl
2023-09-12 15:26:12 +02:00
include $tplPath ;
return true ;
}
2023-09-12 15:12:30 +02:00
}