Repository navigation
feat(contexts): give applications a menu #3135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Db; | ||
|
|
||
| use JsonSerializable; | ||
|
|
||
| /** | ||
| * An item of the menu of an application. It opens a view, a table, a page of the | ||
| * application or a link, and may sit under a top-level item. | ||
| * | ||
| * @method getContextId(): int | ||
| * @method setContextId(int $contextId): void | ||
| * @method getLabel(): string | ||
| * @method setLabel(string $label): void | ||
| * @method getIcon(): ?string | ||
| * @method setIcon(?string $icon): void | ||
| * @method getDescription(): ?string | ||
| * @method setDescription(?string $description): void | ||
| * @method getTargetType(): string | ||
| * @method setTargetType(string $targetType): void | ||
| * @method getTargetId(): ?int | ||
| * @method setTargetId(?int $targetId): void | ||
| * @method getUrl(): ?string | ||
| * @method setUrl(?string $url): void | ||
| * @method getTechnicalName(): string | ||
| * @method setTechnicalName(string $technicalName): void | ||
| * @method getOrder(): int | ||
| * @method setOrder(int $order): void | ||
| * @method getParentId(): ?int | ||
| * @method setParentId(?int $parentId): void | ||
| * @method isBroken(): bool | ||
| * @method setBroken(bool $broken): void | ||
| */ | ||
| class MenuItem extends EntitySuper implements JsonSerializable { | ||
| public const TARGET_VIEW = 'view'; | ||
| public const TARGET_TABLE = 'table'; | ||
| public const TARGET_URL = 'url'; | ||
| public const TARGET_PAGE = 'page'; | ||
| public const TARGET_TYPES = [self::TARGET_VIEW, self::TARGET_TABLE, self::TARGET_URL, self::TARGET_PAGE]; | ||
|
|
||
| protected ?int $contextId = null; | ||
| protected ?string $label = null; | ||
| protected ?string $icon = null; | ||
| protected ?string $description = null; | ||
| protected ?string $targetType = null; | ||
| protected ?int $targetId = null; | ||
| protected ?string $url = null; | ||
| protected ?string $technicalName = null; | ||
| protected ?int $order = null; | ||
| protected ?int $parentId = null; | ||
|
|
||
| /** True when the view, table or page the item opens is gone; not stored. */ | ||
| protected bool $broken = false; | ||
|
|
||
| protected const VIRTUAL_PROPERTIES = ['broken']; | ||
|
|
||
| public function __construct() { | ||
| $this->addType('id', 'integer'); | ||
| $this->addType('contextId', 'integer'); | ||
| $this->addType('targetId', 'integer'); | ||
| $this->addType('order', 'integer'); | ||
| $this->addType('parentId', 'integer'); | ||
| $this->addType('broken', 'boolean'); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{id: int, contextId: int, label: string, icon: string|null, description: string|null, targetType: 'view'|'table'|'url'|'page', targetId: int|null, url: string|null, technicalName: string, order: int, parentId: int|null, broken: bool} | ||
| */ | ||
| public function jsonSerialize(): array { | ||
| return [ | ||
| 'id' => $this->getId(), | ||
| 'contextId' => $this->getContextId(), | ||
| 'label' => $this->getLabel() ?? '', | ||
| 'icon' => $this->getIcon(), | ||
| 'description' => $this->getDescription(), | ||
| 'targetType' => match ($this->targetType) { | ||
| self::TARGET_VIEW => self::TARGET_VIEW, | ||
| self::TARGET_TABLE => self::TARGET_TABLE, | ||
| self::TARGET_PAGE => self::TARGET_PAGE, | ||
| default => self::TARGET_URL, | ||
| }, | ||
| 'targetId' => $this->getTargetId(), | ||
| 'url' => $this->getUrl(), | ||
| 'technicalName' => $this->technicalName ?? '', | ||
| 'order' => $this->getOrder() ?? 0, | ||
| 'parentId' => $this->getParentId(), | ||
| 'broken' => $this->broken, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Db; | ||
|
|
||
| use OCP\AppFramework\Db\QBMapper; | ||
| use OCP\AppFramework\Db\TTransactional; | ||
| use OCP\DB\Exception; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
|
|
||
| /** @template-extends QBMapper<MenuItem> */ | ||
| class MenuItemMapper extends QBMapper { | ||
| use TTransactional; | ||
|
|
||
| private const DB_CHUNK_SIZE = 1_000; | ||
|
|
||
| protected string $table = 'tables_contexts_menu_item'; | ||
|
|
||
| public function __construct(IDBConnection $db) { | ||
| parent::__construct($db, $this->table, MenuItem::class); | ||
| } | ||
|
|
||
| /** | ||
| * Inserts a menu item under the default technical name `item_<id>`. | ||
| * | ||
| * @throws Exception | ||
| */ | ||
| public function insertWithDefaultName(MenuItem $menuItem): MenuItem { | ||
| return $this->atomic(function () use ($menuItem): MenuItem { | ||
| $menuItem = $this->insert($menuItem); | ||
| $menuItem->setTechnicalName('item_' . $menuItem->getId()); | ||
| return $this->update($menuItem); | ||
| }, $this->db); | ||
| } | ||
|
|
||
| /** | ||
| * @return MenuItem[] ordered by their position in the menu | ||
| * @throws Exception | ||
| */ | ||
| public function findByContextId(int $contextId): array { | ||
| return $this->findByContextIds([$contextId])[$contextId] ?? []; | ||
| } | ||
|
|
||
| /** | ||
| * The menu items of the given contexts, each list ordered by position. An item whose | ||
| * view or table no longer exists, or whose page is gone or belongs to another context, | ||
| * is marked broken. | ||
| * | ||
| * @param int[] $contextIds | ||
| * @return array<int, list<MenuItem>> keyed by context id | ||
| * @throws Exception | ||
| */ | ||
| public function findByContextIds(array $contextIds): array { | ||
| if ($contextIds === []) { | ||
| return []; | ||
| } | ||
|
|
||
| $qb = $this->db->getQueryBuilder(); | ||
| $brokenExpression = sprintf( | ||
| 'CASE WHEN (m.target_type = %s AND v.id IS NULL) OR (m.target_type = %s AND t.id IS NULL) OR (m.target_type = %s AND p.id IS NULL) THEN 1 ELSE 0 END', | ||
| $qb->createNamedParameter(MenuItem::TARGET_VIEW), | ||
| $qb->createNamedParameter(MenuItem::TARGET_TABLE), | ||
| $qb->createNamedParameter(MenuItem::TARGET_PAGE), | ||
| ); | ||
| $qb->select('m.*') | ||
| ->selectAlias($qb->createFunction($brokenExpression), 'broken') | ||
| ->from($this->table, 'm') | ||
| ->leftJoin('m', 'tables_views', 'v', $qb->expr()->andX( | ||
| $qb->expr()->eq('v.id', 'm.target_id'), | ||
| $qb->expr()->eq('m.target_type', $qb->createNamedParameter(MenuItem::TARGET_VIEW)), | ||
| )) | ||
| ->leftJoin('m', 'tables_tables', 't', $qb->expr()->andX( | ||
| $qb->expr()->eq('t.id', 'm.target_id'), | ||
| $qb->expr()->eq('m.target_type', $qb->createNamedParameter(MenuItem::TARGET_TABLE)), | ||
| )) | ||
| ->leftJoin('m', 'tables_contexts_page', 'p', $qb->expr()->andX( | ||
| $qb->expr()->eq('p.id', 'm.target_id'), | ||
| $qb->expr()->eq('m.target_type', $qb->createNamedParameter(MenuItem::TARGET_PAGE)), | ||
| $qb->expr()->eq('p.context_id', 'm.context_id'), | ||
| )) | ||
| ->where($qb->expr()->in('m.context_id', $qb->createParameter('contextIds'))) | ||
| ->orderBy('m.context_id', 'ASC') | ||
| ->addOrderBy('m.order', 'ASC') | ||
| ->addOrderBy('m.id', 'ASC'); | ||
|
|
||
| $byContext = []; | ||
| foreach (array_chunk(array_values(array_unique($contextIds)), self::DB_CHUNK_SIZE) as $chunk) { | ||
| $qb->setParameter('contextIds', $chunk, IQueryBuilder::PARAM_INT_ARRAY); | ||
| foreach ($this->findEntities($qb) as $menuItem) { | ||
| $byContext[$menuItem->getContextId()][] = $menuItem; | ||
| } | ||
| } | ||
| return $byContext; | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function deleteAllByContextId(int $contextId): void { | ||
| $qb = $this->db->getQueryBuilder(); | ||
| $qb->delete($this->table) | ||
| ->where($qb->expr()->eq('context_id', $qb->createNamedParameter($contextId, IQueryBuilder::PARAM_INT))); | ||
| $qb->executeStatement(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Permission checks use
findById(), so each check now runs this menu query for no reason. Maybe should be conditional