i would go down a oop way and put any changes in a class
(notice i use self:: in last function)
<?php
class U2
{
// Default Header
public static function header($title = "")
{
if (!isset($_SESSION['loggedin'])) {
Guests::guestadd();
}
if ($title == "") {
$title = Config::get('SITENAME');
} else {
$title = Config::get('SITENAME') . " : " . htmlspecialchars($title);
}
require_once "assets/themes/" . (Users::get('stylesheet') ?: Config::get('DEFAULTTHEME')) . "/header.php";
}
// Default Footer
public static function footer()
{
require_once "assets/themes/" . (Users::get('stylesheet') ?: Config::get('DEFAULTTHEME')) . "/footer.php";
}
// Default Block Header
public static function begin($caption = "-")
{
$blockId = 'f-' . sha1($caption);
?>
<div class="card">
<div class="card-header text-center frame-header">
<?php echo $caption ?>
<a data-toggle="collapse" href="#" class="showHide" id="<?php echo $blockId; ?>" style="float: right;"></a>
</div>
<div class="card-body frame-body slidingDiv<?php echo $blockId; ?>">
<?php
}
// Default Block Footer
public static function end()
{
?>
</div>
</div>
<div class="block-footer"></div>
<?php
}
public static function render($file, $data = [], $page = false)
{
// Start With No Error
$error = 0;
// Debug
$GLOBALS['debugviewdata'] = debugviewdata($data);
if ($page == 'utorrents') {
// Does User View Exist
if (file_exists('../app/views/user/' . $file . '.php')) {
self::header($data['title']);
self::begin($data['title']);
require_once "../app/views/user/" . $file . ".php";
self::end();
self::footer();
return;
} else {
$error = 1;
}
}
// The View Isnt There !
if ($error === 1) {
Redirect::autolink(URLROOT, "View $file does not exist");
}
}
}
this way on home for example you can use U2::header & in other controllers U2::render('page',$data,'u2torrent);
now lets say you update in a year & only have changed 50% of views - with controllers just mass edit view::render to u2::render and drop your template folder in job done.
or another example of the last function where instead we add condition on url if you have seperate style for login, signup etc
public static function render($file, $data = [], $page = false)
{
// Start With No Error
$error = 0;
// Debug
$GLOBALS['debugviewdata'] = debugviewdata($data);
// Does User View Exist
$url = $_GET['url'] ?? '';
if ($url == 'signup' || $url == 'recover') {
if (file_exists('../app/views/u2torrent/' . $file . '.php')) {
self::signupheader($data['title']);
self::begin($data['title']);
require_once "../app/views/u2torrent/" . $file . ".php";
self::end();
self::signupfooter();
return;
} else {
$error = 1;
}
} elseif ($url == 'login') {
if (file_exists('../app/views/u2torrent/' . $file . '.php')) {
self::header($data['title']);
self::loginbegin($data['title']);
require_once "../app/views/u2torrent/" . $file . ".php";
self::end();
self::loginfooter();
return;
} else {
$error = 1;
}
} else {
if (file_exists('../app/views/user/' . $file . '.php')) {
self::header($data['title']);
self::begin($data['title']);
require_once "../app/views/user/" . $file . ".php";
self::end();
self::footer();
return;
} else {
$error = 1;
}
}
}
// The View Isnt There !
if ($error === 1) {
Redirect::autolink(URLROOT, "View $file does not exist");
}
}
the last option it good because if url is not listed you could fall back on default so updating would be put controllers in notepad mass edit View::render('signup/index', $data, 'user'); to U2::render('signup/index', $data, 'user');
in last option i removed if ($page == 'utorrents') { so you could keep as 'user' notice the require_once the url should be able to cover all changes
update - lets make it so you only edit one file you could replace the View class with something like this
this means you wouldnt have to change to u2torrents from user View::render('signup/index', $data, 'user');
<?php
class View
{
// Default Header
public static function u2header($title = "")
{
if (!isset($_SESSION['loggedin'])) {
Guests::guestadd();
}
if ($title == "") {
$title = Config::get('SITENAME');
} else {
$title = Config::get('SITENAME') . " : " . htmlspecialchars($title);
}
require_once "assets/themes/" . (Users::get('stylesheet') ?: Config::get('DEFAULTTHEME')) . "/header.php";
}
// Default Footer
public static function u2footer()
{
require_once "assets/themes/" . (Users::get('stylesheet') ?: Config::get('DEFAULTTHEME')) . "/footer.php";
}
// Default Block Header
public static function u2begin($caption = "-")
{
$blockId = 'f-' . sha1($caption);
?>
<div class="card">
<div class="card-header text-center frame-header">
<?php echo $caption ?>
<a data-toggle="collapse" href="#" class="showHide" id="<?php echo $blockId; ?>" style="float: right;"></a>
</div>
<div class="card-body frame-body slidingDiv<?php echo $blockId; ?>">
<?php
}
// Default Block Footer
public static function u2end()
{
?>
</div>
</div>
<div class="block-footer"></div>
<?php
}
// Return View Template
public static function render($file, $data = [], $page = false)
{
// Start With No Error
$error = 0;
// Debug
$GLOBALS['debugviewdata'] = debugviewdata($data);
if ($page == 'user') {
// GET URL
$url = $_GET['url'] ?? '';
if ($url == 'signup' || $url == 'recover') {
if (file_exists('../app/views/u2torrent/' . $file . '.php')) {
self::u2header($data['title']);
self::u2begin($data['title']);
require_once "../app/views/u2torrent/" . $file . ".php";
self::u2end();
self::u2footer();
return;
} else {
$error = 1;
}
} elseif ($url == 'login') {
if (file_exists('../app/views/u2torrent/' . $file . '.php')) {
self::u2header($data['title']);
self::u2begin($data['title']);
require_once "../app/views/u2torrent/" . $file . ".php";
self::u2end();
self::u2footer();
return;
} else {
$error = 1;
}
} else {
if (file_exists('../app/views/user/' . $file . '.php')) {
Style::header($data['title']);
Style::begin($data['title']);
require_once "../app/views/user/" . $file . ".php";
Style::end();
Style::footer();
return;
} else {
$error = 1;
}
}
} elseif ($page == 'admin') {
// Does Admin View Exist
if (file_exists('../app/views/admin/' . $file . '.php')) {
Style::adminheader('Staff Panel');
Style::adminnavmenu();
Style::begin($data['title']);
require_once "../app/views/admin/" . $file . ".php";
Style::end();
Style::adminfooter();
return;
} else {
$error = 1;
}
} else {
// Does Requalar View Exist
if (file_exists('../app/views/user/' . $file . '.php')) {
require_once "../app/views/user/" . $file . ".php";
return;
} else {
$error = 1;
}
}
// The View Isnt There !
if ($error === 1) {
Redirect::autolink(URLROOT, "View $file does not exist");
}
}
}
Last edited by M-jay
on 2022-06-12 05:07:54