-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkyTroubleshooterAttachment.php
327 lines (287 loc) · 8.28 KB
/
kyTroubleshooterAttachment.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Kayako TroubleshooterAttachment object.
*
* @author Saloni Dhall (https://github.com/SaloniDhall)
* @link http://wiki.kayako.com/display/DEV/REST+-+TroubleshooterCategory
* @since Kayako version 4.64.1
* @package Object\TroubleshooterCategory
*
*/
class kyTroubleshooterAttachment extends kyObjectBase
{
protected static $controller = '/Troubleshooter/Attachment';
protected static $object_xml_name = 'troubleshooterattachment';
/**
* TroubleshooterStep attachment identifier.
* @apiField
* @var int
*/
protected $id;
/**
* Identifier of TroubleshooterStep that this attachment is attached to.
* @apiField required_create=true
* @var int
*/
protected $troubleshooter_step_id;
/**
* Attachment file name.
* @apiField required_create=true
* @var string
*/
protected $file_name;
/**
* Attachment size in bytes.
* @apiField
* @var int
*/
protected $file_size;
/**
* Attachment MIME type.
* @apiField
* @var string
*/
protected $file_type;
/**
* Timestamp of when this attachment was created.
* @apiField
* @var int
*/
protected $dateline;
/**
* Raw contents of attachment.
* @apiField required_create=true
* @var string
*/
protected $contents;
protected function parseData($data)
{
$this->id = intval($data['id']);
$this->troubleshooter_step_id = ky_assure_positive_int($data['troubleshooterstepid']);
$this->file_name = $data['filename'];
$this->file_size = intval($data['filesize']);
$this->file_type = $data['filetype'];
$this->dateline = ky_assure_positive_int($data['dateline']);
if (array_key_exists('contents', $data) && strlen($data['contents']) > 0) {
$this->contents = base64_decode($data['contents']);
}
}
public function buildData($create)
{
$this->checkRequiredAPIFields($create);
$data = array();
$data['troubleshooterstepid'] = $this->troubleshooter_step_id;
$data['filename'] = $this->file_name;
$data['contents'] = $this->contents;
return $data;
}
/**
* Returns all attachments of the TroubleshooterStep
*
* @param int $troubelshooter_step_id TroubleshooterStep identifier.
* @return kyResultSet
*/
public static function getAll()
{
list($troubelshooter_step_id) = func_get_args();
$search_parameters = array('ListAll');
$search_parameters[] = $troubelshooter_step_id;
return parent::getAll($search_parameters);
}
/**
* Returns TroubleshooterStep attachment.
*
* @param int $troubelshooter_step_id TroubleshooterStep identifier.
* @param int $id TroubleshooterStep attachment identifier.
* @return kyTroubleshooterAttachment
*/
public static function get()
{
list($troubelshooter_step_id, $id) = func_get_args();
return parent::get(array($troubelshooter_step_id, $id));
}
public function update()
{
throw new BadMethodCallException("You can't update objects of type kyTroubleshooterAttachment.");
}
public function delete()
{
self::getRESTClient()->delete(static::$controller, array($this->troubleshooter_step_id, $this->id));
}
public function toString()
{
return sprintf("%s (filetype: %s, filesize: %s)", $this->getFileName(), $this->getFileType(), $this->getFileSize(true));
}
public function getId($complete = false)
{
return $complete ? array($this->troubleshooter_step_id, $this->id) : $this->id;
}
/**
* Returns identifier of the TroubleshooterStep this attachment belongs to.
*
* @return int
*/
public function getTroubleshooterStepId()
{
return $this->troubleshooter_step_id;
}
/**
* Sets identifier of the TroubleshooterStep this attachment will belong to.
*
* @param int $troubleshooter_step_id TroubleshooterStep identifier.
* @return kyTroubleshooterAttachment
*/
public function setTroubleshooterStepId($troubleshooter_step_id)
{
$this->troubleshooter_step_id = ky_assure_positive_int($troubleshooter_step_id);
return $this;
}
/**
* Returns attachment file name.
*
* @return string
* @filterBy
* @orderBy
*/
public function getFileName()
{
return $this->file_name;
}
/**
* Sets the attachment file name.
*
* @param string $file_name File name.
* @return kyTroubleshooterAttachment
*/
public function setFileName($file_name)
{
$this->file_name = ky_assure_string($file_name);
return $this;
}
/**
* Returns attachment file size.
*
* @param bool $formatted True to format result nicely (KB, MB, and so on).
* @return mixed
* @filterBy
* @orderBy
*/
public function getFileSize($formatted = false)
{
if ($formatted) {
return ky_bytes_format($this->file_size);
}
return $this->file_size;
}
/**
* Returns attachment MIME type.
*
* @return string
* @filterBy
* @orderBy
*/
public function getFileType()
{
return $this->file_type;
}
/**
* Returns date and time of when this attachment was created.
*
* @see http://www.php.net/manual/en/function.date.php
*
* @param string $format Output format of the date. If null the format set in client configuration is used.
* @return string
* @filterBy
* @orderBy
*/
public function getDateline($format = null)
{
if ($this->dateline == null) {
return null;
}
if ($format === null) {
$format = kyConfig::get()->getDatetimeFormat();
}
return date($format, $this->dateline);
}
/**
* Return raw contents of the attachment (NOT base64 encoded).
*
* @param bool $auto_fetch True to automatically fetch the contents of the attachment if not present.
* @return string
*/
public function &getContents($auto_fetch = true)
{
if ($this->contents === null && is_numeric($this->id) && is_numeric($this->troubleshooter_step_id) && $auto_fetch) {
$attachment = $this->get($this->troubleshooter_step_id, $this->id);
$this->contents = $attachment->getContents(false);
}
return $this->contents;
}
/**
* Sets raw contents of the attachment (NOT base64 encoded).
*
* @param string $contents Raw contents of the attachment (NOT base64 encoded).
* @return kyTroubleshooterAttachment
*/
public function setContents(&$contents)
{
$this->contents =& $contents;
return $this;
}
/**
* Sets contents of the attachment by reading it from a physical file.
*
* @param string $file_path Path to file.
* @param string $file_name Optional. Use to set filename other than physical file.
* @throws kyException
* @return kyTroubleshooterAttachment
*/
public function setContentsFromFile($file_path, $file_name = null)
{
$contents = base64_encode(file_get_contents($file_path));
if ($contents === false) {
throw new kyException(sprintf("Error reading contents of %s.", $file_path));
}
$this->contents =& $contents;
if ($file_name === null) {
$file_name = basename($file_path);
}
$this->file_name = $file_name;
return $this;
}
/**
* Creates new attachment for troubleshooterstep with contents provided as parameter.
* WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
*
* @param kyTroubleshooterStep $troubleshooter_step TroubleshooterStep.
* @param string $contents Raw contents of the file.
* @param string $file_name Filename.
* @return kyTroubleshooterAttachment
*/
public static function createNew()
{
list($troubleshooter_step, $contents, $file_name) = func_get_args();
$new_troubleshooter_attachment = new kyTroubleshooterAttachment();
$new_troubleshooter_attachment->setTroubleshooterStepId($troubleshooter_step->getId());
$new_troubleshooter_attachment->setContents($contents);
$new_troubleshooter_attachment->setFileName($file_name);
return $new_troubleshooter_attachment;
}
/**
* Creates new attachment for troubleshooterstep with contents read from physical file.
* WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
*
* @param kyTroubleshooterStep $troubleshooter_step troubleshooterstep.
* @param string $file_path Path to file.
* @param string $file_name Optional. Use to set filename other than physical file.
* @return kyTroubleshooterAttachment
*/
public static function createNewFromFile($troubleshooter_step, $file_path, $file_name = null)
{
$new_troubleshooter_attachment = new kyTroubleshooterAttachment();
$new_troubleshooter_attachment->setTroubleshooterStepId($troubleshooter_step->getId());
$new_troubleshooter_attachment->setContentsFromFile($file_path, $file_name);
return $new_troubleshooter_attachment;
}
}