memory.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,354 行 · 第 1/4 页
PHP
1,354 行
$this->data[$key]['childId'] = $this->children[$key][0]; $this->data[$key]['child'] = &$this->data[ $this->children[$key][0] ]; } $lastParentId = $value['parentId']; } } if( $this->debug ) { $endTime = split(" ",microtime()); $endTime = $endTime[1]+$endTime[0]; print( " building took: ".($endTime - $startTime)." <br>" ); } // build the property 'structure' $this->structure = array(); // empty it, just to be sure everything will be set properly if( $this->debug ) { $startTime = split(" ",microtime()); $startTime = $startTime[1]+$startTime[0]; } // build all the children that are on the root level, if we wouldnt do that // we would have to create a root element with an id 0, but since this is not // read from the db we dont add another element, the user wants to get what he had saved if( sizeof($this->children[0]) ) foreach( $this->children[0] as $rootElement ) { $this->buildStructure( $rootElement , $this->structure ); } if( $this->debug ) { $endTime = split(" ",microtime()); $endTime = $endTime[1]+$endTime[0]; print( " buildStructure took: ".($endTime - $startTime)." <br>" ); } return true; } /** * adds _one_ new element in the tree under the given parent * the values' keys given have to match the db-columns, because the * value gets inserted in the db directly * to add an entire node containing children and so on see 'addNode()' * @see addNode() * @version 2001/10/09 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param array $newValues this array contains the values that shall be inserted in the db-table * @param int the parent id * @param int the prevId * @return mixed either boolean false on failure or the id of the inserted row */ function add( $newValues , $parentId=0 , $prevId=0 ) { // see comments in 'move' and 'remove' if (method_exists($this->dataSourceClass,'add')) { return $this->dataSourceClass->add( $newValues , $parentId , $prevId ); } else { return $this->_throwError( 'method not implemented yet.' , __LINE__ ); } } // end of function /** * removes the given node and all children if removeRecursively is on * * @version 2002/01/24 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param mixed $id the id of the node to be removed * @return boolean true on success */ function remove( $id ) { // if removing recursively is not allowed, which means every child should be removed // then check if this element has a child and return "sorry baby cant remove :-) " if ($this->removeRecursively != true) { if (isset( $this->data[$id]['child'] )) {// TODO raise PEAR warning return $this->_throwError("Element with id=$id has children, cant be removed. Set 'setRemoveRecursively' to true to allow this.",__LINE__); } } // see comment in 'move' // if the prevId is in use we need to update the prevId of the element after the one that // is removed too, to have the prevId of the one that is removed!!! if (method_exists($this->dataSourceClass,'remove')) { return $this->dataSourceClass->remove( $id ); } else { return $this->_throwError( 'method not implemented yet.' , __LINE__ ); } } /** * collects the ID's of the elements to be removed * * @version 2001/10/09 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param mixed $id the id of the node to be removed * @return boolean true on success */ function _remove( $element ) { return $element['id']; } // end of function /** * move an entry under a given parent or behind a given entry. * !!! the 'move behind another element' is only implemented for nested trees now!!! * If a newPrevId is given the newParentId is dismissed! * call it either like this: * $tree->move( x , y ) * to move the element (or entire tree) with the id x * under the element with the id y * or * $tree->move( x , 0 , y ); // ommit the second parameter by setting it to 0 * to move the element (or entire tree) with the id x * behind the element with the id y * or * $tree->move( array(x1,x2,x3) , ... * the first parameter can also be an array of elements that shall be moved * the second and third para can be as described above * * @version 2002/06/08 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param integer the id(s) of the element(s) that shall be moved * @param integer the id of the element which will be the new parent * @param integer if prevId is given the element with the id idToMove * shall be moved _behind_ the element with id=prevId * if it is 0 it will be put at the beginning * @return boolean true for success */ function move( $idsToMove , $newParentId , $newPrevId=0 ) { settype($idsToMove,'array'); $errors = array(); foreach( $idsToMove as $idToMove ) { $ret = $this->_move( $idToMove , $newParentId , $newPrevId ); if( PEAR::isError($ret) ) $errors[] = $ret; }// FIXXME return a Tree_Error, not an array !!!!! if( sizeof($errors) ) return $errors; return true; } /** * this method moves one tree element * * @see move() * @version 2001/10/10 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param integer the id of the element that shall be moved * @param integer the id of the element which will be the new parent * @param integer if prevId is given the element with the id idToMove * shall be moved _behind_ the element with id=prevId * if it is 0 it will be put at the beginning * @return mixed true for success, Tree_Error on failure */ function _move( $idToMove , $newParentId , $prevId=0 ) { if( $idToMove == $newParentId ) // itself can not be a parent of itself// TODO PEAR-ize error return TREE_ERROR_INVALID_PARENT; // check if $newParentId is a child (or a child-child ...) of $idToMove // if so prevent moving, because that is not possible// if( @$this->data[$idToMove]['children'] ) // does this element have children? if( $this->hasChildren($idToMove) ) // does this element have children? {// $allChildren = $this->data[$idToMove]['children']; $allChildren = $this->getChildren($idToMove);// FIXXME what happens here we are changing $allChildren, doesnt this change the// property data too??? since getChildren (might, not yet) return a reference while (list(, $aChild) = each ($allChildren)) // use while since foreach only works on a copy of the data to loop through, but we are changing $allChildren in the loop { array_shift( $allChildren ); // remove the first element because if array_merge is called the array pointer seems to be // set to the beginning and this way the beginning is always the current element, simply work off and truncate in front if( @$aChild['children'] ) { $allChildren = array_merge( $allChildren , $aChild['children'] ); } if( $newParentId == $aChild['id'] )// TODO PEAR-ize error return TREE_ERROR_INVALID_PARENT; } } // what happens if i am using the prevId too, then the db class also // needs to know where the element should be moved to // and it has to change the prevId of the element that will be after it // so we may be simply call some method like 'update' too? if( method_exists($this->dataSourceClass,'move') ) return $this->dataSourceClass->move( $idToMove , $newParentId , $prevId ); else return $this->_throwError( 'method not implemented yet.' , __LINE__ ); } // end of function /** * update data in a node * * @version 2002/01/29 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param array $data the data to update * @return */ function update( $id , $data ) { if (method_exists($this->dataSourceClass,'update')) { return $this->dataSourceClass->update($id,$data); } else { return $this->_throwError( 'method not implemented yet.' , __LINE__ ); } } // end of function // // // from here all methods are not interacting on the 'dataSourceClass' // // /** * builds the structure in the parameter $insertIn * this function works recursively down into depth of the folder structure * it builds an array which goes as deep as the structure goes * * @access public * @version 2001/05/02 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param integer $parentId the parent for which it's structure shall be built * @param integer $insertIn the array where to build the structure in * given as a reference to be sure the substructure is built * in the same array as passed to the function * @return boolean returns always true * */ function buildStructure( $parentId , &$insertIn ) { // create the element, so it exists in the property "structure" // also if there are no children below $insertIn[$parentId] = array(); // set the level, since we are walking through the structure here anyway we // can do this here, instead of up in the setup method :-) // always set the level to one higher than the parent's level, easy ha? if (isset($this->data[$parentId]['parent']['level'])) { // this applies only to the root element(s) $this->data[$parentId]['level'] = $this->data[$parentId]['parent']['level']+1; if ($this->data[$parentId]['level']>$this->_treeDepth) { $this->_treeDepth = $this->data[$parentId]['level']; } } else { $this->data[$parentId]['level'] = 0; // set first level number to 0 } if (isset($this->children[$parentId]) && sizeof($this->children[$parentId])) { // go thru all the folders foreach ($this->children[$parentId] as $child) { // build the structure under this folder, // use the current folder as the new parent and call build recursively // to build all the children // by calling build with $insertIn[someindex] the array is filled // since the array was empty before $this->buildStructure( $child , $insertIn[$parentId] ); } } return true; } // end of function /** * this method only serves to call the _walk method and reset $this->walkReturn * that will be returned by all the walk-steps * * @version 2001/11/25 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param mixed $walkFunction the name of the function to call for each walk step, * or an array for a method, where * [0] is the method name and [1] the object * @param array $id the id to start walking through the tree, everything below is walked through * @param string $returnType the return of all the walk data will be of the given type (values: string, array) * @return mixed this is all the return data collected from all the walk-steps * */ function walk( $walkFunction , $id=0 , $returnType='string') { $useNode = $this->structure; // by default all of structure is used if ($id == 0) { $keys = array_keys($this->structure); $id = $keys[0]; } else { $path = $this->getPath($id); // get the path, to be able to go to the element in this->structure array_pop($path); // pop off the last element, since it is the one requested $curNode = $this->structure; // start at the root of structure foreach ($path as $node) { $curNode = $curNode[$node['id']]; // go as deep into structure as path defines } $useNode = array(); // empty it first, so we dont have the other stuff in there from before $useNode[$id] = $curNode[$id]; // copy only the branch of the tree that the parameter $id requested } unset($this->walkReturn); // a new walk starts, unset the return value return $this->_walk( $walkFunction , $useNode , $returnType ); } /** * walks through the entire tree and returns the current element and the level * so a user can use this to build a treemap or whatever * * @version 2001/06/xx * @access private * @author Wolfram Kriesing <wolfram@kriesing.de> * @param mixed $walkFunction the name of the function to call for each walk step, * or an array for a method, where * [0] is the method name and [1] the object * @param array $curLevel the reference in the this->structure, to walk everything below * @param string $returnType the return of all the walk data will be of the given type (values: string, array, ifArray) * @return mixed this is all the return data collected from all the walk-steps *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?