forked from AaronDDM/Kayako-REST-API-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkyTicketAttachment.php
261 lines (228 loc) · 6.97 KB
/
kyTicketAttachment.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
<?php
require_once('kyObjectBase.php');
/**
* Part of PHP client to REST API of Kayako v4 (Kayako Fusion).
* Compatible with Kayako version >= 4.01.204.
*
* Kayako TicketAttachment object.
*
* @author Tomasz Sawicki (https://github.com/Furgas)
*/
class kyTicketAttachment extends kyObjectBase {
static protected $controller = '/Tickets/TicketAttachment';
static protected $object_xml_name = 'attachment';
private $id = null;
private $ticket_id = null;
private $ticket_post_id = null;
private $file_name = null;
private $file_size = null;
private $file_type = null;
private $dateline = null;
private $contents = null;
protected function parseData($data) {
$this->id = intval($data['id']);
$this->ticket_id = intval($data['ticketid']);
$this->ticket_post_id = intval($data['ticketpostid']);
$this->file_name = $data['filename'];
$this->file_size = intval($data['filesize']);
$this->file_type = $data['filetype'];
$this->dateline = intval($data['dateline']) > 0 ? date(self::$datetime_format, $data['dateline']) : null;
if (array_key_exists('contents', $data) && strlen($data['contents']) > 0)
$this->contents = base64_decode($data['contents']);
}
protected function buildData($method) {
$data = array();
$data['ticketid'] = $this->ticket_id;
$data['ticketpostid'] = $this->ticket_post_id;
$data['filename'] = $this->file_name;
$data['contents'] = base64_encode($this->contents);
return $data;
}
/**
* Returns all attachments in posts of the ticket.
*
* @param int $ticket_id Ticket identifier.
* @return kyResultSet
*/
static public function getAll($ticket_id) {
$search_parameters = array('ListAll');
$search_parameters[] = $ticket_id;
return parent::getAll($search_parameters);
}
/**
* Returns ticket (post) attachment.
*
* @param int $ticket_id Ticket identifier.
* @param int $id Ticket attachement identifier.
* @return kyTicketAttachment
*/
static public function get($ticket_id, $id) {
return parent::get(array($ticket_id, $id));
}
public function update() {
throw new Exception("You can't update objects of type kyTicketAttachment.");
}
public function delete() {
static::_delete(array($this->ticket_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->ticket_id, $this->id) : $this->id;
}
/**
*
* @return int
*/
public function getTicketId() {
return $this->ticket_id;
}
/**
*
* @param int $ticket_id
* @return kyTicketAttachment
*/
public function setTicketId($ticket_id) {
$this->ticket_id = $ticket_id;
return $this;
}
/**
*
* @return int
*/
public function getTicketPostId() {
return $this->ticket_post_id;
}
/**
*
* @param int $ticket_post_id
* @return kyTicketAttachment
*/
public function setTicketPostId($ticket_post_id) {
$this->ticket_post_id = $ticket_post_id;
return $this;
}
/**
*
* @return string
* @filterBy()
* @orderBy()
*/
public function getFileName() {
return $this->file_name;
}
/**
*
* @param string $file_name
* @return kyTicketAttachment
*/
public function setFileName($file_name) {
$this->file_name = $file_name;
return $this;
}
/**
*
* @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);
else
return $this->file_size;
}
/**
*
* @return string
* @filterBy()
* @orderBy()
*/
public function getFileType() {
return $this->file_type;
}
/**
*
* @return string
* @filterBy()
* @orderBy()
*/
public function getDateline() {
return $this->dateline;
}
/**
* Return raw contents of the attachment (base64 decoded).
*
* @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->ticket_id) && $auto_fetch) {
$attachment = $this->get($this->ticket_id, $this->id);
$this->contents = $attachment->getContents(false);
}
return $this->contents;
}
/**
* Sets raw contents of the attachment (base64 decoded).
*
* @param string $contents Raw contents of the attachment (base64 decoded).
* @return kyTicketAttachment
*/
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.
* @return kyTicketAttachment
*/
public function setContentsFromFile($file_path, $file_name = null) {
$contents = file_get_contents($file_path);
if ($contents === false)
throw new Exception(sprintf("Error reading contents of %s.", $file_path));
$this->contents =& $contents;
if ($file_name === null)
$file_name = pathinfo($file_path, PATHINFO_BASENAME);
$this->file_name = $file_name;
return $this;
}
/**
* Creates new attachment for ticket post with contents provided as parameter.
* WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
*
* @param kyTicketPost $ticket_post Ticket post.
* @param string $contents Raw contents of the file.
* @param string $file_name Filename.
* @return kyTicketAttachment
*/
static public function createNew($ticket_post, $contents, $file_name) {
$new_ticket_attachment = new kyTicketAttachment();
$new_ticket_attachment->setTicketId($ticket_post->getTicketId());
$new_ticket_attachment->setTicketPostId($ticket_post->getId());
$new_ticket_attachment->setContents($contents);
$new_ticket_attachment->setFileName($file_name);
return $new_ticket_attachment;
}
/**
* Creates new attachment for ticket post with contents read from physical file.
* WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
*
* @param kyTicketPost $ticket_post Ticket post.
* @param string $file_path Path to file.
* @param string $file_name Optional. Use to set filename other than physical file.
* @return kyTicketAttachment
*/
static public function createNewFromFile($ticket_post, $file_path, $file_name = null) {
$new_ticket_attachment = new kyTicketAttachment();
$new_ticket_attachment->setTicketId($ticket_post->getTicketId());
$new_ticket_attachment->setTicketPostId($ticket_post->getId());
$new_ticket_attachment->setContentsFromFile($file_path, $file_name);
return $new_ticket_attachment;
}
}