function move_branch($dir, $order_key, $table, $field, $where) { $tier = tree_tier($order_key); if ($where != '') { $where = " AND {$where}"; } $tree_order = $dir == 'up' ? 'ORDER BY order_key ASC' : 'ORDER BY order_key DESC'; $arrow = $dir == 'up' ? '<' : '>'; $order = $dir == 'up' ? 'DESC' : 'ASC'; /* take a peek at the current tree structure */ $pre_tree = db_fetch_assoc("SELECT * FROM graph_tree_items WHERE local_graph_id='0' AND host_id='0' {$tree_order}"); $sql = "SELECT * FROM {$table} WHERE {$field} {$arrow} '{$order_key}' AND {$field} LIKE '%" . substr($order_key, $tier * CHARS_PER_TIER) . "'\n\t\tAND {$field} NOT LIKE '%" . str_repeat('0', CHARS_PER_TIER) . substr($order_key, $tier * CHARS_PER_TIER) . "' {$where} ORDER BY {$field} {$order}"; $displaced_row = db_fetch_row($sql); if (sizeof($displaced_row) > 0) { $old_root = substr($order_key, 0, $tier * CHARS_PER_TIER); $new_root = substr($displaced_row[$field], 0, $tier * CHARS_PER_TIER); db_execute("UPDATE {$table} SET {$field} = CONCAT('" . str_pad('', $tier * CHARS_PER_TIER, 'Z') . "',SUBSTRING({$field}," . ($tier * CHARS_PER_TIER + 1) . ")) WHERE {$field} LIKE '{$new_root}%'{$where}"); db_execute("UPDATE {$table} SET {$field} = CONCAT('{$new_root}',SUBSTRING({$field}," . ($tier * CHARS_PER_TIER + 1) . ")) WHERE {$field} LIKE '{$old_root}%' {$where}"); db_execute("UPDATE {$table} SET {$field} = CONCAT('{$old_root}',SUBSTRING({$field}," . ($tier * CHARS_PER_TIER + 1) . ")) WHERE {$field} LIKE '" . str_pad('', $tier * CHARS_PER_TIER, 'Z') . "%' {$where}"); } /* move session variables around */ reset_session_variables($pre_tree); }
function move_branch($dir, $order_key, $table, $field, $where) { $tier = tree_tier($order_key); if ($where != '') { $where = " AND $where"; } $tree_order = $dir == 'up' ? 'ORDER BY order_key ASC' : 'ORDER BY order_key DESC'; $arrow = $dir == 'up' ? '<' : '>'; $order = $dir == 'up' ? 'DESC' : 'ASC'; /* take a peek at the current tree structure */ $pre_tree = db_fetch_assoc("SELECT * FROM graph_tree_items WHERE local_graph_id='0' AND device_id='0' $tree_order"); $sql = "SELECT * FROM $table WHERE $field $arrow '$order_key' AND $field LIKE '%" . substr($order_key, ($tier * CHARS_PER_TIER))."' AND $field NOT LIKE '%" . str_repeat('0', CHARS_PER_TIER) . substr($order_key, ($tier * CHARS_PER_TIER)) . "' $where ORDER BY $field $order"; $displaced_row = db_fetch_row($sql); if (sizeof($displaced_row) > 0) { $old_root = substr($order_key, 0, ($tier * CHARS_PER_TIER)); $new_root = substr($displaced_row[$field], 0, ($tier * CHARS_PER_TIER)); db_execute("UPDATE $table SET $field = CONCAT('" . str_pad('', ($tier * CHARS_PER_TIER), 'Z') . "',SUBSTRING($field," . (($tier * CHARS_PER_TIER) + 1).")) WHERE $field LIKE '$new_root%'$where"); db_execute("UPDATE $table SET $field = CONCAT('$new_root',SUBSTRING($field," . (($tier * CHARS_PER_TIER) + 1) . ")) WHERE $field LIKE '$old_root%' $where"); db_execute("UPDATE $table SET $field = CONCAT('$old_root',SUBSTRING($field," . (($tier * CHARS_PER_TIER) + 1) . ")) WHERE $field LIKE '".str_pad('', ($tier * CHARS_PER_TIER), 'Z') . "%' $where"); } /* move session variables around */ reset_session_variables($pre_tree); }