/** * Help to save a model by setting the models properties. * Validation is based on the ModelInformation implementation. * * @param Phprojekt_Model_Interface $model The model * @param array $params The parameters used to feed the model. * * @throws Exception If validation of parameters fails. * * @return boolean True for a sucessful save. */ protected static function _saveModel(Phprojekt_Model_Interface $model, array $params) { $newItem = empty($params['id']); $model = self::parameterToModel($model, $params, $newItem); $projectId = $model->hasField('projectId') ? $model->projectId : 0; $userId = Phprojekt_Auth_Proxy::getEffectiveUserId(); $moduleName = Phprojekt_Loader::getModuleFromObject($model); $moduleId = Phprojekt_Module::getId($moduleName); if (!$model->recordValidate()) { $errors = $model->getError(); $error = array_pop($errors); throw new Zend_Controller_Action_Exception($error['label'] . ': ' . $error['message'], 400); } if (!self::_checkModule($moduleId, $projectId)) { throw new Zend_Controller_Action_Exception('The parent project do not have enabled this module', 400); } $rights = Default_Helpers_Right::getRights($params); if ($model instanceof Phprojekt_Item_Abstract) { if ($newItem && !Phprojekt_Module::saveTypeIsGlobal($moduleId)) { $project = new Project_Models_Project(); $project->find($projectId); if (!$project->hasRight($userId, Phprojekt_Acl::CREATE)) { throw new Zend_Controller_Action_Exception('You do not have the necessary create right', 403); } $rights[$userId] = Phprojekt_Acl::ALL; } else { if (!$model->hasRight($userId, Phprojekt_Acl::WRITE)) { throw new Zend_Controller_Action_Exception('You do not have the necessary write right', 403); } } // Set the projectId to 1 for global modules // @TODO Remove the Timecard limitation if ($model->hasField('projectId') && Phprojekt_Module::saveTypeIsGlobal($moduleId) && Phprojekt_Module::getModuleName($moduleId) != 'Timecard') { $model->projectId = 1; } $model->save(); // Save access only if the user have "admin" right if ($newItem || $model->hasRight(Phprojekt_Auth_Proxy::getEffectiveUserId(), Phprojekt_Acl::ADMIN)) { if (!Phprojekt_Auth_Proxy::isAdminUser() && count($rights) <= 0) { throw new Zend_Controller_Action_Exception('At least one person must have access to this item', 400); } $model->saveRights($rights); } } else { $model->save(); $model->saveRights($rights); } return $model; }