/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionNew()
 {
     if (isset($_GET['form'])) {
         $form_id = $_GET['form'];
         $model = new FormField();
         // Uncomment the following line if AJAX validation is needed
         // $this->performAjaxValidation($model);
         if (isset($_POST['FormField'])) {
             $model->attributes = $_POST['FormField'];
             $model->VARNAME = str_replace(' ', '_', $model->TITLE);
             $model->VARNAME = strtoupper($model->VARNAME);
             $model->FORM_ID = $form_id;
             //if($model->save())
             //	$this->redirect(array('view','field'=>$model->FIELD_ID));
             $form = Form::model()->findByPk($form_id);
             if ($model->FIELD_TYPE == 'DATE') {
                 $column_t = 'TIMESTAMP ';
                 $column_t .= 'DEFAULT "0000-00-00 00:00:00"';
             } elseif ($model->FIELD_TYPE == 'INTEGER') {
                 //$column_t='INT('.$model->FIELD_SIZE.') NOT NULL DEFAULT'.($model->DEFAULT)?$model->DEFAULT:$model->DEFAULT;
                 $column_t = 'INT(' . $model->FIELD_SIZE . ') ';
                 if (is_int($model->DEFAULT)) {
                     $column_t .= 'DEFAULT ' . $model->DEFAULT;
                 } else {
                     $model->DEFAULT = '';
                 }
             } elseif ($model->FIELD_TYPE == 'VARCHAR') {
                 $column_t = 'VARCHAR(' . $model->FIELD_SIZE . ') ';
                 if ($model->DEFAULT) {
                     $column_t .= 'DEFAULT ' . $model->DEFAULT;
                 } else {
                     $model->DEFAULT = '';
                 }
             } else {
                 $column_t = $model->FIELD_TYPE . ' ';
                 if ($model->DEFAULT) {
                     $column_t .= 'DEFAULT ' . $model->DEFAULT;
                 } else {
                     $model->DEFAULT = '';
                 }
             }
             if (!FormField::model()->findAllByAttributes(array('VARNAME' => $model->VARNAME, 'FORM_ID' => $form_id))) {
                 if ($model->save()) {
                     Yii::app()->db->createCommand()->addColumn($form->TABLE_NAME, $model->VARNAME, $column_t);
                     $this->redirect(array('fields/index', 'form' => $model->FORM_ID));
                 }
             } else {
                 $model->addError('VARNAME', 'Column "' . $model->VARNAME . '" already exists. Please pick a new column name.');
             }
         }
         $this->render('new', array('model' => $model, 'form_id' => $form_id));
     } else {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
 }
Ejemplo n.º 2
0
 /**
  * Actualiza los campos del formulario
  * @param $fieldsConfig
  */
 public function updateFormFields($fieldsConfig)
 {
     DB::beginTransaction();
     try {
         $this->fields()->delete();
         foreach ($fieldsConfig as $fieldConfig) {
             $field = new FormField();
             $field->form_id = $this->id;
             $field->name = array_get($fieldConfig, 'name', '');
             $field->alias = array_get($fieldConfig, 'alias', '');
             $field->type = array_get($fieldConfig, 'type.type', '');
             $field->config = array_get($fieldConfig, 'config', null);
             $field->save();
         }
     } catch (\Exception $e) {
         DB::rollBack();
         Log::error($e->getMessage());
     }
     DB::commit();
 }