Rename DashboardControls -> DashboardEntries & add an internal flag for sorting

This commit is contained in:
Yonas Habteab 2022-04-22 09:19:44 +02:00
parent c96da1da98
commit 5253db1842

View File

@ -9,7 +9,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Web\Dashboard\Dashboard; use Icinga\Web\Dashboard\Dashboard;
use function ipl\Stdlib\get_php_type; use function ipl\Stdlib\get_php_type;
trait DashboardControls trait DashboardEntries
{ {
/** /**
* A list of @see BaseDashboard assigned to this dashboard widget * A list of @see BaseDashboard assigned to this dashboard widget
@ -18,6 +18,13 @@ trait DashboardControls
*/ */
private $dashboards = []; private $dashboards = [];
/**
* Whether to sort the entries when retrieving using getEntries()
*
* @var bool
*/
private $sortEntries = false;
public function hasEntries() public function hasEntries()
{ {
return ! empty($this->dashboards); return ! empty($this->dashboards);
@ -39,11 +46,15 @@ trait DashboardControls
public function getEntries() public function getEntries()
{ {
// An entry can be added individually afterwards, it might be the case that the priority if ($this->sortEntries) {
// order gets mixed up, so we have to sort things here before being able to render them $this->sortEntries = false;
uasort($this->dashboards, function (BaseDashboard $x, BaseDashboard $y) {
return $x->getPriority() <=> $y->getPriority(); // An entry can be added individually afterwards, it might be the case that the priority
}); // order gets mixed up, so we have to sort things here before being able to render them
uasort($this->dashboards, function (BaseDashboard $x, BaseDashboard $y) {
return $x->getPriority() <=> $y->getPriority();
});
}
return $this->dashboards; return $this->dashboards;
} }
@ -51,6 +62,7 @@ trait DashboardControls
public function setEntries(array $entries) public function setEntries(array $entries)
{ {
$this->dashboards = $entries; $this->dashboards = $entries;
$this->sortEntries = true;
return $this; return $this;
} }
@ -61,6 +73,7 @@ trait DashboardControls
$this->getEntry($dashboard->getName())->fromArray($dashboard->toArray(false)); $this->getEntry($dashboard->getName())->fromArray($dashboard->toArray(false));
} else { } else {
$this->dashboards[$dashboard->getName()] = $dashboard; $this->dashboards[$dashboard->getName()] = $dashboard;
$this->sortEntries = true;
} }
return $this; return $this;