Completed
Push — develop ( 6a0b8a...755a17 )
by Daniel
07:17
created

choice::get_field_value()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 2
crap 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 6
	public function get_default_props()
18
	{
19
		return array(
20 6
			'defaults'		=> array(),
21 6
			'options'		=> array(),
22 6
			'multi_select'	=> false,
23 6
			'per_col'		=> 1,
24 6
		);
25
	}
26
27
	/**
28
	 * @inheritdoc
29
	 */
30 4
	public function get_field_value($name, $default)
31
	{
32 4
		$default = is_array($default) ? $default : explode("\n", $default);
33 4
		$value =  $this->request->variable($name, $default, true);
34
35 4
		if (empty($value) && $this->request->server('REQUEST_METHOD') !== 'POST')
36 4
		{
37 4
			$value = $default;
38 4
		}
39
40 4
		return $value;
41
	}
42
43
	/**
44
	 * @inheritdoc
45
	 */
46 4
	public function display_field(array $data)
47
	{
48 4
		$field_value = array_filter(explode('<br>', $data['field_value']));
49 4
		return sizeof($field_value) ? join(', ', $field_value) : '';
50
	}
51
52
	/**
53
	 * @inheritdoc
54
	 */
55 4
	public function show_form_field($name, array &$data)
56
	{
57 4
		$field = $this->get_name();
58 4
		$selected = $this->get_selected_options($name, $data);
59
60 4
		$data['field_name'] = $name;
61 4
		$data['field_value'] = join("\n", $selected);
62
63 4
		if (isset($data['field_props']['per_col']) && $data['field_props']['per_col'] < 1)
64 4
		{
65
			$data['field_props']['per_col'] = 1;
66
		}
67
68 4
		$this->set_field_options($name, $data, $selected);
69 4
		$this->ptemplate->assign_vars($data);
70
71 4
		return $this->ptemplate->render_view('blitze/content', "fields/$field.twig", $field . '_field');
72
	}
73
74
	/**
75
	 * @param $name
76
	 * @param array $data
77
	 * @param array $selected
78
	 */
79 4
	protected function set_field_options($name, array &$data, array $selected)
80
	{
81 4
		if ($data['field_type'] === 'radio' || $data['field_type'] === 'checkbox')
82 4
		{
83 4
			$data['field_id'] .= '-0';
84 4
		}
85
86 4
		$count = 0;
87 4
		$options = array();
88 4
		if (is_array($data['field_props']['options']))
89 4
		{
90 4
			foreach ($data['field_props']['options'] as $option)
91
			{
92 4
				$options[] = array(
93 4
					'id'		=> 'smc-'. $name . '-' . $count,
94 4
					'label'		=> $this->language->lang($option),
95 4
					'selected'	=> (in_array($option, $selected, true)) ? true : false,
96 4
					'value'		=> $option,
97
				);
98 4
				$count++;
99 4
			}
100 4
		}
101 4
		$data['field_props']['options'] = $options;
102 4
	}
103
104
	/**
105
	 * @param string $name
106
	 * @param array $data
107
	 * @return array
108
	 */
109 4
	protected function get_selected_options($name, array $data)
110
	{
111 4
		$selected = $this->get_field_value($name, ($data['field_value']) ? $data['field_value'] : $data['field_props']['defaults']);
112 4
		return (is_array($selected)) ? $selected : array($selected);
113
	}
114
}
115