|
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($name, $default) |
|
30
|
|
|
{ |
|
31
|
13 |
|
$default = is_array($default) ? $default : explode("\n", $default); |
|
32
|
13 |
|
$value = $this->request->variable($name, $default, true); |
|
33
|
|
|
|
|
34
|
13 |
|
if (empty($value) && $this->request->server('REQUEST_METHOD') !== 'POST') |
|
35
|
13 |
|
{ |
|
36
|
7 |
|
$value = $default; |
|
37
|
7 |
|
} |
|
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(', ', $field_value) : ''; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
13 |
|
public function show_form_field($name, array &$data) |
|
55
|
|
|
{ |
|
56
|
13 |
|
$selected = $this->get_selected_options($name, $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 = $this->get_options($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 $options |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
13 |
|
protected function get_options(array $options) |
|
104
|
|
|
{ |
|
105
|
13 |
|
return array_combine($options, $options); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $name |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
13 |
|
protected function get_selected_options($name, array $data) |
|
114
|
|
|
{ |
|
115
|
13 |
|
$selected = $this->get_field_value($name, ($data['field_value']) ? $data['field_value'] : $data['field_props']['defaults']); |
|
116
|
13 |
|
return (is_array($selected)) ? $selected : array($selected); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|