[ticket/289] Add helper class for controller
This commit is contained in:
@@ -11,18 +11,18 @@ namespace board3\portal\controller;
|
||||
|
||||
class main
|
||||
{
|
||||
/**
|
||||
* Auth object
|
||||
* @var \phpbb\auth\auth
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* phpBB Config object
|
||||
* @var \phpbb\config\config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Board3 Portal controller helper
|
||||
* @var \board3\portal\controller\helper
|
||||
*/
|
||||
protected $controller_helper;
|
||||
|
||||
/**
|
||||
* Template object
|
||||
* @var \phpbb\template
|
||||
@@ -65,12 +65,6 @@ class main
|
||||
*/
|
||||
protected $path_helper;
|
||||
|
||||
/**
|
||||
* Portal Helper object
|
||||
* @var \board3\portal\includes\helper
|
||||
*/
|
||||
protected $portal_helper;
|
||||
|
||||
/**
|
||||
* Portal modules count
|
||||
* @var array
|
||||
@@ -87,29 +81,27 @@ 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 \phpbb\auth\auth $auth Auth object
|
||||
* @param \phpbb\config\config $config phpBB Config object
|
||||
* @param \board3\portal\controller\helper $controller_helper Controller helper
|
||||
* @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\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
|
||||
* @param string $modules_table Board3 modules table
|
||||
*/
|
||||
public function __construct($auth, $config, $template, $user, $path_helper, $portal_helper, $phpbb_root_path, $php_ext, $config_table, $modules_table)
|
||||
public function __construct($config, $controller_helper, $template, $user, $path_helper, $phpbb_root_path, $php_ext, $config_table, $modules_table)
|
||||
{
|
||||
global $portal_root_path;
|
||||
|
||||
$this->auth = $auth;
|
||||
$this->config = $config;
|
||||
$this->controller_helper = $controller_helper;
|
||||
$this->template = $template;
|
||||
$this->user = $user;
|
||||
$this->path_helper = $path_helper;
|
||||
$this->phpbb_root_path = $phpbb_root_path;
|
||||
$this->php_ext = $php_ext;
|
||||
$this->portal_helper = $portal_helper;
|
||||
|
||||
$this->includes_path = $phpbb_root_path . 'ext/board3/portal/includes/';
|
||||
$this->root_path = $phpbb_root_path . 'ext/board3/portal/';
|
||||
@@ -132,7 +124,7 @@ class main
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->run_initial_tasks();
|
||||
$this->controller_helper->run_initial_tasks();
|
||||
|
||||
// Set default data
|
||||
$this->portal_modules = obtain_portal_modules();
|
||||
@@ -156,13 +148,13 @@ class main
|
||||
*/
|
||||
foreach ($this->portal_modules as $row)
|
||||
{
|
||||
if (!($module = $this->get_portal_module($row)))
|
||||
if (!($module = $this->controller_helper->get_portal_module($row)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Load module language file
|
||||
$this->load_module_language($module);
|
||||
$this->controller_helper->load_module_language($module);
|
||||
|
||||
if ($row['module_column'] == column_string_num('left') && $this->config['board3_left_column'])
|
||||
{
|
||||
@@ -195,10 +187,10 @@ class main
|
||||
}
|
||||
|
||||
// Custom Blocks that have been defined in the ACP will return an array instead of just the name of the template file
|
||||
$this->assign_module_vars($row, $template_module);
|
||||
$this->controller_helper->assign_module_vars($row, $template_module);
|
||||
|
||||
// Check if we need to show the online list
|
||||
$display_online = $this->check_online_list($row['module_classname'], $display_online);
|
||||
$display_online = $this->controller_helper->check_online_list($row['module_classname'], $display_online);
|
||||
|
||||
unset($template_module);
|
||||
}
|
||||
@@ -220,20 +212,6 @@ class main
|
||||
page_footer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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'))
|
||||
{
|
||||
redirect(append_sid($this->phpbb_root_path . 'index' . $this->php_ext));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if portal needs to redirect to index page
|
||||
*/
|
||||
@@ -247,71 +225,6 @@ class main
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if online list should be displayed
|
||||
*
|
||||
* @return mixed True if online list should be display, current value
|
||||
* if unsure
|
||||
*/
|
||||
protected function check_online_list($module_classname, $display_online)
|
||||
{
|
||||
return ($module_classname === '\board3\portal\modules\whois_online') ? true : $display_online;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get portal module and run module start checks
|
||||
*
|
||||
* @param array $row Module row
|
||||
*
|
||||
* @return mixed False if one of the module checks failed, the module
|
||||
* object if checks were successful
|
||||
*/
|
||||
protected function get_portal_module($row)
|
||||
{
|
||||
// Do not try to load non-existent or disabled modules
|
||||
if ($row['module_status'] == B3_MODULE_DISABLED || !is_object($module = $this->portal_helper->get_module($row['module_classname'])))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for permissions before loading anything
|
||||
* the default group of a user always defines his/her permission
|
||||
*/
|
||||
return ($this->check_group_access($row)) ? $module : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user is in required groups
|
||||
*
|
||||
* @param array $row Module row
|
||||
*/
|
||||
protected function check_group_access($row)
|
||||
{
|
||||
$group_ary = (!empty($row['module_group_ids'])) ? explode(',', $row['module_group_ids']) : '';
|
||||
if ((is_array($group_ary) && !in_array($this->user->data['group_id'], $group_ary)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load language file of module
|
||||
*
|
||||
* @param object $module Module of which language file should be loaded
|
||||
*/
|
||||
protected function load_module_language($module)
|
||||
{
|
||||
if ($language_file = $module->get_language())
|
||||
{
|
||||
$this->user->add_lang_ext('board3/portal', 'modules/' . $language_file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign template vars for portal
|
||||
*
|
||||
@@ -333,41 +246,6 @@ class main
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign module's template vars
|
||||
*
|
||||
* @param array $row Database row of module
|
||||
* @param mixed $template_module Template data as returned by module
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
protected function assign_module_vars($row, $template_module)
|
||||
{
|
||||
if (is_array($template_module))
|
||||
{
|
||||
$this->template->assign_block_vars('modules_' . column_num_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'],
|
||||
'CODE' => $template_module['code'],
|
||||
'MODULE_ID' => $row['module_id'],
|
||||
'IMAGE_WIDTH' => $row['module_image_width'],
|
||||
'IMAGE_HEIGHT' => $row['module_image_height'],
|
||||
));
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->template->assign_block_vars('modules_' . column_num_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'],
|
||||
'IMAGE_HEIGHT' => $row['module_image_height'],
|
||||
'MODULE_ID' => $row['module_id'],
|
||||
'TITLE' => (isset($this->user->lang[$row['module_name']])) ? $this->user->lang[$row['module_name']] : utf8_normalize_nfc($row['module_name']),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check module count and related config setting
|
||||
*
|
||||
@@ -380,21 +258,4 @@ class main
|
||||
{
|
||||
return $this->module_count[$column] > 0 && $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user