Completed
Push — develop ( c77227...1859c0 )
by Daniel
14:00
created

choice   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 1
dl 0
loc 99
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_default_props() 0 8 1
B get_default_value() 0 7 5
A get_field_value() 0 12 2
A display_field() 0 5 2
A show_form_field() 0 13 2
B set_field_options() 0 25 5
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\form\field;
11
12
abstract class choice extends base
13
{
14
	/**
15
	 * @inheritdoc
16
	 */
17 17
	public function get_default_props()
18
	{
19
		return array(
20 17
			'defaults'		=> array(),
21 17
			'options'		=> array(),
22 17
			'multi_select'	=> false,
23 17
		);
24
	}
25
26
	/**
27
	 * @param array $data
28
	 * @return string
29 13
	 */
30
	protected function get_default_value(array $data)
31 13
	{
32 13
		$default = $data['field_value'] ?: ($data['field_props']['defaults'] ?: array(0 => ''));
33
		$default = is_array($default) ? $default : explode("\n", $default);
34 13
35 13
		return ($data['field_props']['multi_select']) ? $default : array_shift($default);
36 13
	}
37 13
38
	/**
39 13
	 * @inheritdoc
40
	 */
41
	public function get_field_value(array $data)
42
	{
43
		$value = $this->get_default_value($data);
44
45 12
		// form has been submitted so get value from request object
46
		if ($this->request->is_set_post('cp'))
47 12
		{
48 12
			$value = $this->request->variable($data['field_name'], $value, true);
49
		}
50
51
		return $value;
52
	}
53
54 13
	/**
55
	 * @inheritdoc
56 13
	 */
57
	public function display_field(array $data)
58 13
	{
59 13
		$field_value = array_filter(explode('<br>', $data['field_value']));
60
		return sizeof($field_value) ? join($this->language->lang('COMMA_SEPARATOR'), $field_value) : '';
61 13
	}
62 13
63
	/**
64 13
	 * @inheritdoc
65 13
	 */
66
	public function show_form_field($name, array &$data)
67
	{
68
		$selected = (array) $this->get_field_value($data);
69
70
		$data['field_name'] = $name;
71
		$data['field_value'] = join("\n", $selected);
72
73 13
		$this->set_field_options($name, $data, $selected);
74
		$this->ptemplate->assign_vars($data);
75 13
76 13
		$tpl_name = ($data['field_type'] === 'select') ? 'select' : 'pickem';
77 8
		return $this->ptemplate->render_view('blitze/content', "fields/$tpl_name.html", $data['field_type'] . '_field');
78 8
	}
79
80 13
	/**
81 13
	 * @param $name
82 13
	 * @param array $data
83 13
	 * @param array $selected
84 13
	 */
85 13
	protected function set_field_options($name, array &$data, array $selected)
86
	{
87 13
		if ($data['field_type'] === 'radio' || $data['field_type'] === 'checkbox')
88 13
		{
89 13
			$data['field_id'] .= '-0';
90 13
		}
91 13
92
		$count = 0;
93 13
		$options = array();
94 13
		if (is_array($data['field_props']['options']))
95 13
		{
96 13
			$choices = (array) $data['field_props']['options'];
97 13
			foreach ($choices as $value => $option)
98
			{
99
				$options[] = array(
100
					'id'		=> 'smc-'. $name . '-' . $count,
101
					'label'		=> $this->language->lang($option),
102
					'selected'	=> (int) (in_array($value, $selected)),
103 13
					'value'		=> $value,
104
				);
105 13
				$count++;
106 13
			}
107 13
		}
108
		$data['field_props']['options'] = $options;
109 13
	}
110
}
111