Ejemplo n.º 1
0
 /**
  * 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)
 {
     foreach ($params as $k => $v) {
         if (isset($model->{$k})) {
             // Don't allow to set the id on save, since it is done by the ActiveRecord
             if (!in_array($k, array('id'))) {
                 $model->{$k} = $v;
             }
         }
     }
     if (empty($model->id)) {
         $newItem = true;
     } else {
         $newItem = false;
     }
     // Set the owner
     if ($newItem && isset($model->ownerId)) {
         $model->ownerId = Phprojekt_Auth::getUserId();
     }
     // Parent Project
     if (isset($model->projectId)) {
         $projectId = $model->projectId;
     } else {
         $projectId = 0;
     }
     // Checks
     $moduleName = Phprojekt_Loader::getModuleFromObject($model);
     $moduleId = Phprojekt_Module::getId($moduleName);
     if (!$model->recordValidate()) {
         $errors = $model->getError();
         $error = array_pop($errors);
         throw new Phprojekt_PublishedException($error['label'] . ': ' . $error['message']);
     } else {
         if (!self::_checkModule($moduleId, $projectId)) {
             throw new Phprojekt_PublishedException('The parent project do not have enabled this module');
         } else {
             if (!self::_checkItemRights($model, $moduleName)) {
                 throw new Phprojekt_PublishedException('You do not have access to do this action');
             } else {
                 // Set the projectId to 1 for global modules
                 if (isset($model->projectId) && Phprojekt_Module::saveTypeIsGlobal($moduleId)) {
                     $model->projectId = 1;
                 }
                 $model->save();
                 // Save access only if the user have "admin" right
                 $itemRights = Phprojekt_Loader::getLibraryClass('Phprojekt_Item_Rights');
                 $check = $itemRights->getRights($moduleId, $model->id);
                 if ($check['currentUser']['admin']) {
                     if ($moduleName == 'Core') {
                         $rights = Default_Helpers_Right::getModuleRights($params);
                     } else {
                         $rights = Default_Helpers_Right::getItemRights($params, $moduleId, $newItem);
                     }
                     if (count($rights) > 0) {
                         $model->saveRights($rights);
                     }
                 }
                 return $model;
             }
         }
     }
 }