]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add to grup function sorting group capability
authorMarcin Haba <marcin.haba@bacula.pl>
Wed, 7 Jun 2023 08:42:21 +0000 (10:42 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Mon, 3 Jul 2023 08:46:57 +0000 (10:46 +0200)
gui/baculum/protected/API/Modules/Database.php

index 8e135bb86e4bcaba948544916d0cc4be2bedca4f..322a6eac045ce2a05fe8304e7d085d7ff6823f93 100644 (file)
@@ -234,9 +234,11 @@ class Database extends APIModule {
         * @param integer $group_limit group limit (zero means no limit)
         * @param integer $group_offset group offset
         * @param string|null $overview_by prepare overview (counts) by given object output property
+        * @param string|null $sort_by sort group by given object property
+        * @param string $sort_direction sort direction (asc|desc)
         * @param array overview array or empty array if no overview requested
         */
-       public static function groupBy($group_by, &$result, $group_limit = 0, $group_offset = 0, $overview_by = null) {
+       public static function groupBy($group_by, &$result, $group_limit = 0, $group_offset = 0, $overview_by = null, $sort_by = null, $sort_direction = 'asc') {
                $overview = [];
                if (is_string($group_by) && is_array($result)) {
                        // Group results
@@ -275,6 +277,26 @@ class Database extends APIModule {
                                }
                        }
                        $result = $new_result;
+                       if (is_string($sort_by) && is_string($sort_direction)) {
+                               $sort_direction = strtolower($sort_direction);
+                               $func = function ($a, $b) use ($sort_by, $sort_direction) {
+                                       if (!property_exists($a, $sort_by) || !property_exists($b, $sort_by)) {
+                                               return 0;
+                                       }
+                                       $res = strcmp($a->{$sort_by}, $b->{$sort_by});
+                                       if ($sort_direction == 'desc') {
+                                               if ($a->{$sort_by} === $b->{$sort_by}) {
+                                                       return 0;
+                                               }
+                                               return ($a->{$sort_by} < $b->{$sort_by}) ? 1 : -1;
+                                       } else {
+                                               return $res;
+                                       }
+                               };
+                               foreach ($result as $key => &$val) {
+                                       usort($val, $func);
+                               }
+                       }
                }
                return array_values($overview);
        }