acp_modules::move_module_by PHP Метод

move_module_by() публичный Метод

Move module position by $steps up/down
public move_module_by ( $module_row, $action = 'move_up', $steps = 1 )
    function move_module_by($module_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 module_id, left_id, right_id, module_langname
			FROM ' . MODULES_TABLE . "\n\t\t\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\tAND parent_id = " . (int) $module_row['parent_id'] . '
				AND ' . ($action == 'move_up' ? 'right_id < ' . (int) $module_row['right_id'] . ' ORDER BY right_id DESC' : 'left_id > ' . (int) $module_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 module 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 = (int) $target['left_id'];
            $right_id = (int) $module_row['right_id'];
            $diff_up = (int) ($module_row['left_id'] - $target['left_id']);
            $diff_down = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
            $move_up_left = (int) $module_row['left_id'];
            $move_up_right = (int) $module_row['right_id'];
        } else {
            $left_id = (int) $module_row['left_id'];
            $right_id = (int) $target['right_id'];
            $diff_up = (int) ($module_row['right_id'] + 1 - $module_row['left_id']);
            $diff_down = (int) ($target['right_id'] - $module_row['right_id']);
            $move_up_left = (int) ($module_row['right_id'] + 1);
            $move_up_right = (int) $target['right_id'];
        }
        // Now do the dirty job
        $sql = 'UPDATE ' . MODULES_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\tWHERE module_class = '" . $db->sql_escape($this->module_class) . "'\n\t\t\t\tAND left_id BETWEEN {$left_id} AND {$right_id}\n\t\t\t\tAND right_id BETWEEN {$left_id} AND {$right_id}";
        $db->sql_query($sql);
        $this->remove_cache_file();
        return $this->lang_name($target['module_langname']);
    }

Usage Example

Пример #1
0
function _add_modules($modules_to_install)
{
	global $phpbb_root_path, $phpEx, $db;

	include_once($phpbb_root_path . 'includes/acp/acp_modules.' . $phpEx);

	$_module = new acp_modules();

	foreach ($modules_to_install as $module_mode => $module_data)
	{
		$_module->module_class = $module_data['class'];

		// Determine parent id first
		$sql = 'SELECT module_id
			FROM ' . MODULES_TABLE . "
			WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
				AND module_langname = '" . $db->sql_escape($module_data['cat']) . "'
				AND module_mode = ''
				AND module_basename = ''";
		$result = $db->sql_query($sql);

		// There may be more than one categories with the same name
		$categories = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$categories[] = (int) $row['module_id'];
		}
		$db->sql_freeresult($result);

		if (!sizeof($categories))
		{
			continue;
		}

		// Add the module to all categories found
		foreach ($categories as $parent_id)
		{
			// Check if the module already exists
			$sql = 'SELECT *
				FROM ' . MODULES_TABLE . "
				WHERE module_basename = '" . $db->sql_escape($module_data['base']) . "'
					AND module_class = '" . $db->sql_escape($module_data['class']) . "'
					AND module_langname = '" . $db->sql_escape($module_data['title']) . "'
					AND module_mode = '" . $db->sql_escape($module_mode) . "'
					AND module_auth = '" . $db->sql_escape($module_data['auth']) . "'
					AND parent_id = {$parent_id}";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);

			// If it exists, we simply continue with the next category
			if ($row)
			{
				continue;
			}

			// Build the module sql row
			$module_row = array(
				'module_basename'	=> $module_data['base'],
				'module_enabled'	=> (isset($module_data['enabled'])) ? (int) $module_data['enabled'] : 1,
				'module_display'	=> (isset($module_data['display'])) ? (int) $module_data['display'] : 1,
				'parent_id'			=> $parent_id,
				'module_class'		=> $module_data['class'],
				'module_langname'	=> $module_data['title'],
				'module_mode'		=> $module_mode,
				'module_auth'		=> $module_data['auth'],
			);

			$_module->update_module_data($module_row, true);

			// Ok, do we need to re-order the module, move it up or down?
			if (!isset($module_data['after']))
			{
				continue;
			}

			$after_mode = $module_data['after'][0];
			$after_langname = $module_data['after'][1];

			// First of all, get the module id for the module this one has to be placed after
			$sql = 'SELECT left_id
				FROM ' . MODULES_TABLE . "
				WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
					AND module_basename = '" . $db->sql_escape($module_data['base']) . "'
					AND module_langname = '" . $db->sql_escape($after_langname) . "'
					AND module_mode = '" . $db->sql_escape($after_mode) . "'
					AND parent_id = '{$parent_id}'";
			$result = $db->sql_query($sql);
			$first_left_id = (int) $db->sql_fetchfield('left_id');
			$db->sql_freeresult($result);

			if (!$first_left_id)
			{
				continue;
			}

			// Ok, count the number of modules between $after_mode and the added module
			$sql = 'SELECT COUNT(module_id) as num_modules
				FROM ' . MODULES_TABLE . "
				WHERE module_class = '" . $db->sql_escape($module_data['class']) . "'
					AND parent_id = {$parent_id}
					AND left_id BETWEEN {$first_left_id} AND {$module_row['left_id']}
				GROUP BY left_id
				ORDER BY left_id";
			$result = $db->sql_query($sql);
			$steps = (int) $db->sql_fetchfield('num_modules');
			$db->sql_freeresult($result);

			// We need to substract 2
			$steps -= 2;

			if ($steps <= 0)
			{
				continue;
			}

			// Ok, move module up $num_modules times. ;)
			$_module->move_module_by($module_row, 'move_up', $steps);
		}
	}

	$_module->remove_cache_file();
}
All Usage Examples Of acp_modules::move_module_by