[ticket/289] Add method for generating select boxes in modules

This commit is contained in:
Marc Alexander
2014-07-24 13:16:48 +02:00
parent 53a58f8a7c
commit 240168e925
4 changed files with 76 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ services:
arguments:
- @auth
- @config
- @board3.portal.modules_helper
- @template
- @dbal.conn
- @request

View File

@@ -47,4 +47,28 @@ class modules_helper
return $disallow_access;
}
/**
* Generate select box
*
* @param string $key Key of select box
* @param array $select_ary Array of select box options
* @param array $selected_options Array of selected options
* @param string $select_key Key inside select box options
* that holds the option value
* @return string HTML code of select box
* @access public
*/
public function generate_select_box($key, $select_ary, $selected_options)
{
// Build options
$options = '<select id="' . $key . '" name="' . $key . '[]" multiple="multiple">';
foreach ($select_ary as $id => $option)
{
$options .= '<option value="' . $option['value'] . '"' . ((in_array($option['value'], $selected_options)) ? ' selected="selected"' : '') . '>' . $option['title'] . '</option>';
}
$options .= '</select>';
return $options;
}
}

View File

@@ -47,6 +47,9 @@ class attachments extends module_base
/** @var \phpbb\config\config */
protected $config;
/** @var \board3\portal\includes\modules_helper */
protected $helper;
/** @var \phpbb\request\request */
protected $request;
@@ -70,6 +73,7 @@ class attachments extends module_base
*
* @param \phpbb\auth\auth $auth phpBB auth service
* @param \phpbb\config\config $config phpBB config
* @param \board3\portal\includes\modules_helper $helper Modules helper
* @param \phpbb\template $template phpBB template
* @param \phpbb\db\driver $db Database driver
* @param \phpbb\request\request $request phpBB request
@@ -77,10 +81,11 @@ class attachments extends module_base
* @param string $phpbb_root_path phpBB root path
* @param \phpbb\user $user phpBB user object
*/
public function __construct($auth, $config, $template, $db, $request, $phpEx, $phpbb_root_path, $user)
public function __construct($auth, $config, $helper, $template, $db, $request, $phpEx, $phpbb_root_path, $user)
{
$this->auth = $auth;
$this->config = $config;
$this->helper = $helper;
$this->template = $template;
$this->db = $db;
$this->request = $request;
@@ -177,20 +182,15 @@ class attachments extends module_base
while ($row = $this->db->sql_fetchrow($result))
{
$extensions[] = $row;
$extensions[] = array(
'value' => $row['extension'],
'title' => $row['extension'],
);
}
$selected = $this->get_selected_filetypes($module_id);
// Build options
$ext_options = '<select id="' . $key . '" name="' . $key . '[]" multiple="multiple">';
foreach ($extensions as $id => $ext)
{
$ext_options .= '<option value="' . $ext['extension'] . '"' . ((in_array($ext['extension'], $selected)) ? ' selected="selected"' : '') . '>' . $ext['extension'] . '</option>';
}
$ext_options .= '</select>';
return $ext_options;
return $this->helper->generate_select_box($key, $extensions, $selected);
}
/**

View File

@@ -43,4 +43,44 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe
{
$this->assertEquals($expected, $this->modules_helper->get_disallowed_forums($input));
}
public function data_generate_select_box()
{
return array(
array('<select id="foobar" name="foobar[]" multiple="multiple"><option value="one">one</option><option value="two" selected="selected">two</option></select>',
'foobar',
array(
'1' => array(
'value' => 'one',
'title' => 'one',
),
'2' => array(
'value' => 'two',
'title' => 'two',
),
),
array('two')),
array('<select id="foobar" name="foobar[]" multiple="multiple"><option value="one" selected="selected">two</option><option value="two">three</option></select>',
'foobar',
array(
'1' => array(
'value' => 'one',
'title' => 'two',
),
'2' => array(
'value' => 'two',
'title' => 'three',
),
),
array('one')),
);
}
/**
* @dataProvider data_generate_select_box
*/
public function test_generate_select_box($expected, $key, $select_ary, $selected_options)
{
$this->assertEquals($expected, $this->modules_helper->generate_select_box($key, $select_ary, $selected_options));
}
}