[ticket/416] Add one method for moving modules horizontally

B3P-416
This commit is contained in:
Marc Alexander
2014-11-29 14:33:05 +01:00
parent 99c5b164d9
commit 3a2782b578
6 changed files with 109 additions and 145 deletions

View File

@@ -411,11 +411,11 @@ class portal_module
} }
else if($action == 'move_right') else if($action == 'move_right')
{ {
$this->modules_manager->move_module_right($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
} }
else if($action == 'move_left') else if($action == 'move_left')
{ {
$this->modules_manager->move_module_left($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT);
} }
else if ($action == 'delete') else if ($action == 'delete')
{ {

View File

@@ -19,6 +19,12 @@ class database_handler
/** @var int Move driection down */ /** @var int Move driection down */
const MOVE_DIRECTION_DOWN = 1; const MOVE_DIRECTION_DOWN = 1;
/** @var int Move direction right */
const MOVE_DIRECTION_RIGHT = 1;
/** @var int Move direction left */
const MOVE_DIRECTION_LEFT = -1;
/** @var \phpbb\db\driver\driver_interface */ /** @var \phpbb\db\driver\driver_interface */
protected $db; protected $db;

View File

@@ -259,188 +259,138 @@ class manager
} }
/** /**
* Move module left one column * Move module horizontally
* *
* @param int $module_id ID of the module that should be moved * @param int $module_id ID of the module that should be moved
* @param int $direction The direction to move the module
*
* @return null
*/ */
public function move_module_left($module_id) public function move_module_horizontal($module_id, $direction)
{ {
$module_data = $this->get_move_module_data($module_id); $module_data = $this->get_move_module_data($module_id);
$this->get_module($module_data['module_classname']); $this->get_module($module_data['module_classname']);
$move_action = 0; $move_action = $this->get_horizontal_move_action($module_data, $direction);
$this->check_module_conflict($module_data, $move_action);
if ($module_data !== false && $module_data['module_column'] > $this->portal_columns->string_to_number('left')) $sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
{
if($this->module->columns & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] - 1)))
{
$move_action = 1; // we move 1 column to the left
}
else if($this->module->columns & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] - 2)) && $module_data['module_column'] != 2)
{
$move_action = 2; // we move 2 columns to the left
}
else
{
$this->handle_after_move(false);
}
/**
* moving only 1 column to the left means we will either end up in the left column
* or in the center column. this is not possible when moving 2 columns to the left.
* therefore we only need to check if we won't end up with a duplicate module in the
* new column (side columns (left & right) or center columns (top, center, bottom)).
* of course this does not apply to custom modules.
*/
if ($module_data['module_classname'] != '\board3\portal\modules\custom' && $move_action == 1)
{
$column_string = $this->portal_columns->number_to_string($module_data['module_column'] - $move_action);
// we can only move left to the left & center column
if ($column_string == 'left' && !$this->can_move_module(array('right', 'left'), $module_data['module_classname']))
{
trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action));
}
else if ($column_string == 'center' && !$this->can_move_module(array('top', 'center', 'bottom'), $module_data['module_classname']))
{
// we are moving from the right to the center column so we should move to the left column instead
$move_action = 2;
}
}
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order + 1 SET module_order = module_order + 1
WHERE module_order >= ' . $module_data['module_order'] . ' WHERE module_order >= ' . $module_data['module_order'] . '
AND module_column = ' . ($module_data['module_column'] - $move_action); AND module_column = ' . ($module_data['module_column'] + $move_action);
$this->db->sql_query($sql); $this->db->sql_query($sql);
$updated = $this->db->sql_affectedrows(); $updated = $this->db->sql_affectedrows();
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' $sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_column = module_column - ' . $move_action . ' SET module_column = ' . ($module_data['module_column'] + $move_action) . '
WHERE module_id = ' . (int) $module_id; WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql); $this->db->sql_query($sql);
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' $sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order - 1 SET module_order = module_order - 1
WHERE module_order >= ' . $module_data['module_order'] . ' WHERE module_order >= ' . $module_data['module_order'] . '
AND module_column = ' . $module_data['module_column']; AND module_column = ' . $module_data['module_column'];
$this->db->sql_query($sql); $this->db->sql_query($sql);
// the module that needs to moved is in the last row // the module that needs to moved is in the last row
if(!$updated) if (!$updated)
{ {
$sql = 'SELECT MAX(module_order) as new_order $sql = 'SELECT MAX(module_order) as new_order
FROM ' . PORTAL_MODULES_TABLE . ' FROM ' . PORTAL_MODULES_TABLE . '
WHERE module_order < ' . $module_data['module_order'] . ' WHERE module_order < ' . $module_data['module_order'] . '
AND module_column = ' . (int) ($module_data['module_column'] - $move_action); AND module_column = ' . (int) ($module_data['module_column'] + $move_action);
$this->db->sql_query($sql); $this->db->sql_query($sql);
$new_order = $this->db->sql_fetchfield('new_order') + 1; $new_order = $this->db->sql_fetchfield('new_order') + 1;
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' $sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = ' . $new_order . ' SET module_order = ' . $new_order . '
WHERE module_id = ' . (int) $module_id; WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql); $this->db->sql_query($sql);
}
}
else
{
$this->handle_after_move(false);
} }
$this->handle_after_move(true); $this->handle_after_move(true);
} }
/** /**
* Move module right one column * Get the horizontal move action (columns to move)
* *
* @param int $module_id ID of the module that should be moved * @param array $module_data Array containing the module data
* @param int $direction Direction to move; 1 for right, -1 for left
*
* @return int|null Move action if module can be moved, calls
* handle_after_move() if it can't be moved
*/ */
public function move_module_right($module_id) public function get_horizontal_move_action($module_data, $direction)
{ {
$module_data = $this->get_move_module_data($module_id); if ($this->can_move_horizontally($module_data, $direction))
$this->get_module($module_data['module_classname']);
$move_action = 0;
if ($module_data !== false && $module_data['module_column'] < $this->portal_columns->string_to_number('right'))
{ {
if($this->module->columns & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + 1))) if ($this->module->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + $direction)))
{ {
$move_action = 1; // we move 1 column to the right return $direction; // we move 1 column
} }
else if($this->module->columns & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + 2)) && $module_data['module_column'] != 2) else if ($this->module->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($module_data['module_column'] + $direction * 2)) && $module_data['module_column'] != $this->portal_columns->string_to_number('center'))
{ {
$move_action = 2; // we move 2 columns to the right return 2 * $direction; // we move 2 columns
} }
else }
$this->handle_after_move(false);
}
/**
* Check if there is conflict between the move action and existing modules
*
* @param array $module_data The module's data
* @param int $move_action The move action
*
* @return null
*/
protected function check_module_conflict($module_data, &$move_action)
{
/**
* Moving only 1 column means we will either end up in a side column
* or in the center column. This is not possible when moving 2 columns.
* Therefore we only need to check if we won't end up with a duplicate
* module in the new column (side columns (left & right) or center
* columns (top, center, bottom)). Of course this does not apply to
* custom modules.
*/
if ($module_data['module_classname'] != '\board3\portal\modules\custom' && abs($move_action) == 1)
{
$column_string = $this->portal_columns->number_to_string($module_data['module_column'] + $move_action);
// we can only move horizontally to center or side columns
if (in_array($column_string, array('right', 'left')) && !$this->can_move_module(array('right', 'left'), $module_data['module_classname']))
{ {
$this->handle_after_move(false); trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action));
} }
else if ($column_string == 'center' && !$this->can_move_module(array('top', 'center', 'bottom'), $module_data['module_classname']))
/**
* moving only 1 column to the right means we will either end up in the right column
* or in the center column. this is not possible when moving 2 columns to the right.
* therefore we only need to check if we won't end up with a duplicate module in the
* new column (side columns (left & right) or center columns (top, center, bottom)).
* of course this does not apply to custom modules.
*/
if ($module_data['module_classname'] != '\board3\portal\modules\custom' && $move_action == 1)
{ {
$column_string = $this->portal_columns->number_to_string($module_data['module_column'] + $move_action); // we are moving from the right to the center column so we should move to the left column instead
$move_action = 2 * $move_action;
// we can only move right to the right & center column
if ($column_string == 'right' && !$this->can_move_module(array('right', 'left'), $module_data['module_classname']))
{
trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action));
}
else if ($column_string == 'center' && !$this->can_move_module(array('top', 'center', 'bottom'), $module_data['module_classname']))
{
// we are moving from the left to the center column so we should move to the right column instead
$move_action = 2;
}
} }
}
}
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' /**
SET module_order = module_order + 1 * Check if module can be moved horizontally
WHERE module_order >= ' . (int) $module_data['module_order'] . ' *
AND module_column = ' . (int) ($module_data['module_column'] + $move_action); * @param array $module_data Module's module data
$this->db->sql_query($sql); * @param int $direction Direction to move the module
$updated = $this->db->sql_affectedrows(); *
* @return bool True if module can be moved, false if not
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' */
SET module_column = module_column + ' . $move_action . ' protected function can_move_horizontally($module_data, $direction)
WHERE module_id = ' . (int) $module_id; {
$this->db->sql_query($sql); if (isset($module_data['module_column']))
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . ' return ($direction === database_handler::MOVE_DIRECTION_RIGHT) ? $module_data['module_column'] < $this->portal_columns->string_to_number('right') : $module_data['module_column'] > $this->portal_columns->string_to_number('left');
SET module_order = module_order - 1
WHERE module_order >= ' . (int) $module_data['module_order'] . '
AND module_column = ' . (int) $module_data['module_column'];
$this->db->sql_query($sql);
// the module that needs to moved is in the last row
if(!$updated)
{
$sql = 'SELECT MAX(module_order) as new_order
FROM ' . PORTAL_MODULES_TABLE . '
WHERE module_order < ' . (int) $module_data['module_order'] . '
AND module_column = ' . (int) ($module_data['module_column'] + $move_action);
$this->db->sql_query($sql);
$new_order = $this->db->sql_fetchfield('new_order') + 1;
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = ' . (int) $new_order . '
WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql);
}
} }
else else
{ {
$this->handle_after_move(false); return false;
} }
$this->handle_after_move(true);
} }
/** /**

View File

@@ -204,7 +204,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
if ($column_start > 3) if ($column_start > 3)
{ {
$this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND'); $this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND');
$this->modules_manager->move_module_right($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
return; return;
} }
@@ -218,12 +218,12 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
{ {
$data = $this->modules_manager->get_move_module_data($module_id); $data = $this->modules_manager->get_move_module_data($module_id);
$this->assertEquals($column_start, $data['module_column']); $this->assertEquals($column_start, $data['module_column']);
$this->modules_manager->move_module_right($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
$column_start++; $column_start++;
$this->update_portal_modules(); $this->update_portal_modules();
} }
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE'); $this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE');
$this->modules_manager->move_module_right($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
} }
public function data_move_module_left() public function data_move_module_left()
@@ -246,7 +246,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
if ($column_start > 3) if ($column_start > 3)
{ {
$this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND'); $this->setExpectedTriggerError(E_USER_ERROR, 'CLASS_NOT_FOUND');
$this->modules_manager->move_module_left($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT);
return; return;
} }
@@ -254,7 +254,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
{ {
$data = $this->modules_manager->get_move_module_data($module_id); $data = $this->modules_manager->get_move_module_data($module_id);
$this->assertEquals($column_start, $data['module_column']); $this->assertEquals($column_start, $data['module_column']);
$this->modules_manager->move_module_right($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_RIGHT);
$this->update_portal_modules(); $this->update_portal_modules();
$column_start++; $column_start++;
} }
@@ -271,12 +271,12 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
{ {
$data = $this->modules_manager->get_move_module_data($module_id); $data = $this->modules_manager->get_move_module_data($module_id);
$this->assertEquals($column_start, $data['module_column']); $this->assertEquals($column_start, $data['module_column']);
$this->modules_manager->move_module_left($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT);
$this->update_portal_modules(); $this->update_portal_modules();
$column_start--; $column_start--;
} }
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE'); $this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE');
$this->modules_manager->move_module_left($module_id); $this->modules_manager->move_module_horizontal($module_id, \board3\portal\portal\modules\database_handler::MOVE_DIRECTION_LEFT);
} }
public function data_can_move_module() public function data_can_move_module()

View File

@@ -100,6 +100,7 @@ class modules_manager_confirm_box_test extends \board3\portal\tests\testframewor
), self::$meta_refresh); ), self::$meta_refresh);
$this->assertEquals(phpbb_acp_move_module_test::$error_type, E_USER_NOTICE); $this->assertEquals(phpbb_acp_move_module_test::$error_type, E_USER_NOTICE);
$this->assertEquals(phpbb_acp_move_module_test::$error, 'adm/index.php?i=15&amp;mode=foobar&amp;module_id=6'); $this->assertEquals(phpbb_acp_move_module_test::$error, 'adm/index.php?i=15&amp;mode=foobar&amp;module_id=6');
phpbb_acp_move_module_test::$override_trigger_error = false;
} }
public function test_module_delete() public function test_module_delete()
@@ -140,6 +141,7 @@ class modules_manager_confirm_box_test extends \board3\portal\tests\testframewor
$this->assertNull($this->modules_manager->module_delete(6, 'foobar', 'module_delete', 6)); $this->assertNull($this->modules_manager->module_delete(6, 'foobar', 'module_delete', 6));
$this->assertEquals(E_USER_NOTICE, phpbb_acp_move_module_test::$error_type); $this->assertEquals(E_USER_NOTICE, phpbb_acp_move_module_test::$error_type);
$this->assertEquals('SUCCESS_DELETEadm/index.php?i=15&amp;mode=foobar', phpbb_acp_move_module_test::$error); $this->assertEquals('SUCCESS_DELETEadm/index.php?i=15&amp;mode=foobar', phpbb_acp_move_module_test::$error);
phpbb_acp_move_module_test::$override_trigger_error = false;
} }
} }

View File

@@ -80,4 +80,10 @@ class board3_portal_modules_manager_test extends \board3\portal\tests\testframew
{ {
$this->assertNull($this->modules_manager->handle_ajax_request(array('foobar' => true))); $this->assertNull($this->modules_manager->handle_ajax_request(array('foobar' => true)));
} }
public function test_get_horizontal_move_action()
{
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE');
$this->modules_manager->get_horizontal_move_action(array(), 6);
}
} }