Ejemplo n.º 1
0
 public function actionCustomEdit()
 {
     $model = (string) filter_input(INPUT_POST, 'model');
     $form = (string) filter_input(INPUT_POST, 'form');
     $delid = (string) filter_input(INPUT_POST, 'delid');
     $values = (string) filter_input(INPUT_POST, 'values');
     if (!class_exists($model) || !(method_exists($model, 'model') && $model::model() instanceof CModel || get_parent_class($model) === 'CFormModel')) {
         throw new CHttpException(500, 'Не существует модель "' . $model . '"');
     }
     if (!method_exists($model, 'GetPermissions')) {
         throw new CHttpException(500, 'Не существует метод "GetPermissions" модели "' . $model . '"');
     }
     parse_str(urldecode($form), $result);
     if (empty($form) && !empty($values)) {
         $result = json_decode($values, true);
     }
     if (count((array) $result) > 0 || $delid !== '') {
         $editid = (string) filter_input(INPUT_POST, 'editid');
         if ($editid !== '') {
             if ($model::model()->GetPermissions('edit')) {
                 $model = $model::model()->findbyPk($editid);
             } else {
                 throw new CHttpException(500, 'Пользователь не имеет доступ на изменение записей модели "' . $model . '"');
             }
         } elseif ($delid !== '') {
             if ($model::model()->GetPermissions('del')) {
                 $model::model()->deleteByPk($delid);
                 exit;
             } else {
                 throw new CHttpException(500, 'Пользователь не имеет доступ на удаление записи модели "' . $model . '"');
             }
         } elseif (method_exists($model, 'model') && $model::model() instanceof CModel && $model::model()->GetPermissions('new') || get_parent_class($model) === 'CFormModel' && $model::GetPermissions('new')) {
             $model = new $model();
         } else {
             throw new CHttpException(500, 'Пользователь не имеет доступ на добавление новой записи в модель "' . $model . '"');
         }
         $model->setScenario((string) filter_input(INPUT_POST, 'scenario'));
         $model->attributes = array_merge($model->attributes, $result[get_class($model)]);
         foreach (array_keys($model->attributes) as $key) {
             if (array_key_exists($key, $result[get_class($model)])) {
                 /* Если формат атрибута datetime */
                 if (($model instanceof CFormModel || $model->getTableSchema()->getColumn($key)->dbType === 'datetime') && $model[$key] !== '') {
                     $model[$key] = Proc::DateTimeFormat($model, $result[get_class($model)][$key]);
                 } else {
                     $model[$key] = $result[get_class($model)][$key];
                 }
             }
         }
         if ($model->validate()) {
             $model->save(false);
             if ($editid === '' && $model instanceof CActiveRecord) {
                 echo $model->getPrimaryKey();
             }
         } else {
             throw new CHttpException(500, json_encode($model->getErrors()));
         }
     } else {
         throw new CHttpException(500, 'Парсинг формы выдал пустой массив');
     }
 }