|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Files\Dashboard; |
| 11 | + |
| 12 | +use OCA\Files\AppInfo\Application; |
| 13 | +use OCP\Dashboard\IAPIWidget; |
| 14 | +use OCP\Dashboard\IAPIWidgetV2; |
| 15 | +use OCP\Dashboard\IButtonWidget; |
| 16 | +use OCP\Dashboard\IIconWidget; |
| 17 | +use OCP\Dashboard\IWidget; |
| 18 | +use OCP\Dashboard\Model\WidgetButton; |
| 19 | +use OCP\Dashboard\Model\WidgetItem; |
| 20 | +use OCP\Dashboard\Model\WidgetItems; |
| 21 | +use OCP\Files\IMimeTypeDetector; |
| 22 | +use OCP\Files\IRootFolder; |
| 23 | +use OCP\IL10N; |
| 24 | +use OCP\IPreview; |
| 25 | +use OCP\ITagManager; |
| 26 | +use OCP\IURLGenerator; |
| 27 | +use OCP\IUserManager; |
| 28 | +use OCP\IUserSession; |
| 29 | + |
| 30 | +class FavouriteWidget implements IWidget, IIconWidget, IAPIWidget, IAPIWidgetV2, IButtonWidget { |
| 31 | + private IUserSession $userSession; |
| 32 | + private IL10N $l10n; |
| 33 | + private IURLGenerator $urlGenerator; |
| 34 | + private IMimeTypeDetector $mimeTypeDetector; |
| 35 | + private IUserManager $userManager; |
| 36 | + private ITagManager $tagManager; |
| 37 | + private IRootFolder $rootFolder; |
| 38 | + private IPreview $previewManager; |
| 39 | + public const FAVORITE_LIMIT = 50; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + IUserSession $userSession, |
| 43 | + IL10N $l10n, |
| 44 | + IURLGenerator $urlGenerator, |
| 45 | + IMimeTypeDetector $mimeTypeDetector, |
| 46 | + IUserManager $userManager, |
| 47 | + ITagManager $tagManager, |
| 48 | + IRootFolder $rootFolder, |
| 49 | + IPreview $previewManager, |
| 50 | + ) { |
| 51 | + $this->userSession = $userSession; |
| 52 | + $this->l10n = $l10n; |
| 53 | + $this->urlGenerator = $urlGenerator; |
| 54 | + $this->mimeTypeDetector = $mimeTypeDetector; |
| 55 | + $this->userManager = $userManager; |
| 56 | + $this->tagManager = $tagManager; |
| 57 | + $this->rootFolder = $rootFolder; |
| 58 | + $this->previewManager = $previewManager; |
| 59 | + } |
| 60 | + |
| 61 | + public function getId(): string { |
| 62 | + return Application::APP_ID; |
| 63 | + } |
| 64 | + |
| 65 | + public function getTitle(): string { |
| 66 | + return $this->l10n->t('Favorites'); |
| 67 | + } |
| 68 | + |
| 69 | + public function getOrder(): int { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + public function getIconClass(): string { |
| 74 | + return 'icon-files-dark'; |
| 75 | + } |
| 76 | + |
| 77 | + public function getIconUrl(): string { |
| 78 | + return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'app-dark.svg')); |
| 79 | + } |
| 80 | + |
| 81 | + public function getUrl(): ?string { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + public function load(): void { |
| 86 | + $user = $this->userSession->getUser(); |
| 87 | + if ($user === null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + return; |
| 91 | + //Util::addScript(Application::APP_ID, 'recommendations-dashboard'); |
| 92 | + } |
| 93 | + |
| 94 | + public function getItems(string $userId, ?string $since = null, int $limit = 7): array { |
| 95 | + $user = $this->userManager->get($userId); |
| 96 | + |
| 97 | + if (!$user) { |
| 98 | + return []; |
| 99 | + } |
| 100 | + $tags = $this->tagManager->load('files', [], false, $userId); |
| 101 | + $favorites = $tags->getFavorites(); |
| 102 | + if (empty($favorites)) { |
| 103 | + return []; |
| 104 | + } elseif (isset($favorites[self::FAVORITE_LIMIT])) { |
| 105 | + return []; |
| 106 | + } |
| 107 | + $favoriteNodes = []; |
| 108 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
| 109 | + foreach ($favorites as $favorite) { |
| 110 | + $node = $userFolder->getFirstNodeById($favorite); |
| 111 | + if ($node) { |
| 112 | + $url = $this->urlGenerator->linkToRouteAbsolute( |
| 113 | + 'files.viewcontroller.showFile', ['fileid' => $node->getId()] |
| 114 | + ); |
| 115 | + if ($this->previewManager->isAvailable($node)) { |
| 116 | + $icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [ |
| 117 | + 'x' => 256, |
| 118 | + 'y' => 256, |
| 119 | + 'fileId' => $node->getId(), |
| 120 | + 'c' => $node->getEtag(), |
| 121 | + ]); |
| 122 | + } else { |
| 123 | + $icon = $this->urlGenerator->getAbsoluteURL( |
| 124 | + $this->mimeTypeDetector->mimeTypeIcon($node->getMimetype()) |
| 125 | + ); |
| 126 | + } |
| 127 | + $favoriteNodes[] = new WidgetItem( |
| 128 | + $node->getName(), |
| 129 | + '', |
| 130 | + $url, |
| 131 | + $icon, |
| 132 | + (string)$node->getCreationTime() |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return $favoriteNodes; |
| 138 | + } |
| 139 | + |
| 140 | + public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems { |
| 141 | + $items = $this->getItems($userId, $since, $limit); |
| 142 | + return new WidgetItems( |
| 143 | + $items, |
| 144 | + count($items) === 0 ? $this->l10n->t('No favorites') : '', |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + public function getWidgetButtons(string $userId): array { |
| 149 | + return [ |
| 150 | + new WidgetButton( |
| 151 | + WidgetButton::TYPE_MORE, |
| 152 | + $this->urlGenerator->getAbsoluteURL('index.php/apps/files/favorites'), |
| 153 | + $this->l10n->t('More favorites') |
| 154 | + ), |
| 155 | + ]; |
| 156 | + } |
| 157 | +} |
0 commit comments