Skip to content
Merged
Next Next commit
add dashboard api to list widgets
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 authored and Julien Veyssier committed Sep 15, 2022
commit 0e5944748df24fde54e34cb1fcc0f15fb0d734de
1 change: 1 addition & 0 deletions apps/dashboard/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
['name' => 'dashboard#updateStatuses', 'url' => '/statuses', 'verb' => 'POST'],
],
'ocs' => [
['name' => 'dashboardApi#getWidgets', 'url' => '/api/v1/widgets', 'verb' => 'GET'],
['name' => 'dashboardApi#getWidgetItems', 'url' => '/api/v1/widget-items', 'verb' => 'GET'],
]
];
26 changes: 26 additions & 0 deletions apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

use OCP\AppFramework\OCSController;
use OCP\AppFramework\Http\DataResponse;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IManager;
use OCP\Dashboard\IWidget;
use OCP\IConfig;
use OCP\IRequest;

Expand Down Expand Up @@ -83,4 +85,28 @@ public function getWidgetItems(array $sinceIds = [], int $limit = 7): DataRespon

return new DataResponse($items);
}

/**
* Example request with Curl:
* curl -u user:passwd http://my.nc/ocs/v2.php/apps/dashboard/api/v1/widgets
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getWidgets(): DataResponse {
$widgets = $this->dashboardManager->getWidgets();

$items = array_map(function(IWidget $widget) {
return [
'id' => $widget->getId(),
'title' => $widget->getTitle(),
'order' => $widget->getOrder(),
'icon_class' => $widget->getIconClass(),
'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
'url' => $widget->getUrl(),
];
}, $widgets);

return new DataResponse($items);
}
}
37 changes: 37 additions & 0 deletions lib/public/Dashboard/IIconWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Dashboard;

/**
* Allow getting the absolute icon url for a widget
*
* @since 25.0.0
*/
interface IIconWidget extends IWidget {
/**
* Get the absolute url for the widget icon
*
* @return string
*/
public function getIconUrl(): string;
}