/framework/LoginTasks/lib/Horde/LoginTasks/Tasklist.php

https://github.com/finger2000/horde · PHP · 158 lines · 69 code · 17 blank · 72 comment · 13 complexity · a4a9b29784a519bab7e4c8fddefa98dc MD5 · raw file

  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-2011 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. * Has this tasklist been processed yet?
  26. *
  27. * @var boolean
  28. */
  29. public $processed = false;
  30. /**
  31. * The list of tasks to run during this login.
  32. *
  33. * KEY: Task name
  34. * VALUE: array => (
  35. * 'display' => boolean,
  36. * 'task' => integer
  37. * )
  38. *
  39. * @var array
  40. */
  41. protected $_tasks = array();
  42. /**
  43. * The list of system tasks to run during this login.
  44. *
  45. * @see $_tasks
  46. *
  47. * @var array
  48. */
  49. protected $_stasks = array();
  50. /**
  51. * Current task location pointer.
  52. *
  53. * @var integer
  54. */
  55. protected $_ptr = 0;
  56. /**
  57. * Adds a task to the tasklist.
  58. *
  59. * @param Horde_LoginTasks_Task $task The task to execute.
  60. */
  61. public function addTask($task)
  62. {
  63. if ($task instanceof Horde_LoginTasks_SystemTask) {
  64. $this->_stasks[] = $task;
  65. } else {
  66. switch ($task->priority) {
  67. case Horde_LoginTasks::PRIORITY_HIGH:
  68. array_unshift($this->_tasks, $task);
  69. break;
  70. case Horde_LoginTasks::PRIORITY_NORMAL:
  71. $this->_tasks[] = $task;
  72. break;
  73. }
  74. }
  75. }
  76. /**
  77. * Returns the list of tasks to perform.
  78. *
  79. * @param boolean $advance If true, mark ready tasks as completed.
  80. *
  81. * @return array The list of tasks to perform.
  82. */
  83. public function ready($advance = false)
  84. {
  85. $tmp = array();
  86. /* Always loop through system tasks first. */
  87. foreach ($this->_stasks as $key => $val) {
  88. if (!$val->skip()) {
  89. $tmp[] = $val;
  90. unset($this->_stasks[$key]);
  91. }
  92. }
  93. reset($this->_tasks);
  94. while (list($k, $v) = each($this->_tasks)) {
  95. if ($v->needsDisplay() && ($k >= $this->_ptr)) {
  96. break;
  97. }
  98. $tmp[] = $v;
  99. }
  100. if ($advance) {
  101. $this->_tasks = array_slice($this->_tasks, count($tmp));
  102. $this->_ptr = 0;
  103. }
  104. return $tmp;
  105. }
  106. /**
  107. * Returns the next batch of tasks that need display.
  108. *
  109. * @param boolean $advance If true, advance the internal pointer.
  110. *
  111. * @return array The list of tasks to display.
  112. */
  113. public function needDisplay($advance = false)
  114. {
  115. $tmp = array();
  116. $previous = null;
  117. reset($this->_tasks);
  118. while (list(, $v) = each($this->_tasks)) {
  119. if (!$v->needsDisplay() ||
  120. (!is_null($previous) && !$v->joinDisplayWith($previous))) {
  121. break;
  122. }
  123. $tmp[] = $v;
  124. $previous = $v;
  125. }
  126. if ($advance) {
  127. $this->_ptr = count($tmp);
  128. }
  129. return $tmp;
  130. }
  131. /**
  132. * Are all tasks complete?
  133. *
  134. * @return boolean True if all tasks are complete.
  135. */
  136. public function isDone()
  137. {
  138. return (empty($this->_stasks) &&
  139. ($this->_ptr == count($this->_tasks)));
  140. }
  141. }