- Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathkyObjectWithCustomFieldsBase.php
More file actions
Latest commit
236 lines (198 loc) · 6.96 KB
/
Copy pathkyObjectWithCustomFieldsBase.php
File metadata and controls
236 lines (198 loc) · 6.96 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
* Base class for Kayako object with custom fields.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @since Kayako version 4.40.1079
* @package Object\Base
*/
abstractclass kyObjectWithCustomFieldsBase extends kyObjectBase {
/**
* Name of class representing object custom field group.
* @var string
*/
staticprotected$custom_field_group_class = null;
/**
* Name of URL parameter for sending object identifier.
* @var string
*/
staticprotected$object_id_field = null;
/**
* For fast lookup of custom fields based on their name.
* @var array
*/
protected$custom_fields = null;
/**
* Object custom field groups.
* @var kyResultSet
*/
protected$custom_field_groups = null;
/**
* Adds updating custom fields to base object update.
*
* @see kyObjectBase::update()
*/
publicfunctionupdate() {
parent::update();
if (!$this->isNew()) {
$this->updateCustomFields();
}
}
/**
* Returns list of custom field groups for this object.
* Result is cached until the end of script.
*
* @param bool $reload True to reload data from server. False to use the cached value (if present).
* @throws BadMethodCallException
* @return kyResultSet
*/
protectedfunctionloadCustomFieldGroups($reload = false) {
if ($this->isNew())
thrownewBadMethodCallException("Custom fields are not available for new objects. Save the object before accessing itd custom fields.");
if ($this->custom_field_groups !== null && !$reload)
return$this->custom_field_groups;
$custom_field_group_class = static::$custom_field_group_class;
/** @noinspection PhpUndefinedMethodInspection */
$this->custom_field_groups = $custom_field_group_class::getAll($this->getId());
$this->initFields();
return$this->custom_field_groups;
}
/**
* Prepares local array for custom field fast lookup based on its name.
*/
privatefunctioninitFields() {
$this->custom_fields = array();
foreach ($this->custom_field_groupsas$custom_field_groups) {
/* @var $custom_field_groups kyCustomFieldGroupBase */
foreach ($custom_field_groups->getFields() as$field) {
/** @var $field kyCustomField */
$this->custom_fields[$field->getName()] = $field;
}
}
}
/**
* Returns list of custom fields.
* Result is cached until the end of script.
*
* @param bool $reload True to reload data from server. False to use the cached value (if present).
* @return kyResultSet
*/
publicfunctiongetCustomFields($reload = false) {
$this->loadCustomFieldGroups($reload);
returnnewkyResultSet(array_values($this->custom_fields));
}
/**
* Returns list of custom fields groups.
* Result is cached until the end of script.
*
* @param bool $reload True to reload data from server. False to use the cached value (if present).
* @return kyResultSet
*/
publicfunctiongetCustomFieldGroups($reload = false) {
$this->loadCustomFieldGroups($reload);
return$this->custom_field_groups;
}
/**
* Returns custom field based on its name.
*
* @param string $name Field name.
* @return kyCustomField
*/
publicfunctiongetCustomField($name) {
$this->loadCustomFieldGroups();
if (!array_key_exists($name, $this->custom_fields))
returnnull;
return$this->custom_fields[$name];
}
/**
* Returns value of this custom field.
* Value interpretation depends on field type.
*
* @param string $name Field name.
* @return mixed
*/
publicfunctiongetCustomFieldValue($name) {
$this->getCustomField($name)->getValue();
}
/**
* Sets custom field value.
*
* @param string $name Field name.
* @param mixed $value New field value.
* @return kyObjectWithCustomFieldsBase
*/
publicfunctionsetCustomFieldValue($name, $value) {
$this->loadCustomFieldGroups();
$custom_field = $this->getCustomField($name);
$custom_field_definition = $custom_field->getDefinition();
if (!$custom_field_definition->getIsUserEditable())
thrownewkyException(sprintf("usereditable flag is disabled for custom field %s.", $custom_field->getTitle()));
if ($custom_field_definition->getIsRequired() && empty($value)) {
thrownewkyException(sprintf("Field '%s' is required, cannot be empty", $custom_field->getTitle()));
}
$this->getCustomField($name)->setValue($value);
return$this;
}
/**
* Sets custom field values using POST data from current PHP request.
*
* @throws Exception
*/
publicfunctionsetCustomFieldValuesFromPOST() {
foreach ($this->getCustomFields() as$custom_field) {
/* @var $custom_field kyCustomField */
/** @var $custom_field_definition kyCustomFieldDefinition */
$custom_field_definition = $custom_field->getDefinition();
if (!$custom_field_definition->getIsUserEditable())
thrownewkyException(sprintf("usereditable flag is disabled for custom field %s.", $custom_field->getTitle()));
if ($custom_field_definition->getType() === kyCustomFieldDefinition::TYPE_FILE) {
/** @var $custom_field kyCustomFieldFile */
if (array_key_exists($custom_field->getName(), $_FILES) && $_FILES[$custom_field->getName()]['error'] != UPLOAD_ERR_NO_FILE) {
if ($_FILES[$custom_field->getName()]['error'] != UPLOAD_ERR_OK || !is_uploaded_file($_FILES[$custom_field->getName()]['tmp_name']))
thrownewkyException(sprintf("Error uploading file '%s'.", $custom_field->getTitle()));
$file_data = $_FILES[$custom_field->getName()];
$custom_field->setContentsFromFile($file_data['tmp_name'], $file_data['name']);
} else {
if ($custom_field_definition->getIsRequired())
thrownewkyException(sprintf("Field '%s' is required.", $custom_field->getTitle()));
}
} else {
$custom_field->setValue(ky_get_post_value($custom_field_definition));
}
}
}
/**
* Updates all custom fields values on Kayako server.
*
* @return kyObjectWithCustomFieldsBase
*/
publicfunctionupdateCustomFields() {
//ignore saving fields if they weren't even loaded
if ($this->custom_field_groups === null)
return$this;
$custom_field_group_class = static::$custom_field_group_class;
//collect all field values into request data
$data = array();
foreach ($this->getCustomFieldGroups() as$custom_field_group) {
/* @var $custom_field_group kyCustomFieldGroupBase */
$data = array_merge_recursive($data, $custom_field_group->buildData(true));
}
if (count($data) === 0)
return$this;
//prepare URL controller and parameters
$parameters = array(static::$object_id_field => $this->getId());
/** @noinspection PhpUndefinedMethodInspection */
$controller = $custom_field_group_class::getController();
//get files from data
$files = array();
if (array_key_exists(kyObjectBase::FILES_DATA_NAME, $data)) {
$files = $data[kyObjectBase::FILES_DATA_NAME];
unset($data[kyObjectBase::FILES_DATA_NAME]);
}
//send request
self::getRESTClient()->post($controller, $parameters, $data, $files);
//reload custom fields from server, there's no way (yet) to update objects state based on POST response
$this->loadCustomFieldGroups(true);
return$this;
}
}