Merge pull request #313 from marc1706/ticket/289

[ticket/289] Add method for assigning template vars
This commit is contained in:
Marc Alexander
2014-07-25 14:48:02 +02:00
4 changed files with 502 additions and 124 deletions

View File

@@ -17,6 +17,19 @@ parameters:
services:
board3.portal.main:
class: board3\portal\controller\main
arguments:
- @config
- @board3.portal.controller_helper
- @template
- @user
- @path_helper
- %core.root_path%
- .%core.php_ext%
- %board3.config.table%
- %board3.modules.table%
board3.portal.controller_helper:
class: board3\portal\controller\helper
arguments:
- @auth
- @config
@@ -26,8 +39,6 @@ services:
- @board3.portal.helper
- %core.root_path%
- .%core.php_ext%
- %board3.config.table%
- %board3.modules.table%
board3.version.check:
class: board3\portal\includes\version_check

231
controller/helper.php Normal file
View File

@@ -0,0 +1,231 @@
<?php
/**
*
* @package Board3 Portal v2.1
* @copyright (c) 2013 Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace board3\portal\controller;
class helper
{
/**
* Auth object
* @var \phpbb\auth\auth
*/
protected $auth;
/**
* phpBB Config object
* @var \phpbb\config\config
*/
protected $config;
/**
* Template object
* @var \phpbb\template
*/
protected $template;
/**
* User object
* @var \phpbb\user
*/
protected $user;
/**
* phpBB root path
* @var string
*/
protected $phpbb_root_path;
/**
* PHP file extension
* @var string
*/
protected $php_ext;
/**
* Portal root path
* @var string
*/
protected $root_path;
/**
* phpBB path helper
* @var \phpbb\path_helper
*/
protected $path_helper;
/**
* Portal Helper object
* @var \board3\portal\includes\helper
*/
protected $portal_helper;
/**
* Portal modules array
* @var array
*/
protected $portal_modules;
/**
* 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 \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
*/
public function __construct($auth, $config, $template, $user, $path_helper, $portal_helper, $phpbb_root_path, $php_ext)
{
$this->auth = $auth;
$this->config = $config;
$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->root_path = $phpbb_root_path . 'ext/board3/portal/';
}
/**
* 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));
}
}
/**
* Return true if online list should be displayed
*
* @return mixed True if online list should be display, current value
* if unsure
*/
public 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
*/
public 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
*/
public function load_module_language($module)
{
if ($language_file = $module->get_language())
{
$this->user->add_lang_ext('board3/portal', 'modules/' . $language_file);
}
}
/**
* Assign module's template vars
*
* @param array $row Database row of module
* @param mixed $template_module Template data as returned by module
*
* @return null
*/
public 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']),
));
}
}
/**
* Run initial tasks that are required for a properly setup extension
*
* @return null
*/
public 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();
}
}

View File

@@ -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
@@ -66,38 +66,42 @@ class main
protected $path_helper;
/**
* Portal Helper object
* @var \board3\portal\includes\helper
* Portal modules count
* @var array
*/
protected $portal_helper;
protected $module_count;
/**
* Portal modules array
* @var array
*/
protected $portal_modules;
/**
* 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/';
@@ -120,17 +124,17 @@ class main
*/
public function handle()
{
$this->run_initial_tasks();
$this->controller_helper->run_initial_tasks();
// Set default data
$portal_modules = obtain_portal_modules();
$this->portal_modules = obtain_portal_modules();
$display_online = false;
/**
* set up column_count array
* with this we can hide unneeded parts of the portal
*/
$module_count = array(
$this->module_count = array(
'total' => 0,
'top' => 0,
'left' => 0,
@@ -142,118 +146,37 @@ class main
/**
* start assigning block vars
*/
foreach ($portal_modules as $row)
foreach ($this->portal_modules as $row)
{
if($row['module_status'] == B3_MODULE_DISABLED)
if (!($module = $this->controller_helper->get_portal_module($row)))
{
continue;
}
// Do not try to load non-existent modules
if (!($module = $this->portal_helper->get_module($row['module_classname'])))
{
continue;
}
// Load module language file
$this->controller_helper->load_module_language($module);
/**
* Check for permissions before loading anything
* the default group of a user always defines his/her permission (KISS)
*/
$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)))
{
continue;
}
$template_module = $this->get_module_template($row, $module);
if ($language_file = $module->get_language())
{
$this->user->add_lang_ext('board3/portal', 'modules/' . $language_file);
}
if ($row['module_column'] == column_string_num('left') && $this->config['board3_left_column'])
{
$template_module = $module->get_template_side($row['module_id']);
++$module_count['left'];
}
if ($row['module_column'] == column_string_num('center'))
{
$template_module = $module->get_template_center($row['module_id']);
++$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']);
++$module_count['right'];
}
if ($row['module_column'] == column_string_num('top'))
{
$template_module = $module->get_template_center($row['module_id']);
++$module_count['top'];
}
if ($row['module_column'] == column_string_num('bottom'))
{
$template_module = $module->get_template_center($row['module_id']);
++$module_count['bottom'];
}
if (!isset($template_module))
if (empty($template_module))
{
continue;
}
// Custom Blocks that have been defined in the ACP will return an array instead of just the name of the template file
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']),
));
}
$this->controller_helper->assign_module_vars($row, $template_module);
// Check if we need to show the online list
if ($row['module_classname'] === '\board3\portal\modules\whois_online')
{
$display_online = true;
}
$display_online = $this->controller_helper->check_online_list($row['module_classname'], $display_online);
unset($template_module);
}
$module_count['total'] = sizeof($portal_modules);
// Redirect to index if there are currently no active modules
if($module_count['total'] < 1)
{
redirect(append_sid($this->phpbb_root_path . 'index' . $this->php_ext));
}
$this->check_redirect();
// Assign specific vars
$this->template->assign_vars(array(
// 'S_SMALL_BLOCK' => true,
'S_PORTAL_LEFT_COLUMN' => $this->config['board3_left_column_width'],
'S_PORTAL_RIGHT_COLUMN' => $this->config['board3_right_column_width'],
'S_LEFT_COLUMN' => ($module_count['left'] > 0 && $this->config['board3_left_column']) ? true : false,
'S_CENTER_COLUMN' => ($module_count['center'] > 0) ? true : false,
'S_RIGHT_COLUMN' => ($module_count['right'] > 0 && $this->config['board3_right_column']) ? true : false,
'S_TOP_COLUMN' => ($module_count['top'] > 0) ? true : false,
'S_BOTTOM_COLUMN' => ($module_count['bottom'] > 0) ? true : false,
'S_DISPLAY_PHPBB_MENU' => $this->config['board3_phpbb_menu'],
'B3P_DISPLAY_JUMPBOX' => $this->config['board3_display_jumpbox'],
'T_EXT_THEME_PATH' => $this->path_helper->get_web_root_path() . $this->root_path . 'styles/' . $this->user->style['style_path'] . '/theme/',
));
$this->assign_template_vars();
// And now to output the page.
page_header($this->user->lang('PORTAL'), $display_online);
@@ -267,33 +190,77 @@ class main
}
/**
* Check if user should be able to access this page. Redirect to index
* if this does not apply.
* Get module's template
*
* @return null
* @param array $row Database row of module
* @param object $module Module object
*
* @return mixed False if module is not inside possible columns or if
* module shouldn't be shown, otherwise module's template
*/
protected function check_permission()
public function get_module_template($row, $module)
{
if (!isset($this->config['board3_enable']) || !$this->config['board3_enable'] || !$this->auth->acl_get('u_view_portal'))
$template_module = false;
$column = column_num_string($row['module_column']);
if (in_array($column, array('left', 'right')) && $this->config['board3_' . $column . '_column'])
{
++$this->module_count[$column];
$template_module = $module->get_template_side($row['module_id']);
}
else if (in_array($column, array('top', 'center', 'bottom')))
{
++$this->module_count[$column];
$template_module = $module->get_template_center($row['module_id']);
}
return $template_module;
}
/**
* Check if portal needs to redirect to index page
*/
protected function check_redirect()
{
$this->module_count['total'] = sizeof($this->portal_modules);
if ($this->module_count['total'] < 1)
{
redirect(append_sid($this->phpbb_root_path . 'index' . $this->php_ext));
}
}
/**
* Run initial tasks that are required for a properly setup extension
* Assign template vars for portal
*
* @return null
*/
protected function run_initial_tasks()
protected function assign_template_vars()
{
// Check for permissions first
$this->check_permission();
$this->template->assign_vars(array(
'S_PORTAL_LEFT_COLUMN' => $this->config['board3_left_column_width'],
'S_PORTAL_RIGHT_COLUMN' => $this->config['board3_right_column_width'],
'S_LEFT_COLUMN' => $this->check_module_count('left', $this->config['board3_left_column']),
'S_CENTER_COLUMN' => $this->check_module_count('center'),
'S_RIGHT_COLUMN' => $this->check_module_count('right', $this->config['board3_right_column']),
'S_TOP_COLUMN' => $this->check_module_count('top'),
'S_BOTTOM_COLUMN' => $this->check_module_count('bottom'),
'S_DISPLAY_PHPBB_MENU' => $this->config['board3_phpbb_menu'],
'B3P_DISPLAY_JUMPBOX' => $this->config['board3_display_jumpbox'],
'T_EXT_THEME_PATH' => $this->path_helper->get_web_root_path() . $this->root_path . 'styles/' . $this->user->style['style_path'] . '/theme/',
));
}
// Load language file
$this->user->add_lang_ext('board3/portal', 'portal');
// Obtain portal config
obtain_portal_config();
/**
* Check module count and related config setting
*
* @param string $column Column to check
* @param bool $config Config value to check
*
* @return bool True if module count is bigger than 0 and $config is true
*/
protected function check_module_count($column, $config = true)
{
return $this->module_count[$column] > 0 && $config;
}
}

View File

@@ -0,0 +1,169 @@
<?php
/**
*
* @package testing
* @copyright (c) Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace board3\portal\controller;
class helper_test extends \board3\portal\tests\testframework\test_case
{
protected $controller_helper;
protected $modules;
static public $redirect = false;
public function setUp()
{
global $cache;
parent::setUp();
define(B3_MODULE_DISABLED, 0);
define(B3_MODULE_ENABLED, 1);
$cache = $this->getMock('\phpbb\cache\driver', array('get', 'put'));
$this->auth = $this->getMock('\phpbb\auth\auth', array('acl_get'));
$this->auth->expects($this->any())
->method('acl_get')
->with($this->anything())
->will($this->returnValue(true));
$this->config = new \phpbb\config\config(array(
'board3_enable' => true,
));
$this->template = new \board3\portal\tests\mock\template($this);
$this->user = $this->getMock('\phpbb\user', array('add_lang_ext'));
$this->user->data['group_id'] = 2;
$this->phpbb_root_path = dirname(__FILE__) . '/../../../../../../';
$this->php_ext = 'php';
$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),
);
$this->portal_helper = new \board3\portal\includes\helper($this->modules);
$this->path_helper = new \phpbb\path_helper(
new \phpbb\symfony_request(
new \phpbb_mock_request()
),
new \phpbb\filesystem(),
new \phpbb_mock_request(),
$this->phpbb_root_path,
$this->php_ext
);
$this->controller_helper = $this->get_controller_helper();
}
protected function get_controller_helper()
{
$controller_helper = new \board3\portal\controller\helper(
$this->auth,
$this->config,
$this->template,
$this->user,
$this->path_helper,
$this->portal_helper,
$this->phpbb_root_path,
$this->php_ext
);
return $controller_helper;
}
public function test_check_online_list()
{
$this->assertFalse($this->controller_helper->check_online_list('foobar', false));
$this->assertTrue($this->controller_helper->check_online_list('\board3\portal\modules\whois_online', false));
$this->assertTrue($this->controller_helper->check_online_list('foobar', true));
}
public function test_check_permission()
{
self::$redirect = false;
$this->controller_helper->run_initial_tasks();
$this->assertFalse(self::$redirect);
$this->config['board3_enable'] = false;
$controller_helper = $this->get_controller_helper();
$controller_helper->run_initial_tasks();
$this->assertTrue(self::$redirect);
}
public function data_get_portal_module()
{
return array(
array(false, array(
'module_status' => 1,
'module_classname' => 'foo',
)),
array(false, array(
'module_status' => 0,
'module_classname' => '\board3\portal\modules\link_us',
)),
array(true, array(
'module_status' => 1,
'module_classname' => '\board3\portal\modules\link_us',
)),
array(false, array(
'module_status' => 1,
'module_classname' => '\board3\portal\modules\link_us',
'module_group_ids' => 3,4,
)),
);
}
/**
* @dataProvider data_get_portal_module
*/
public function test_get_portal_module($expected, $row)
{
$this->assertEquals(($expected) ? $this->modules['\board3\portal\modules\link_us'] : false, $this->controller_helper->get_portal_module($row));
}
public function test_load_module_language()
{
$this->assertNull($this->controller_helper->load_module_language($this->modules['\board3\portal\modules\link_us']));
}
public function data_assign_module_vars()
{
return array(
array(array(
'module_column' => 1,
'module_id' => 1,
'module_image_width' => 16,
'module_image_height' => 16,
), array(
'template' => 'foobar.html',
'title' => 'foo',
'code' => 'bar',
)),
array(array(
'module_column' => 1,
'module_id' => 1,
'module_image_width' => 16,
'module_image_height' => 16,
), 'foobar.html'),
);
}
/**
* @dataProvider data_assign_module_vars
*/
public function test_assign_module_vars($row, $template_module)
{
$this->assertNull($this->controller_helper->assign_module_vars($row, $template_module));
}
}
function redirect($foo)
{
\board3\portal\controller\helper_test::$redirect = true;
}
function append_sid($link)
{
return $link;
}