/
class.task.php
1642 lines (1318 loc) · 50.3 KB
/
class.task.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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
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
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
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
<?php
/*********************************************************************
class.task.php
Task
Peter Rotich <peter@osticket.com>
Copyright (c) 2014 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.role.php';
class TaskModel extends VerySimpleModel {
static $meta = array(
'table' => TASK_TABLE,
'pk' => array('id'),
'joins' => array(
'dept' => array(
'constraint' => array('dept_id' => 'Dept.id'),
),
'lock' => array(
'constraint' => array('lock_id' => 'Lock.lock_id'),
'null' => true,
),
'staff' => array(
'constraint' => array('staff_id' => 'Staff.staff_id'),
'null' => true,
),
'team' => array(
'constraint' => array('team_id' => 'Team.team_id'),
'null' => true,
),
'thread' => array(
'constraint' => array(
'id' => 'TaskThread.object_id',
"'A'" => 'TaskThread.object_type',
),
'list' => false,
'null' => false,
),
'cdata' => array(
'constraint' => array('id' => 'TaskCData.task_id'),
'list' => false,
),
'entries' => array(
'constraint' => array(
"'A'" => 'DynamicFormEntry.object_type',
'id' => 'DynamicFormEntry.object_id',
),
'list' => true,
),
'ticket' => array(
'constraint' => array(
'object_id' => 'Ticket.ticket_id',
),
'null' => true,
),
),
);
const PERM_CREATE = 'task.create';
const PERM_EDIT = 'task.edit';
const PERM_ASSIGN = 'task.assign';
const PERM_TRANSFER = 'task.transfer';
const PERM_REPLY = 'task.reply';
const PERM_CLOSE = 'task.close';
const PERM_DELETE = 'task.delete';
static protected $perms = array(
self::PERM_CREATE => array(
'title' =>
/* @trans */ 'Create',
'desc' =>
/* @trans */ 'Ability to create tasks'),
self::PERM_EDIT => array(
'title' =>
/* @trans */ 'Edit',
'desc' =>
/* @trans */ 'Ability to edit tasks'),
self::PERM_ASSIGN => array(
'title' =>
/* @trans */ 'Assign',
'desc' =>
/* @trans */ 'Ability to assign tasks to agents or teams'),
self::PERM_TRANSFER => array(
'title' =>
/* @trans */ 'Transfer',
'desc' =>
/* @trans */ 'Ability to transfer tasks between departments'),
self::PERM_REPLY => array(
'title' =>
/* @trans */ 'Post Reply',
'desc' =>
/* @trans */ 'Ability to post task update'),
self::PERM_CLOSE => array(
'title' =>
/* @trans */ 'Close',
'desc' =>
/* @trans */ 'Ability to close tasks'),
self::PERM_DELETE => array(
'title' =>
/* @trans */ 'Delete',
'desc' =>
/* @trans */ 'Ability to delete tasks'),
);
const ISOPEN = 0x0001;
const ISOVERDUE = 0x0002;
protected function hasFlag($flag) {
return ($this->get('flags') & $flag) !== 0;
}
protected function clearFlag($flag) {
return $this->set('flags', $this->get('flags') & ~$flag);
}
protected function setFlag($flag) {
return $this->set('flags', $this->get('flags') | $flag);
}
function getId() {
return $this->id;
}
function getNumber() {
return $this->number;
}
function getStaffId() {
return $this->staff_id;
}
function getStaff() {
return $this->staff;
}
function getTeamId() {
return $this->team_id;
}
function getTeam() {
return $this->team;
}
function getDeptId() {
return $this->dept_id;
}
function getDept() {
return $this->dept;
}
function getCreateDate() {
return $this->created;
}
function getDueDate() {
return $this->duedate;
}
function getCloseDate() {
return $this->isClosed() ? $this->closed : '';
}
function isOpen() {
return $this->hasFlag(self::ISOPEN);
}
function isClosed() {
return !$this->isOpen();
}
function isCloseable() {
if ($this->isClosed())
return true;
$warning = null;
if ($this->getMissingRequiredFields()) {
$warning = sprintf(
__( '%1$s is missing data on %2$s one or more required fields %3$s and cannot be closed'),
__('This task'),
'', '');
}
return $warning ?: true;
}
protected function close() {
return $this->clearFlag(self::ISOPEN);
}
protected function reopen() {
return $this->setFlag(self::ISOPEN);
}
function isAssigned($to=null) {
if (!$this->isOpen())
return false;
if (is_null($to))
return ($this->getStaffId() || $this->getTeamId());
switch (true) {
case $to instanceof Staff:
return ($to->getId() == $this->getStaffId() ||
$to->isTeamMember($this->getTeamId()));
break;
case $to instanceof Team:
return ($to->getId() == $this->getTeamId());
break;
}
return false;
}
function isOverdue() {
return $this->hasFlag(self::ISOVERDUE);
}
static function getPermissions() {
return self::$perms;
}
}
RolePermission::register(/* @trans */ 'Tasks', TaskModel::getPermissions());
class Task extends TaskModel implements RestrictedAccess, Threadable {
var $form;
var $entry;
var $_thread;
var $_entries;
var $_answers;
var $lastrespondent;
function __onload() {
$this->loadDynamicData();
}
function loadDynamicData() {
if (!isset($this->_answers)) {
$this->_answers = array();
foreach (DynamicFormEntryAnswer::objects()
->filter(array(
'entry__object_id' => $this->getId(),
'entry__object_type' => ObjectModel::OBJECT_TYPE_TASK
)) as $answer
) {
$tag = mb_strtolower($answer->field->name)
?: 'field.' . $answer->field->id;
$this->_answers[$tag] = $answer;
}
}
return $this->_answers;
}
function getStatus() {
return $this->isOpen() ? __('Open') : __('Completed');
}
function getTitle() {
return $this->__cdata('title', ObjectModel::OBJECT_TYPE_TASK);
}
function checkStaffPerm($staff, $perm=null) {
// Must be a valid staff
if (!$staff instanceof Staff && !($staff=Staff::lookup($staff)))
return false;
// Check access based on department or assignment
if (!$staff->canAccessDept($this->getDept())
&& $this->isOpen()
&& $staff->getId() != $this->getStaffId()
&& !$staff->isTeamMember($this->getTeamId()))
return false;
// At this point staff has access unless a specific permission is
// requested
if ($perm === null)
return true;
// Permission check requested -- get role.
if (!($role=$staff->getRole($this->getDept())))
return false;
// Check permission based on the effective role
return $role->hasPerm($perm);
}
function getAssignee() {
if (!$this->isOpen() || !$this->isAssigned())
return false;
if ($this->staff)
return $this->staff;
if ($this->team)
return $this->team;
return null;
}
function getAssigneeId() {
if (!($assignee=$this->getAssignee()))
return null;
$id = '';
if ($assignee instanceof Staff)
$id = 's'.$assignee->getId();
elseif ($assignee instanceof Team)
$id = 't'.$assignee->getId();
return $id;
}
function getAssignees() {
$assignees=array();
if ($this->staff)
$assignees[] = $this->staff->getName();
//Add team assignment
if ($this->team)
$assignees[] = $this->team->getName();
return $assignees;
}
function getAssigned($glue='/') {
$assignees = $this->getAssignees();
return $assignees ? implode($glue, $assignees):'';
}
function getLastRespondent() {
if (!isset($this->lastrespondent)) {
$this->lastrespondent = Staff::objects()
->filter(array(
'staff_id' => static::objects()
->filter(array(
'thread__entries__type' => 'R',
'thread__entries__staff_id__gt' => 0
))
->values_flat('thread__entries__staff_id')
->order_by('-thread__entries__id')
->limit(1)
))
->first()
?: false;
}
return $this->lastrespondent;
}
function getDynamicFields($criteria=array()) {
$fields = DynamicFormField::objects()->filter(array(
'id__in' => $this->entries
->filter($criteria)
->values_flat('answers__field_id')));
return ($fields && count($fields)) ? $fields : array();
}
function getMissingRequiredFields() {
return $this->getDynamicFields(array(
'answers__field__flags__hasbit' => DynamicFormField::FLAG_ENABLED,
'answers__field__flags__hasbit' => DynamicFormField::FLAG_CLOSE_REQUIRED,
'answers__value__isnull' => true,
));
}
function getParticipants() {
$participants = array();
foreach ($this->getThread()->collaborators as $c)
$participants[] = $c->getName();
return $participants ? implode(', ', $participants) : ' ';
}
function getThreadId() {
return $this->thread->getId();
}
function getThread() {
return $this->thread;
}
function getThreadEntry($id) {
return $this->getThread()->getEntry($id);
}
function getThreadEntries($type=false) {
$thread = $this->getThread()->getEntries();
if ($type && is_array($type))
$thread->filter(array('type__in' => $type));
return $thread;
}
function postThreadEntry($type, $vars, $options=array()) {
$errors = array();
$poster = isset($options['poster']) ? $options['poster'] : null;
$alert = isset($options['alert']) ? $options['alert'] : true;
switch ($type) {
case 'N':
case 'M':
return $this->getThread()->addDescription($vars);
break;
default:
return $this->postNote($vars, $errors, $poster, $alert);
}
}
function getForm() {
if (!isset($this->form)) {
// Look for the entry first
if ($this->form = DynamicFormEntry::lookup(
array('object_type' => ObjectModel::OBJECT_TYPE_TASK))) {
return $this->form;
}
// Make sure the form is in the database
elseif (!($this->form = DynamicForm::lookup(
array('type' => ObjectModel::OBJECT_TYPE_TASK)))) {
$this->__loadDefaultForm();
return $this->getForm();
}
// Create an entry to be saved later
$this->form = $this->form->instanciate();
$this->form->object_type = ObjectModel::OBJECT_TYPE_TASK;
}
return $this->form;
}
function getAssignmentForm($source=null, $options=array()) {
$prompt = $assignee = '';
// Possible assignees
$assignees = array();
switch (strtolower($options['target'])) {
case 'agents':
$dept = $this->getDept();
foreach ($dept->getAssignees() as $member)
$assignees['s'.$member->getId()] = $member;
if (!$source && $this->isOpen() && $this->staff)
$assignee = sprintf('s%d', $this->staff->getId());
$prompt = __('Select an Agent');
break;
case 'teams':
if (($teams = Team::getActiveTeams()))
foreach ($teams as $id => $name)
$assignees['t'.$id] = $name;
if (!$source && $this->isOpen() && $this->team)
$assignee = sprintf('t%d', $this->team->getId());
$prompt = __('Select a Team');
break;
}
// Default to current assignee if source is not set
if (!$source)
$source = array('assignee' => array($assignee));
$form = AssignmentForm::instantiate($source, $options);
if ($assignees)
$form->setAssignees($assignees);
if ($prompt && ($f=$form->getField('assignee')))
$f->configure('prompt', $prompt);
return $form;
}
function getClaimForm($source=null, $options=array()) {
global $thisstaff;
$id = sprintf('s%d', $thisstaff->getId());
if(!$source)
$source = array('assignee' => array($id));
$form = ClaimForm::instantiate($source, $options);
$form->setAssignees(array($id => $thisstaff->getName()));
return $form;
}
function getTransferForm($source=null) {
if (!$source)
$source = array('dept' => array($this->getDeptId()));
return TransferForm::instantiate($source);
}
function addDynamicData($data) {
$tf = TaskForm::getInstance($this->id, true);
foreach ($tf->getFields() as $f)
if (isset($data[$f->get('name')]))
$tf->setAnswer($f->get('name'), $data[$f->get('name')]);
$tf->save();
return $tf;
}
function getDynamicData($create=true) {
if (!isset($this->_entries)) {
$this->_entries = DynamicFormEntry::forObject($this->id,
ObjectModel::OBJECT_TYPE_TASK)->all();
if (!$this->_entries && $create) {
$f = TaskForm::getInstance($this->id, true);
$f->save();
$this->_entries[] = $f;
}
}
return $this->_entries ?: array();
}
function setStatus($status, $comments='', &$errors=array()) {
global $thisstaff;
$ecb = null;
switch($status) {
case 'open':
if ($this->isOpen())
return false;
$this->reopen();
$this->closed = null;
$ecb = function ($t) use($thisstaff) {
$t->logEvent('reopened', false, null, 'closed');
$type = array('type' => 'reopened', 'data' => array('name' => $this->getNumber()));
Signal::send('object.edited', $this, $type);
if ($t->ticket) {
$t->ticket->reopen();
$vars = array(
'title' => sprintf('Task %s Reopened',
$t->getNumber()),
'note' => __('Task reopened')
);
$t->ticket->logNote($vars['title'], $vars['note'], $thisstaff);
}
};
break;
case 'closed':
if ($this->isClosed())
return false;
// Check if task is closeable
$closeable = $this->isCloseable();
if ($closeable !== true)
$errors['err'] = $closeable ?: sprintf(__('%s cannot be closed'), __('This task'));
if ($errors)
return false;
$this->close();
$this->closed = SqlFunction::NOW();
$ecb = function($t) use($thisstaff) {
$t->logEvent('closed');
$type = array('type' => 'closed', 'data' => array('name' => $t->getNumber()));
Signal::send('object.edited', $t, $type);
if ($t->ticket) {
$vars = array(
'title' => sprintf('Task %s Closed',
$t->getNumber()),
'note' => __('Task closed')
);
$t->ticket->logNote($vars['title'], $vars['note'], $thisstaff);
}
};
break;
default:
return false;
}
if (!$this->save(true))
return false;
// Log events via callback
if ($ecb) $ecb($this);
if ($comments) {
$errors = array();
$this->postNote(array(
'note' => $comments,
'title' => sprintf(
__('Status changed to %s'),
$this->getStatus())
),
$errors,
$thisstaff);
}
return true;
}
function to_json() {
$info = array(
'id' => $this->getId(),
'title' => $this->getTitle()
);
return JsonDataEncoder::encode($info);
}
function __cdata($field, $ftype=null) {
foreach ($this->getDynamicData() as $e) {
// Make sure the form type matches
if (!$e->form
|| ($ftype && $ftype != $e->form->get('type')))
continue;
// Get the named field and return the answer
if ($a = $e->getAnswer($field))
return $a;
}
return null;
}
function __toString() {
return (string) $this->getTitle();
}
/* util routines */
function logEvent($state, $data=null, $user=null, $annul=null) {
$this->getThread()->getEvents()->log($this, $state, $data, $user, $annul);
}
function claim(ClaimForm $form, &$errors) {
global $thisstaff;
$dept = $this->getDept();
$assignee = $form->getAssignee();
if (!($assignee instanceof Staff)
|| !$thisstaff
|| $thisstaff->getId() != $assignee->getId()) {
$errors['err'] = __('Unknown assignee');
} elseif (!$assignee->isAvailable()) {
$errors['err'] = __('Agent is unavailable for assignment');
} elseif (!$dept->canAssign($assignee)) {
$errors['err'] = __('Permission denied');
}
if ($errors)
return false;
$type = array('type' => 'assigned', 'data' => array('claim' => true));
Signal::send('object.edited', $this, $type);
return $this->assignToStaff($assignee, $form->getComments(), false);
}
function assignToStaff($staff, $note, $alert=true) {
if(!is_object($staff) && !($staff = Staff::lookup($staff)))
return false;
if (!$staff->isAvailable())
return false;
$this->staff_id = $staff->getId();
if (!$this->save())
return false;
$this->onAssignment($staff, $note, $alert);
global $thisstaff;
$data = array();
if ($thisstaff && $staff->getId() == $thisstaff->getId())
$data['claim'] = true;
else
$data['staff'] = $staff->getId();
$this->logEvent('assigned', $data);
return true;
}
function assign(AssignmentForm $form, &$errors, $alert=true) {
global $thisstaff;
$evd = array();
$audit = array();
$assignee = $form->getAssignee();
if ($assignee instanceof Staff) {
$dept = $this->getDept();
if ($this->getStaffId() == $assignee->getId()) {
$errors['assignee'] = sprintf(__('%s already assigned to %s'),
__('Task'),
__('the agent')
);
} elseif(!$assignee->isAvailable()) {
$errors['assignee'] = __('Agent is unavailable for assignment');
} elseif (!$dept->canAssign($assignee)) {
$errors['err'] = __('Permission denied');
}
else {
$this->staff_id = $assignee->getId();
if ($thisstaff && $thisstaff->getId() == $assignee->getId())
$evd['claim'] = true;
else
$evd['staff'] = array($assignee->getId(), $assignee->getName());
$audit = array('name' => $this->getNumber(), 'staff' => $assignee->getName()->name);
}
} elseif ($assignee instanceof Team) {
if ($this->getTeamId() == $assignee->getId()) {
$errors['assignee'] = sprintf(__('%s already assigned to %s'),
__('Task'),
__('the team')
);
} else {
$this->team_id = $assignee->getId();
$evd = array('team' => $assignee->getId());
$audit = array('name' => $this->getNumber(), 'team' => $assignee->getName());
}
} else {
$errors['assignee'] = __('Unknown assignee');
}
if ($errors || !$this->save(true))
return false;
$this->logEvent('assigned', $evd);
$type = array('type' => 'assigned', 'data' => $audit);
Signal::send('object.edited', $this, $type);
$this->onAssignment($assignee,
$form->getField('comments')->getClean(),
$alert);
return true;
}
function onAssignment($assignee, $comments='', $alert=true) {
global $thisstaff, $cfg;
if (!is_object($assignee))
return false;
$assigner = $thisstaff ?: __('SYSTEM (Auto Assignment)');
//Assignment completed... post internal note.
$note = null;
if ($comments) {
$title = sprintf(__('Task assigned to %s'),
(string) $assignee);
$errors = array();
$note = $this->postNote(
array('note' => $comments, 'title' => $title),
$errors,
$assigner,
false);
}
// Send alerts out if enabled.
if (!$alert || !$cfg->alertONTaskAssignment())
return false;
if (!($dept=$this->getDept())
|| !($tpl = $dept->getTemplate())
|| !($email = $dept->getAlertEmail())
) {
return true;
}
// Recipients
$recipients = array();
if ($assignee instanceof Staff) {
if ($cfg->alertStaffONTaskAssignment())
$recipients[] = $assignee;
} elseif (($assignee instanceof Team) && $assignee->alertsEnabled()) {
if ($cfg->alertTeamMembersONTaskAssignment() && ($members=$assignee->getMembers()))
$recipients = array_merge($recipients, $members);
elseif ($cfg->alertTeamLeadONTaskAssignment() && ($lead=$assignee->getTeamLead()))
$recipients[] = $lead;
}
if ($recipients
&& ($msg=$tpl->getTaskAssignmentAlertMsgTemplate())) {
$msg = $this->replaceVars($msg->asArray(),
array('comments' => $comments,
'assignee' => $assignee,
'assigner' => $assigner
)
);
// Send the alerts.
$sentlist = array();
$options = $note instanceof ThreadEntry
? array('thread' => $note)
: array();
foreach ($recipients as $k => $staff) {
if (!is_object($staff)
|| !$staff->isAvailable()
|| in_array($staff->getEmail(), $sentlist)) {
continue;
}
$alert = $this->replaceVars($msg, array('recipient' => $staff));
$email->sendAlert($staff, $alert['subj'], $alert['body'], null, $options);
$sentlist[] = $staff->getEmail();
}
}
return true;
}
function transfer(TransferForm $form, &$errors, $alert=true) {
global $thisstaff, $cfg;
$cdept = $this->getDept();
$dept = $form->getDept();
if (!$dept || !($dept instanceof Dept))
$errors['dept'] = __('Department selection is required');
elseif ($dept->getid() == $this->getDeptId())
$errors['dept'] = __('Task already in the department');
else
$this->dept_id = $dept->getId();
if ($errors || !$this->save(true))
return false;
// Log transfer event
$this->logEvent('transferred');
$type = array('type' => 'transferred', 'data' => array('name' => $this->getNumber(), 'dept' => $dept->getName()));
Signal::send('object.edited', $this, $type);
// Post internal note if any
$note = $form->getField('comments')->getClean();
if ($note) {
$title = sprintf(__('%1$s transferred from %2$s to %3$s'),
__('Task'),
$cdept->getName(),
$dept->getName());
$_errors = array();
$note = $this->postNote(
array('note' => $note, 'title' => $title),
$_errors, $thisstaff, false);
}
// Send alerts if requested && enabled.
if (!$alert || !$cfg->alertONTaskTransfer())
return true;
if (($email = $dept->getAlertEmail())
&& ($tpl = $dept->getTemplate())
&& ($msg=$tpl->getTaskTransferAlertMsgTemplate())) {
$msg = $this->replaceVars($msg->asArray(),
array('comments' => $note, 'staff' => $thisstaff));
// Recipients
$recipients = array();
// Assigned staff or team... if any
if ($this->isAssigned() && $cfg->alertAssignedONTaskTransfer()) {
if($this->getStaffId())
$recipients[] = $this->getStaff();
elseif ($this->getTeamId()
&& ($team=$this->getTeam())
&& ($members=$team->getMembers())
) {
$recipients = array_merge($recipients, $members);
}
} elseif ($cfg->alertDeptMembersONTaskTransfer() && !$this->isAssigned()) {
// Only alerts dept members if the task is NOT assigned.
foreach ($dept->getMembersForAlerts() as $M)
$recipients[] = $M;
}
// Always alert dept manager??
if ($cfg->alertDeptManagerONTaskTransfer()
&& ($manager=$dept->getManager())) {
$recipients[] = $manager;
}
$sentlist = $options = array();
if ($note instanceof ThreadEntry) {
$options += array('thread'=>$note);
}
foreach ($recipients as $k=>$staff) {
if (!is_object($staff)
|| !$staff->isAvailable()
|| in_array($staff->getEmail(), $sentlist)
) {
continue;
}
$alert = $this->replaceVars($msg, array('recipient' => $staff));
$email->sendAlert($staff, $alert['subj'], $alert['body'], null, $options);
$sentlist[] = $staff->getEmail();
}
}
return true;
}
function postNote($vars, &$errors, $poster='', $alert=true) {
global $cfg, $thisstaff;
$vars['staffId'] = 0;
$vars['poster'] = 'SYSTEM';
if ($poster && is_object($poster)) {
$vars['staffId'] = $poster->getId();
$vars['poster'] = $poster->getName();
} elseif ($poster) { //string
$vars['poster'] = $poster;
}
if (!($note=$this->getThread()->addNote($vars, $errors)))
return null;
$assignee = $this->getStaff();
if (isset($vars['task:status']))
$this->setStatus($vars['task:status']);
$this->onActivity(array(
'activity' => $note->getActivity(),
'threadentry' => $note,
'assignee' => $assignee
), $alert);
return $note;
}
/* public */
function postReply($vars, &$errors, $alert = true) {
global $thisstaff, $cfg;
if (!$vars['poster'] && $thisstaff)
$vars['poster'] = $thisstaff;
if (!$vars['staffId'] && $thisstaff)
$vars['staffId'] = $thisstaff->getId();
if (!$vars['ip_address'] && $_SERVER['REMOTE_ADDR'])
$vars['ip_address'] = $_SERVER['REMOTE_ADDR'];
if (!($response = $this->getThread()->addResponse($vars, $errors)))
return null;
$assignee = $this->getStaff();
if (isset($vars['task:status']))
$this->setStatus($vars['task:status']);
/*
// TODO: add auto claim setting for tasks.
// Claim on response bypasses the department assignment restrictions
if ($thisstaff
&& $this->isOpen()
&& !$this->getStaffId()
&& $cfg->autoClaimTasks)
) {
$this->staff_id = $thisstaff->getId();
}
*/