[ticket/416] Move database handling of moving modules vertically to handler

B3P-416
This commit is contained in:
Marc Alexander
2014-11-28 00:19:31 +01:00
parent 8c0d537841
commit f6e79075d8
2 changed files with 47 additions and 27 deletions

View File

@@ -13,6 +13,11 @@ use phpbb\db\driver\driver_interface;
class database_handler
{
/** @var int Move direction up */
const MOVE_DIRECTION_UP = -1;
/** @var int Move driection down */
const MOVE_DIRECTION_DOWN = 1;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
@@ -70,4 +75,44 @@ class database_handler
return $this->db->sql_affectedrows();
}
/**
* Move module vertically
*
* @param int $module_id Module ID
* @param array $module_data Module data array
* @param int $direction Direction to move, see constants in this class
* @param int $step Moving step
*
* @return int Number of affected rows, 0 if unsuccessful
*/
public function move_module_vertical($module_id, $module_data, $direction, $step = 1)
{
if ($direction == self::MOVE_DIRECTION_DOWN)
{
$current_increment = ' + ' . $step;
$other_increment = ' - ' . $step;
}
else
{
$current_increment = ' - ' . $step;
$other_increment = ' + ' . $step;
}
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order' . $other_increment . '
WHERE module_order = ' . (int) ($module_data['module_order'] + ($direction * $step)) . '
AND module_column = ' . (int) $module_data['module_column'];
$this->db->sql_query($sql);
$updated = $this->db->sql_affectedrows();
if ($updated)
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order' . $current_increment . '
WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql);
}
return $updated;
}
}

View File

@@ -246,20 +246,7 @@ class manager
if (($module_data !== false) && ($module_data['module_order'] > 1))
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order + 1
WHERE module_order = ' . (int) ($module_data['module_order'] - 1) . '
AND module_column = ' . (int) $module_data['module_column'];
$this->db->sql_query($sql);
$updated = $this->db->sql_affectedrows();
if ($updated)
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order - 1
WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql);
}
$updated = $this->database_handler->move_module_vertical($module_id, $module_data, database_handler::MOVE_DIRECTION_UP, 1);
}
$this->handle_after_move($updated, true);
@@ -277,19 +264,7 @@ class manager
if ($module_data !== false && $this->get_last_module_order($module_data['module_column']) != $module_data['module_order'])
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order - 1
WHERE module_order = ' . (int) ($module_data['module_order'] + 1) . '
AND module_column = ' . (int) $module_data['module_column'];
$this->db->sql_query($sql);
$updated = $this->db->sql_affectedrows();
if ($updated)
{
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
SET module_order = module_order + 1
WHERE module_id = ' . (int) $module_id;
$this->db->sql_query($sql);
}
$updated = $this->database_handler->move_module_vertical($module_id, $module_data, database_handler::MOVE_DIRECTION_DOWN, 1);
}
$this->handle_after_move($updated, true);