diff --git a/acp/portal_module.php b/acp/portal_module.php index 81ed7867..a9f14f29 100644 --- a/acp/portal_module.php +++ b/acp/portal_module.php @@ -15,7 +15,7 @@ class portal_module public $new_config = array(); protected $c_class; protected $db, $user, $cache, $template, $display_vars, $config, $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_container; - protected $root_path, $version_check, $request, $php_ext, $portal_helper, $modules_helper, $log; + protected $root_path, $version_check, $request, $php_ext, $portal_helper, $modules_helper, $log, $portal_columns; public $module_column = array(); public function __construct() @@ -43,14 +43,10 @@ class portal_module $this->portal_helper = $this->phpbb_container->get('board3.portal.helper'); $this->modules_helper = $this->phpbb_container->get('board3.portal.modules_helper'); $this->log = $phpbb_log; + $this->portal_columns = $this->phpbb_container->get('board3.portal.columns'); define('PORTAL_MODULES_TABLE', $this->phpbb_container->getParameter('board3.portal.modules.table')); define('PORTAL_CONFIG_TABLE', $this->phpbb_container->getParameter('board3.portal.config.table')); - if (!function_exists('column_string_const')) - { - include($this->root_path . 'includes/functions_modules.' . $this->php_ext); - } - if(!function_exists('obtain_portal_config')) { include($this->root_path . 'includes/functions.' . $this->php_ext); @@ -120,7 +116,7 @@ class portal_module 'MODULE_IMAGE_HEIGHT' => $module_data['module_image_height'], 'MODULE_IMAGE_SRC' => ($module_data['module_image_src']) ? $this->root_path . 'styles/' . $this->user->style['style_path'] . '/theme/images/portal/' . $module_data['module_image_src'] : '', 'MODULE_ENABLED' => ($module_data['module_status']) ? true : false, - 'MODULE_SHOW_IMAGE' => (in_array(column_num_string($module_data['module_column']), array('center', 'top', 'bottom'))) ? false : true, + 'MODULE_SHOW_IMAGE' => (in_array($this->portal_columns->number_to_string($module_data['module_column']), array('center', 'top', 'bottom'))) ? false : true, )); if($module_data['module_classname'] != '\board3\portal\modules\custom') @@ -392,7 +388,7 @@ class portal_module { $installed_modules[] = $cur_module['module_classname']; // Create an array with the columns the module is in - $this->module_column[$cur_module['module_classname']][] = column_num_string($cur_module['module_column']); + $this->module_column[$cur_module['module_classname']][] = $this->portal_columns->number_to_string($cur_module['module_column']); } if ($action == 'move_up') @@ -418,7 +414,7 @@ class portal_module $add_list = $this->request->variable('add', array('' => '')); $add_module = key($add_list); - $add_column = $this->request->variable('add_column', column_string_num($add_module)); + $add_column = $this->request->variable('add_column', $this->portal_columns->string_to_number($add_module)); if ($add_column) { $submit = ($this->request->is_set_post('submit')) ? true : false; @@ -493,7 +489,7 @@ class portal_module // Find new modules foreach ($modules_list as $module_class => $module) { - if ($module->get_allowed_columns() & column_string_const($add_module)) + if ($module->get_allowed_columns() & $this->portal_columns->string_to_constant($add_module)) { if ($module->get_language()) { @@ -520,7 +516,7 @@ class portal_module } $s_hidden_fields = build_hidden_fields(array( - 'add_column' => column_string_num($add_module), + 'add_column' => $this->portal_columns->string_to_number($add_module), )); $this->template->assign_vars(array( 'S_MODULE_NAMES' => $options, @@ -563,10 +559,10 @@ class portal_module { $this->user->add_lang_ext('board3/portal', 'modules/' . $this->c_class->get_language()); } - $template_column = column_num_string($row['module_column']); + $template_column = $this->portal_columns->number_to_string($row['module_column']); // find out of we can move modules to the left or right - if(($this->c_class->get_allowed_columns() & column_string_const(column_num_string($row['module_column'] + 1))) || ($this->c_class->get_allowed_columns() & column_string_const(column_num_string($row['module_column'] + 2)) && $row['module_column'] != 2)) + if(($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] + 1))) || ($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] + 2)) && $row['module_column'] != 2)) { /** * check if we can actually move @@ -575,7 +571,7 @@ class portal_module */ if ($row['module_classname'] != '\board3\portal\modules\custom') { - $column_string = column_num_string($row['module_column'] + 1); // move 1 right + $column_string = $this->portal_columns->number_to_string($row['module_column'] + 1); // move 1 right if ($column_string == 'right' && !$this->can_move_module(array('left', 'right'), $row['module_classname'])) { @@ -596,7 +592,7 @@ class portal_module $move_right = false; } - if(($this->c_class->get_allowed_columns() & column_string_const(column_num_string($row['module_column'] - 1))) || ($this->c_class->get_allowed_columns() & column_string_const(column_num_string($row['module_column'] - 2)) && $row['module_column'] != 2)) + if(($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] - 1))) || ($this->c_class->get_allowed_columns() & $this->portal_columns->string_to_constant($this->portal_columns->number_to_string($row['module_column'] - 2)) && $row['module_column'] != 2)) { /** * check if we can actually move @@ -605,7 +601,7 @@ class portal_module */ if ($row['module_classname'] != '\board3\portal\modules\custom') { - $column_string = column_num_string($row['module_column'] - 1); // move 1 left + $column_string = $this->portal_columns->number_to_string($row['module_column'] - 1); // move 1 left if ($column_string == 'left' && !$this->can_move_module(array('left', 'right'), $row['module_classname'])) { @@ -868,13 +864,13 @@ class portal_module $move_action = 0; - if ($module_data !== false && $module_data['module_column'] > column_string_num('left')) + if ($module_data !== false && $module_data['module_column'] > $this->portal_columns->string_to_number('left')) { - if($this->c_class->columns & column_string_const(column_num_string($module_data['module_column'] - 1))) + if($this->c_class->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->c_class->columns & column_string_const(column_num_string($module_data['module_column'] - 2)) && $module_data['module_column'] != 2) + else if($this->c_class->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 } @@ -892,7 +888,7 @@ class portal_module */ 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 = $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'])) { @@ -963,13 +959,13 @@ class portal_module $move_action = 0; - if ($module_data !== false && $module_data['module_column'] < column_string_num('right')) + if ($module_data !== false && $module_data['module_column'] < $this->portal_columns->string_to_number('right')) { - if($this->c_class->columns & column_string_const(column_num_string($module_data['module_column'] + 1))) + if($this->c_class->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 right } - else if($this->c_class->columns & column_string_const(column_num_string($module_data['module_column'] + 2)) && $module_data['module_column'] != 2) + else if($this->c_class->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 right } @@ -987,7 +983,7 @@ class portal_module */ 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 = $this->portal_columns->number_to_string($module_data['module_column'] + $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'])) { diff --git a/config/services.yml b/config/services.yml index 1b5c8aaa..568eed95 100644 --- a/config/services.yml +++ b/config/services.yml @@ -17,6 +17,7 @@ services: board3.portal.main: class: board3\portal\controller\main arguments: + - @board3.portal.columns - @config - @board3.portal.controller_helper - @template @@ -31,6 +32,7 @@ services: class: board3\portal\controller\helper arguments: - @auth + - @board3.portal.columns - @config - @template - @user @@ -68,6 +70,9 @@ services: - @config - @request + board3.portal.columns: + class: board3\portal\portal\columns + board3.portal.fetch_posts: class: board3\portal\portal\fetch_posts arguments: diff --git a/controller/helper.php b/controller/helper.php index 44671acf..230fcbcb 100644 --- a/controller/helper.php +++ b/controller/helper.php @@ -11,6 +11,9 @@ namespace board3\portal\controller; class helper { + /** @var \board3\portal\portal\columns */ + protected $portal_columns; + /** * Auth object * @var \phpbb\auth\auth @@ -76,6 +79,7 @@ class helper * NOTE: The parameters of this method must match in order and type with * the dependencies defined in the services.yml file for this service. * @param \phpbb\auth\auth $auth Auth object + * @param \board3\portal\portal\columns $portal_columns Board3 Portal columns object * @param \phpbb\config\config $config phpBB Config object * @param \phpbb\template $template Template object * @param \phpbb\user $user User object @@ -84,9 +88,10 @@ class helper * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP file extension */ - public function __construct($auth, $config, $template, $user, $path_helper, $portal_helper, $phpbb_root_path, $php_ext) + public function __construct($auth, $portal_columns, $config, $template, $user, $path_helper, $portal_helper, $phpbb_root_path, $php_ext) { $this->auth = $auth; + $this->portal_columns = $portal_columns; $this->config = $config; $this->template = $template; $this->user = $user; @@ -161,13 +166,15 @@ class helper */ protected function check_column_disabled($row) { - return ($this->config['board3_left_column'] === false && column_num_string($row['module_column']) === 'left') || ($this->config['board3_right_column'] === false && column_num_string($row['module_column']) === 'right'); + return ($this->config['board3_left_column'] === false && $this->portal_columns->number_to_string($row['module_column']) === 'left') || ($this->config['board3_right_column'] === false && $this->portal_columns->number_to_string($row['module_column']) === 'right'); } /** * Check if user is in required groups * * @param array $row Module row + * + * @return bool True if group has access, false if not */ protected function check_group_access($row) { @@ -215,7 +222,7 @@ class helper { if (is_array($template_module)) { - $this->template->assign_block_vars('modules_' . column_num_string($row['module_column']), array( + $this->template->assign_block_vars('modules_' . $this->portal_columns->number_to_string($row['module_column']), array( 'TEMPLATE_FILE' => 'portal/modules/' . $template_module['template'], 'IMAGE_SRC' => $this->path_helper->get_web_root_path() . $this->root_path . 'styles/' . $this->user->style['style_path'] . '/theme/images/portal/' . $template_module['image_src'], 'TITLE' => $template_module['title'], @@ -227,7 +234,7 @@ class helper } else { - $this->template->assign_block_vars('modules_' . column_num_string($row['module_column']), array( + $this->template->assign_block_vars('modules_' . $this->portal_columns->number_to_string($row['module_column']), array( 'TEMPLATE_FILE' => 'portal/modules/' . $template_module, 'IMAGE_SRC' => $this->path_helper->get_web_root_path() . $this->root_path . 'styles/' . $this->user->style['style_path'] . '/theme/images/portal/' . $row['module_image_src'], 'IMAGE_WIDTH' => $row['module_image_width'], diff --git a/controller/main.php b/controller/main.php index 61d32abb..155e7637 100644 --- a/controller/main.php +++ b/controller/main.php @@ -11,6 +11,9 @@ namespace board3\portal\controller; class main { + /** @var \board3\portal\portal\columns */ + protected $portal_columns; + /** * phpBB Config object * @var \phpbb\config\config @@ -81,6 +84,7 @@ class main * Constructor * NOTE: The parameters of this method must match in order and type with * the dependencies defined in the services.yml file for this service. + * @param \board3\portal\portal\columns $portal_columns Board3 Portal columns object * @param \phpbb\config\config $config phpBB Config object * @param \board3\portal\controller\helper $controller_helper Controller helper * @param \phpbb\template $template Template object @@ -91,10 +95,11 @@ class main * @param string $config_table Board3 config table * @param string $modules_table Board3 modules table */ - public function __construct($config, $controller_helper, $template, $user, $path_helper, $phpbb_root_path, $php_ext, $config_table, $modules_table) + public function __construct($portal_columns, $config, $controller_helper, $template, $user, $path_helper, $phpbb_root_path, $php_ext, $config_table, $modules_table) { global $portal_root_path; + $this->portal_columns = $portal_columns; $this->config = $config; $this->controller_helper = $controller_helper; $this->template = $template; @@ -203,7 +208,7 @@ class main public function get_module_template($row, $module) { $template_module = false; - $column = column_num_string($row['module_column']); + $column = $this->portal_columns->number_to_string($row['module_column']); if (in_array($column, array('left', 'right')) && $this->config['board3_' . $column . '_column']) { diff --git a/includes/functions_modules.php b/includes/functions_modules.php index 78196106..01bbc513 100644 --- a/includes/functions_modules.php +++ b/includes/functions_modules.php @@ -7,59 +7,44 @@ * */ +/** + * Convert column number to string equivalent + * + * @param int $column Column number + * + * @return string String representation of column number; default: '' + * @deprecated 2.1.0-RC1 (to be removed: 2.1.0) + */ function column_num_string($column) { - switch ($column) - { - case 1: - return 'left'; - case 2: - return 'center'; - case 3: - return 'right'; - case 4: - return 'top'; - case 5: - return 'bottom'; - default: - return 0; - } + $portal_columns = new \board3\portal\portal\columns(); + return $portal_columns->number_to_string($column); } +/** + * Convert column string to equivalent number + * + * @param string $column Column name + * + * @return int The column number; default: 0 + * @deprecated 2.1.0-RC1 (to be removed: 2.1.0) + */ function column_string_num($column) { - switch ($column) - { - case 'left': - return 1; - case 'center': - return 2; - case 'right': - return 3; - case 'top': - return 4; - case 'bottom': - return 5; - default: - return 0; - } + $portal_columns = new \board3\portal\portal\columns(); + return $portal_columns->string_to_number($column); } +/** + * Convert column string to equivalent constant + * + * @param string $column Column name + * + * @return int Column constant; default: 0 + * @deprecated 2.1.0-RC1 (to be removed: 2.1.0) + */ function column_string_const($column) { - switch ($column) - { - case 'top': - return 1; - case 'left': - return 2; - case 'center': - return 4; - case 'right': - return 8; - case 'bottom': - return 16; - default: - return 0; - } + $portal_columns = new \board3\portal\portal\columns(); + return $portal_columns->string_to_constant($column); } diff --git a/portal/columns.php b/portal/columns.php new file mode 100644 index 00000000..07380acd --- /dev/null +++ b/portal/columns.php @@ -0,0 +1,91 @@ +set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), $config, $request)); $phpbb_container->setParameter('board3.portal.modules.table', $table_prefix . 'portal_modules'); $phpbb_container->setParameter('board3.portal.config.table', $table_prefix . 'portal_config'); + $this->portal_columns = new \board3\portal\portal\columns(); + $phpbb_container->set('board3.portal.columns', $this->portal_columns); $cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put')); $cache->expects($this->any()) ->method('destroy') @@ -72,7 +74,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data $portal_modules = obtain_portal_modules(); foreach($portal_modules as $cur_module) { - $this->portal_module->module_column[$cur_module['module_classname']][] = column_num_string($cur_module['module_column']); + $this->portal_module->module_column[$cur_module['module_classname']][] = $this->portal_columns->number_to_string($cur_module['module_column']); } } @@ -191,7 +193,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data if ($add_to_column) { $module_data = $this->portal_module->get_move_module_data($module_id); - $this->portal_module->module_column[$module_data['module_classname']][] = column_num_string($add_to_column); + $this->portal_module->module_column[$module_data['module_classname']][] = $this->portal_columns->number_to_string($add_to_column); } for ($i = 1; $i <= $move_right; $i++) @@ -242,7 +244,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data if ($add_to_column) { $module_data = $this->portal_module->get_move_module_data($module_id); - $this->portal_module->module_column[$module_data['module_classname']][] = column_num_string($add_to_column); + $this->portal_module->module_column[$module_data['module_classname']][] = $this->portal_columns->number_to_string($add_to_column); } // We always start in the right column = 3 diff --git a/tests/unit/controller/helper_test.php b/tests/unit/controller/helper_test.php index d1ba28e7..45419abb 100644 --- a/tests/unit/controller/helper_test.php +++ b/tests/unit/controller/helper_test.php @@ -40,6 +40,7 @@ class helper_test extends \board3\portal\tests\testframework\test_case $this->user->data['group_id'] = 2; $this->phpbb_root_path = dirname(__FILE__) . '/../../../../../../'; $this->php_ext = 'php'; + $this->portal_columns = new \board3\portal\portal\columns(); $this->modules = array( '\board3\portal\modules\link_us' => new \board3\portal\modules\link_us($config, new \board3\portal\tests\mock\template($this), new \board3\portal\tests\mock\user), ); @@ -60,6 +61,7 @@ class helper_test extends \board3\portal\tests\testframework\test_case { $controller_helper = new \board3\portal\controller\helper( $this->auth, + $this->portal_columns, $this->config, $this->template, $this->user, diff --git a/tests/unit/functions/functions_modules_test.php b/tests/unit/functions/functions_modules_test.php index 4c586095..e6755a33 100644 --- a/tests/unit/functions/functions_modules_test.php +++ b/tests/unit/functions/functions_modules_test.php @@ -14,8 +14,8 @@ class phpbb_functions_functions_modules_test extends PHPUnit_Framework_TestCase public function data_column_num_string() { return array( - array(0, ''), - array(0, false), + array('', ''), + array('', false), array('left', 1), array('center', 2), array('right', 3), diff --git a/tests/unit/portal/columns_test.php b/tests/unit/portal/columns_test.php new file mode 100644 index 00000000..b9ccf553 --- /dev/null +++ b/tests/unit/portal/columns_test.php @@ -0,0 +1,67 @@ +portal_columns = new \board3\portal\portal\columns(); + } + public function data_column_number_string() + { + return array( + array(4, 'top'), + array(1, 'left'), + array(2, 'center'), + array(3, 'right'), + array(5, 'bottom'), + array(0, ''), + ); + } + + /** + * @dataProvider data_column_number_string + */ + public function test_number_to_string($number, $string) + { + $this->assertEquals($string, $this->portal_columns->number_to_string($number)); + } + + /** + * @dataProvider data_column_number_string + */ + public function test_string_to_number($number, $string) + { + $this->assertEquals($number, $this->portal_columns->string_to_number($string)); + } + + public function data_column_string_constant() + { + return array( + array('top', 1), + array('left', 2), + array('center', 4), + array('right', 8), + array('bottom', 16), + array('', 0), + ); + } + + /** + * @dataProvider data_column_string_constant + */ + public function test_string_to_constant($string, $constant) + { + $this->assertEquals($constant, $this->portal_columns->string_to_constant($string)); + } +} \ No newline at end of file