/** * Get the previous units * * This function returns the previous units, in a flat array. * <br/>Example: * <code> * $units = $content -> getPreviousNodes(); //$units now holds all the previous units of the current unit * $units = $content -> getPreviousNodes(32); //$units now holds all the previous units of unit 32 * </code> * * @param int $queryUnit A unit id, to get its previous units * @return array The previous units array * @since 3.5.0 * @access public * @todo Correct it! */ public function getPreviousNodes($queryUnit = false) { $queryUnit === false ? $unitId = $this->currentUnitId : ($unitId = $queryUnit); //If queryUnit is not specified, use the current unit $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($this->tree), RecursiveIteratorIterator::SELF_FIRST)); //Create iterators for the tree $iterator->rewind(); //Initialize iterator while ($iterator->valid() && $iterator->key() != $unitId) { //Advance iterator until we reach the designated unit $flatTree[] = $this->filterOutChildren($iterator->current()); //Assign units in each loop to the flat array (without children) $iterator->next(); } if ($iterator->valid()) { //We reached the designated unit, so return return $flatTree; } else { if (!isset($flatTree)) { //If iterator value is not valid, and $flatTree is not set, this means that the designated unit was the first, so return empty array return array(); } else { //If $flatTree is set and iterator is invalid, the unit speify did not exist throw new EfrontContentException(_UNITDOESNOTEXIST . ': ' . $unitId, EfrontContentException::UNIT_NOT_EXISTS); } } }
public function getPreviousSiblingNode($queryNode) { $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($this->tree), RecursiveIteratorIterator::SELF_FIRST)); //Create iterators for the tree $iterator->rewind(); //Initialize iterator while ($iterator->valid() && $iterator->key() != $queryNode['parent_content_ID']) { //Advance iterator until we reach the designated node $iterator->next(); } if ($iterator->valid()) { $iterator = new EfrontNodeFilterIterator(new IteratorIterator(new ArrayIterator($iterator->current()))); $iterator->rewind(); //Initialize iterator while ($iterator->valid() && $iterator->key() != $queryNode['id']) { //Advance iterator until we reach the designated node $previousNode = $iterator->current(); $iterator->next(); } if ($iterator->valid()) { if (!isset($previousNode)) { //The designated node was apparently the first one, so return false return false; } else { if (!$queryNode) { $this->currentNodeId = $previousNode['id']; //If a $queryNode was not specified, we must set the internal pointer to the previous node we just found } //$previousNode = $this -> filterOutChildren(new RecursiveArrayIterator($previousNode)); //Cut off node's children return $previousNode; } } } else { throw new EfrontTreeException(_NODEDOESNOTEXIST . ': ' . $queryNode['id'], EfrontTreeException::NODE_NOT_EXISTS); } }