acp_forums::move_forum_by PHP Method

move_forum_by() public method

Move forum position by $steps up/down
public move_forum_by ( $forum_row, $action = 'move_up', $steps = 1 )
    function move_forum_by($forum_row, $action = 'move_up', $steps = 1)
    {
        global $db;
        /**
         * Fetch all the siblings between the module's current spot
         * and where we want to move it to. If there are less than $steps
         * siblings between the current spot and the target then the
         * module will move as far as possible
         */
        $sql = 'SELECT forum_id, forum_name, left_id, right_id
			FROM ' . FORUMS_TABLE . "\n\t\t\tWHERE parent_id = {$forum_row['parent_id']}\n\t\t\t\tAND " . ($action == 'move_up' ? "right_id < {$forum_row['right_id']} ORDER BY right_id DESC" : "left_id > {$forum_row['left_id']} ORDER BY left_id ASC");
        $result = $db->sql_query_limit($sql, $steps);
        $target = array();
        while ($row = $db->sql_fetchrow($result)) {
            $target = $row;
        }
        $db->sql_freeresult($result);
        if (!sizeof($target)) {
            // The forum is already on top or bottom
            return false;
        }
        /**
         * $left_id and $right_id define the scope of the nodes that are affected by the move.
         * $diff_up and $diff_down are the values to substract or add to each node's left_id
         * and right_id in order to move them up or down.
         * $move_up_left and $move_up_right define the scope of the nodes that are moving
         * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
         */
        if ($action == 'move_up') {
            $left_id = $target['left_id'];
            $right_id = $forum_row['right_id'];
            $diff_up = $forum_row['left_id'] - $target['left_id'];
            $diff_down = $forum_row['right_id'] + 1 - $forum_row['left_id'];
            $move_up_left = $forum_row['left_id'];
            $move_up_right = $forum_row['right_id'];
        } else {
            $left_id = $forum_row['left_id'];
            $right_id = $target['right_id'];
            $diff_up = $forum_row['right_id'] + 1 - $forum_row['left_id'];
            $diff_down = $target['right_id'] - $forum_row['right_id'];
            $move_up_left = $forum_row['right_id'] + 1;
            $move_up_right = $target['right_id'];
        }
        // Now do the dirty job
        $sql = 'UPDATE ' . FORUMS_TABLE . "\n\t\t\tSET left_id = left_id + CASE\n\t\t\t\tWHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}\n\t\t\t\tELSE {$diff_down}\n\t\t\tEND,\n\t\t\tright_id = right_id + CASE\n\t\t\t\tWHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}\n\t\t\t\tELSE {$diff_down}\n\t\t\tEND,\n\t\t\tforum_parents = ''\n\t\t\tWHERE\n\t\t\t\tleft_id BETWEEN {$left_id} AND {$right_id}\n\t\t\t\tAND right_id BETWEEN {$left_id} AND {$right_id}";
        $db->sql_query($sql);
        return $target['forum_name'];
    }