1: <?php
2: /**
3: * The Horde_LoginTasks_Tasklist:: class is used to store the list of
4: * login tasks that need to be run during this login.
5: *
6: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Michael Slusarz <slusarz@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
14: * @package LoginTasks
15: */
16: class Horde_LoginTasks_Tasklist
17: {
18: /**
19: * The URL of the web page to load after login tasks are complete.
20: *
21: * @var string
22: */
23: public $target;
24:
25: /**
26: * Has this tasklist been processed yet?
27: *
28: * @var boolean
29: */
30: public $processed = false;
31:
32: /**
33: * The list of tasks to run during this login.
34: *
35: * KEY: Task name
36: * VALUE: array => (
37: * 'display' => boolean,
38: * 'task' => integer
39: * )
40: *
41: * @var array
42: */
43: protected $_tasks = array();
44:
45: /**
46: * The list of system tasks to run during this login.
47: *
48: * @see $_tasks
49: *
50: * @var array
51: */
52: protected $_stasks = array();
53:
54: /**
55: * Current task location pointer.
56: *
57: * @var integer
58: */
59: protected $_ptr = 0;
60:
61: /**
62: * Adds a task to the tasklist.
63: *
64: * @param Horde_LoginTasks_Task $task The task to execute.
65: */
66: public function addTask($task)
67: {
68: if ($task instanceof Horde_LoginTasks_SystemTask) {
69: $this->_stasks[] = $task;
70: } else {
71: switch ($task->priority) {
72: case Horde_LoginTasks::PRIORITY_HIGH:
73: array_unshift($this->_tasks, $task);
74: break;
75:
76: case Horde_LoginTasks::PRIORITY_NORMAL:
77: $this->_tasks[] = $task;
78: break;
79: }
80: }
81: }
82:
83: /**
84: * Returns the list of tasks to perform.
85: *
86: * @param boolean $advance If true, mark ready tasks as completed.
87: *
88: * @return array The list of tasks to perform.
89: */
90: public function ready($advance = false)
91: {
92: $stasks = $tasks = array();
93:
94: /* Always loop through system tasks first. */
95: foreach ($this->_stasks as $key => $val) {
96: if (!$val->skip()) {
97: $stasks[] = $val;
98: unset($this->_stasks[$key]);
99: }
100: }
101:
102: reset($this->_tasks);
103: while (list($k, $v) = each($this->_tasks)) {
104: if ($v->needsDisplay() && ($k >= $this->_ptr)) {
105: break;
106: }
107: $tasks[] = $v;
108: }
109:
110: if ($advance) {
111: $this->_tasks = array_slice($this->_tasks, count($tasks));
112: $this->_ptr = 0;
113: }
114:
115: return array_merge($stasks, $tasks);
116: }
117:
118: /**
119: * Returns the next batch of tasks that need display.
120: *
121: * @param boolean $advance If true, advance the internal pointer.
122: *
123: * @return array The list of tasks to display.
124: */
125: public function needDisplay($advance = false)
126: {
127: $tmp = array();
128: $previous = null;
129:
130: reset($this->_tasks);
131: while (list(, $v) = each($this->_tasks)) {
132: if (!$v->needsDisplay() ||
133: (!is_null($previous) && !$v->joinDisplayWith($previous))) {
134: break;
135: }
136: $tmp[] = $v;
137: $previous = $v;
138: }
139:
140: if ($advance) {
141: $this->_ptr = count($tmp);
142: }
143:
144: return $tmp;
145: }
146:
147: /**
148: * Are all tasks complete?
149: *
150: * @return boolean True if all tasks are complete.
151: */
152: public function isDone()
153: {
154: return (empty($this->_stasks) &&
155: ($this->_ptr == count($this->_tasks)));
156: }
157:
158: }
159: