-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkyCustomFieldMultiSelect.php
171 lines (150 loc) · 3.62 KB
/
kyCustomFieldMultiSelect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Class for select custom field with multiple options.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @since Kayako version 4.40.1079
* @package Object\CustomField
*
* @noinspection PhpDocSignatureInspection
*/
class kyCustomFieldMultiSelect extends kyCustomField
{
/**
* Separator of field selected values.
* @var string
*/
const VALUES_SEPARATOR = ', ';
/**
* List of selected field options.
* @var kyCustomFieldOption[]
*/
protected $options;
protected function parseData($data)
{
parent::parseData($data);
$values = explode(self::VALUES_SEPARATOR, $data['_contents']);
$this->options = array();
foreach ($values as $value) {
$field_option = $this->getOption($value);
if ($field_option === null) {
continue;
}
$this->options[] = $field_option;
}
}
public function buildData($create)
{
$this->checkRequiredAPIFields($create);
$data = array();
foreach ($this->options as $key => $option) {
/* @var $option kyCustomFieldOption */
$data[sprintf('%s[%d]', $this->name, $key)] = $option->getId();
}
return $data;
}
/**
* Returns list of selected options of this custom field.
*
* @return kyResultSet
*/
public function getSelectedOptions()
{
/** @noinspection PhpParamsInspection */
return new kyResultSet($this->options);
}
/**
* Sets selected options of this custom field.
*
* @param kyCustomFieldOption[] $options List of options.
* @return kyCustomFieldMultiSelect
*/
public function setSelectedOptions($options)
{
//make sure it's array
if (!is_array($options)) {
if ($options === null) {
$options = array();
} else {
$options = array($options);
}
}
//check for proper class and eliminate duplicates
$options_ids = array();
$this->options = array();
foreach ($options as $option) {
$option = ky_assure_object($option, 'kyCustomFieldOption');
if ($option !== null && !in_array($option->getId(), $options_ids)) {
$this->options[] = $option;
$options_ids[] = $option->getId();
}
}
//update raw value
$option_values = array();
foreach ($this->options as $field_option) {
$option_values[] = $field_option->getValue();
}
$this->raw_value = implode(self::VALUES_SEPARATOR, $option_values);
}
/**
* Returns list of selected options of this custom field.
*
* @see kyCustomField::getValue()
* @see kyCustomFieldMultiSelect::getSelectedOptions()
*
* @return kyCustomFieldOption[]
*/
public function getValue()
{
return $this->options;
}
/**
* Returns selected options values as array:
* array(
* <field option id> => '<field option value>',
* ...
* )
*
* @return array
*/
public function getValues()
{
$values = array();
foreach ($this->options as $field_option) {
/* @var $field_option kyCustomFieldOption */
$values[$field_option->getId()] = $field_option->getValue();
}
return $values;
}
/**
* Sets selected options of this custom field.
*
* @param array $value List of values where each value can be: identifier of field option OR value of field option OR an option.
* @return kyCustomFieldMultiSelect
*/
public function setValue($value)
{
//make sure it's array
if (!is_array($value)) {
if ($value === null) {
$values = array();
} else {
$values = array($value);
}
} else {
$values = $value;
}
//build list of kyCustomFieldOption objects
$options = array();
foreach ($values as $value) {
$field_option = $this->getOption($value);
if ($field_option === null) {
continue;
}
$options[] = $field_option;
}
//set selected options
$this->setSelectedOptions($options);
return $this;
}
}