forked from Furgas/php-api-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyCommentBase.php
552 lines (487 loc) · 13 KB
/
kyCommentBase.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
/**
* Base class for comment objects.
* Known issues:
* - creatorid is not set when logged user posts a comment from client area
* - when creating User comment through API, exception is thrown by Kayako (NewsItem and TroubleshooterStep)
*
* @author Tomasz Sawicki (https://github.com/Furgas)
* @since Kayako version 4.51.1891
* @package Object\Base
*
* @noinspection PhpDocSignatureInspection
*/
abstract class kyCommentBase extends kyObjectBase {
/**
* Comment creator type - Staff.
* @var int
*/
const CREATOR_TYPE_STAFF = 1;
/**
* Comment creator type - User.
* @var int
*/
const CREATOR_TYPE_USER = 2;
/**
* Comment status - Pending approval.
* @var int
*/
const STATUS_PENDING = 1;
/**
* Comment status - Approved.
* @var int
*/
const STATUS_APPROVED = 2;
/**
* Comment status - Marked as spam.
* @var int
*/
const STATUS_SPAM = 3;
/**
* Comment identifier.
* @apiField
* @var int
*/
protected $id;
/**
* Type of this comment creator.
* @apiField required_create=true
* @var int
*/
protected $creator_type;
/**
* Identifier of this comment creator.
* @apiField
* @var int
*/
protected $creator_id;
/**
* Staff creator of this comment (if applicable).
* @var kyStaff
*/
protected $creator_staff;
/**
* User creator of this comment (if applicable).
* @var kyUser
*/
protected $creator_user;
/**
* Fullname of this comment creator.
* @apiField name=fullname
* @var string
*/
protected $creator_fullname;
/**
* E-mail of this comment creator.
* @apiField name=email
* @var string
*/
protected $creator_email;
/**
* IP address of machine where this comment originated from.
* @apiField
* @var string
*/
protected $ip_address;
/**
* Timestamp of when the comment was created.
* @apiField
* @var int
*/
protected $dateline;
/**
* Identifier of parent comment (comment that this comment is a reply to).
* @apiField
* @var int
*/
protected $parent_comment_id;
/**
* Parent comment (comment that this comment is a reply to).
* @var kyCommentBase
*/
protected $parent_comment;
/**
* Status of this comment.
* @apiField name=commentstatus
* @var int
*/
protected $status;
/**
* Information about browser on which this comment was created.
* @apiField
* @var string
*/
protected $user_agent;
/**
* URL of page on which this comment was created.
* @apiField
* @var string
*/
protected $referrer;
/**
* URL of commented item.
* @apiField
* @var string
*/
protected $parent_url;
/**
* Contents of this comment.
* @apiField required_create=true
* @var string
*/
protected $contents;
protected function parseData($data) {
$this->id = ky_assure_positive_int($data['id']);
$this->creator_type = ky_assure_positive_int($data['creatortype']);
$this->creator_id = ky_assure_positive_int($data['creatorid']);
$this->creator_fullname = ky_assure_string($data['fullname']);
$this->creator_email = ky_assure_string($data['email']);
$this->ip_address = ky_assure_string($data['ipaddress']);
$this->dateline = ky_assure_positive_int($data['dateline']);
$this->parent_comment_id = ky_assure_positive_int($data['parentcommentid']);
$this->status = ky_assure_positive_int($data['commentstatus']);
$this->user_agent = ky_assure_string($data['useragent']);
$this->referrer = ky_assure_string($data['referrer']);
$this->parent_url = ky_assure_string($data['parenturl']);
$this->contents = ky_assure_string($data['contents']);
}
public function buildData($create) {
$this->checkRequiredAPIFields($create);
$data = array();
if ($this->creator_type === self::CREATOR_TYPE_STAFF) {
if ($this->creator_id == null) {
throw new kyException("Value for API field 'creatorid' is required for this operation to complete.");
}
$this->buildDataNumeric($data, 'creatorid', $this->creator_id);
} elseif ($this->creator_type === self::CREATOR_TYPE_USER) {
if ($this->creator_id == null && $this->creator_fullname == null) {
throw new kyException("Value for API fields 'creatorid' or 'fullname' is required for this operation to complete.");
}
if ($this->creator_id != null) {
$this->buildDataNumeric($data, 'creatorid', $this->creator_id);
} else {
$this->buildDataString($data, 'fullname', $this->creator_fullname);
}
}
$this->buildDataString($data, 'contents', $this->contents);
$this->buildDataNumeric($data, 'creatortype', $this->creator_type);
$this->buildDataString($data, 'email', $this->creator_email);
$this->buildDataNumeric($data, 'parentcommentid', $this->parent_comment_id);
return $data;
}
public function toString() {
return sprintf("%s%s (author: %s, status: %d)", strtr(substr($this->getContents(), 0, 20), "\n", " "), strlen($this->getContents()) > 20 ? "..." : "", $this->getCreatorFullname(), $this->getStatus());
}
public function getId($complete = false) {
return $complete ? array($this->id) : $this->id;
}
/**
* Returns creator type of the comment.
*
* @see kyCommentBase::CREATOR_TYPE constants.
*
* @return int
* @filterBy
* @orderBy
*/
public function getCreatorType() {
return $this->creator_type;
}
/**
* Sets creator type of the comment.
*
* Automatically resets creator fullname to null when set to Staff.
*
* @see kyCommentBase::CREATOR_TYPE constants.
*
* @param int $creator_type Creator type of the comment.
* @return $this
*/
public function setCreatorType($creator_type) {
$this->creator_type = ky_assure_constant($creator_type, $this, 'CREATOR_TYPE');
switch ($this->creator_type) {
case self::CREATOR_TYPE_STAFF:
$this->creator_fullname = null;
$this->creator_user = null;
break;
case self::CREATOR_TYPE_USER:
$this->creator_staff = null;
break;
}
return $this;
}
/**
* Returns identifier of creator of this comment.
*
* @return int
* @filterBy
* @orderBy
*/
public function getCreatorId() {
return $this->creator_id;
}
/**
* Sets identifier of creator of this comment.
*
* Automatically resets creator fullname to null.
*
* @param int $creator_id Identifier of creator of this comment.
* @return $this
*/
public function setCreatorId($creator_id) {
$this->creator_id = ky_assure_positive_int($creator_id);
$this->creator_fullname = null;
return $this;
}
/**
* Returns the creator of this comment.
*
* 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 kyUser|kyStaff
* @filterBy
*/
public function getCreator($reload = false) {
switch ($this->creator_type) {
case self::CREATOR_TYPE_STAFF:
if ($this->creator_staff !== null && !$reload)
return $this->creator_staff;
if ($this->creator_id === null)
return null;
$this->creator_staff = kyStaff::get($this->creator_id);
return $this->creator_staff;
case self::CREATOR_TYPE_USER:
if ($this->creator_user !== null && !$reload)
return $this->creator_user;
if ($this->creator_id === null)
return null;
$this->creator_user = kyUser::get($this->creator_id);
return $this->creator_user;
}
return null;
}
/**
* Sets the creator of this comment.
*
* @param kyUser|kyStaff|string $creator Creator (staff object, user object or user fullname) of this comment.
* @return $this
*/
public function setCreator($creator) {
if ($creator instanceof kyStaff) {
$this->creator_staff = $creator;
$this->creator_id = $this->creator_staff->getId();
$this->creator_type = self::CREATOR_TYPE_STAFF;
$this->creator_user = null;
} elseif ($creator instanceof kyUser) {
$this->creator_user = $creator;
$this->creator_id = $this->creator_user->getId();
$this->creator_type = self::CREATOR_TYPE_USER;
$this->creator_staff = null;
} elseif (is_string($creator) && strlen($creator) > 0) {
$this->setCreatorFullname($creator);
} else {
$this->creator_id = null;
$this->creator_type = null;
$this->creator_staff = null;
$this->creator_user = null;
}
return $this;
}
/**
* Returns fullname of creator of this comment.
*
* @return string
* @filterBy
* @orderBy
*/
public function getCreatorFullname() {
return $this->creator_fullname;
}
/**
* Sets fullname of creator of this comment.
*
* Automatically changes creator type to User and resets creator identifiers to null.
*
* @param string $fullname Fullname of creator of this comment.
* @return $this
*/
public function setCreatorFullname($fullname) {
$this->creator_fullname = $fullname;
$this->creator_type = self::CREATOR_TYPE_USER;
$this->creator_id = null;
$this->creator_user = null;
$this->creator_staff = null;
return $this;
}
/**
* Returns email of creator of this comment.
*
* @return string
* @filterBy
* @orderBy
*/
public function getCreatorEmail() {
return $this->creator_email;
}
/**
* Sets email of creator of this comment.
*
* @param string $email Email of creator of this comment.
* @return $this
*/
public function setCreatorEmail($email) {
$this->creator_email = ky_assure_string($email);
return $this;
}
/**
* Returns IP address of machine where this comment originated from.
*
* @return string
* @filterBy
* @orderBy
*/
public function getIPAddress() {
return $this->ip_address;
}
/**
* Returns date and time when the comment 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);
}
/**
* Returns identifier of parent comment (comment that this comment is a reply to).
*
* @return int
* @filterBy
* @orderBy
*/
public function getParentCommentId() {
return $this->parent_comment_id;
}
/**
* Sets identifier of parent comment (comment that this comment is a reply to).
*
* @param int $parent_comment_id Identifier of parent comment (comment that this comment is a reply to).
* @return $this
*/
public function setParentCommentId($parent_comment_id) {
$this->parent_comment_id = ky_assure_positive_int($parent_comment_id);
$this->parent_comment = null;
return $this;
}
/**
* Returns parent comment (comment that this comment is a reply to).
*
* 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 kyCommentBase
* @filterBy
* @orderBy
*/
public function getParentComment($reload = false) {
if ($this->parent_comment !== null && !$reload)
return $this->parent_comment;
if ($this->parent_comment_id === null)
return null;
$this->parent_comment = kyNewsComment::get($this->parent_comment_id);
return $this->parent_comment;
}
/**
* Sets the parent comment (comment that this comment is a reply to).
*
* @param kyCommentBase $parent_comment Parent comment (comment that this comment is a reply to).
* @return $this
*/
public function setParentComment($parent_comment) {
$this->parent_comment = ky_assure_object($parent_comment, get_class($this));
$this->parent_comment_id = $this->parent_comment !== null ? $this->parent_comment->getId() : null;
return $this;
}
/**
* Returns status of this comment.
*
* @see kyCommentBase::STATUS constants.
*
* @return int
* @filterBy
* @orderBy
*/
public function getStatus() {
return $this->status;
}
/**
* Returns information about browser on which this comment was created.
*
* @return string
* @filterBy
*/
public function getUserAgent() {
return $this->user_agent;
}
/**
* Returns URL of page on which this comment was created.
*
* @return string
* @filterBy
*/
public function getReferrer() {
return $this->referrer;
}
/**
* Returns URL of commented item.
*
* @return string
*/
public function getParentURL() {
return $this->parent_url;
}
/**
* Returns contents of this comment.
*
* @return int
* @filterBy
*/
public function getContents() {
return $this->contents;
}
/**
* Sets contents of this comment.
*
* @param string $contents Contents of this comment.
* @return $this
*/
public function setContents($contents) {
$this->contents = ky_assure_string($contents);
return $this;
}
/**
* Creates a new comment.
* WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
*
* @param kyUser|kyStaff|string $creator Creator (staff object, user object or user fullname) of this comment.
* @param string $contents Contents of this comment.
* @return static
*/
static public function createNew($creator, $contents) {
/** @var $new_comment kyCommentBase */
$new_comment = new static();
$new_comment->setCreator($creator);
$new_comment->setContents($contents);
return $new_comment;
}
}