Ejemplo n.º 1
0
 /**
  * Save application form.
  *
  * @return Profile|null the saved model or null if saving fails
  */
 public function save()
 {
     if ($this->validate()) {
         $person = Person::findById($this->id_person);
         if (empty($person)) {
             $person = new Person();
         }
         if (trim($this->parent1_type) != '') {
             $parent1 = Person::findById($person->id_parent1);
             if (empty($parent1)) {
                 $parent1 = new Person();
             }
             $parent1->attributes = $this->getMyAttributes('parent1_');
             $parent1->save();
             $person->id_parent1 = $parent1->id;
         } else {
             $parent1 = Person::findById($person->id_parent1);
             if (!empty($parent1)) {
                 $parent1->delete();
             }
             $person->id_parent1 = null;
         }
         if (trim($this->parent2_type) != '') {
             $parent2 = Person::findById($person->id_parent2);
             if (empty($parent2)) {
                 $parent2 = new Person();
             }
             $parent2->attributes = $this->getMyAttributes('parent2_');
             $parent2->save();
             $person->id_parent2 = $parent2->id;
         } else {
             $parent2 = Person::findById($person->id_parent2);
             if (!empty($parent2)) {
                 $parent2->delete();
             }
             $person->id_parent2 = null;
         }
         $person->attributes = $this->attributes;
         if ($person->save()) {
             $this->id_person = $person->id;
             $profile = Profile::findById($this->id);
             if (empty($profile)) {
                 $profile = new Profile();
             }
             $profile->attributes = $this->attributes;
             $profile->id_person = $person->id;
             $profile->status = $this->status;
             if ($profile->validate()) {
                 $profile->save();
                 $this->id = $profile->id;
                 $specs = Yii::$app->request->post('spec');
                 $SpecArray = array();
                 $i = 0;
                 if (!empty($specs)) {
                     foreach ($specs as $id => $spec) {
                         if ($spec != 'none') {
                             $SpecArray[$i]['id_profile'] = $profile->id;
                             $SpecArray[$i]['id_spec'] = $id;
                             $spec == 'form_fulltime' ? $SpecArray[$i]['form_fulltime'] = 1 : ($SpecArray[$i]['form_fulltime'] = 0);
                             $spec == 'form_extramural' ? $SpecArray[$i]['form_extramural'] = 1 : ($SpecArray[$i]['form_extramural'] = 0);
                             $SpecArray[$i]['priority'] = $i + 1;
                             $i++;
                         }
                     }
                 }
                 Yii::$app->db->createCommand()->delete('profile_spec', 'id_profile=:id', [':id' => $profile->id])->execute();
                 if (!empty($SpecArray)) {
                     Yii::$app->db->createCommand()->batchInsert('profile_spec', ['id_profile', 'id_spec', 'form_fulltime', 'form_extramural', 'priority'], $SpecArray)->execute();
                 }
                 return true;
             } else {
                 return false;
             }
         }
     }
     return false;
 }