Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions lib/AppInfo/DashboardWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@

namespace OCA\Notes\AppInfo;

use OCA\Notes\Service\Note;
use OCA\Notes\Service\NotesService;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IButtonWidget;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IWidget;
use OCP\Dashboard\Model\WidgetButton;
use OCP\Dashboard\Model\WidgetItem;
use OCP\IL10N;
use OCP\IURLGenerator;

class DashboardWidget implements IWidget {
class DashboardWidget implements IWidget, IButtonWidget, IAPIWidget, IIconWidget {
private IURLGenerator $url;
private IL10N $l10n;
private NotesService $notesService;

public function __construct(
IURLGenerator $url,
IL10N $l10n
IL10N $l10n,
NotesService $notesService
) {
$this->url = $url;
$this->l10n = $l10n;
$this->notesService = $notesService;
}

/**
Expand Down Expand Up @@ -61,4 +71,37 @@ public function getUrl(): ?string {
public function load(): void {
\OCP\Util::addScript('notes', 'notes-dashboard');
}

public function getWidgetButtons(string $userId): array {
$buttons = [
new WidgetButton(WidgetButton::TYPE_NEW, $this->url->linkToRouteAbsolute('notes.page.create'), $this->l10n->t('Create new note'))
];
if ($this->notesService->countNotes($userId) > 7) {
$buttons[] = new WidgetButton(WidgetButton::TYPE_MORE, $this->url->linkToRouteAbsolute('notes.page.index'), $this->l10n->t('More notes'));
}
return $buttons;
}

public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
$notes = $this->notesService->getTopNotes($userId);
$notes = array_slice($notes, 0, $limit);
return array_map(function (Note $note) {
$excerpt = '';
try {
$excerpt = $note->getExcerpt();
} catch (\Throwable $e) {
}
$link = $this->url->linkToRouteAbsolute('notes.page.indexnote', ['id' => $note->getId()]);
$icon = $this->url->getAbsoluteURL(
$note->getFavorite()
? $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/starred.svg'))
: $this->getIconUrl()
);
return new WidgetItem($note->getTitle(), $excerpt, $link, $icon, (string)$note->getModified());
}, $notes);
}

public function getIconUrl(): string {
return $this->url->getAbsoluteURL($this->url->imagePath('notes', 'notes-dark.svg'));
}
}
11 changes: 11 additions & 0 deletions lib/Service/NotesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public function getTopNotes(string $userId) : array {
return $notes;
}

public function countNotes(string $userId) : int {
$customExtension = $this->getCustomExtension($userId);
try {
$notesFolder = $this->getNotesFolder($userId, false);
$data = self::gatherNoteFiles($customExtension, $notesFolder);
return count($data['files']);
} catch (NotesFolderException $e) {
return 0;
}
}

public function get(string $userId, int $id) : Note {
$customExtension = $this->getCustomExtension($userId);
$notesFolder = $this->getNotesFolder($userId);
Expand Down