public function actionEditField()
 {
     $id = (int) Yii::$app->request->get('id');
     // Get Base Field
     $field = ProfileField::findOne(['id' => $id]);
     if ($field == null) {
         $field = new ProfileField();
     }
     // Get all Available Field Class Instances, also bind current profilefield to the type
     $profileFieldTypes = new BaseType();
     $fieldTypes = $profileFieldTypes->getTypeInstances($field);
     // Build Form Definition
     $definition = array();
     $definition['elements'] = array();
     // Add all sub forms
     $definition['elements'] = array_merge($definition['elements'], $field->getFormDefinition());
     foreach ($fieldTypes as $fieldType) {
         $definition['elements'] = array_merge($definition['elements'], $fieldType->getFormDefinition());
     }
     // Add Form Buttons
     $definition['buttons'] = array('save' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Save'), 'class' => 'btn btn-primary'));
     if (!$field->isNewRecord && !$field->is_system) {
         $definition['buttons']['delete'] = array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Delete'), 'class' => 'btn btn-danger pull-right');
     }
     // Create Form Instance
     $form = new HForm($definition);
     // Add used models to the CForm, so we can validate it
     $form->models['ProfileField'] = $field;
     foreach ($fieldTypes as $fieldType) {
         $form->models[get_class($fieldType)] = $fieldType;
     }
     // Form Submitted?
     if ($form->submitted('save') && $form->validate()) {
         // Use ProfileField Instance from Form with new Values
         $field = $form->models['ProfileField'];
         $fieldType = $form->models[$field->field_type_class];
         if ($field->save() && $fieldType->save()) {
             return $this->redirect(Url::to(['/admin/user-profile']));
         }
     }
     if ($form->submitted('delete')) {
         $field->delete();
         return $this->redirect(Url::to(['/admin/user-profile']));
     }
     return $this->render('editField', array('hForm' => $form, 'field' => $field));
 }
Esempio n. 2
0
 /**
  * Validator which checks the fieldtype
  *
  * Also ensures that field_type_class could not be changed on existing records.
  */
 public function checkType()
 {
     if (!$this->isNewRecord) {
         // Dont allow changes of internal_name - Maybe not the best way to check it.
         $currentProfileField = ProfileField::findOne(['id' => $this->id]);
         if ($this->field_type_class != $currentProfileField->field_type_class) {
             $this->addError('field_type_class', Yii::t('UserModule.models_ProfileField', 'Field Type could not be changed!'));
         }
     } else {
         $profileFieldTypes = new fieldtype\BaseType();
         if (!key_exists($this->field_type_class, $profileFieldTypes->getFieldTypes())) {
             $this->addError('field_type_class', Yii::t('UserModule.models_ProfileField', 'Invalid field type!'));
         }
     }
 }