From 65f48828c8aa5bb807b4805d725f2e9ccee50b03 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Fri, 21 Aug 2015 07:48:07 +0000 Subject: [PATCH] add legacy_move_config_list_items to legacy base, to replace move pattern in several input forms --- src/etc/inc/legacy_bindings.inc | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/etc/inc/legacy_bindings.inc b/src/etc/inc/legacy_bindings.inc index ff4ff9555..03994b981 100644 --- a/src/etc/inc/legacy_bindings.inc +++ b/src/etc/inc/legacy_bindings.inc @@ -95,3 +95,51 @@ function legacy_list_aliasses($type) { } return $result; } + +/** + * Function to move selected array items before another one. + * Mainly used in form processing. + * @param array $source config section to apply move on + * @param int $id item number to move selected to (before) + * @param array $items item numbers to move + * @return new constructed list + */ +function legacy_move_config_list_items($source, $id, $items) { + $new_config = array(); + + if (!is_array($source)) { + // input of wrong type, return empty array + return array(); + } elseif ( !is_array($items) || !is_numericint($id)) { + // selected items isn't an array or selected item isn't an int, return input + return $source; + } else { + // input types are valid, move items around + // copy all rules before selected target ($id) and not in items + for ($i = 0; $i < min($id, count($source)); $i++) { + if (!in_array($i, $items)) { + $new_config[] = $source[$i]; + } + } + + // next copy all selected rules (=before $id) + for ($i = 0; $i < count($source); $i++) { + if ($i != $id && in_array($i, $items)) { + $new_config[] = $source[$i]; + } + } + + // copy $id rule + if ($id < count($source)) { + $new_config[] = $source[$id]; + } + + /* copy all rules > $id and not selected */ + for ($i = $id+1; $i < count($source); $i++) { + if (!in_array($i, $items)) { + $new_config[] = $source[$i]; + } + } + return $new_config; + } +}