示例#1
0
 /**
  * actionAddOption
  * 
  * @access public
  * @return void
  */
 public function actionAddOption($id)
 {
     Yii::log("actionIndex OptionsController called", "trace", self::LOG_CAT);
     $model = new Options();
     $model->setCustomScenario($id);
     if (isset($_POST['Options'])) {
         $model->attributes = $_POST['Options'];
         //print_r($model); die;
         if ($model->save()) {
             Yii::app()->user->setFlash('success', "Option successfully added.");
             $this->redirect(['options/index/id/' . $id]);
         }
         if (!$model->hasErrors()) {
             Yii::app()->user->setFlash('error', "An error occurred while adding the option, please contact your administrator.");
         }
     }
     // add options via ajax from chosen plugin
     //		if(isset($_POST['options'])) {
     //			$model->setScenario($_POST['options']['scenario']);
     //			unset($_POST['options']['scenario']);
     //			$model->attributes = $_POST['options'];
     //			if($model->save()) {
     //				echo json_encode(ModelToArray::convertModelToArray($model, [$model->tableName() => 'optionId, label']));
     //				return;
     //			}
     //			$model->unsetAttributes();
     //			//print_r($model->attributeNames()); die;
     //			echo json_encode(['optionId' => '']);
     //			return;
     //
     //		}
     //		echo get_class($this->optionsMask['formFieldsModels'][$id]);
     // Select all values whose inputType is ""Select"
     $formElementsCriteria = new CDbCriteria();
     $formElementsCriteria->condition = "t.inputType='dropdownlist'";
     if ($this->optionsMask['formFieldsModels'][$id] == 'FrameworkFields') {
         //			$formElementsCriteria->join = 'LEFT JOIN parentLabel AS frameworkFields ON parentLabel.id = t.parentId';
         $formElementsCriteria->with = 'parent';
     }
     //$formElementsModel = new {$this->optionsMask['formFieldsModels'][$id]};
     $fetchOptions = CActiveRecord::model($this->optionsMask['formFieldsModels'][$id])->findAll($formElementsCriteria);
     //print_r($fetchOptions); die;
     $formElements = CHtml::listData($fetchOptions, $this->optionsMask['joinLabels'][$id][0], function ($option) use($id) {
         if (isset($option->parent)) {
             return $option->parent->label . ' - ' . $option->{$this->optionsMask['joinLabels'][$id][1]};
         }
         return $option->{$this->optionsMask['joinLabels'][$id][1]};
     });
     //print_r($formElements); die;
     $this->render('add', ['model' => $model, 'dropDownAttribute' => $this->optionsMask['relationNames'][$id] . 'Id', 'formElements' => $formElements]);
 }