From 0b9b5c2fefc3799864e25f8ef7fc08bdc397050b Mon Sep 17 00:00:00 2001 From: sunsetsystems Date: Tue, 27 Oct 2009 19:52:48 +0000 Subject: [PATCH] fixed some nasty bugs that can corrupt layout data --- interface/super/edit_layout.php | 103 +++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/interface/super/edit_layout.php b/interface/super/edit_layout.php index 2ae83f900..558f74a39 100644 --- a/interface/super/edit_layout.php +++ b/interface/super/edit_layout.php @@ -50,6 +50,13 @@ $datatypes = array( "27" => xl("Radio buttons"), ); +function nextGroupOrder($order) { + if ($order == '9') $order = 'A'; + else if ($order == 'Z') $order = 'a'; + else $order = chr(ord($order) + 1); + return $order; +} + // Check authorization. $thisauth = acl_check('admin', 'super'); if (!$thisauth) die(xl('Not authorized')); @@ -183,11 +190,10 @@ else if ($_POST['formaction'] == "addgroup" && $layout_id) { " from layout_options where ". " form_id = '".$_POST['layout_id']."'" ); - $maxnum = 0; + $maxnum = '1'; while ($result = sqlFetchArray($results)) { - // split the number from the group name - $parts = preg_split("/([A-Z]|[a-z])/", $result['gname']); - if ($parts[0] >= $maxnum) { $maxnum = $parts[0] + 1; } + $tmp = substr($result['gname'], 0, 1); + if ($tmp >= $maxnum) $maxnum = nextGroupOrder($tmp); } $data_type = formTrim($_POST['gnewdatatype']); @@ -259,53 +265,60 @@ else if ($_POST['formaction'] == "deletegroup" && $layout_id) { } else if ($_POST['formaction'] == "movegroup" && $layout_id) { - - // split the numeric order out of the group name - $parts = preg_split("/(^\d)/", $_POST['movegroupname'], -1, PREG_SPLIT_DELIM_CAPTURE); - $currpos = $newpos = $parts[1]; - $groupname = $parts[2]; - - // inc/dec the order number - if ($_POST['movedirection'] == 'up') { - $newpos--; - if ($newpos < 0) { $newpos = 0; } + $results = sqlStatement("SELECT DISTINCT(group_name) AS gname " . + "FROM layout_options WHERE form_id = '$layout_id' " . + "ORDER BY gname"); + $garray = array(); + $i = 0; + while ($result = sqlFetchArray($results)) { + if ($result['gname'] == $_POST['movegroupname']) { + if ($_POST['movedirection'] == 'up') { // moving up + if ($i > 0) { + $garray[$i] = $garray[$i - 1]; + $garray[$i - 1] = $result['gname']; + $i++; + } + else { + $garray[$i++] = $result['gname']; + } + } + else { // moving down + $garray[$i++] = ''; + $garray[$i++] = $result['gname']; + } } - else if ($_POST['movedirection'] == 'down') { - $newpos++; + else if ($i > 1 && $garray[$i - 2] == '') { + $garray[$i - 2] = $result['gname']; } - - // if we can't determine a position, then assign it a zero - if ($newpos == "") $newpos = "0"; - - // update the database rows - sqlStatement("UPDATE layout_options SET ". - "group_name='".$newpos.$groupname."'". - " WHERE ". - "group_name='".$currpos.$groupname."'" - ); + else { + $garray[$i++] = $result['gname']; + } + } + $nextord = '1'; + foreach ($garray as $value) { + if ($value === '') continue; + $newname = $nextord . substr($value, 1); + sqlStatement("UPDATE layout_options SET " . + "group_name = '$newname' WHERE " . + "form_id = '$layout_id' AND " . + "group_name = '$value'"); + $nextord = nextGroupOrder($nextord); + } } -else if ($_POST['formaction']=="renamegroup" && $layout_id) { - - // split the numeric order out of the group name - $parts = preg_split("/(^\d)/", $_POST['renameoldgroupname'], -1, PREG_SPLIT_DELIM_CAPTURE); - $currpos = $parts[1]; - - // if we can't determine a position, then assign it a zero - if ($currpos == "") $currpos = "0"; - - // update the database rows - sqlStatement("UPDATE layout_options SET ". - "group_name='".$currpos.$_POST['renamegroupname']."'". - " WHERE ". - "group_name='".$_POST['renameoldgroupname']."'" - ); +else if ($_POST['formaction'] == "renamegroup" && $layout_id) { + $currpos = substr($_POST['renameoldgroupname'], 0, 1); + // update the database rows + sqlStatement("UPDATE layout_options SET " . + "group_name = '" . $currpos . $_POST['renamegroupname'] . "' ". + "WHERE form_id = '$layout_id' AND ". + "group_name = '" . $_POST['renameoldgroupname'] . "'"); } // Get the selected form's elements. if ($layout_id) { $res = sqlStatement("SELECT * FROM layout_options WHERE " . - "form_id = '$layout_id' ORDER BY group_name, seq"); + "form_id = '$layout_id' ORDER BY group_name, seq"); } // global counter for field numbers @@ -537,11 +550,13 @@ while ($row = sqlFetchArray($res)) { if ($firstgroup == false) { echo "\n"; } echo "
"; echo "
"; - echo preg_replace("/^\d+/", "", $row['group_name']); + // echo preg_replace("/^\d+/", "", $row['group_name']); + echo substr($row['group_name'], 1); echo "  "; // if not english and set to translate layout labels, then show the translation of group name if ($GLOBALS['translate_layout'] && $_SESSION['language_choice'] > 1) { - echo ">>  " . xl(preg_replace("/^\d+/", "", $row['group_name'])) . ""; + // echo ">>  " . xl(preg_replace("/^\d+/", "", $row['group_name'])) . ""; + echo ">>  " . xl(substr($row['group_name'], 1)) . ""; echo "  "; } echo "  "; -- 2.11.4.GIT