diff --git a/acp/portal_module.php b/acp/portal_module.php index 42f705db..52d065a7 100644 --- a/acp/portal_module.php +++ b/acp/portal_module.php @@ -15,12 +15,9 @@ 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; + protected $root_path, $version_check, $request, $php_ext, $portal_helper; public $module_column = array(); - /** @var \phpbb\di\service_collection Portal modules */ - protected $modules; - public function __construct() { global $db, $user, $cache, $request, $template, $table_prefix; @@ -43,7 +40,7 @@ class portal_module $this->php_ext = $phpEx; $this->phpbb_container = $phpbb_container; $this->version_check = $this->phpbb_container->get('board3.version.check'); - $this->register_modules($this->phpbb_container->get('board3.module_collection')); + $this->portal_helper = $this->phpbb_container->get('board3.portal.helper'); define('PORTAL_MODULES_TABLE', $this->phpbb_container->getParameter('board3.modules.table')); define('PORTAL_CONFIG_TABLE', $this->phpbb_container->getParameter('board3.config.table')); @@ -103,25 +100,10 @@ class portal_module if ($module_data !== false) { - if (!isset($this->modules[$module_data['module_classname']])) + + if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname']))) { - $class = 'portal_' . $module_data['module_classname'] . '_module'; - if (!class_exists($class)) - { - include($this->root_path . 'portal/modules/portal_' . $module_data['module_classname'] . '.' . $this->php_ext); - } - if (class_exists($class)) - { - $this->c_class = new $class(); - } - else - { - continue; - } - } - else - { - $this->c_class = $this->modules[$module_data['module_classname']]; + continue; } if ($this->c_class->get_language()) @@ -434,11 +416,7 @@ class portal_module trigger_error($this->user->lang['MODULE_ADD_ONCE'] . adm_back_link($this->u_action), E_USER_WARNING); } - if (isset($this->modules[$module_classname])) - { - $this->c_class = $this->modules[$module_classname]; - } - else + if (!($this->c_class = $this->portal_helper->get_module($module_classname))) { continue; } @@ -501,9 +479,10 @@ class portal_module $this->template->assign_var('S_EDIT', true); $fileinfo = $name_ary = array(); $column_string = column_num_string($add_column); + $modules_list = $this->portal_helper->get_all_modules(); // Find new modules - foreach ($this->modules as $module_class => $module) + foreach ($modules_list as $module_class => $module) { if ($module_class !== '\board3\portal\modules\custom') { @@ -586,14 +565,10 @@ class portal_module foreach($portal_modules as $row) { - if (!isset($this->modules[$row['module_classname']])) + if (!($this->c_class = $this->portal_helper->get_module($row['module_classname']))) { continue; } - else - { - $this->c_class = $this->modules[$row['module_classname']]; - } if ($this->c_class->get_language()) { @@ -753,13 +728,11 @@ class portal_module $module_data = $this->db->sql_fetchrow($result); $this->db->sql_freeresult($result); - if (!isset($this->modules[$module_data['module_classname']])) + if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname']))) { trigger_error('CLASS_NOT_FOUND', E_USER_ERROR); } - $this->c_class = $this->modules[$module_data['module_classname']]; - $sql_ary = array( 'module_name' => $this->c_class->get_name(), 'module_image_src' => $this->c_class->get_image(), @@ -939,12 +912,11 @@ class portal_module { $module_data = $this->get_move_module_data($module_id); - if (!isset($this->modules[$module_data['module_classname']])) + if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname']))) { trigger_error('CLASS_NOT_FOUND', E_USER_ERROR); } - $this->c_class = $this->modules[$module_data['module_classname']]; $move_action = 0; if ($module_data !== false && $module_data['module_column'] > column_string_num('left')) @@ -1035,12 +1007,11 @@ class portal_module { $module_data = $this->get_move_module_data($module_id); - if (!isset($this->modules[$module_data['module_classname']])) + if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname']))) { trigger_error('CLASS_NOT_FOUND', E_USER_ERROR); } - $this->c_class = $this->modules[$module_data['module_classname']]; $move_action = 0; if ($module_data !== false && $module_data['module_column'] < column_string_num('right')) @@ -1143,14 +1114,13 @@ class portal_module { $module_classname = $this->request->variable('module_classname', ''); - if (!isset($this->modules[$module_classname])) + if (!($this->c_class = $this->portal_helper->get_module($module_data['module_classname']))) { trigger_error('CLASS_NOT_FOUND', E_USER_ERROR); } if (confirm_box(true)) { - $this->c_class = $this->modules[$module_classname]; $this->c_class->uninstall($module_data['module_id'], $this->db); $sql = 'DELETE FROM ' . PORTAL_MODULES_TABLE . ' @@ -1178,7 +1148,6 @@ class portal_module } else { - $this->c_class = $this->modules[$module_classname]; if ($this->c_class->get_language()) { $this->user->add_lang_ext('board3/portal', 'modules/' . $this->c_class->get_language()); @@ -1209,25 +1178,6 @@ class portal_module return preg_replace(array('/i=[0-9]+/', '/mode=[a-zA-Z0-9_]+/'), array('i=%5C' . str_replace('\\', '%5C', __CLASS__), 'mode=' . $mode), $this->u_action) . (($module_id) ? '&module_id=' . $module_id : ''); } - /** - * Register list of Board3 Portal modules - * - * @param \phpbb\di\service_collection $modules Board3 Modules service - * collection - * @return null - */ - protected function register_modules($modules) - { - foreach ($modules as $current_module) - { - $class_name = '\\' . get_class($current_module); - if (!isset($this->modules[$class_name])) - { - $this->modules[$class_name] = $current_module; - } - } - } - /** * Check if module can be moved to desired column(s) * diff --git a/controller/main.php b/controller/main.php index 674996ce..96c84e2c 100644 --- a/controller/main.php +++ b/controller/main.php @@ -80,7 +80,7 @@ class main * @param \phpbb\template $template Template object * @param \phpbb\user $user User object * @param \phpbb\path_helper $path_helper phpBB path helper - * @param \board3\portal\includes\portal_helper $portal_helper Portal helper class + * @param \board3\portal\includes\helper $portal_helper Portal helper class * @param string $phpbb_root_path phpBB root path * @param string $php_ext PHP file extension * @param string $config_table Board3 config table @@ -120,19 +120,9 @@ class main */ public function handle() { - $this->check_permission(); - // We defined the phpBB objects in __construct() and can use them in the rest of our class like this - //echo 'Welcome, ' . $this->user->data['username']; + $this->run_initial_tasks(); - // The following takes two arguments: - // 1) which extension language folder we're using (it's not smart enough to use its own automatically) - // 2) what language file to use - $this->user->add_lang_ext('board3/portal', 'portal'); - - /** - * get initial data - */ - $portal_config = obtain_portal_config(); + // Set default data $portal_modules = obtain_portal_modules(); $display_online = false; @@ -182,19 +172,16 @@ class main if ($row['module_column'] == column_string_num('left') && $this->config['board3_left_column']) { $template_module = $module->get_template_side($row['module_id']); - $template_column = 'left'; ++$module_count['left']; } if ($row['module_column'] == column_string_num('center')) { $template_module = $module->get_template_center($row['module_id']); - $template_column = 'center'; ++$module_count['center']; } if ($row['module_column'] == column_string_num('right') && $this->config['board3_right_column']) { $template_module = $module->get_template_side($row['module_id']); - $template_column = 'right'; ++$module_count['right']; } if ($row['module_column'] == column_string_num('top')) @@ -279,7 +266,12 @@ class main page_footer(); } - // check if user should be able to access this page + /** + * Check if user should be able to access this page. Redirect to index + * if this does not apply. + * + * @return null + */ protected function check_permission() { if (!isset($this->config['board3_enable']) || !$this->config['board3_enable'] || !$this->auth->acl_get('u_view_portal')) @@ -287,4 +279,21 @@ class main redirect(append_sid($this->phpbb_root_path . 'index' . $this->php_ext)); } } + + /** + * Run initial tasks that are required for a properly setup extension + * + * @return null + */ + protected function run_initial_tasks() + { + // Check for permissions first + $this->check_permission(); + + // Load language file + $this->user->add_lang_ext('board3/portal', 'portal'); + + // Obtain portal config + obtain_portal_config(); + } } diff --git a/includes/helper.php b/includes/helper.php index 813e3c56..97ff11a7 100644 --- a/includes/helper.php +++ b/includes/helper.php @@ -52,6 +52,8 @@ class helper * Get module specified by module class name * * @param string $module_name Module class name + * + * @return bool|object The module object if it exists, false if not */ public function get_module($module_name) { @@ -64,4 +66,14 @@ class helper return false; } } + + /** + * Get all supported modules + * + * @return array An array containing all supported modules + */ + public function get_all_modules() + { + return $this->modules; + } } diff --git a/tests/unit/acp/move_module_test.php b/tests/unit/acp/move_module_test.php index 335153a8..5d1aaea1 100644 --- a/tests/unit/acp/move_module_test.php +++ b/tests/unit/acp/move_module_test.php @@ -38,6 +38,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data new \board3\portal\modules\welcome($config, new \phpbb_mock_request, $this->db, $user, $phpbb_root_path, $phpEx), new \board3\portal\modules\donation($config, $template, $user), )); + $phpbb_container->set('board3.portal.helper', new \board3\portal\includes\helper($phpbb_container->get('board3.module_collection'))); $phpbb_container->setParameter('board3.modules.table', $table_prefix . 'portal_modules'); $phpbb_container->setParameter('board3.config.table', $table_prefix . 'portal_config'); $cache = $this->getMock('\phpbb\cache\cache', array('destroy', 'sql_exists', 'get', 'put')); diff --git a/tests/unit/includes/helper_test.php b/tests/unit/includes/helper_test.php index 3b012495..2489d3e3 100644 --- a/tests/unit/includes/helper_test.php +++ b/tests/unit/includes/helper_test.php @@ -50,4 +50,9 @@ class board3_includes_helper_test extends \board3\portal\tests\testframework\tes } $this->assertEquals($expected, $this->portal_helper->get_module($module_name)); } + + public function test_get_all_modules() + { + $this->assertEquals($this->modules, $this->portal_helper->get_all_modules()); + } }