Newer
Older
<?php
/*********************************************************************
class.ticket.php
The most important class! Don't play with fire please.
Peter Rotich <peter@osticket.com>
Copyright (c) 2006-2012 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
include_once(INCLUDE_DIR.'class.staff.php');
include_once(INCLUDE_DIR.'class.client.php');
include_once(INCLUDE_DIR.'class.team.php');
include_once(INCLUDE_DIR.'class.email.php');
include_once(INCLUDE_DIR.'class.dept.php');
include_once(INCLUDE_DIR.'class.topic.php');
include_once(INCLUDE_DIR.'class.lock.php');
include_once(INCLUDE_DIR.'class.file.php');
include_once(INCLUDE_DIR.'class.attachment.php');
include_once(INCLUDE_DIR.'class.banlist.php');
include_once(INCLUDE_DIR.'class.template.php');
include_once(INCLUDE_DIR.'class.priority.php');
class Ticket{
var $id;
var $extid;
var $email;
var $status;
var $created;
var $reopened;
var $updated;
var $lastrespdate;
var $lastmsgdate;
var $duedate;
var $priority;
var $priority_id;
var $fullname;
var $staff_id;
var $team_id;
var $dept_id;
var $topic_id;
var $dept_name;
var $subject;
var $helptopic;
var $overdue;
var $lastMsgId;
var $dept; //Dept obj
var $sla; // SLA obj
var $staff; //Staff obj
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
var $team; //Team obj
var $topic; //Topic obj
var $tlock; //TicketLock obj
function Ticket($id){
$this->id = 0;
$this->load($id);
}
function load($id=0) {
if(!$id && !($id=$this->getId()))
return false;
//TODO: delete helptopic field in ticket table.
$sql='SELECT ticket.*, topic.topic as helptopic, lock_id, dept_name, priority_desc '
.' ,count(attach.attach_id) as attachments '
.' ,count(DISTINCT message.msg_id) as messages '
.' ,count(DISTINCT response.response_id) as responses '
.' ,count(DISTINCT note.note_id) as notes '
.' FROM '.TICKET_TABLE.' ticket '
.' LEFT JOIN '.DEPT_TABLE.' dept ON (ticket.dept_id=dept.dept_id) '
.' LEFT JOIN '.TICKET_PRIORITY_TABLE.' pri ON (ticket.priority_id=pri.priority_id) '
.' LEFT JOIN '.TOPIC_TABLE.' topic ON (ticket.topic_id=topic.topic_id) '
.' LEFT JOIN '.TICKET_LOCK_TABLE.' tlock ON (ticket.ticket_id=tlock.ticket_id AND tlock.expire>NOW()) '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON (ticket.ticket_id=attach.ticket_id) '
.' LEFT JOIN '.TICKET_MESSAGE_TABLE.' message ON (ticket.ticket_id=message.ticket_id) '
.' LEFT JOIN '.TICKET_RESPONSE_TABLE.' response ON (ticket.ticket_id=response.ticket_id) '
.' LEFT JOIN '.TICKET_NOTE_TABLE.' note ON (ticket.ticket_id=note.ticket_id ) '
.' WHERE ticket.ticket_id='.db_input($id)
.' GROUP BY ticket.ticket_id';
//echo $sql;
if(!($res=db_query($sql)) || !db_num_rows($res))
return false;
$this->ht=db_fetch_array($res);
$this->id = $this->ht['ticket_id'];
$this->extid = $this->ht['ticketID'];
$this->email = $this->ht['email'];
$this->fullname = $this->ht['name'];
$this->status = $this->ht['status'];
$this->created = $this->ht['created'];
$this->reopened = $this->ht['reopened'];
$this->updated = $this->ht['updated'];
$this->duedate = $this->ht['duedate'];
$this->closed = $this->ht['closed'];
$this->lastmsgdate = $this->ht['lastmessagedate'];
$this->lastrespdate = $this->ht['lastresponsedate'];
$this->lock_id = $this->ht['lock_id'];
$this->priority_id = $this->ht['priority_id'];
$this->priority = $this->ht['priority_desc'];
$this->staff_id = $this->ht['staff_id'];
$this->team_id = $this->ht['team_id'];
$this->dept_id = $this->ht['dept_id'];
$this->dept_name = $this->ht['dept_name'];
$this->sla_id = $this->ht['sla_id'];
$this->topic_id = $this->ht['topic_id'];
$this->helptopic = $this->ht['helptopic'];
$this->subject = $this->ht['subject'];
$this->overdue = $this->ht['isoverdue'];
//Reset the sub classes (initiated ondemand)...good for reloads.
$this->staff = null;
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
$this->team = null;
$this->dept = null;
$this->sla = null;
$this->tlock = null;
$this->stats = null;
$this->topic = null;
return true;
}
function reload() {
return $this->load();
}
function isOpen() {
return (strcasecmp($this->getStatus(),'Open')==0);
}
function isReopened() {
return ($this->getReopenDate());
}
function isClosed() {
return (strcasecmp($this->getStatus(),'Closed')==0);
}
function isAssigned() {
return ($this->isOpen() && ($this->getStaffId() || $this->getTeamId()));
}
function isOverdue() {
return ($this->overdue);
}
function isAnswered() {
return ($this->ht['isanswered']);
}
function isLocked() {
return ($this->getLockId());
}
function checkStaffAccess($staff) {
if(!is_object($staff) && !($staff=Staff::lookup($staff)))
return false;
return ((!$staff->showAssignedOnly() && $staff->canAccessDept($this->getDeptId()))
|| ($this->getTeamId() && $staff->isTeamMember($this->getTeamId()))
|| $staff->getId()==$this->getStaffId());
}
function checkClientAccess($client) {
global $cfg;
if(!is_object($client) && !($client=Client::lookup($client)))
return false;
if(!strcasecmp($client->getEmail(),$this->getEmail()))
return true;
return ($cfg && $cfg->showRelatedTickets() && $client->getTicketId()==$ticket->getExtId());
}
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
//Getters
function getId(){
return $this->id;
}
function getExtId(){
return $this->extid;
}
function getEmail(){
return $this->email;
}
function getName(){
return $this->fullname;
}
function getSubject() {
return $this->subject;
}
/* Help topic title - NOT object -> $topic */
function getHelpTopic() {
if(!$this->helpTopic && ($topic=$this->getTopic()))
$this->helpTopic = $topic->getName();
return $this->helptopic;
}
function getCreateDate(){
return $this->created;
}
function getOpenDate() {
return $this->getCreateDate();
}
function getReopenDate() {
return $this->reopened;
}
function getUpdateDate(){
return $this->updated;
}
function getDueDate(){
return $this->duedate;
}
function getCloseDate(){
return $this->closed;
}
function getStatus(){
return $this->status;
}
function getDeptId(){
return $this->dept_id;
}
function getDeptName(){
return $this->dept_name;
}
function getPriorityId() {
return $this->priority_id;
}
function getPriority() {
return $this->priority;
}
function getPhone() {
return $this->ht['phone'];
}
function getPhoneExt() {
return $this->ht['phone_ext'];
}
function getPhoneNumber() {
$phone=Format::phone($this->getPhone());
if(($ext=$this->getPhoneExt()))
$phone.=" $ext";
return $phone;
}
function getSource() {
return $this->ht['source'];
}
function getIP() {
return $this->ht['ip_address'];
}
function getHashtable() {
return $this->ht;
}
function getUpdateInfo() {
$info=array('name' => $this->getName(),
'email' => $this->getEmail(),
'phone' => $this->getPhone(),
'phone_ext' => $this->getPhoneExt(),
'subject' => $this->getSubject(),
'source' => $this->getSource(),
'topicId' => $this->getTopicId(),
'priorityId' => $this->getPriorityId(),
'slaId' => $this->getSLAId(),
'duedate' => $this->getDueDate()?(Format::userdate('m/d/Y', Misc::db2gmtime($this->getDueDate()))):'',
'time' => $this->getDueDate()?(Format::userdate('G:i', Misc::db2gmtime($this->getDueDate()))):'',
);
return $info;
}
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
function getLockId() {
return $this->lock_id;
}
function getLock(){
if(!$this->tlock && $this->getLockId())
$this->tlock= TicketLock::lookup($this->getLockId(),$this->getId());
return $this->tlock;
}
function acquireLock($staffId, $lockTime) {
if(!$staffId or !$lockTime) //Lockig disabled?
return null;
//Check if the ticket is already locked.
if(($lock=$this->getLock()) && !$lock->isExpired()) {
if($lock->getStaffId()!=$staffId) //someone else locked the ticket.
return null;
//Lock already exits...renew it
$lock->renew($lockTime); //New clock baby.
return $lock;
}
//No lock on the ticket or it is expired
$this->tlock=null; //clear crap
$this->lock_id=TicketLock::acquire($this->getId(), $staffId, $lockTime); //Create a new lock..
//load and return the newly created lock if any!
return $this->getLock();
}
function getDept(){
if(!$this->dept && $this->getDeptId())
$this->dept= Dept::lookup($this->getDeptId());
return $this->dept;
}
function getClient() {
if(!$this->client)
$this->client = Client::lookup($this->getExtId(), $this->getEmail());
return $this->client;
}
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
function getStaffId(){
return $this->staff_id;
}
function getStaff(){
if(!$this->staff && $this->getStaffId())
$this->staff= Staff::lookup($this->getStaffId());
return $this->staff;
}
function getTeamId(){
return $this->team_id;
}
function getTeam(){
if(!$this->team && $this->getTeamId())
$this->team = Team::lookup($this->getTeamId());
return $this->team;
}
function getAssignee() {
if($staff=$this->getStaff())
return $staff->getName();
if($team=$this->getTeam())
return $team->getName();
return '';
}
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
function getTopicId(){
return $this->topic_id;
}
function getTopic() {
if(!$this->topic && $this->getTopicId())
$this->topic = Topic::lookup($this->getTopicId);
return $this->topic;
}
function getSLAId() {
return $this->sla_id;
}
function getSLA() {
if(!$this->sla && $this->getSLAId())
$this->sla = SLA::lookup($this->getSLAId);
return $this->sla;
}
function getLastRespondent() {
$sql ='SELECT resp.staff_id '
.' FROM '.TICKET_RESPONSE_TABLE.' resp '
.' LEFT JOIN '.STAFF_TABLE. ' USING(staff_id) '
.' WHERE resp.ticket_id='.db_input($this->getId()).' AND resp.staff_id>0 '
.' ORDER BY resp.created DESC LIMIT 1';
if(!($res=db_query($sql)) || !db_num_rows($res))
return null;
list($id)=db_fetch_row($res);
return Staff::lookup($id);
}
function getLastMessageDate() {
if($this->lastmsgdate)
return $this->lastmsgdate;
//for old versions...XXX: still needed????
$sql='SELECT created FROM '.TICKET_MESSAGE_TABLE
.' WHERE ticket_id='.db_input($this->getId())
.' ORDER BY created DESC LIMIT 1';
if(($res=db_query($sql)) && db_num_rows($res))
list($this->lastmsgdate)=db_fetch_row($res);
return $this->lastmsgdate;
}
function getLastMsgDate() {
return $this->getLastMessageDate();
}
function getLastResponseDate() {
if($this->lastrespdate)
return $this->lastrespdate;
$sql='SELECT created FROM '.TICKET_RESPONSE_TABLE
.' WHERE ticket_id='.db_input($this->getId())
.' ORDER BY created DESC LIMIT 1';
if(($res=db_query($sql)) && db_num_rows($res))
list($this->lastrespdate)=db_fetch_row($res);
return $this->lastrespdate;
}
function getLastRespDate() {
return $this->getLastResponseDate();
}
function getLastMsgId() {
return $this->lastMsgId;
}
function getRelatedTicketsCount(){
$sql='SELECT count(*) FROM '.TICKET_TABLE
.' WHERE email='.db_input($this->getEmail());
return db_result(db_query($sql));
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
}
function getThreadCount() {
return $this->getNumMessages() + $this->getNumResponses();
}
function getNumMessages() {
return $this->ht['messages'];
}
function getNumResponses() {
return $this->ht['responses'];
}
function getNumNotes() {
return $this->ht['notes'];
}
function getNotes($order='') {
if(!$order || !in_array($order, array('DESC','ASC')))
$order='DESC';
$sql ='SELECT note.*, count(DISTINCT attach.attach_id) as attachments '
.' FROM '.TICKET_NOTE_TABLE.' note '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
ON (note.ticket_id=attach.ticket_id AND note.note_id=attach.ref_id AND ref_type="N") '
.' WHERE note.ticket_id='.db_input($this->getId())
.' GROUP BY note.note_id '
.' ORDER BY note.created '.$order;
$notes=array();
if(($res=db_query($sql)) && db_num_rows($res))
while($rec=db_fetch_array($res))
$notes[]=$rec;
return $notes;
}
function getMessages() {
$sql='SELECT msg.msg_id, msg.created, msg.message '
.' ,count(DISTINCT attach.attach_id) as attachments, count( DISTINCT resp.response_id) as responses '
.' FROM '.TICKET_MESSAGE_TABLE.' msg '
.' LEFT JOIN '.TICKET_RESPONSE_TABLE. ' resp ON(resp.msg_id=msg.msg_id) '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
ON (msg.ticket_id=attach.ticket_id AND msg.msg_id=attach.ref_id AND ref_type="M") '
.' WHERE msg.ticket_id='.db_input($this->getId())
.' GROUP BY msg.msg_id '
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
$messages=array();
if(($res=db_query($sql)) && db_num_rows($res))
while($rec=db_fetch_array($res))
$messages[] = $rec;
return $messages;
}
function getResponses($msgId) {
$sql='SELECT resp.*, count(DISTINCT attach.attach_id) as attachments '
.' FROM '.TICKET_RESPONSE_TABLE. ' resp '
.' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach
ON (resp.ticket_id=attach.ticket_id AND resp.response_id=attach.ref_id AND ref_type="R") '
.' WHERE resp.ticket_id='.db_input($this->getId())
.' GROUP BY resp.response_id '
.' ORDER BY resp.created';
$responses=array();
if(($res=db_query($sql)) && db_num_rows($res))
while($rec= db_fetch_array($res))
$responses[] = $rec;
return $responses;
}
function getAttachments($refId=0, $type=null) {
if($refId && !$type)
return NULL;
//XXX: inner join the file table instead?
$sql='SELECT a.attach_id, f.id as file_id, f.size, f.hash as file_hash, f.name '
.' FROM '.FILE_TABLE.' f '
.' INNER JOIN '.TICKET_ATTACHMENT_TABLE.' a ON(f.id=a.file_id) '
.' WHERE a.ticket_id='.db_input($this->getId());
if($refId)
$sql.=' AND a.ref_id='.db_input($refId);
if($type)
$sql.=' AND a.ref_type='.db_input($type);
$attachments = array();
if(($res=db_query($sql)) && db_num_rows($res)) {
while($rec=db_fetch_array($res))
$attachments[] = $rec;
}
return $attachments;
}
function getAttachmentsLinks($refId, $type, $separator=' ',$target='') {
$str='';
foreach($this->getAttachments($refId, $type) as $attachment ) {
/* The has here can be changed but must match validation in attachment.php */
$hash=md5($attachment['file_id'].session_id().$attachment['file_hash']);
if($attachment['size'])
$size=sprintf('(<i>%s</i>)',Format::file_size($attachment['size']));
$str.=sprintf('<a class="Icon file" href="attachment.php?id=%d&h=%s" target="%s">%s</a>%s %s',
$attachment['attach_id'], $hash, $target, Format::htmlchars($attachment['name']), $size, $separator);
}
return $str;
}
/* -------------------- Setters --------------------- */
function setLastMsgId($msgid) {
return $this->lastMsgId=$msgid;
}
function setPriority($priorityId) {
//XXX: what happens to SLA priority???
if(!$priorityId || $priorityId==$this->getPriorityId())
return ($priorityId);
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW() '
.', priority_id='.db_input($priorityId)
.' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows($res));
}
//DeptId can NOT be 0. No orphans please!
function setDeptId($deptId){
//Make sure it's a valid department//
if(!($dept=Dept::lookup($deptId)))
return false;
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW(), dept_id='.db_input($deptId)
.' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows());
}
//Set staff ID...assign/unassign/release (id can be 0)
function setStaffId($staffId){
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW(), staff_id='.db_input($staffId)
.' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows());
}
function setSLAId($slaId) {
if ($slaId == $this->getSLAId()) return true;
return db_query(
'UPDATE '.TICKET_TABLE.' SET sla_id='.db_input($slaId)
.' WHERE ticket_id='.db_input($this->getId()))
&& db_affected_rows();
}
/**
* Selects the appropriate service-level-agreement plan for this ticket.
* When tickets are transfered between departments, the SLA of the new
* department should be applied to the ticket. This would be usefule,
* for instance, if the ticket is transferred to a different department
* which has a shorter grace period, the ticket should be considered
* overdue in the shorter window now that it is owned by the new
* department.
*
* $trump - if received, should trump any other possible SLA source.
* This is used in the case of email filters, where the SLA
* specified in the filter should trump any other SLA to be
* considered.
*/
function selectSLAId($trump=null) {
global $cfg;
# XXX Should the SLA be overwritten if it was originally set via an
# email filter? This method doesn't consider such a case
if ($trump !== null) {
$slaId = $trump;
} elseif ($this->getDept()->getSLAId()) {
$slaId = $this->getDept()->getSLAId();
} elseif ($this->getTopicId() && $this->getTopic()) {
$slaId = $this->getTopic()->getSLAId();
} else {
$slaId = $cfg->getDefaultSLAId();
}
return ($slaId && $this->setSLAId($slaId)) ? $slaId : false;
}
//Set team ID...assign/unassign/release (id can be 0)
function setTeamId($teamId){
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW(), team_id='.db_input($teamId)
.' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows());
}
//Status helper.
function setStatus($status) {
if(strcasecmp($this->getStatus(),$status)==0)
return true; //No changes needed.
switch(strtolower($status)) {
case 'open':
return $this->reopen();
break;
case 'closed':
return $this->close();
break;
}
return false;
}
function setState($state, $alerts=false) {
switch(strtolower($state)) {
case 'open':
return $this->setStatus('open');
break;
case 'closed':
return $this->setStatus('closed');
break;
case 'answered':
return $this->setAnsweredState(1);
break;
case 'unanswered':
return $this->setAnsweredState(0);
break;
case 'overdue':
return $this->markOverdue();
break;
}
return false;
}
function setAnsweredState($isanswered) {
$sql='UPDATE '.TICKET_TABLE.' SET isanswered='.db_input($isanswered)
.' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows());
}
//Close the ticket
function close(){
global $thisstaff;
$sql='UPDATE '.TICKET_TABLE.' SET closed=NOW(), isoverdue=0, duedate=NULL, updated=NOW(), status='.db_input('closed');
if($thisstaff) //Give the closing staff credit.
$sql.=', staff_id='.db_input($thisstaff->getId());
$sql.=' WHERE ticket_id='.db_input($this->getId());
return (db_query($sql) && db_affected_rows());
}
//set status to open on a closed ticket.
function reopen($isanswered=0){
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW(), reopened=NOW() '
.' ,status='.db_input('open')
.' ,isanswered='.db_input($isanswered)
.' WHERE ticket_id='.db_input($this->getId());
//TODO: log reopen event here
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
return (db_query($sql) && db_affected_rows());
}
function onNewTicket($message, $autorespond=true, $alertstaff=true) {
global $cfg;
//Log stuff here...
if(!$autorespond && !$alertstaff) return true; //No alerts to send.
/* ------ SEND OUT NEW TICKET AUTORESP && ALERTS ----------*/
$this->reload(); //get the new goodies.
$dept= $this->getDept();
if(!$dept || !($tpl = $dept->getTemplate()))
$tpl= $cfg->getDefaultTemplate();
if(!$tpl) return false; //bail out...missing stuff.
if(!$dept || !($email=$dept->getAutoRespEmail()))
$email =$cfg->getDefaultEmail();
//Send auto response - if enabled.
if($autorespond && $email && $cfg->autoRespONNewTicket()
&& $dept->autoRespONNewTicket()
&& ($msg=$tpl->getAutoRespMsgTemplate())) {
$body=$this->replaceTemplateVars($msg['body']);
$subj=$this->replaceTemplateVars($msg['subj']);
$body = str_replace('%message', $message, $body);
$body = str_replace('%signature',($dept && $dept->isPublic())?$dept->getSignature():'',$body);
if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
$body ="\n$tag\n\n".$body;
//TODO: add auto flags....be nice to mail servers and sysadmins!!
$email->send($this->getEmail(),$subj,$body);
}
if(!($email=$cfg->getAlertEmail()))
$email =$cfg->getDefaultEmail();
//Send alert to out sleepy & idle staff.
if($alertstaff && $email
&& $cfg->alertONNewTicket()
&& ($msg=$tpl->getNewTicketAlertMsgTemplate())) {
$body=$this->replaceTemplateVars($msg['body']);
$subj=$this->replaceTemplateVars($msg['subj']);
$body = str_replace('%message', $message, $body);
$recipients=$sentlist=array();
//Alert admin??
if($cfg->alertAdminONNewTicket()) {
$alert = str_replace("%staff",'Admin',$body);
$email->send($cfg->getAdminEmail(),$subj,$alert);
$sentlist[]=$cfg->getAdminEmail();
}
//Only alerts dept members if the ticket is NOT assigned.
if($cfg->alertDeptMembersONNewTicket() && !$this->isAssigned()) {
if(($members=$dept->getAvailableMembers()))
$recipients=array_merge($recipients, $members);
}
if($cfg->alertDeptManagerONNewTicket() && $dept && ($manager=$dept->getManager()))
$recipients[]= $manager;
foreach( $recipients as $k=>$staff){
if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(),$sentlist)) continue;
$alert = str_replace("%staff",$staff->getFirstName(),$body);
$email->send($staff->getEmail(),$subj,$alert);
}
}
return true;
}
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
function onOpenLimit($sendNotice=true) {
global $cfg;
//Log the limit notice as a warning for admin.
$msg=sprintf('Max open tickets (%d) reached for %s ', $cfg->getMaxOpenTickets(), $this->getEmail());
sys::log(LOG_WARNING, 'Max. Open Tickets Limit ('.$this->getEmail().')', $msg);
if(!$sendNotice || !$cfg->sendOverlimitNotice()) return true;
//Send notice to user.
$dept = $this->getDept();
if(!$dept || !($tpl=$dept->getTemplate()))
$tpl=$cfg->getDefaultTemplate();
if(!$dept || !($email=$dept->getAutoRespEmail()))
$email=$cfg->getDefaultEmail();
if($tpl && ($msg=$tpl->getOverlimitMsgTemplate()) && $email) {
$body=$this->replaceTemplateVars($msg['body']);
$subj=$this->replaceTemplateVars($msg['subj']);
$body = str_replace('%signature',($dept && $dept->isPublic())?$dept->getSignature():'',$body);
$email->send($this->getEmail(), $subj, $body);
}
$client= $this->getClient();
//Alert admin...this might be spammy (no option to disable)...but it is helpful..I think.
$msg='Max. open tickets reached for '.$this->getEmail()."\n"
.'Open ticket: '.$client->getNumOpenTickets()."\n"
.'Max Allowed: '.$cfg->getMaxOpenTickets()."\n\nNotice sent to the user.";
Sys::alertAdmin('Overlimit Notice',$msg);
return true;
}
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function onResponse(){
db_query('UPDATE '.TICKET_TABLE.' SET isanswered=1,lastresponse=NOW(), updated=NOW() WHERE ticket_id='.db_input($this->getId()));
}
function onMessage($autorespond=true, $alert=true){
global $cfg;
db_query('UPDATE '.TICKET_TABLE.' SET isanswered=0,lastmessage=NOW() WHERE ticket_id='.db_input($this->getId()));
//auto-assign to closing staff or last respondent
if(!($staff=$this->getStaff()) || !$staff->isAvailable()) {
if($cfg->autoAssignReopenedTickets() && ($lastrep=$this->getLastRespondent()) && $lastrep->isAvailable()) {
$this->setStaffId($lastrep->getId()); //direct assignment;
} else {
$this->setStaffId(0); //unassign - last respondent is not available.
}
}
if($this->isClosed()) $this->reopen(); //reopen..
/********** double check auto-response ************/
if($autorespond && (Email::getIdByEmail($this->getEmail())))
$autorespond=false;
elseif($autorespond && ($dept=$this->getDept()))
$autorespond=$dept->autoRespONNewMessage();
if(!$autorespond && !$cfg->autoRespONNewMessage()) return; //no autoresp or alerts.
$this->reload();
if(!$dept && !($tpl = $dept->getTemplate()))
$tpl= $cfg->getDefaultTemplate();
//If enabled...send confirmation to user. ( New Message AutoResponse)
if($tpl && ($msg=$tpl->getNewMessageAutorepMsgTemplate())) {
$body=$this->replaceTemplateVars($msg['body']);
$subj=$this->replaceTemplateVars($msg['subj']);
$body = str_replace('%signature',($dept && $dept->isPublic())?$dept->getSignature():'',$body);
//Reply separator tag.
if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
$body ="\n$tag\n\n".$body;
if(!$dept || !($email=$dept->getAutoRespEmail()))
$email=$cfg->getDefaultEmail();
if($email) {
$email->send($this->getEMail(),$subj,$body);
}
}
}
function onAssign($note, $alert=true) {
global $cfg;
if($this->isClosed()) $this->reopen(); //Assigned tickets must be open - otherwise why assign?
$this->reload();
//Log an internal note - no alerts on the internal note.
$note=$note?$note:'Ticket assignment';
$this->postNote('Ticket Assigned to '.$this->getAssignee(),$note,false);
//See if we need to send alerts
if(!$alert || !$cfg->alertONAssignment()) return true; //No alerts!
$dept = $this->getDept();
//Get template.
if(!$dept && !($tpl = $dept->getTemplate()))
$tpl= $cfg->getDefaultTemplate();
//Email to use!
if(!($email=$cfg->getAlertEmail()))
$email =$cfg->getDefaultEmail();
//Get the message template
if($tpl && ($msg=$tpl->getAssignedAlertMsgTemplate()) && $email) {
$body=$this->replaceTemplateVars($msg['body']);
$subj=$this->replaceTemplateVars($msg['subj']);
$body = str_replace('%note', $note, $body);
$body = str_replace('%message', $note, $body); //Previous versions used message.
$body = str_replace('%assignee', $this->getAssignee(), $body);
$body = str_replace('%assigner', ($thisstaff)?$thisstaff->getName():'System',$body);
//recipients
$recipients=array();
//Assigned staff or team... if any
// Assigning a ticket to a team when already assigned to staff disables alerts to the team (!))
if($cfg->alertStaffONAssign() && $this->getStaffId())
$recipients[]=$this->getStaff();
elseif($this->getTeamId() && ($team=$this->getTeam())) {