[ticket/416] Move database handling of moving modules vertically to handler
B3P-416
This commit is contained in:
@@ -13,6 +13,11 @@ use phpbb\db\driver\driver_interface;
|
|||||||
|
|
||||||
class database_handler
|
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 */
|
/** @var \phpbb\db\driver\driver_interface */
|
||||||
protected $db;
|
protected $db;
|
||||||
@@ -70,4 +75,44 @@ class database_handler
|
|||||||
|
|
||||||
return $this->db->sql_affectedrows();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -246,20 +246,7 @@ class manager
|
|||||||
|
|
||||||
if (($module_data !== false) && ($module_data['module_order'] > 1))
|
if (($module_data !== false) && ($module_data['module_order'] > 1))
|
||||||
{
|
{
|
||||||
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
|
$updated = $this->database_handler->move_module_vertical($module_id, $module_data, database_handler::MOVE_DIRECTION_UP, 1);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handle_after_move($updated, true);
|
$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'])
|
if ($module_data !== false && $this->get_last_module_order($module_data['module_column']) != $module_data['module_order'])
|
||||||
{
|
{
|
||||||
$sql = 'UPDATE ' . PORTAL_MODULES_TABLE . '
|
$updated = $this->database_handler->move_module_vertical($module_id, $module_data, database_handler::MOVE_DIRECTION_DOWN, 1);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handle_after_move($updated, true);
|
$this->handle_after_move($updated, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user