Skip to content

Commit

Permalink
wip panel pages. let modules set icon
Browse files Browse the repository at this point in the history
  • Loading branch information
tadhgboyle committed Oct 22, 2024
1 parent f646f2a commit 7a45b67
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 34 deletions.
20 changes: 10 additions & 10 deletions core/classes/Core/ComposerModuleWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,6 @@ public function getDebugInfo(): array
return $provider->provide();
}

private function migrationsPath(): string
{
return ROOT_PATH . '/vendor/' . $this->_packageName . '/migrations';
}

private function hasMigrations(): bool
{
return file_exists($this->migrationsPath());
}

public function runMigrations(): void
{
if (!$this->hasMigrations()) {
Expand All @@ -139,6 +129,16 @@ public function rollbackMigrations(): void
PhinxAdapter::rollback($this->getPrivateName(), $this->migrationsPath());
}

private function migrationsPath(): string
{
return ROOT_PATH . '/vendor/' . $this->_packageName . '/migrations';
}

private function hasMigrations(): bool
{
return file_exists($this->migrationsPath());
}

private function callLifecycleHooks(array $hooks): void
{
foreach ($hooks as $callback) {
Expand Down
13 changes: 0 additions & 13 deletions core/classes/Core/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ public function add(
float $order = 10,
?string $icon = ''
): void {
if ($this->_panel && $location == 'top') {
// Discard order
// TODO: only a temporary solution to the link conflict issue in the StaffCP
if (count($this->_topNavbar)) {
$key = array_keys($this->_topNavbar)[count($this->_topNavbar) - 1];
$previous_order = $this->_topNavbar[$key]['order'];
} else {
$previous_order = 0;
}

$order = $previous_order + 1;
}

// Add the link to the navigation
if ($location === 'top') {
// Add to top navbar
Expand Down
3 changes: 2 additions & 1 deletion core/classes/Core/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ class Pages
* @param bool $widgets Can widgets be used on the page? Default false.
* @param bool $controllerBased Is the page controller based? Default false.
*/
public function add(string $module, string $url, string $file, string $name = '', bool $widgets = false, bool $controllerBased = false): void
public function add(string $module, string $url, string $file, string $name = '', bool $widgets = false, string $moduleSafeName = null, bool $controllerBased = false): void
{
$this->_pages[$url] = [
'module' => $module,
'moduleSafeName' => $moduleSafeName,
'file' => $file,
'name' => $name,
'widgets' => $widgets,
Expand Down
3 changes: 2 additions & 1 deletion core/classes/Extend/FrontendPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public function extend(Container $container): void {
}

$pages->add(
$this->moduleName,
$this->moduleDisplayName,
$path,
$page['handler'],
$pageFriendlyName,
$page['allowWidgets'],
$this->moduleName,
true,
);
}
Expand Down
21 changes: 15 additions & 6 deletions core/classes/Extend/PanelPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,38 @@ public function extend(Container $container): void {
/** @var \Navigation */
$panelNavigation = $container->get('PanelNavigation');

$moduleSidebarOrder = $cache->fetch("{$this->moduleName}_order", fn () => 10);
$moduleSidebarOrder = array_reduce(array_filter($cache->retrieveAll(), function ($item) {
return str_ends_with($item, '_order');
}, ARRAY_FILTER_USE_KEY), function ($carry, $item) {
return $item > $carry ? $item : $carry;
}, 0) + 1;
$panelNavigation->add("{$this->moduleName}_divider", mb_strtoupper($this->moduleDisplayName), 'divider', 'top', null, $moduleSidebarOrder);

$lastSubPageOrder = $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, '/');
$order = $lastSubPageOrder + 0.1;
$lastSubPageOrder = $order;

$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);
$pageIcon = "<i class='nav-icon {$page['icon']}'></i>";
$panelNavigation->add($page['name'], $friendlyName, \URL::build($path), 'top', null, $order, $pageIcon);
}

$pages->add(
$this->moduleName,
$this->moduleDisplayName,
$path,
$page['handler'],
$friendlyName,
false,
$this->moduleName,
true,
);
}
Expand All @@ -63,13 +71,14 @@ public function extend(Container $container): void {
}
}

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

return $this;
Expand Down
3 changes: 2 additions & 1 deletion core/classes/Extend/Queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function extend(Container $container): void {
$path = rtrim($path, '/');

$pages->add(
$this->moduleName,
$this->moduleDisplayName,
$path,
$page['handler'],
'',
false,
$this->moduleName,
true,
);
}
Expand Down
2 changes: 2 additions & 0 deletions custom/panel_templates/Default/core/seo.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@
<thead>
<tr>
<th>{$PAGE_TITLE}</th>
<th>{$PAGE_MODULE}</th>
</tr>
</thead>
<tbody>
{foreach from=$PAGE_LIST key=key item=item}
<tr>
<td><a href="{$EDIT_LINK|replace:'{x}':$item.id}">{$key|escape}</a></td>
<td>{$item.module}</a></td>
</tr>
{/foreach}
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
}

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

if (str_contains($route, 'queries')) {
/** @var \NamelessMC\Framework\Queries\Query */
Expand All @@ -136,7 +136,7 @@
$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('PARENT_PAGE', $active_page['moduleSafeName']);
define('PANEL_PAGE', $controller->pageName());
$smarty->assign(['TITLE' => $active_page['name'], 'PAGE' => $controller->pageName()]);
$template->onPageLoad();
Expand Down
1 change: 1 addition & 0 deletions modules/Core/pages/panel/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
}
} else {
// Check if composer module
// TODO, should we try to run the `composer remove` command?
foreach (ComposerModuleDiscovery::discoverModules() as $composerModule) {
if ($composerModule->getName() === $name) { // compare by ID?
$composerModule->rollbackMigrations();
Expand Down
1 change: 1 addition & 0 deletions modules/Core/pages/panel/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
'GENERATE' => $language->get('admin', 'generate_sitemap'),
'GOOGLE_ANALYTICS_VALUE' => Settings::get('ga_script'),
'PAGE_TITLE' => $language->get('admin', 'page'),
'PAGE_MODULE' => $language->get('admin', 'module'),
'PAGE_LIST' => $pages->returnPages(),
'EDIT_LINK' => URL::build('/panel/core/seo/', 'metadata={x}'),
'GOOGLE_ANALYTICS' => $language->get('admin', 'google_analytics'),
Expand Down

0 comments on commit 7a45b67

Please sign in to comment.