mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 07:37:18 +01:00
Add dashboard widget to show the latest notes. Issue #51
p.s: I would like to be able to select a particular tag to show, but none of the dashboard widgets are configurable. I hate increasing the app size from 300k to almost 3 mb for something so simple, but we must adapt to the majority and use vue here.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* @copyright 2016-2021 Matias De lellis <mati86dl@gmail.com>
|
||||
* @copyright 2016-2022 Matias De lellis <mati86dl@gmail.com>
|
||||
*
|
||||
* @author 2016 Matias De lellis <mati86dl@gmail.com>
|
||||
*
|
||||
@@ -27,6 +27,8 @@ use OCP\AppFramework\Bootstrap\IBootstrap;
|
||||
use OCP\AppFramework\Bootstrap\IBootContext;
|
||||
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
||||
|
||||
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
|
||||
|
||||
use OCP\IL10N;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IServerContainer;
|
||||
@@ -48,6 +50,11 @@ class Application extends App implements IBootstrap {
|
||||
public function register(IRegistrationContext $context): void {
|
||||
$context->registerSearchProvider(NoteSearchProvider::class);
|
||||
$context->registerCapability(Capabilities::class);
|
||||
$context->registerDashboardWidget(DashboardWidget::class);
|
||||
$context->registerEventListener(
|
||||
BeforeTemplateRenderedEvent::class,
|
||||
BeforeTemplateRenderedListener::class
|
||||
);
|
||||
}
|
||||
|
||||
public function boot(IBootContext $context): void {
|
||||
|
||||
26
lib/AppInfo/BeforeTemplateRenderedListener.php
Normal file
26
lib/AppInfo/BeforeTemplateRenderedListener.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\QuickNotes\AppInfo;
|
||||
|
||||
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
|
||||
use OCP\EventDispatcher\Event;
|
||||
use OCP\EventDispatcher\IEventListener;
|
||||
|
||||
class BeforeTemplateRenderedListener implements IEventListener {
|
||||
|
||||
public function handle(Event $event): void {
|
||||
|
||||
if (!($event instanceof BeforeTemplateRenderedEvent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$event->isLoggedIn()) {
|
||||
return;
|
||||
}
|
||||
|
||||
\OCP\Util::addStyle('quicknotes', 'global');
|
||||
}
|
||||
|
||||
}
|
||||
64
lib/AppInfo/DashboardWidget.php
Normal file
64
lib/AppInfo/DashboardWidget.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\QuickNotes\AppInfo;
|
||||
|
||||
use OCP\Dashboard\IWidget;
|
||||
use OCP\IL10N;
|
||||
use OCP\IURLGenerator;
|
||||
|
||||
class DashboardWidget implements IWidget {
|
||||
|
||||
private IURLGenerator $url;
|
||||
private IL10N $l10n;
|
||||
|
||||
public function __construct(IURLGenerator $url,
|
||||
IL10N $l10n)
|
||||
{
|
||||
$this->url = $url;
|
||||
$this->l10n = $l10n;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getId(): string {
|
||||
return 'quicknotes';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string {
|
||||
return $this->l10n->t('Quick notes');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOrder(): int {
|
||||
return 30;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getIconClass(): string {
|
||||
return 'icon-quicknotes';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUrl(): ?string {
|
||||
return $this->url->linkToRouteAbsolute('quicknotes.page.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function load(): void {
|
||||
\OCP\Util::addScript('quicknotes', 'quicknotes-dashboard');
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,41 @@ class NoteController extends Controller {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*/
|
||||
public function dashboard(): JSONResponse {
|
||||
$notes = $this->noteService->getAll($this->userId);
|
||||
if (count($notes) === 0) {
|
||||
return new JSONResponse([
|
||||
'notes' => []
|
||||
]);
|
||||
}
|
||||
|
||||
$items = array_map(function ($note) {
|
||||
return [
|
||||
'id' => $note->getId(),
|
||||
'title' => strip_tags($note->getTitle()),
|
||||
'content' => strip_tags($note->getContent()),
|
||||
'pinned' => $note->getIsPinned(),
|
||||
'timestamp' => $note->getTimestamp(),
|
||||
];
|
||||
}, $notes);
|
||||
|
||||
usort($items, function ($a, $b) {
|
||||
if ($a['pinned'] == $b['pinned'])
|
||||
return $b['timestamp'] - $a['timestamp'];
|
||||
if ($a['pinned'] && !$b['pinned'])
|
||||
return -1;
|
||||
if (!$a['pinned'] && $b['pinned'])
|
||||
return 1;
|
||||
});
|
||||
|
||||
return new JSONResponse([
|
||||
'notes' => array_slice($items, 0, 7)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @NoAdminRequired
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user