Skip to content

Commit

Permalink
wip panel pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle committed Oct 20, 2024
1 parent f27ad85 commit 0672dde
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/classes/Extend/FrontendPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function extend(Container $container): void {
$frontendNavigation = $container->get('FrontendNavigation');

foreach ($this->pages as $page) {
// Remove loading / from path - allows devs to ->register('/')
// Remove leading / from path - allows devs to ->register('/')
$path = ltrim($page['path'], '/');
$path = "/{$this->moduleName}/{$path}";
// Remove ending / if it exists
Expand Down
42 changes: 40 additions & 2 deletions core/classes/Extend/PanelPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class PanelPages extends BaseExtender {

private $pages = [];
private $templateDirectories = [];


public function extend(Container $container): void {
/** @var \Language */
Expand All @@ -15,31 +17,67 @@ public function extend(Container $container): void {
/** @var \Pages */
$pages = $container->get(\Pages::class);

/** @var \User */
$user = $container->get(\User::class);

/** @var \Cache */
$cache = $container->get(\Cache::class);
$cache->setCache('panel_sidebar');

/** @var \Navigation */
$panelNavigation = $container->get('PanelNavigation');

$moduleSidebarOrder = $cache->fetch("{$this->moduleName}_order", fn () => 10);
$panelNavigation->add("{$this->moduleName}_divider", mb_strtoupper($this->moduleDisplayName), 'divider', 'top', null, $moduleSidebarOrder);

foreach ($this->pages as $page) {
// Remove loading / from path - allows devs to ->register('/')
$path = ltrim($page['path'], '/');
$path = "/panel/{$this->moduleName}/{$path}";
// Remove ending / if it exists
$path = rtrim($path, '/');

$friendlyName = $moduleLanguage->get($page['friendly_name_translation']);

if ($user->hasPermission($page['permission'])) {
$pageIcon = $cache->fetch("{$page['name']}_icon", fn () => '<i class="nav-icon fas fa-cogs"></i>');

$panelNavigation->add($page['name'], $friendlyName, \URL::build($path), 'top', null, $moduleSidebarOrder + 0.1, $pageIcon);
}

$pages->add(
$this->moduleName,
$path,
$page['handler'],
$moduleLanguage->get($page['name']),
$friendlyName,
false,
true,
);
}

/** @var \Smarty */
$smarty = $container->get(\Smarty::class);

foreach ($this->templateDirectories as $directory) {
$smarty->addTemplateDir($directory);
}
}

public function register(string $path, string $name, string $handler): PanelPages {
public function register(string $path, string $name, string $friendlyNameTranslation, string $handler, string $permission): PanelPages {
$this->pages[] = [
'path' => $path,
'name' => $name,
'friendly_name_translation' => $friendlyNameTranslation,
'handler' => $handler,
'permission' => $permission,
];

return $this;
}

public function templateDirectory(string $path): PanelPages {
$this->templateDirectories[] = $path;

return $this;
}
}
4 changes: 3 additions & 1 deletion core/classes/Pages/PanelPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
namespace NamelessMC\Framework\Pages;

abstract class PanelPage extends Page {
// ...

abstract public function permission(): string;

}
1 change: 1 addition & 0 deletions core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@
$staffcp_nav = new Navigation(true); // $staffcp_nav = panel nav

$container->set('FrontendNavigation', $navigation);
$container->set('PanelNavigation', $staffcp_nav);

// Add links to cc_nav
$cc_nav->add('cc_overview', $language->get('user', 'overview'), URL::build('/user'));
Expand Down
4 changes: 2 additions & 2 deletions core/templates/backend_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
$smarty->setCompileDir(ROOT_PATH . '/cache/templates_c');

if (file_exists(ROOT_PATH . '/custom/panel_templates/' . PANEL_TEMPLATE . '/template.php')) {
$smarty->setTemplateDir(ROOT_PATH . '/custom/panel_templates/' . PANEL_TEMPLATE);
$smarty->addTemplateDir(ROOT_PATH . '/custom/panel_templates/' . PANEL_TEMPLATE);

require(ROOT_PATH . '/custom/panel_templates/' . PANEL_TEMPLATE . '/template.php');
} else {
$smarty->setTemplateDir(ROOT_PATH . '/custom/panel_templates/Default');
$smarty->addTemplateDir(ROOT_PATH . '/custom/panel_templates/Default');

require(ROOT_PATH . '/custom/panel_templates/Default/template.php');
}
Expand Down
29 changes: 25 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,36 @@
}

if ($active_page['controllerBased']) {
$languageKey = "{$active_page['module']}Language";

if (str_contains($route, 'queries')) {
/** @var \NamelessMC\Framework\Queries\Query */
$controller = $container->make($active_page['file']);
$controller->handle();
} elseif (str_contains($route, 'panel')) {
/** @var \NamelessMC\Framework\Pages\PanelPage */
$controller = $container->make($active_page['file'], [
$languageKey => $container->get($languageKey),
]);

if (!$user->handlePanelPageLoad($controller->permission())) {
require_once(ROOT_PATH . '/403.php');
die();
}

require_once(ROOT_PATH . '/core/templates/backend_init.php');

$controller->render();
Module::loadPage($user, $pages, $cache, $smarty, [$navigation, $cc_nav, $staffcp_nav], $widgets, $template);
define('PAGE', 'panel');
define('PARENT_PAGE', $active_page['module']);
define('PANEL_PAGE', $controller->pageName());
$smarty->assign(['TITLE' => $active_page['name'], 'PAGE' => $controller->pageName()]);
$template->onPageLoad();
require(ROOT_PATH . '/core/templates/panel_navbar.php');
return $template->displayTemplate($controller->viewFile(), $smarty);
} else {
require_once(ROOT_PATH . '/core/templates/frontend_init.php');
// todo: may have to change shit for panel pages or queries
$languageKey = "{$active_page['module']}Language";

require_once(ROOT_PATH . '/core/templates/frontend_init.php');
/** @var \NamelessMC\Framework\Pages\Page */
$controller = $container->make($active_page['file'], [
'templatePagination' => $template_pagination,
Expand Down

0 comments on commit 0672dde

Please sign in to comment.