choice::set_field_options()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 4
nop 1
dl 0
loc 24
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
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