Passed
Push — release-3.0.0 ( e8971e...d75c10 )
by Daniel
04:23
created

choice::get_submitted_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 10
rs 10
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
	public function get_default_props()
18
	{
19
		return array(
20
			'defaults'		=> array(),
21
			'options'		=> array(),
22
			'multi_select'	=> false,
23
		);
24
	}
25
26
	/**
27
	 * @inheritdoc
28
	 */
29
	public function display_field(array $data, array $topic_data, $view_mode)
30
	{
31
		$value = $this->ensure_is_array($data['field_value']);
32
		return sizeof($value) ? join($this->language->lang('COMMA_SEPARATOR'), $value) : '';
33
	}
34
35
	/**
36
	 * @inheritdoc
37
	 * @return array
38
	 */
39
	public function get_submitted_value(array $data, $form_is_submitted = false)
40
	{
41
		$default = $this->get_default_value($data);
42
43
		if ($form_is_submitted)
44
		{
45
			return $this->request->variable($data['field_name'], $default, true);
46
		}
47
48
		return $default;
49
	}
50
51
	/**
52
	 * @inheritdoc
53
	 */
54
	public function show_form_field(array &$data)
55
	{
56
		$this->set_field_options($data);
57
		$this->ptemplate->assign_vars($data);
58
59
		$tpl_name = ($data['field_type'] === 'select') ? 'select' : 'pickem';
60
		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
	 */
67
	protected function get_default_value(array $data)
68
	{
69
		$value = $this->ensure_is_array($data['field_value']);
70
		$default = $value ?: $data['field_props']['defaults'] ?: array(0 => '');
71
		return ($data['field_props']['multi_select']) ? $default : array_shift($default);
72
	}
73
74
	/**
75
	 * @param array $data
76
	 * @return void
77
	 */
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
		$count = 0;
86
		$options = array();
87
		if (is_array($data['field_props']['options']))
88
		{
89
			$choices = (array) $data['field_props']['options'];
90
			foreach ($choices as $value => $option)
91
			{
92
				$options[] = array(
93
					'id'		=> 'smc-'. $data['field_name'] . '-' . $count,
94
					'label'		=> $this->language->lang($option),
95
					'selected'	=> (int) (in_array($value, (array) $data['field_value'])),
96
					'value'		=> $value,
97
				);
98
				$count++;
99
			}
100
		}
101
		$data['field_props']['options'] = $options;
102
	}
103
}
104