IMP_Ftree::delete PHP Method

delete() public method

Delete an element from the tree.
public delete ( $id )
    public function delete($id)
    {
        if (is_array($id)) {
            /* We want to delete from the TOP of the tree down to ensure that
             * parents have an accurate view of what children are left. */
            $this->sortList($id);
            $id = array_reverse($id);
        } else {
            $id = array($id);
        }
        foreach (array_filter(array_map(array($this, 'offsetGet'), $id)) as $elt) {
            $account = $this->getAccount($elt);
            if (!($mask = $account->delete($elt))) {
                continue;
            }
            $this->_changed = true;
            if ($mask & IMP_Ftree_Account::DELETE_RECURSIVE) {
                foreach (array_map('strval', iterator_to_array(new IMP_Ftree_Iterator($elt), false)) as $val) {
                    unset($this->_elts[$val], $this->_parent[$val]);
                    $this->eltdiff->delete($val);
                }
                unset($this->_parent[strval($elt)]);
            }
            if (strval($account) == strval($elt)) {
                unset($this->_accounts[strval($elt)]);
            }
            if ($mask & IMP_Ftree_Account::DELETE_ELEMENT) {
                /* Do not delete from tree if there are child elements -
                 * instead, convert to a container element. */
                if ($elt->children) {
                    $elt->container = true;
                    continue;
                }
                /* Remove the mailbox from the expanded folders list. */
                unset($this->expanded[$elt]);
                /* Remove the mailbox from the polled list. */
                $this->poll->removePollList($elt);
            }
            $parent = strval($elt->parent);
            $this->eltdiff->delete($elt);
            /* Delete the entry from the parent tree. */
            unset($this->_elts[strval($elt)], $this->_parent[$parent][array_search(strval($elt), $this->_parent[$parent], true)]);
            if (empty($this->_parent[$parent])) {
                /* This mailbox is now completely empty (no children). */
                unset($this->_parent[$parent]);
                if ($p_elt = $this[$parent]) {
                    if ($p_elt->container && !$p_elt->namespace) {
                        $this->delete($p_elt);
                    } else {
                        $p_elt->open = false;
                        $this->eltdiff->change($p_elt);
                    }
                }
            }
            if (!empty($this->_parent[$parent])) {
                $this->_parent[$parent] = array_values($this->_parent[$parent]);
            }
        }
    }