/** * Remove unit * * This function is used to remove a unit from the content tree * <br/>Example: * <code> * $content = new EfrontContentTree(4); //Initialize content tree for lesson with id 4 * $content -> removeNode(57); //Remove the unit 57 and all of its subunits * </code> * * @param int $removeId The unit id that will be removed * @since 3.5.0 * @access public */ public function removeNode($removeId) { $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator($this->tree, RecursiveIteratorIterator::SELF_FIRST)); //Get an iterator for the current tree. This iterator returns only whole unit arrays and not unit members separately (such as id, timestamp etc) $iterator->rewind(); //Initialize iterator while ($iterator->valid() && $iterator->key() != $removeId) { //Forward iterator index until you reach the designated element, which has an index equal to the unit id that will be removed $iterator->next(); } if ($iterator->valid()) { $iterator->current()->delete(); //Delete the current unit from the database $previousUnit = $this->getPreviousNode($iterator->key()); //Get the deleted unit's previous unit $iterator->offsetUnset($removeId); //Delete the unit from the content tree if ($previousUnit) { //If we are deleting the first unit, there is no previous unit $nextUnit = $this->getNextNode($previousUnit['id']); //Get the previous unit's next unit, which still points to the old unit if ($nextUnit) { //If we are deleting the last unit, there is not next unit $nextUnit['previous_content_ID'] = $previousUnit['id']; //Update the next unit to point at the deleted unit' previous unit $nextUnit->persist(); //Persist these changes to the database } } else { $firstUnit = $this->getFirstNode(); //If we deleted the first unit, then we need to set the new first unit to have 0 as previous unit if ($firstUnit) { //...Unless the deleted unit was the last content unit $firstUnit['previous_content_ID'] = 0; $firstUnit->persist(); //Persist these changes to the database } } } else { throw new EfrontContentException(_UNITDOESNOTEXIST . ': ' . $removeId, EfrontContentException::UNIT_NOT_EXISTS); } }