nestedset.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,963 行 · 第 1/5 页

PHP
1,963
字号
            $this->node_table,            $this->secondarySort);        } elseif ($this->_sortMode == NESE_SORT_PREORDER) {            $nodeSet = array();            $rootnodes = $this->getRootNodes(true);            foreach($rootnodes AS $rid=>$rootnode) {                $nodeSet = $nodeSet+$this->getBranch($rootnode, true);            }            return $nodeSet;        }        if (!$this->_caching) {            $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields);        } else {            $nodeSet = $this->cache->call('DB_NestedSet->_processResultSet', $sql, $keepAsArray, $aliasFields);        }        if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeLoad'])) {            // EVENT (nodeLoad)            foreach (array_keys($nodeSet) as $key) {                $this->triggerEvent('nodeLoad', $nodeSet[$key]);            }        }        return $nodeSet;    }    // }}}    // {{{ getRootNodes()    /**    * Fetches the first level (the rootnodes) of the NestedSet    *    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or an array of nodes    */    function getRootNodes($keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getRootNodes()');        }        $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s.%s %s ORDER BY %s.%s ASC',        $this->_getSelectFields($aliasFields),        $this->_addSQL($addSQL, 'cols'),        $this->node_table,        $this->_addSQL($addSQL, 'join'),        $this->node_table,        $this->_flparams['id'],        $this->node_table,        $this->_flparams['rootid'],        $this->_addSQL($addSQL, 'append'),        $this->node_table,        $this->secondarySort);        if (!$this->_caching) {            $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields);        } else {            $nodeSet = $this->cache->call('DB_NestedSet->_processResultSet', $sql, $keepAsArray, $aliasFields);        }        if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeLoad'])) {            // EVENT (nodeLoad)            foreach (array_keys($nodeSet) as $key) {                $this->triggerEvent('nodeLoad', $nodeSet[$key]);            }        }        return $nodeSet;    }    // }}}    // {{{ getBranch()    /**    * Fetch the whole branch where a given node id is in    *    * @param int  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or an array of nodes    */    function getBranch($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getBranch($id)');        }        if (!($thisnode = $this->pickNode($id, true))) {            $epr = array('getBranch()', $id);            return $this->_raiseError(NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_NOTICE, $epr);        }        if($this->_sortMode == NESE_SORT_LEVEL) {            $firstsort = $this->_flparams['level'];            $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s, %s.%s ASC',            $this->_getSelectFields($aliasFields),            $this->_addSQL($addSQL, 'cols'),            $this->node_table,            $this->_addSQL($addSQL, 'join'),            $this->node_table,            $this->_flparams['rootid'],            $thisnode['rootid'],            $this->_addSQL($addSQL, 'append'),            $this->node_table,            $firstsort,            $this->node_table,            $this->secondarySort);        } elseif($this->_sortMode == NESE_SORT_PREORDER) {            $firstsort = $this->_flparams['l'];            $sql = sprintf('SELECT %s %s FROM %s %s WHERE %s.%s=%s %s ORDER BY %s.%s ASC',            $this->_getSelectFields($aliasFields),            $this->_addSQL($addSQL, 'cols'),            $this->node_table,            $this->_addSQL($addSQL, 'join'),            $this->node_table,            $this->_flparams['rootid'],            $thisnode['rootid'],            $this->_addSQL($addSQL, 'append'),            $this->node_table,            $firstsort);        }        if (!$this->_caching) {            $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields);        } else {            $nodeSet = $this->cache->call('DB_NestedSet->_processResultSet', $sql, $keepAsArray, $aliasFields);        }        if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeLoad'])) {            // EVENT (nodeLoad)            foreach (array_keys($nodeSet) as $key) {                $this->triggerEvent('nodeLoad', $nodeSet[$key]);            }        }        if($this->_sortMode == NESE_SORT_PREORDER && ($this->params[$this->secondarySort] != $this->_defaultSecondarySort)) {            uasort($nodeSet, array($this, '_secSort'));        }        return $nodeSet;    }    // }}}    // {{{ getParents()    /**    * Fetch the parents of a node given by id    *    * @param int  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or an array of nodes    */    function getParents($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getParents($id)');        }        if (!($child = $this->pickNode($id, true))) {            $epr = array('getParents()', $id);            return $this->_raiseError(NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_NOTICE, $epr);        }        $sql = sprintf('SELECT %s %s FROM %s %s                        WHERE %s.%s=%s AND %s.%s<%s AND %s.%s<%s AND %s.%s>%s %s                        ORDER BY %s.%s ASC',        $this->_getSelectFields($aliasFields),        $this->_addSQL($addSQL, 'cols'),        $this->node_table,        $this->_addSQL($addSQL, 'join'),        $this->node_table,        $this->_flparams['rootid'],        $child['rootid'],        $this->node_table,        $this->_flparams['level'],        $child['level'],        $this->node_table,        $this->_flparams['l'],        $child['l'],        $this->node_table,        $this->_flparams['r'],        $child['r'],        $this->_addSQL($addSQL, 'append'),        $this->node_table,        $this->_flparams['level']);        if (!$this->_caching) {            $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields);        } else {            $nodeSet = $this->cache->call('DB_NestedSet->_processResultSet', $sql, $keepAsArray, $aliasFields);        }        if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeLoad'])) {            // EVENT (nodeLoad)            foreach (array_keys($nodeSet) as $key) {                $this->triggerEvent('nodeLoad', $nodeSet[$key]);            }        }        return $nodeSet;    }    // }}}    // {{{ getParent()    /**    * Fetch the immediate parent of a node given by id    *    * @param int  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or the parent node    */    function getParent($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getParent($id)');        }        if (!($child = $this->pickNode($id, true))) {            $epr = array('getParent()', $id);            return $this->_raiseError(NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_NOTICE, $epr);        }        if($child['id'] == $child['rootid']) {            return false;        }        // If parent node is set inside the db simply return it        if(isset($child['parent']) && !empty($child['parent'])) {            return $this->pickNode($child['parent'], $keepAsArray, $aliasFields, 'id', $addSQL);        }        $addSQL['append'] = sprintf('AND %s.%s = %s',        $this->node_table,        $this->_flparams['level'],        $child['level']-1);        $nodeSet =  $this->getParents($id, $keepAsArray, $aliasFields, $addSQL);        if(!empty($nodeSet)) {            $keys = array_keys($nodeSet);            return $nodeSet[$keys[0]];        } else {            return false;        }    }    // }}}    // {{{ getSiblings)    /**    * Fetch all siblings of the node given by id    * Important: The node given by ID will also be returned    * Do a unset($array[$id]) on the result if you don't want that    *    * @param int  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or the parent node    */    function getSiblings($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getParents($id)');        }        if (!($sibling = $this->pickNode($id, true))) {            $epr = array('getSibling()', $id);            return $this->_raiseError(NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_NOTICE, $epr);        }        $parent = $this->getParent($sibling, true);        return $this->getChildren($parent, $keepAsArray, $aliasFields, $addSQL);    }    // }}}    // {{{ getChildren()    /**    * Fetch the children _one level_ after of a node given by id    *    * @param int  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param bool $forceNorder (optional) Force the result to be ordered by the norder    *             param (as opposed to the value of secondary sort).  Used by the move and    *             add methods.    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or an array of nodes    */    function getChildren($id, $keepAsArray = false, $aliasFields = true, $forceNorder = false, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getChildren($id)');        }        if (!($parent = $this->pickNode($id, true))) {            $epr = array('getChildren()', $id);            return $this->_raiseError(NESE_ERROR_NOT_FOUND, PEAR_ERROR_TRIGGER, E_USER_NOTICE, $epr);        }        if (!$parent || $parent['l'] == ($parent['r'] - 1)) {            return false;        }        $sql = sprintf('SELECT %s %s FROM %s %s                        WHERE %s.%s=%s AND %s.%s=%s+1 AND %s.%s BETWEEN %s AND %s %s                        ORDER BY %s.%s ASC',        $this->_getSelectFields($aliasFields),        $this->_addSQL($addSQL, 'cols'),        $this->node_table,        $this->_addSQL($addSQL, 'join'),        $this->node_table,        $this->_flparams['rootid'],        $parent['rootid'],        $this->node_table,        $this->_flparams['level'],        $parent['level'],        $this->node_table,        $this->_flparams['l'],        $parent['l'],        $parent['r'],        $this->_addSQL($addSQL, 'append'),        $this->node_table,        $this->secondarySort);        if (!$this->_caching) {            $nodeSet = $this->_processResultSet($sql, $keepAsArray, $aliasFields);        } else {            $nodeSet = $this->cache->call('DB_NestedSet->_processResultSet', $sql, $keepAsArray, $aliasFields);        }        if (!$this->_skipCallbacks && isset($this->_hasListeners['nodeLoad'])) {            // EVENT (nodeLoad)            foreach (array_keys($nodeSet) as $key) {                $this->triggerEvent('nodeLoad', $nodeSet[$key]);            }        }        return $nodeSet;    }    // }}}    // {{{ getSubBranch()    /**    * Fetch all the children of a node given by id    *    * getChildren only queries the immediate children    * getSubBranch returns all nodes below the given node    *    * @param string  $id The node ID    * @param bool $keepAsArray (optional) Keep the result as an array or transform it into    *             a set of DB_NestedSet_Node objects?    * @param bool $aliasFields (optional) Should we alias the fields so they are the names    *             of the parameter keys, or leave them as is?    * @param array $addSQL (optional) Array of additional params to pass to the query.    *    * @see _addSQL()    * @access public    * @return mixed False on error, or an array of nodes    */    function getSubBranch($id, $keepAsArray = false, $aliasFields = true, $addSQL = array()) {        if ($this->debug) {            $this->_debugMessage('getSubBranch($id)');        }        if (!($parent = $this->pickNode($id, true))) {            $epr = array('getSubBranch()', $id);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?