/
Task.php
1634 lines (1472 loc) · 47 KB
/
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
/**
* Nag_Task handles as single task as well as a list of tasks and implements a
* recursive iterator to handle a (hierarchical) list of tasks.
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Jan Schneider <jan@horde.org>
* @package Nag
*
* @property tags array An array of tags this task is tagged with.
*/
class Nag_Task
{
/**
* The task id.
*
* @var string
*/
public $id;
/**
* This task's tasklist id.
*
* @var string
*/
public $tasklist;
/**
* This task's tasklist name.
*
* Overrides the $tasklist's share name.
*
* @var string
*/
public $tasklist_name;
/**
* The task uid.
*
* @var string
*/
public $uid;
/**
* The task owner.
*
* @var string
*/
public $owner;
/**
* The task assignee.
*
* @var string
*/
public $assignee;
/**
* The task title.
*
* @var string
*/
public $name;
/**
* The task decription.
*
* @var string
*/
public $desc;
/**
* The start date timestamp.
*
* @var integer
*/
public $start;
/**
* The due date timestamp.
*
* @var integer
*/
public $due;
/**
* Recurrence rules for recurring tasks.
*
* @var Horde_Date_Recurrence
*/
public $recurrence;
/**
* The task priority from 1 = highest to 5 = lowest.
*
* @var integer
*/
public $priority;
/**
* The estimated task length.
*
* @var float
*/
public $estimate;
/**
* Whether the task is completed.
*
* @var boolean
*/
public $completed;
/**
* The completion date timestamp.
*
* @var integer
*/
public $completed_date;
/**
* The creation time.
*
* @var Horde_Date
*/
public $created;
/**
* The creator string.
*
* @var string
*/
public $createdby;
/**
* The last modification time.
*
* @var Horde_Date
*/
public $modified;
/**
* The last-modifier string.
*
* @var string
*/
public $modifiedby;
/**
* The task alarm threshold in minutes.
*
* @var integer
*/
public $alarm;
/**
* The particular alarm methods overridden for this task.
*
* @var array
*/
public $methods;
/**
* Whether the task is private.
*
* @var boolean
*/
public $private;
/**
* URL to view the task.
*
* @var string
*/
public $view_link;
/**
* URL to complete the task.
*
* @var string
*/
public $complete_link;
/**
* URL to edit the task.
*
* @var string
*/
public $edit_link;
/**
* URL to delete the task.
*
* @var string
*/
public $delete_link;
/**
* The parent task's id.
*
* @var string
*/
public $parent_id = '';
/**
* The parent task.
*
* @var Nag_Task
*/
public $parent;
/**
* The sub-tasks.
*
* @var array
*/
public $children = array();
/**
* This task's idention (child) level.
*
* @var integer
*/
public $indent = 0;
/**
* Whether this is the last sub-task.
*
* @var boolean
*/
public $lastChild;
/**
* A storage driver.
*
* @var Nag_Driver
*/
protected $_storage;
/**
* Internal flag.
*
* @var boolean
* @see each()
*/
protected $_inlist = false;
/**
* Internal pointer.
*
* @var integer
* @see each()
*/
protected $_pointer = 0;
/**
* Task id => pointer dictionary.
*
* @var array
*/
protected $_dict = array();
/**
* Task tags from the storage backend (e.g. Kolab)
*
* @var array
*/
public $internaltags;
/**
* Task tags (lazy loaded).
*
* @var string
*/
protected $_tags;
/**
* Constructor.
*
* Takes a hash and returns a nice wrapper around it.
*
* @param Nag_Driver $storage A storage driver.
* @param array $task A task hash.
*/
public function __construct(Nag_Driver $storage = null, array $task = null)
{
if ($storage) {
$this->_storage = $storage;
}
if ($task) {
$this->merge($task);
}
}
/**
* Getter.
*
* Returns the 'id' and 'creator' properties.
*
* @param string $name Property name.
*
* @return mixed Property value.
*/
public function __get($name)
{
switch ($name) {
case 'tags':
if (!isset($this->_tags)) {
$this->synchronizeTags($GLOBALS['injector']->getInstance('Nag_Tagger')->getTags($this->uid, 'task'));
}
return $this->_tags;
}
$trace = debug_backtrace();
trigger_error('Undefined property via __get(): ' . $name
. ' in ' . $trace[0]['file']
. ' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/**
* Setter.
*
* @param string $name Property name.
* @param mixed $value Property value.
*/
public function __set($name, $value)
{
switch ($name) {
case 'tags':
$this->_tags = $value;
return;
}
$trace = debug_backtrace();
trigger_error('Undefined property via __set(): ' . $name
. ' in ' . $trace[0]['file']
. ' on line ' . $trace[0]['line'],
E_USER_NOTICE);
}
/**
* Deep clone so we can clone the child objects too.
*
*/
public function __clone()
{
foreach ($this->children as $key => $value) {
$this->children[$key] = clone $value;
}
}
/**
* Merges a task hash into this task object.
*
* @param array $task A task hash.
*/
public function merge(array $task)
{
foreach ($task as $key => $val) {
switch ($key) {
case 'tasklist_id':
$key = 'tasklist';
break;
case 'task_id':
$key = 'id';
break;
case 'parent':
$key = 'parent_id';
break;
}
$this->$key = $val;
}
}
/**
* Disconnect this task from any child tasks. Used when building search
* result sets since child tasks will be re-added if they actually match
* the result, and there is no guarentee that a tasks's parent will
* be present in the result set.
*/
public function orphan()
{
$this->children = array();
$this->_dict = array();
$this->lastChild = null;
$this->indent = null;
}
/**
* Saves this task in the storage backend.
*
* @throws Nag_Exception
*/
public function save()
{
$this->_storage->modify($this->id, $this->toHash(true));
}
/**
* Returns the parent task of this task, if one exists.
*
* @return mixed The parent task, null if none exists
*/
public function getParent()
{
if (!$this->parent_id) {
return null;
}
return Nag::getTask($this->tasklist, $this->parent_id);
}
/**
* Adds a sub task to this task.
*
* @param Nag_Task $task A sub task.
*/
public function add(Nag_Task $task, $replace = false)
{
if (!isset($this->_dict[$task->id])) {
$this->_dict[$task->id] = count($this->children);
$task->parent = $this;
$this->children[] = $task;
} elseif ($replace) {
$this->children[$this->_dict[$task->id]]= $task;
}
}
/**
* Loads all sub-tasks.
*
* @param
*/
public function loadChildren($include_history = true)
{
try {
$this->children = $this->_storage->getChildren($this->id, $include_history);
} catch (Nag_Exception $e) {}
}
/**
* Merges an array of tasks into this task's children.
*
* @param array $children A list of Nag_Tasks.
*
*/
public function mergeChildren(array $children)
{
for ($i = 0, $c = count($children); $i < $c; ++$i) {
$this->add($children[$i]);
}
}
/**
* Returns a sub task by its id.
*
* The methods goes recursively through all sub tasks until it finds the
* searched task.
*
* @param string $key A task id.
*
* @return Nag_Task The searched task or null.
*/
public function get($key)
{
return isset($this->_dict[$key]) ?
$this->children[$this->_dict[$key]] :
null;
}
/**
* Returns whether this is a task (not a container) or contains any sub
* tasks.
*
* @return boolean True if this is a task or has sub tasks.
*/
public function hasTasks()
{
return ($this->id) ? true : $this->hasSubTasks();
}
/**
* Returns whether this task contains any sub tasks.
*
* @return boolean True if this task has sub tasks.
*/
public function hasSubTasks()
{
foreach ($this->children as $task) {
if ($task->hasTasks()) {
return true;
}
}
return false;
}
/**
* Returns whether all sub tasks are completed.
*
* @return boolean True if all sub tasks are completed.
*/
public function childrenCompleted()
{
foreach ($this->children as $task) {
if (!$task->completed || !$task->childrenCompleted()) {
return false;
}
}
return true;
}
/**
* Returns whether any tasks in the list are overdue.
*
* @return boolean True if any task or sub tasks are overdue.
*/
public function childrenOverdue()
{
if (!empty($this->due)) {
$due = new Horde_Date($this->due);
if ($due->compareDate(new Horde_Date(time())) <= 0) {
return true;
}
}
foreach ($this->children as $task) {
if ($task->childrenOverdue()) {
return true;
}
}
return false;
}
/**
* Returns the number of tasks including this and any sub tasks.
*
* @return integer The number of tasks and sub tasks.
*/
public function count()
{
$count = $this->id ? 1 : 0;
foreach ($this->children as $task) {
$count += $task->count();
}
return $count;
}
/**
* Returns the estimated length for this and any sub tasks.
*
* @return integer The estimated length sum.
*/
public function estimation()
{
$estimate = $this->estimate;
foreach ($this->children as $task) {
$estimate += $task->estimation();
}
return $estimate;
}
/**
* Returns whether this task is a recurring task.
*
* @return boolean True if this is a recurring task.
*/
public function recurs()
{
return isset($this->recurrence) &&
!$this->recurrence->hasRecurType(Horde_Date_Recurrence::RECUR_NONE);
}
/**
* Toggles completion status of this task. Moves a recurring task
* to the next occurence on completion. Enforces the rule that sub
* tasks must be completed before parent tasks.
*/
public function toggleComplete($ignore_children = false)
{
if ($ignore_children) {
$this->loadChildren();
if (!$this->completed && !$this->childrenCompleted()) {
throw new Nag_Exception(_("Must complete all children tasks."));
}
}
if ($this->completed) {
$this->completed_date = null;
$this->completed = false;
if ($parent = $this->getParent()) {
if ($parent->completed) {
$parent->toggleComplete(true);
$parent->save();
}
}
if ($this->recurs()) {
/* Only delete the latest completion. */
$completions = $this->recurrence->getCompletions();
sort($completions);
list($year, $month, $mday) = sscanf(
end($completions),
'%04d%02d%02d'
);
$this->recurrence->deleteCompletion($year, $month, $mday);
}
return;
}
if ($this->recurs()) {
/* Get current occurrence (task due date) */
$current = $this->recurrence->nextActiveRecurrence(new Horde_Date($this->due));
if ($current) {
$this->recurrence->addCompletion($current->year,
$current->month,
$current->mday);
/* Advance this occurence by a day to indicate that we
* want the following occurence (Recurrence uses days
* as minimal time duration between occurrences). */
$current->mday++;
/* Only mark this due date completed if there is another
* occurence. */
if ($next = $this->recurrence->nextActiveRecurrence($current)) {
$this->completed = false;
return;
}
}
}
$this->completed_date = time();
$this->completed = true;
}
/**
* Returns the next start date of this task.
*
* Takes recurring tasks into account.
*
* @return Horde_Date The next start date.
*/
public function getNextStart()
{
if (!$this->start) {
return null;
}
if (!$this->recurs() ||
!($completions = $this->recurrence->getCompletions())) {
return new Horde_Date($this->start);
}
sort($completions);
list($year, $month, $mday) = sscanf(
end($completions),
'%04d%02d%02d'
);
$lastCompletion = new Horde_Date($year, $month, $mday);
$recurrence = clone $this->recurrence;
$recurrence->start = new Horde_Date($this->start);
return $recurrence->nextRecurrence($lastCompletion);
}
/**
* Returns the next due date of this task.
*
* Takes recurring tasks into account.
*
* @return Horde_Date|null The next due date or null if no due date.
*/
public function getNextDue()
{
if (!$this->due) {
return null;
}
if (!$this->recurs()) {
return new Horde_Date($this->due);
}
if (!($nextActive = $this->recurrence->nextActiveRecurrence($this->due))) {
return null;
}
return $nextActive;
}
/**
* Format the description - link URLs, etc.
*
* @return string
*/
public function getFormattedDescription()
{
$desc = $GLOBALS['injector']
->getInstance('Horde_Core_Factory_TextFilter')
->filter($this->desc,
'text2html',
array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
try {
return Horde::callHook('format_description', array($desc), 'nag');
} catch (Horde_Exception_HookNotSet $e) {
return $desc;
}
}
/**
* Resets the tasks iterator.
*
* Call this each time before looping through the tasks.
*
* @see each()
*/
public function reset()
{
foreach (array_keys($this->children) as $key) {
$this->children[$key]->reset();
}
$this->_pointer = 0;
$this->_inlist = false;
}
/**
* Return the task, if present anywhere in this tasklist, regardless of
* child depth.
*
* @param string $taskId The task id we are looking for.
*
* @return Nag_Task|false The task object, if found. Otherwise false.
*/
public function hasTask($taskId)
{
$this->reset();
while ($task = $this->each()) {
if ($task->id == $taskId) {
return $task;
}
}
return false;
}
/**
* Returns the next task iterating through all tasks and sub tasks.
*
* Call reset() each time before looping through the tasks:
* <code>
* $tasks->reset();
* while ($task = $tasks->each() {
* ...
* }
*
* @see reset()
*/
public function each()
{
if ($this->id && !$this->_inlist) {
$this->_inlist = true;
return $this;
}
if ($this->_pointer >= count($this->children)) {
return false;
}
$next = $this->children[$this->_pointer]->each();
if ($next) {
return $next;
}
$this->_pointer++;
return $this->each();
}
/**
* Helper method for getting only a slice of the total tasks in this list.
*
* @param integer $page The starting page.
* @param integer $perpage The count of tasks per page.
*
* @return Nag_Task The resulting task list.
*/
public function getSlice($page = 0, $perpage = null)
{
$this->reset();
// Position at start task
$start = $page * (empty($perpage) ? 0 : $perpage);
$count = 0;
while ($count < $start) {
if (!$this->each()) {
return new Nag_Task();
}
++$count;
}
$count = 0;
$results = new Nag_Task();
$max = (empty($perpage) ? ($this->count() - $start) : $perpage);
while ($count < $max) {
if ($next = $this->each()) {
$results->add($next);
++$count;
} else {
$count = $max;
}
}
$results->process();
return $results;
}
/**
* Processes a list of tasks by adding action links, obscuring details of
* private tasks and calculating indentation.
*
* @param integer $indent The indention level of the tasks.
*/
public function process($indent = null)
{
global $conf;
/* Link cache. */
static $view_url_list, $task_url_list;
/* Set indention. */
if (is_null($indent)) {
$indent = 0;
if ($parent = $this->getParent()) {
$indent = $parent->indent + 1;
}
}
$this->indent = $indent;
if ($this->id) {
$indent++;
}
/* Process children. */
for ($i = 0, $c = count($this->children); $i < $c; ++$i) {
$this->children[$i]->process($indent);
}
/* Mark last child. */
if (count($this->children)) {
$this->children[count($this->children) - 1]->lastChild = true;
}
/* Only process further if this is really a (parent) task, not only a
* task list container. */
if (!$this->id) {
return;
}
if (!isset($view_url_list[$this->tasklist])) {
$view_url_list[$this->tasklist] = Horde::url('view.php')->add('tasklist', $this->tasklist);
$task_url_list[$this->tasklist] = Horde::url('task.php')->add('tasklist', $this->tasklist);
}
/* Obscure private tasks. */
if ($this->private && $this->owner != $GLOBALS['registry']->getAuth()) {
$this->name = _("Private Task");
$this->desc = '';
}
/* Create task links. */
$this->view_link = $view_url_list[$this->tasklist]->copy()->add('task', $this->id);
$task_url_task = $task_url_list[$this->tasklist]->copy()->add('task', $this->id);
$this->complete_link = Horde::url(
$conf['urls']['pretty'] == 'rewrite'
? 't/complete'
: 'task/complete.php'
)->add(array(
'url' => Horde::signUrl(Horde::url('list.php')),
'task' => $this->id,
'tasklist' => $this->tasklist
));
$this->edit_link = $task_url_task->copy()->add('actionID', 'modify_task');
$this->delete_link = $task_url_task->copy()->add('actionID', 'delete_task');
}
/**
* Returns the HTML code for any tree icons, when displaying this task in
* a tree view.
*
* @return string The HTML code for necessary tree icons.
*/
public function treeIcons()
{
$html = '';
$parent = $this->parent;
for ($i = 1; $i < $this->indent; ++$i) {
if ($parent && $parent->lastChild) {
$html = Horde::img('tree/blank.png') . $html;
} else {
$html = Horde::img('tree/line.png', '|') . $html;
}
$parent = $parent->parent;
}
if ($this->indent) {
if ($this->lastChild) {
$html .= Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'tree/rev-joinbottom.png' : 'tree/joinbottom.png', '\\');
} else {
$html .= Horde::img($GLOBALS['registry']->nlsconfig->curr_rtl ? 'tree/rev-join.png' : 'tree/join.png', '+');
}
}
return $html;
}
/**
* Recursively loads tags for all tasks contained in this object.
*/
public function loadTags()
{
$ids = array();
if (!isset($this->_tags)) {
$ids[] = $this->uid;
}
foreach ($this->children as $task) {
$ids[] = $task->uid;
}
if (!$ids) {
return;
}
$results = $GLOBALS['injector']->getInstance('Nag_Tagger')->getTags($ids);
if (isset($results[$this->uid])) {
$this->synchronizeTags($results[$this->uid]);
}
foreach ($this->children as $task) {
if (isset($results[$task->uid])) {
$task->synchronizeTags($results[$task->uid]);
$task->loadTags();
}
}
}
/**
* Syncronizes tags from the tagging backend with the task storage backend,
* if necessary.
*
* @param array $tags Tags from the tagging backend.
*/
public function synchronizeTags(array $tags)
{
if (isset($this->internaltags)) {
usort($tags, 'strcoll');
if (array_diff($this->internaltags, $tags)) {
$GLOBALS['injector']->getInstance('Nag_Tagger')->replaceTags(
$this->uid,
$this->internaltags,
$this->owner,
'task'
);
}
$this->_tags = implode(',', $this->internaltags);
} else {
$this->_tags = $tags;
}
}
/**
* Sorts sub tasks by the given criteria.
*
* @param string $sortby The field by which to sort
* (Nag::SORT_PRIORITY, Nag::SORT_NAME
* Nag::SORT_DUE, Nag::SORT_COMPLETION).
* @param integer $sortdir The direction by which to sort
* (Nag::SORT_ASCEND, Nag::SORT_DESCEND).
* @param string $altsortby The secondary sort field.
*/
public function sort($sortby, $sortdir, $altsortby)
{
/* Sorting criteria for the task list. */
$sort_functions = array(
Nag::SORT_PRIORITY => 'ByPriority',
Nag::SORT_NAME => 'ByName',
Nag::SORT_DUE => 'ByDue',
Nag::SORT_START => 'ByStart',
Nag::SORT_COMPLETION => 'ByCompletion',
Nag::SORT_ASSIGNEE => 'ByAssignee',
Nag::SORT_ESTIMATE => 'ByEstimate',
Nag::SORT_OWNER => 'ByOwner'
);
/* Sort the array if we have a sort function defined for this
* field. */
if (isset($sort_functions[$sortby])) {
$prefix = ($sortdir == Nag::SORT_DESCEND) ? '_rsort' : '_sort';
usort($this->children, array('Nag', $prefix . $sort_functions[$sortby]));
if (isset($sort_functions[$altsortby]) && $altsortby !== $sortby) {
$task_buckets = array();
for ($i = 0, $c = count($this->children); $i < $c; ++$i) {
if (!isset($task_buckets[$this->children[$i]->$sortby])) {
$task_buckets[$this->children[$i]->$sortby] = array();
}
$task_buckets[$this->children[$i]->$sortby][] = $this->children[$i];
}
$tasks = array();
foreach ($task_buckets as $task_bucket) {
usort($task_bucket, array('Nag', $prefix . $sort_functions[$altsortby]));
$tasks = array_merge($tasks, $task_bucket);
}
$this->children = $tasks;