Skip to content

Commit

Permalink
Move navigation registration to the BeforeTemplateRenderedEvent templ…
Browse files Browse the repository at this point in the history
…ate event

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Sep 4, 2020
1 parent ffc6e1a commit c1c961e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 37 deletions.
40 changes: 3 additions & 37 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

namespace OCA\External\AppInfo;

use OCA\External\BeforeTemplateRenderedListener;
use OCA\External\Capabilities;
use OCA\External\Settings\Personal;
use OCA\External\SitesManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\INavigationManager;
use OCP\IServerContainer;
use OCP\IURLGenerator;
Expand All @@ -45,53 +47,17 @@ public function __construct() {

public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
}

public function boot(IBootContext $context): void {
/** @var SitesManager $sitesManager */
$sitesManager = $context->getAppContainer()->get(SitesManager::class);
$sites = $sitesManager->getSitesToDisplay();

$this->registerNavigationEntries($context->getServerContainer(), $sites);
$this->registerPersonalPage($context->getServerContainer(), $sites);
}

/**
* @param IServerContainer $server
* @param array[] $sites
*/
public function registerNavigationEntries(IServerContainer $server, array $sites) {
foreach ($sites as $id => $site) {
if ($site['type'] !== SitesManager::TYPE_LINK && $site['type'] !== SitesManager::TYPE_SETTING && $site['type'] !== SitesManager::TYPE_LOGIN ) {
continue;
}

$server->get(INavigationManager::class)->add(function() use ($site, $server) {
$url = $server->get(IURLGenerator::class);

if ($site['icon'] !== '') {
$image = $url->linkToRoute('external.icon.showIcon', ['icon' => $site['icon']]);
} else {
$image = $url->linkToRoute('external.icon.showIcon', ['icon' => 'external.svg']);
}

$href = $site['url'];
if (!$site['redirect']) {
$href = $url->linkToRoute('external.site.showPage', ['id'=> $site['id']]);
}

return [
'id' => 'external_index' . $site['id'],
'order' => 80 + $site['id'],
'href' => $href,
'icon' => $image,
'type' => $site['type'],
'name' => $site['name'],
];
});
}
}

/**
* @param IServerContainer $server
* @param array[] $sites
Expand Down
84 changes: 84 additions & 0 deletions lib/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\External;

use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\INavigationManager;
use OCP\IURLGenerator;

class BeforeTemplateRenderedListener implements IEventListener {

/** @var SitesManager */
protected $sitesManager;
/** @var INavigationManager */
protected $navigationManager;
/** @var IURLGenerator */
protected $urlGenerator;

public function __construct(SitesManager $sitesManager,
INavigationManager $navigationManager,
IURLGenerator $urlGenerator) {
$this->sitesManager = $sitesManager;
$this->navigationManager = $navigationManager;
$this->urlGenerator = $urlGenerator;
}

public function handle(Event $event): void {
if (!$event instanceof BeforeTemplateRenderedEvent) {
return;
}

$sites = $this->sitesManager->getSitesToDisplay();

foreach ($sites as $id => $site) {
if ($site['type'] !== SitesManager::TYPE_LINK && $site['type'] !== SitesManager::TYPE_SETTING && $site['type'] !== SitesManager::TYPE_LOGIN ) {
continue;
}

$this->navigationManager->add(function() use ($site) {
if ($site['icon'] !== '') {
$image = $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => $site['icon']]);
} else {
$image = $this->urlGenerator->linkToRoute('external.icon.showIcon', ['icon' => 'external.svg']);
}

$href = $site['url'];
if (!$site['redirect']) {
$href = $this->urlGenerator->linkToRoute('external.site.showPage', ['id'=> $site['id']]);
}

return [
'id' => 'external_index' . $site['id'],
'order' => 80 + $site['id'],
'href' => $href,
'icon' => $image,
'type' => $site['type'],
'name' => $site['name'],
];
});
}
}
}

0 comments on commit c1c961e

Please sign in to comment.