diff --git a/library/Icinga/Web/Dashboard/Common/DashboardControls.php b/library/Icinga/Web/Dashboard/Common/DashboardEntries.php similarity index 82% rename from library/Icinga/Web/Dashboard/Common/DashboardControls.php rename to library/Icinga/Web/Dashboard/Common/DashboardEntries.php index fd5501718..8c2f5e620 100644 --- a/library/Icinga/Web/Dashboard/Common/DashboardControls.php +++ b/library/Icinga/Web/Dashboard/Common/DashboardEntries.php @@ -9,7 +9,7 @@ use Icinga\Exception\ProgrammingError; use Icinga\Web\Dashboard\Dashboard; use function ipl\Stdlib\get_php_type; -trait DashboardControls +trait DashboardEntries { /** * A list of @see BaseDashboard assigned to this dashboard widget @@ -18,6 +18,13 @@ trait DashboardControls */ private $dashboards = []; + /** + * Whether to sort the entries when retrieving using getEntries() + * + * @var bool + */ + private $sortEntries = false; + public function hasEntries() { return ! empty($this->dashboards); @@ -39,11 +46,15 @@ trait DashboardControls public function getEntries() { - // 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(); - }); + if ($this->sortEntries) { + $this->sortEntries = false; + + // 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; } @@ -51,6 +62,7 @@ trait DashboardControls public function setEntries(array $entries) { $this->dashboards = $entries; + $this->sortEntries = true; return $this; } @@ -61,6 +73,7 @@ trait DashboardControls $this->getEntry($dashboard->getName())->fromArray($dashboard->toArray(false)); } else { $this->dashboards[$dashboard->getName()] = $dashboard; + $this->sortEntries = true; } return $this;