/** * Validate function for the projectId field. * * @param integer $value Value of the projectId to check. * * @return string Error msg. */ public function validateProjectId($value) { if (null !== $this->id && $this->id > 0) { $node = Phprojekt_Loader::getModel('Project', 'Project')->find($this->id); $tree = new Phprojekt_Tree_Node_Database($node, $this->id); if ($tree->setup()->getActiveRecord()->id == $value) { return Phprojekt::getInstance()->translate('The project can not be saved under itself'); } else { if ($this->_isInTheProject($value, $tree)) { return Phprojekt::getInstance()->translate('The project can not be saved under its children'); } } } return null; }
public function indexAction() { $projectId = (int) $this->getRequest()->getParam('projectId', 0); $range = $this->getRequest()->getHeader('range'); sscanf($range, 'items=%d-%d', $start, $end); $count = $end - $start + 1; $sort = $this->getRequest()->getParam('sort', null); $recursive = $this->getRequest()->getParam('recursive', 'false'); $recursive = $recursive === 'true'; $model = $this->newModelObject(); $moduleId = Phprojekt_Module::getId($this->getRequest()->getModuleName()); $isGlobal = Phprojekt_Module::saveTypeIsGlobal($moduleId); if (empty($projectId) && !$isGlobal) { throw new Zend_Controller_Action_Exception('projectId not given for non-global module', 422); } else { if (!empty($projectId) && $isGlobal) { throw new Zend_Controller_Action_Exception('projectId given for global module', 422); } } $recursive = $isGlobal ? false : $recursive; $records = array(); $recordCount = 0; if ($recursive) { $tree = new Phprojekt_Tree_Node_Database(new Project_Models_Project(), $projectId); $tree->setup(); $where = $this->getFilterWhere(); $records = $tree->getRecordsFor($model, $count, $start, $where, $sort); $recordCount = $tree->getRecordsCount($model, $where); } else { if (!empty($projectId) && $model->hasField('projectId')) { $where = Phprojekt::getInstance()->getDb()->quoteInto('project_id = ?', (int) $projectId); } else { $where = null; } $where = $this->getFilterWhere($where); $records = $model->fetchAll($where, $sort, $count, $start); $recordCount = $model->count($where); } $end = min($end, $recordCount); $this->getResponse()->setHeader('Content-Range', "items {$start}-{$end}/{$recordCount}"); Phprojekt_CompressedSender::send(Zend_Json::encode(Phprojekt_Model_Converter::convertModels($records))); }
/** * Returns the project tree. * * The return is a tree compatible format, with identifier, label, * and the list of items, each one with the name, id, parent, path and children´s id. * * The tree is stored as a file until a user add, edit or delete a project * (tmp/ZendCache/zend_cache---Phprojekt_Tree_Node_Database_setup). * * The return is in JSON format. * * @return void */ public function jsonTreeAction() { $model = Phprojekt_Loader::getModel('Project', 'Project'); $tree = new Phprojekt_Tree_Node_Database($model, 1); Phprojekt_Converter_Json::echoConvert($tree->setup()); }
/** * Returns the list of items for one model. * * The return have: * - The metadata of each field. * - The data of all the rows. * - The number of rows. * * The function use Phprojekt_ModelInformation_Default::ORDERING_LIST for get and sort the fields. * * OPTIONAL request parameters: * <pre> * - integer <b>id</b> List only this id. * - integer <b>nodeId</b> List all the items with projectId == nodeId. * - integer <b>count</b> Use for SQL LIMIT count. * - integer <b>offset</b> Use for SQL LIMIT offset. * - boolean <b>recursive</b> Include items of subprojects. * </pre> * * The return is in JSON format. * * @return void */ public function jsonListAction() { $itemId = (int) $this->getRequest()->getParam('id', null); $projectId = (int) $this->getRequest()->getParam('nodeId', null); $count = (int) $this->getRequest()->getParam('count', null); $offset = (int) $this->getRequest()->getParam('start', null); $recursive = $this->getRequest()->getParam('recursive', 'false'); $this->setCurrentProjectId(); if (!empty($itemId)) { $where = sprintf('id = %d', (int) $itemId); } else { if (!empty($projectId) && $this->getModelObject()->hasField('projectId')) { $where = sprintf('project_id = %d', (int) $projectId); } else { $where = null; } } /* recursive is only supported if nodeId is specified */ if (!empty($projectId) && $this->getModelObject()->hasField('projectId') && 'true' === $recursive) { $tree = new Phprojekt_Tree_Node_Database(new Project_Models_Project(), $projectId); $tree->setup(); Phprojekt_Converter_Json::echoConvert($tree->getRecordsFor($this->getModelObject(), null, null, $this->getFilterWhere()), Phprojekt_ModelInformation_Default::ORDERING_LIST); } else { $where = $this->getFilterWhere($where); $records = $this->getModelObject()->fetchAll($where, null, $count, $offset); Phprojekt_Converter_Json::echoConvert($records, Phprojekt_ModelInformation_Default::ORDERING_LIST); } }
/** * Returns the tree node for this project. * * The node allows navigating to subprojects and other operations on the project tree. * * @return Phprojekt_Tree_Node_Database A Tree node belonging to this project. */ public function getTree() { $tree = new Phprojekt_Tree_Node_Database($this); $tree->setup(); return $tree; }
/** * 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); }