Skip to content

Commit

Permalink
feat(Contexts): add Contexts to navigation when applicable
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Mar 12, 2024
1 parent a600647 commit 79b549f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCA\Analytics\Datasource\DatasourceEvent;
use OCA\Tables\Capabilities;
use OCA\Tables\Listener\AnalyticsDatasourceListener;
use OCA\Tables\Listener\BeforeTemplateRenderedListener;
use OCA\Tables\Listener\TablesReferenceListener;
use OCA\Tables\Listener\UserDeletedListener;
use OCA\Tables\Middleware\PermissionMiddleware;
Expand All @@ -17,6 +18,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\IConfig;
use OCP\Server;
Expand All @@ -32,6 +34,10 @@ class Application extends App implements IBootstrap {

public const OWNER_TYPE_USER = 0;

public const NAV_ENTRY_MODE_HIDDEN = 0;
public const NAV_ENTRY_MODE_RECIPIENTS = 1;
public const NAV_ENTRY_MODE_ALL = 2;

public function __construct() {
parent::__construct(self::APP_ID);
}
Expand All @@ -47,6 +53,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeUserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(DatasourceEvent::class, AnalyticsDatasourceListener::class);
$context->registerEventListener(RenderReferenceEvent::class, TablesReferenceListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);

$context->registerSearchProvider(SearchTablesProvider::class);

Expand Down
37 changes: 37 additions & 0 deletions lib/Db/ContextMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OCA\Tables\Db;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Helper\UserHelper;
use OCP\AppFramework\Db\QBMapper;
Expand Down Expand Up @@ -161,6 +162,42 @@ public function findAll(?string $userId = null): array {
return $resultEntities;
}

public function findForNavBar(string $userId): array {
$qb = $this->getFindContextBaseQuery($userId);
$qb->andWhere($qb->expr()->andX(
// default
$qb->expr()->gt('n.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
// user override
$qb->expr()->orX(
$qb->expr()->gt('n2.display_mode', $qb->createNamedParameter(Application::NAV_ENTRY_MODE_HIDDEN, IQueryBuilder::PARAM_INT)),
$qb->expr()->isNull('n2.display_mode'),
)
));

$result = $qb->executeQuery();
$r = $result->fetchAll();

$contextIds = [];
foreach ($r as $row) {
$contextIds[$row['id']] = 1;
}
$contextIds = array_keys($contextIds);
unset($row);

$resultEntities = [];
foreach ($contextIds as $contextId) {
$workArray = [];
foreach ($r as $row) {
if ($row['id'] === $contextId) {
$workArray[] = $row;
}
}
$resultEntities[] = $this->formatResultRows($workArray, $userId);
}

return $resultEntities;
}

/**
* @throws Exception
* @throws NotFoundError
Expand Down
82 changes: 82 additions & 0 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace OCA\Tables\Listener;

use OCA\Tables\AppInfo\Application;
use OCA\Tables\Service\ContextService;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUserSession;

/**
* @template-implements IEventListener<Event|BeforeTemplateRenderedEvent>
*/
class BeforeTemplateRenderedListener implements IEventListener {
public function __construct(
protected INavigationManager $navigationManager,
protected IURLGenerator $urlGenerator,
protected IUserSession $userSession,
protected ContextService $contextService,
) {
}

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

$user = $this->userSession->getUser();
if ($user === null) {
return;
}

// temporarily show all
//$contexts = $this->contextService->findForNavigation($user->getUID());
$contexts = $this->contextService->findAll($user->getUID());
foreach ($contexts as $context) {
/* temporarily, show all
if ($context->getOwnerType() === Application::OWNER_TYPE_USER
&& $context->getOwnerId() === $user->getUID()) {
// filter out entries for owners unless it is set to be visible
$skipEntry = true;
foreach ($context->getSharing() as $shareInfo) {
// TODO: integrate into DB query in Mapper
if (isset($shareInfo['display_mode']) && $shareInfo['display_mode'] === Application::NAV_ENTRY_MODE_ALL) {
// a custom override makes it visible
$skipEntry = false;
break;
} elseif (!isset($shareInfo['display_mode']) && $shareInfo['display_mode_default'] === Application::NAV_ENTRY_MODE_ALL) {
// no custom override, and visible also for owner by default
$skipEntry = false;
break;
}
}
if ($skipEntry) {
continue;
}
}
*/

$iconRelPath = 'material/' . $context->getIcon() . '.svg';
$icon = file_exists(__DIR__ . '/../../img/' . $iconRelPath) ? $iconRelPath : 'app.svg';

$this->navigationManager->add([
'id' => Application::APP_ID . '::' . $context->getId(),
'name' => $context->getName(),
'href' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
'icon' => $this->urlGenerator->imagePath(Application::APP_ID, $icon),
'order' => 8,
'type' => 'link',
]);
}
}
}
4 changes: 4 additions & 0 deletions lib/Service/ContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function findAll(?string $userId): array {
return $this->contextMapper->findAll($userId);
}

public function findForNavigation(string $userId): array {
return $this->contextMapper->findForNavBar($userId);
}

/**
* @throws Exception
* @throws InternalError
Expand Down

0 comments on commit 79b549f

Please sign in to comment.