/** * Try to receive a tree/subtree from the database using the * active record object to get the name of the tree table. * If the requested id is not set using the constructor this method * usually fails throwing an exception. * * @param Phprojekt_Filter_Interface $filter A filter to chain. * * @throws Phprojekt_Tree_Node_Exception If no id was requested (see constructor) * * @return Phprojekt_Tree_Node_Database An instance of Phprojekt_Tree_Node_Database. */ public function setup(Phprojekt_Filter_Abstract $filter = null) { // @todo: fix this, must be possible with requestid === null if (null === $this->_requestedId) { throw new Phprojekt_Tree_Node_Exception('You have to set a requested treeid in the constructor'); } $cache = Phprojekt::getInstance()->getCache(); if ($this->_requestedId == 0) { return $this; } else { if ($this->_requestedId > 1) { if (!($object = $cache->load(self::CACHE_NAME))) { $tree = new Phprojekt_Tree_Node_Database($this->_activeRecord, 1); $object = $tree->setup(); } $tree = $object->getNodeById($this->_requestedId); if (null === $tree) { throw new Phprojekt_Tree_Node_Exception('Requested node not found'); } $tree->_parentNode = null; return $this->applyRights($tree); } else { if (!($object = $cache->load(self::CACHE_NAME))) { $database = $this->getActiveRecord()->getAdapter(); $table = $this->getActiveRecord()->getTableName(); $select = $database->select(); $select->from($table, 'path')->where(sprintf('id = %d', (int) $this->_requestedId))->limit(1); if (null !== $filter) { $filter->filter($select, $this->getActiveRecord()->getAdapter()); } $rootPath = $database->fetchOne($select); if (null === $rootPath) { throw new Phprojekt_Tree_Node_Exception('Requested node not found'); } // Get all the projects $where = sprintf("(%s OR id = %d)", $database->quoteInto("path LIKE ?", $rootPath . '%'), (int) $this->id); $select = $database->select(); $select->from($table)->where($where)->order('path'); $treeData = $select->query()->fetchAll(Zend_Db::FETCH_CLASS); foreach ($treeData as $index => $record) { foreach ($record as $key => $value) { $newKey = Phprojekt_ActiveRecord_Abstract::convertVarFromSql($key); $treeData[$index]->{$newKey} = $value; } } foreach ($treeData as $record) { $node = null; if ($record->id == $this->_requestedId) { $node = $this; $this->_activeRecord = $record; } elseif (array_key_exists($record->projectId, $this->_index)) { $node = new Phprojekt_Tree_Node_Database($record); $this->_index[$record->projectId]->appendNode($node); } if (null !== $node) { $this->_index[$node->id] = $node; } } $object = $this; $cache->save($this, self::CACHE_NAME); // Delete the session for re-calculate the rights $sessionName = 'Phprojekt_Tree_Node_Database-applyRights'; $rightsNamespace = new Zend_Session_Namespace($sessionName); $rightsNamespace->unsetAll(); } } } return $this->applyRights($object); }
/** * Try to receive a tree/subtree from the database using the * active record object to get the name of the tree table. * If the requested id is not set using the constructor this method * usually fails throwing an exception. * * @param Phprojekt_Filter_Interface $filter A filter to chain. * * @throws Phprojekt_Tree_Node_Exception If no id was requested (see constructor) * * @return Phprojekt_Tree_Node_Database An instance of Phprojekt_Tree_Node_Database. */ public function setup(Phprojekt_Filter_Abstract $filter = null) { // @todo: fix this, must be possible with requestid === null if (null === $this->_requestedId) { throw new Phprojekt_Tree_Node_Exception('You have to set a requested treeid in the constructor'); } if ($this->_requestedId == 0) { return $this; } else { $database = $this->getActiveRecord()->getAdapter(); $table = $this->getActiveRecord()->getTableName(); $select = $database->select(); $select->from(array('t' => $table), array())->join(array('tt' => $table), sprintf('t.id = %d AND (tt.path like CONCAT(t.path, t.id, "/%%") OR tt.id = t.id)', (int) $this->_requestedId), '*')->order('path')->order('id'); if (null !== $filter) { $filter->filter($select, 'tt'); } $treeData = $select->query()->fetchAll(Zend_Db::FETCH_CLASS); foreach ($treeData as $index => $record) { foreach ($record as $key => $value) { $newKey = Phprojekt_ActiveRecord_Abstract::convertVarFromSql($key); $treeData[$index]->{$newKey} = $value; } } foreach ($treeData as $record) { $node = null; if ($record->id == $this->_requestedId) { $node = $this; $this->_activeRecord = $record; } elseif (array_key_exists($record->projectId, $this->_index)) { $node = new Phprojekt_Tree_Node_Database($record); $this->_index[$record->projectId]->appendNode($node); } if (null !== $node) { $this->_index[$node->id] = $node; } } $object = $this; } return $this->applyRights($object); }