From: Marcin Haba Date: Fri, 7 Jul 2023 11:52:14 +0000 (+0200) Subject: baculum: Add second dimensional sorting and use it for sorting jobstatus in sources... X-Git-Tag: Release-13.0.4~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd5dc345f95933e2be9a4f6441ea5d47e8c69cdf;p=thirdparty%2Fbacula.git baculum: Add second dimensional sorting and use it for sorting jobstatus in sources overview endpoint --- diff --git a/gui/baculum/protected/API/Modules/SourceManager.php b/gui/baculum/protected/API/Modules/SourceManager.php index 203ed6176..848ac2ce5 100644 --- a/gui/baculum/protected/API/Modules/SourceManager.php +++ b/gui/baculum/protected/API/Modules/SourceManager.php @@ -201,7 +201,22 @@ class SourceManager extends APIModule { // Sort items if needed if (is_string($order_by)) { // Sort all items - $misc->sortResultsByField($sources, $order_by, $order_direction); + if ($order_by == 'jobstatus') { + // for jobstatus sort also by joberrors + $misc::sortResultsByField( + $sources, + $order_by, + $order_direction, + 'joberrors', + $misc::ORDER_DIRECTION_ASC + ); + } else { + $misc::sortResultsByField( + $sources, + $order_by, + $order_direction + ); + } } if ($mode == self::SOURCE_RESULT_MODE_OVERVIEW) { diff --git a/gui/baculum/protected/Common/Modules/Miscellaneous.php b/gui/baculum/protected/Common/Modules/Miscellaneous.php index 5adc66499..d6ad5c763 100644 --- a/gui/baculum/protected/Common/Modules/Miscellaneous.php +++ b/gui/baculum/protected/Common/Modules/Miscellaneous.php @@ -346,13 +346,26 @@ class Miscellaneous extends TModule { return $jobid; } - public function sortResultsByField(&$result, $order_by, $order_direction) { + public static function sortResultsByField(&$result, $order_by, $order_direction, $sec_order_by = '', $sec_order_direction = '') { $order_by = strtolower($order_by); $order_direction = strtolower($order_direction); - $sort_by_func = function($a, $b) use ($order_by, $order_direction) { - $cmp = strnatcasecmp($a[$order_by], $b[$order_by]); - if ($order_direction === self::ORDER_DIRECTION_DESC) { - $cmp = -$cmp; + $sec_order_by = strtolower($sec_order_by); + $sec_order_direction = strtolower($sec_order_direction); + $sort_by_func = function($a, $b) use ($order_by, $order_direction, $sec_order_by, $sec_order_direction) { + $cmp = 0; + if ($a[$order_by] == $b[$order_by]) { + if ($sec_order_by && $sec_order_direction) { + if ($sec_order_direction === self::ORDER_DIRECTION_ASC) { + $cmp = strnatcasecmp($a[$sec_order_by], $b[$sec_order_by]); + } elseif ($sec_order_direction === self::ORDER_DIRECTION_DESC) { + $cmp = strnatcasecmp($b[$sec_order_by], $a[$sec_order_by]); + } + } + } else { + $cmp = strnatcasecmp($a[$order_by], $b[$order_by]); + if ($order_direction === self::ORDER_DIRECTION_DESC) { + $cmp = -$cmp; + } } return $cmp; };