Completed
Push — develop ( 4a255f...c77227 )
by Daniel
07:47
created

choice::get_default_props()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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