implement IAPIWidget interface
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
@@ -26,18 +26,35 @@ declare(strict_types=1);
|
||||
|
||||
namespace OCA\Deck\Dashboard;
|
||||
|
||||
use OCP\Dashboard\IWidget;
|
||||
use DateTime;
|
||||
use OCA\Deck\AppInfo\Application;
|
||||
use OCA\Deck\Db\Label;
|
||||
use OCA\Deck\Service\OverviewService;
|
||||
use OCP\Dashboard\IAPIWidget;
|
||||
use OCP\Dashboard\Model\WidgetItem;
|
||||
use OCP\IDateTimeFormatter;
|
||||
use OCP\IL10N;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\Util;
|
||||
|
||||
class DeckWidget implements IWidget {
|
||||
class DeckWidget implements IAPIWidget {
|
||||
|
||||
/**
|
||||
* @var IL10N
|
||||
*/
|
||||
private $l10n;
|
||||
private IL10N $l10n;
|
||||
private ?string $userId;
|
||||
private OverviewService $dashboardService;
|
||||
private IURLGenerator $urlGenerator;
|
||||
private IDateTimeFormatter $dateTimeFormatter;
|
||||
|
||||
public function __construct(IL10N $l10n) {
|
||||
public function __construct(IL10N $l10n,
|
||||
OverviewService $dashboardService,
|
||||
IDateTimeFormatter $dateTimeFormatter,
|
||||
IURLGenerator $urlGenerator,
|
||||
?string $userId) {
|
||||
$this->l10n = $l10n;
|
||||
$this->userId = $userId;
|
||||
$this->dashboardService = $dashboardService;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->dateTimeFormatter = $dateTimeFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,6 +96,50 @@ class DeckWidget implements IWidget {
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function load(): void {
|
||||
\OCP\Util::addScript('deck', 'deck-dashboard');
|
||||
Util::addScript('deck', 'deck-dashboard');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
|
||||
$upcomingCards = $this->dashboardService->findUpcomingCards($this->userId);
|
||||
$nowTimestamp = (new Datetime())->getTimestamp();
|
||||
$upcomingCards = array_filter($upcomingCards, static function(array $card) use ($nowTimestamp) {
|
||||
if ($card['duedate']) {
|
||||
$ts = (new Datetime($card['duedate']))->getTimestamp();
|
||||
return $ts > $nowTimestamp;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
usort($upcomingCards, static function($a, $b) {
|
||||
$a = new Datetime($a['duedate']);
|
||||
$ta = $a->getTimestamp();
|
||||
$b = new Datetime($b['duedate']);
|
||||
$tb = $b->getTimestamp();
|
||||
return ($ta > $tb) ? 1 : -1;
|
||||
});
|
||||
$upcomingCards = array_slice($upcomingCards, 0, $limit);
|
||||
$urlGenerator = $this->urlGenerator;
|
||||
$dateTimeFormatter = $this->dateTimeFormatter;
|
||||
return array_map(static function(array $card) use ($urlGenerator, $dateTimeFormatter) {
|
||||
$formattedDueDate = $dateTimeFormatter->formatDateTime(new DateTime($card['duedate']));
|
||||
return new WidgetItem(
|
||||
$card['title'] . ' (' . $formattedDueDate . ')',
|
||||
implode(
|
||||
', ',
|
||||
array_map(static function(Label $label) {
|
||||
return $label->jsonSerialize()['title'];
|
||||
}, $card['labels'])
|
||||
),
|
||||
$urlGenerator->getAbsoluteURL(
|
||||
$urlGenerator->linkToRoute(Application::APP_ID . '.page.redirectToCard', ['cardId' => $card['id']])
|
||||
),
|
||||
$urlGenerator->getAbsoluteURL(
|
||||
$urlGenerator->imagePath(Application::APP_ID, 'deck-dark.svg')
|
||||
),
|
||||
$card['duedate']
|
||||
);
|
||||
}, $upcomingCards);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user