[ticket/216] Add method for checking if module can be moved

Also fixed minor issues with incorrect checks for module class 'custom'.

B3P-216
This commit is contained in:
Marc Alexander
2014-02-18 00:44:41 +01:00
parent 1d2bc15cf4
commit 4655544ed4
2 changed files with 86 additions and 52 deletions

View File

@@ -146,7 +146,7 @@ class portal_module
'MODULE_SHOW_IMAGE' => (in_array(column_num_string($module_data['module_column']), array('center', 'top', 'bottom'))) ? false : true, 'MODULE_SHOW_IMAGE' => (in_array(column_num_string($module_data['module_column']), array('center', 'top', 'bottom'))) ? false : true,
)); ));
if($module_data['module_classname'] != 'custom') if($module_data['module_classname'] != '\board3\portal\modules\custom')
{ {
$groups_ary = explode(',', $module_data['module_group_ids']); $groups_ary = explode(',', $module_data['module_group_ids']);
@@ -283,7 +283,7 @@ class portal_module
if(isset($module_name)) if(isset($module_name))
{ {
if ($module_data['module_classname'] !== 'custom') if ($module_data['module_classname'] !== '\board3\portal\modules\custom')
{ {
add_log('admin', 'LOG_PORTAL_CONFIG', $module_name); add_log('admin', 'LOG_PORTAL_CONFIG', $module_name);
} }
@@ -427,23 +427,11 @@ class portal_module
// do we want to add the module to the side columns or to the center columns? // do we want to add the module to the side columns or to the center columns?
if (in_array($column_string, array('left', 'right'))) if (in_array($column_string, array('left', 'right')))
{ {
// does the module already exist in the side columns? $submit = $this->can_move_module(array('left', 'right'), $module_classname);
if (isset($this->module_column[$module_classname]) &&
(in_array('left', $this->module_column[$module_classname]) || in_array('right', $this->module_column[$module_classname])))
{
$submit = false;
}
} }
elseif (in_array($column_string, array('center', 'top', 'bottom'))) elseif (in_array($column_string, array('center', 'top', 'bottom')))
{ {
// does the module already exist in the center columns? $submit = $this->can_move_module(array('center', 'top', 'bottom'), $module_classname);
if (isset($this->module_column[$module_classname]) &&
(in_array('center', $this->module_column[$module_classname]) ||
in_array('top', $this->module_column[$module_classname]) ||
in_array('bottom', $this->module_column[$module_classname])))
{
$submit = false;
}
} }
// do not install if module already exists in that column // do not install if module already exists in that column
@@ -526,8 +514,7 @@ class portal_module
if (in_array($column_string, array('left', 'right'))) if (in_array($column_string, array('left', 'right')))
{ {
// does the module already exist in the side columns? // does the module already exist in the side columns?
if (isset($this->module_column[$module_class]) && if (!$this->can_move_module(array('left', 'right'), $module_class))
(in_array('left', $this->module_column[$module_class]) || in_array('right', $this->module_column[$module_class])))
{ {
continue; continue;
} }
@@ -535,10 +522,7 @@ class portal_module
elseif (in_array($column_string, array('center', 'top', 'bottom'))) elseif (in_array($column_string, array('center', 'top', 'bottom')))
{ {
// does the module already exist in the center columns? // does the module already exist in the center columns?
if (isset($this->module_column[$module_class]) && if (!$this->can_move_module(array('center', 'top', 'bottom'), $module_class))
(in_array('center', $this->module_column[$module_class]) ||
in_array('top', $this->module_column[$module_class]) ||
in_array('bottom', $this->module_column[$module_class])))
{ {
continue; continue;
} }
@@ -629,14 +613,11 @@ class portal_module
* this only applies to modules in the center column as the side modules * this only applies to modules in the center column as the side modules
* will automatically skip the center column when moving if they need to * will automatically skip the center column when moving if they need to
*/ */
if ($row['module_classname'] != 'custom') if ($row['module_classname'] != '\board3\portal\modules\custom')
{ {
$column_string = column_num_string($row['module_column'] + 1); // move 1 right $column_string = column_num_string($row['module_column'] + 1); // move 1 right
if ($column_string == 'right' && if ($column_string == 'right' && !$this->can_move_module(array('left', 'right'), $row['module_classname']))
isset($this->module_column[$row['module_classname']]) &&
(in_array('left', $this->module_column[$row['module_classname']]) ||
in_array('right', $this->module_column[$row['module_classname']])))
{ {
$move_right = false; $move_right = false;
} }
@@ -662,14 +643,11 @@ class portal_module
* this only applies to modules in the center column as the side modules * this only applies to modules in the center column as the side modules
* will automatically skip the center column when moving if they need to * will automatically skip the center column when moving if they need to
*/ */
if ($row['module_classname'] != 'custom') if ($row['module_classname'] != '\board3\portal\modules\custom')
{ {
$column_string = column_num_string($row['module_column'] - 1); // move 1 left $column_string = column_num_string($row['module_column'] - 1); // move 1 left
if ($column_string == 'left' && if ($column_string == 'left' && !$this->can_move_module(array('left', 'right'), $row['module_classname']))
isset($this->module_column[$row['module_classname']]) &&
(in_array('left', $this->module_column[$row['module_classname']]) ||
in_array('right', $this->module_column[$row['module_classname']])))
{ {
$move_left = false; $move_left = false;
} }
@@ -986,22 +964,15 @@ class portal_module
* new column (side columns (left & right) or center columns (top, center, bottom)). * new column (side columns (left & right) or center columns (top, center, bottom)).
* of course this does not apply to custom modules. * of course this does not apply to custom modules.
*/ */
if ($module_data['module_classname'] != 'custom' && $move_action == 1) if ($module_data['module_classname'] != '\board3\portal\modules\custom' && $move_action == 1)
{ {
$column_string = column_num_string($module_data['module_column'] - $move_action); $column_string = column_num_string($module_data['module_column'] - $move_action);
// we can only move left to the left & center column // we can only move left to the left & center column
if ($column_string == 'left' && if ($column_string == 'left' && !$this->can_move_module(array('right', 'left'), $module_data['module_classname']))
isset($this->module_column[$module_data['module_classname']]) &&
(in_array('left', $this->module_column[$module_data['module_classname']]) ||
in_array('right', $this->module_column[$module_data['module_classname']])))
{ {
trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action)); trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action));
} }
elseif ($column_string == 'center' && elseif ($column_string == 'center' && !$this->can_move_module(array('top', 'center', 'bottom'), $module_data['module_classname']))
isset($this->module_column[$module_data['module_classname']]) &&
(in_array('center', $this->module_column[$module_data['module_classname']]) ||
in_array('top', $this->module_column[$module_data['module_classname']]) ||
in_array('bottom', $this->module_column[$module_data['module_classname']])))
{ {
// we are moving from the right to the center column so we should move to the left column instead // we are moving from the right to the center column so we should move to the left column instead
$move_action = 2; $move_action = 2;
@@ -1088,22 +1059,15 @@ class portal_module
* new column (side columns (left & right) or center columns (top, center, bottom)). * new column (side columns (left & right) or center columns (top, center, bottom)).
* of course this does not apply to custom modules. * of course this does not apply to custom modules.
*/ */
if ($module_data['module_classname'] != 'custom' && $move_action == 1) if ($module_data['module_classname'] != '\board3\portal\modules\custom' && $move_action == 1)
{ {
$column_string = column_num_string($module_data['module_column'] + $move_action); $column_string = column_num_string($module_data['module_column'] + $move_action);
// we can only move right to the right & center column // we can only move right to the right & center column
if ($column_string == 'right' && if ($column_string == 'right' && !$this->can_move_module(array('right', 'left'), $module_data['module_classname']))
isset($this->module_column[$module_data['module_classname']]) &&
(in_array('left', $this->module_column[$module_data['module_classname']]) ||
in_array('right', $this->module_column[$module_data['module_classname']])))
{ {
trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action)); trigger_error($this->user->lang['UNABLE_TO_MOVE'] . adm_back_link($this->u_action));
} }
elseif ($column_string == 'center' && elseif ($column_string == 'center' && !$this->can_move_module(array('top', 'center', 'bottom'), $module_data['module_classname']))
isset($this->module_column[$module_data['module_classname']]) &&
(in_array('center', $this->module_column[$module_data['module_classname']]) ||
in_array('top', $this->module_column[$module_data['module_classname']]) ||
in_array('bottom', $this->module_column[$module_data['module_classname']])))
{ {
// we are moving from the left to the center column so we should move to the right column instead // we are moving from the left to the center column so we should move to the right column instead
$move_action = 2; $move_action = 2;
@@ -1248,4 +1212,53 @@ class portal_module
} }
} }
} }
/**
* Check if module can be moved to desired column(s)
*
* @param array|int $target_column Column(s) the module should be
* moved to
* @param string $module_class Class of the module
* @return bool True if module can be moved to desired column,
* false if not
*/
public function can_move_module($target_column, $module_class)
{
$submit = true;
if (is_array($target_column))
{
foreach ($target_column as $column)
{
if (!$this->can_move_module($column, $module_class))
{
$submit = false;
}
}
}
// do we want to add the module to the side columns or to the center columns?
if (in_array($target_column, array('left', 'right')))
{
// does the module already exist in the side columns?
if (isset($this->module_column[$module_class]) &&
(in_array('left', $this->module_column[$module_class]) || in_array('right', $this->module_column[$module_class])))
{
$submit = false;
}
}
elseif (in_array($target_column, array('center', 'top', 'bottom')))
{
// does the module already exist in the center columns?
if (isset($this->module_column[$module_class]) &&
(in_array('center', $this->module_column[$module_class]) ||
in_array('top', $this->module_column[$module_class]) ||
in_array('bottom', $this->module_column[$module_class])))
{
$submit = false;
}
}
return $submit;
}
} }

View File

@@ -251,6 +251,27 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
$this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE'); $this->setExpectedTriggerError(E_USER_NOTICE, 'UNABLE_TO_MOVE');
$this->portal_module->move_module_left($module_id); $this->portal_module->move_module_left($module_id);
} }
public function data_can_move_module()
{
return array(
array(false, 'left', '\board3\portal\modules\clock'),
array(false, 'right', '\board3\portal\modules\clock'),
array(true, 'center', '\board3\portal\modules\clock'),
array(true, array('top', 'bottom', 'center'), '\board3\portal\modules\clock'),
array(false, array('left', 'right'), '\board3\portal\modules\clock'),
array(false, 'center', '\board3\portal\modules\birthday_list'),
);
}
/**
* @dataProvider data_can_move_module
*/
public function test_can_move_module($expected, $target_column, $module_class)
{
$this->update_portal_modules();
$this->assertEquals($expected, $this->portal_module->can_move_module($target_column, $module_class));
}
} }
function redirect($url) function redirect($url)