File size: 8,511 Bytes
58ce155 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Dashboard;
use Piwik\API\Request;
use Piwik\Piwik;
/**
* The Dashboard API lets you manage user dashboards and retrieve their widget configurations.
*
* @method static \Piwik\Plugins\Dashboard\API getInstance()
*
* @phpstan-type DashboardWidget array{module: string, action: string}
* @phpstan-type DashboardInfo array{name: string, id: int, widgets: list<DashboardWidget>}
* @phpstan-type DashboardRecord array{name: string, iddashboard: int, layout: mixed}
*/
class API extends \Piwik\Plugin\API
{
/**
* @var Dashboard
*/
private $dashboard;
/**
* @var Model
*/
private $model;
public function __construct(Dashboard $dashboard, Model $model)
{
$this->dashboard = $dashboard;
$this->model = $model;
}
/**
* Returns the dashboards available to a user, including the widgets in each dashboard.
*
* @param string $login Login of the user to load dashboards for. Defaults to the current user.
* @param bool $returnDefaultIfEmpty Whether to return the default dashboard when the user has none.
* @return list<DashboardInfo> List of dashboards, each containing name, id, and widgets keys.
*/
public function getDashboards(string $login = '', bool $returnDefaultIfEmpty = true): array
{
$login = $login ?: Piwik::getCurrentUserLogin();
$dashboards = [];
if (!Piwik::isUserIsAnonymous()) {
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$dashboards = $this->getUserDashboards($login);
}
if (empty($dashboards) && $returnDefaultIfEmpty) {
$dashboards = [$this->getDefaultDashboard()];
}
return $dashboards;
}
/**
* Creates a new dashboard for a user.
*
* @param string $login Login of the user the dashboard should be created for.
* @param string $dashboardName Name of the new dashboard.
* @param bool $addDefaultWidgets Whether to populate the dashboard with the default widget layout.
* @return int|string ID of the newly created dashboard.
*/
public function createNewDashboardForUser(string $login, string $dashboardName = '', bool $addDefaultWidgets = true)
{
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$layout = '{}';
if ($addDefaultWidgets) {
$layout = $this->dashboard->getDefaultLayout();
}
return $this->model->createNewDashboardForUser($login, $dashboardName, $layout);
}
/**
* Removes a dashboard for a user.
*
* Note: Deleting the first dashboard (ID = 1) is allowed but will cause buggy behavior
* unless a new dashboard is immediately added. This should only be done for automation purposes.
*
* @param int $idDashboard ID of the dashboard to remove.
* @param string $login Login of the dashboard owner. Defaults to the current user.
*/
public function removeDashboard(int $idDashboard, string $login = ''): void
{
$login = $login ?: Piwik::getCurrentUserLogin();
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$this->model->deleteDashboardForUser($idDashboard, $login);
}
/**
* Copies one of the current user's dashboards to another user. Requires admin access.
*
* @param int $idDashboard ID of the dashboard to copy.
* @param string $copyToUser Login of the user that should receive the dashboard copy.
* @param string $dashboardName Name for the copied dashboard.
* @return int|string ID of the newly created dashboard copy.
*/
public function copyDashboardToUser(int $idDashboard, string $copyToUser, string $dashboardName = '')
{
Piwik::checkUserHasSomeAdminAccess();
// get users only returns users of sites the current user has at least admin access to
/** @var array $users */
$users = Request::processRequest('UsersManager.getUsers', ['filter_limit' => -1]);
$userFound = false;
foreach ($users as $user) {
if ($user['login'] === $copyToUser) {
$userFound = true;
break;
}
}
if (!$userFound) {
throw new \Exception(sprintf('Cannot copy dashboard to user %s, user not found.', $copyToUser));
}
$login = Piwik::getCurrentUserLogin();
$layout = $this->dashboard->getLayoutForUser($login, $idDashboard);
if ($layout !== false) {
return $this->model->createNewDashboardForUser($copyToUser, $dashboardName, $layout);
}
throw new \Exception('Dashboard not found');
}
/**
* Resets a dashboard to the default widget layout.
*
* @param int $idDashboard ID of the dashboard to reset.
* @param string $login Login of the dashboard owner. Defaults to the current user.
*/
public function resetDashboardLayout(int $idDashboard, string $login = ''): void
{
$login = $login ?: Piwik::getCurrentUserLogin();
$this->checkLoginIsNotAnonymous($login);
Piwik::checkUserHasSuperUserAccessOrIsTheUser($login);
$layout = $this->dashboard->getDefaultLayout();
$this->model->updateLayoutForUser($login, $idDashboard, $layout);
}
/**
* @return DashboardInfo
*/
private function getDefaultDashboard(): array
{
$defaultLayout = $this->dashboard->getDefaultLayout();
$defaultLayout = $this->dashboard->decodeLayout($defaultLayout);
$defaultDashboard = ['name' => Piwik::translate('Dashboard_Dashboard'), 'layout' => $defaultLayout, 'iddashboard' => 1];
$widgets = $this->getVisibleWidgetsWithinDashboard($defaultDashboard);
return $this->buildDashboard($defaultDashboard, $widgets);
}
/**
* @return list<DashboardInfo>
*/
private function getUserDashboards(string $userLogin): array
{
$userDashboards = $this->dashboard->getAllDashboards($userLogin);
$dashboards = [];
foreach ($userDashboards as $userDashboard) {
$widgets = $this->getVisibleWidgetsWithinDashboard($userDashboard);
$dashboards[] = $this->buildDashboard($userDashboard, $widgets);
}
return $dashboards;
}
/**
* @param DashboardRecord $dashboard
* @return list<DashboardWidget>
*/
private function getVisibleWidgetsWithinDashboard(array $dashboard): array
{
$columns = $this->getColumnsFromDashboard($dashboard);
$widgets = [];
$columns = array_filter($columns);
foreach ($columns as $column) {
foreach ($column as $widget) {
if ($this->widgetIsNotHidden($widget) && !empty($widget->parameters->module)) {
$module = $widget->parameters->module;
$action = $widget->parameters->action;
$widgets[] = ['module' => $module, 'action' => $action];
}
}
}
return $widgets;
}
private function checkLoginIsNotAnonymous(string $login): void
{
Piwik::checkUserIsNotAnonymous();
if (strtolower($login) === 'anonymous') {
throw new \Exception('This method can\'t be performed for anonymous user');
}
}
/**
* @param DashboardRecord $dashboard
*/
private function getColumnsFromDashboard(array $dashboard): array
{
if (empty($dashboard['layout'])) {
return [];
}
if (is_array($dashboard['layout'])) {
return $dashboard['layout'];
}
if (!empty($dashboard['layout']->columns)) {
return $dashboard['layout']->columns;
}
return [];
}
/**
* @param DashboardRecord $dashboard
* @param list<DashboardWidget> $widgets
* @return DashboardInfo
*/
private function buildDashboard(array $dashboard, array $widgets): array
{
return ['name' => $dashboard['name'], 'id' => (int)$dashboard['iddashboard'], 'widgets' => $widgets];
}
private function widgetIsNotHidden(object $widget): bool
{
return empty($widget->isHidden);
}
}
|