/** * @desc make exist- and recursioncheck before moving Items */ public function checkRecursion() { $model = new AuthItemChild(); // climb up moveTo tree and search for moveFrom Item. $source = $this->moveFrom; $target = $this->moveTo; //quick precheck if moving items are nowhere in the Tree if (!$model->findByAttributes(array('child' => $source)) && !$model->findByAttributes(array('parent' => $source))) { $this->rekursionCheck = true; return true; } /* * this is a bit tricky, cause we can not search by child in top level * so we have to switch searching one last time by parent if no more items are found with searching by child */ $column = 'child'; $currentItem = $target; $depth = 0; $depthLimit = $this->rekursionCheckDepthLimit; while (true) { $result = $model->findAllByAttributes(array("{$column}" => $currentItem)); if (!count($result) && $column == 'parent') { // nothing found in child column // this switch should never entered with $column == 'parent' $this->rekursionCheck = true; return true; } elseif (!count($result)) { // top Level Items can't be detected via child, so we make last search again in column parent $column = 'parent'; continue; } // check parents if they are same with source foreach ($result as $row) { if ($row->parent == $source) { $this->rekursionCheck = false; return false; } } // last top level Item was checked if ($column == 'parent') { $this->rekursionCheck = true; return true; } $currentItem = $row->parent; if ($depth++ >= $depthLimit) { throw new CHttpException("CheckRecursion Error: Depthlimit of {$depthLimit} parents executed"); } } return $this->rekursionCheck; }