choice   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 32
c 2
b 0
f 1
dl 0
loc 90
ccs 41
cts 41
cp 1
rs 10
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_submitted_value() 0 10 2
A get_default_props() 0 6 1
A set_field_options() 0 24 5
A display_field() 0 4 2
A show_form_field() 0 7 2
A get_default_value() 0 5 4
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 18
	public function get_default_props()
18
	{
19
		return array(
20 18
			'defaults'		=> array(),
21 18
			'options'		=> array(),
22 18
			'multi_select'	=> false,
23 18
		);
24
	}
25
26
	/**
27
	 * @inheritdoc
28
	 */
29
	public function display_field(array $data, array $topic_data, $view_mode)
30 14
	{
31
		$value = $this->ensure_is_array($data['field_value']);
32 14
		return sizeof($value) ? join($this->language->lang('COMMA_SEPARATOR'), $value) : '';
33 14
	}
34
35 14
	/**
36
	 * @inheritdoc
37
	 * @return array
38
	 */
39
	public function get_submitted_value(array $data, $form_is_submitted = false)
40
	{
41 14
		$default = $this->get_default_value($data);
42
43 14
		if ($form_is_submitted)
44
		{
45
			return $this->request->variable($data['field_name'], $default, true);
46 14
		}
47 14
48 10
		return $default;
49 10
	}
50
51 14
	/**
52
	 * @inheritdoc
53
	 */
54
	public function show_form_field(array &$data)
55
	{
56
		$this->set_field_options($data);
57 12
		$this->ptemplate->assign_vars($data);
58
59 12
		$tpl_name = ($data['field_type'] === 'select') ? 'select' : 'pickem';
60 12
		return $this->ptemplate->render_view('blitze/content', "fields/$tpl_name.html", $data['field_type'] . '_field');
61
	}
62
63
	/**
64
	 * @param array $data
65
	 * @return mixed
66 14
	 */
67
	protected function get_default_value(array $data)
68 14
	{
69
		$value = $this->ensure_is_array($data['field_value']);
70 14
		$default = $value ?: $data['field_props']['defaults'] ?: array(0 => '');
71 14
		return ($data['field_props']['multi_select']) ? $default : array_shift($default);
72
	}
73 14
74 14
	/**
75
	 * @param array $data
76 14
	 * @return void
77 14
	 */
78
	protected function set_field_options(array &$data)
79
	{
80
		if ($data['field_type'] === 'radio' || $data['field_type'] === 'checkbox')
81
		{
82
			$data['field_id'] .= '-0';
83
		}
84
85 14
		$count = 0;
86
		$options = array();
87 14
		if (is_array($data['field_props']['options']))
88 14
		{
89 9
			$choices = (array) $data['field_props']['options'];
90 9
			foreach ($choices as $value => $option)
91
			{
92 14
				$options[] = array(
93 14
					'id'		=> 'smc-'. $data['field_name'] . '-' . $count,
94 14
					'label'		=> $this->language->lang($option),
95 14
					'selected'	=> (int) (in_array($value, (array) $data['field_value'])),
96 14
					'value'		=> $value,
97 14
				);
98
				$count++;
99 14
			}
100 14
		}
101 14
		$data['field_props']['options'] = $options;
102 14
	}
103
}
104