| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJT_Models_Block_Assignmentpanel_Helpers_Hierarchical { |
| 10 |
|
| 11 |
/** |
| 12 |
* put your comment there... |
| 13 |
* |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
protected $allItems = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @var mixed |
| 22 |
*/ |
| 23 |
protected $iPerPage = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @var mixed |
| 29 |
*/ |
| 30 |
protected $offset = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* put your comment there... |
| 34 |
* |
| 35 |
* @param mixed $offset |
| 36 |
* @param mixed $iPerPage |
| 37 |
* @param mixed $items |
| 38 |
* @return CJT_Models_Block_Assignmentpanel_Helpers_Hierarchical |
| 39 |
*/ |
| 40 |
public function __construct($offset, $iPerPage, $allItems) { |
| 41 |
// Initialize. |
| 42 |
$this->offset = $offset; |
| 43 |
$this->iPerPage = $iPerPage; |
| 44 |
$this->allItems = $allItems; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* put your comment there... |
| 49 |
* |
| 50 |
*/ |
| 51 |
public function getIPerPage() { |
| 52 |
return $this->iPerPage; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* put your comment there... |
| 57 |
* |
| 58 |
*/ |
| 59 |
public function getItems() { |
| 60 |
// Initialize. |
| 61 |
$allItems =& $this->allItems; |
| 62 |
$hierarchicalItems = array(); |
| 63 |
$groupedItems = array(); |
| 64 |
$offset = $this->getOffset(); |
| 65 |
$endOffset = $this->getOffset() + $this->getIPerPage(); |
| 66 |
// Group all items under parent item(s) ID. |
| 67 |
foreach ($allItems as $item) { |
| 68 |
$groupedItems[((int) $item['parent'])][$item['id']] = $item; |
| 69 |
} |
| 70 |
/// Non-Recursive loop for getting |
| 71 |
/// FLAT list for all items with childs items |
| 72 |
/// underneath them |
| 73 |
// Working pointers has the current in process |
| 74 |
// items. Initialize to start with root |
| 75 |
// items with parent = 0 (array(0)). |
| 76 |
$workingPointers = array(0); |
| 77 |
do { |
| 78 |
// Current pointer is the last one. |
| 79 |
$pointer = end($workingPointers); |
| 80 |
// Get current working group. |
| 81 |
$group =& $groupedItems[$pointer]; |
| 82 |
// Shift first item from group. |
| 83 |
$item = array_shift($group); |
| 84 |
// Add item to the list. |
| 85 |
$hierarchicalItems[$item['id']] = $item; |
| 86 |
// Remove current group from the pointers |
| 87 |
// list if all items has been processed. |
| 88 |
if (empty($group)) { |
| 89 |
array_pop($workingPointers); |
| 90 |
} |
| 91 |
// If the current items has childs then |
| 92 |
// add it to the working pointer to be processed |
| 93 |
// in the immediate iteration. |
| 94 |
if (isset($groupedItems[$item['id']])) { |
| 95 |
$workingPointers[] = $item['id']; |
| 96 |
} |
| 97 |
// If there is no other group to process then exit. |
| 98 |
} while(!empty($workingPointers) && (count($hierarchicalItems) != $endOffset)); |
| 99 |
// Get ony items start from the requested offset. |
| 100 |
$items = array_slice($hierarchicalItems, $offset, $this->getIPerPage()); |
| 101 |
// Returns full-tree. |
| 102 |
return $items; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* put your comment there... |
| 107 |
* |
| 108 |
*/ |
| 109 |
public function getOffset() { |
| 110 |
return $this->offset; |
| 111 |
} |
| 112 |
|
| 113 |
} // End class. |
| 114 |
|