Exemplo n.º 1
0
 /**
  * [update description]
  * @return [type] [description]
  */
 public function update($id = null)
 {
     if (!$id) {
         $id = $this->auth_user_id;
     }
     //Validations
     $this->form_validation->set_rules('name', 'Name', 'required');
     $this->form_validation->set_rules('dob', 'Date of Birth', 'required');
     $this->form_validation->set_rules('skills[]', 'Skills', 'required');
     if (!$this->form_validation->run()) {
         $this->session->set_flashdata('error', validation_errors());
     } else {
         $profile = $this->doctrine->em->getRepository('Entity\\Profiles')->findOneBy(array('user' => $id));
         $actingUser = $this->doctrine->em->getReference('Entity\\Users', $this->auth_user_id);
         if (!$profile) {
             //Add a new profile for user
             $profile = new Entity\Profiles();
             $profile->setUser($actingUser);
         }
         $now = date_create(date('Y-m-d H:i:s'));
         $profile->setName(preg_replace('/\\_/', '', $this->input->post('name')));
         $profile->setDob(date_create(date('Y-m-d H:i:s', strtotime($this->input->post('dob')))));
         $profile->setContactNumber($this->input->post('contact'));
         $profile->setAddress($this->input->post('address'));
         $profile->setCreatedOn($now);
         $this->doctrine->em->persist($profile);
         //Add Skills
         if ($skills = $this->input->post("skills")) {
             foreach ($skills as $skill) {
                 if (!is_numeric($skill)) {
                     //Save new tags
                     $terms = new Entity\Terms();
                     $terms->setTitle($skill);
                     $terms->setStatus(1);
                     $terms->setDescription('');
                     $category = $this->doctrine->em->getReference('Entity\\Categories', 8);
                     $weight = $this->user_model->getMaxWeight($category->getId());
                     $terms->setWeight($weight + 1);
                     $terms->setCategory($category);
                     $terms->setCreatedBy($actingUser);
                     $terms->setCreatedOn(date_create(date('Y-m-d H:i:s')));
                     $this->doctrine->em->persist($terms);
                 } else {
                     $terms = $this->doctrine->em->getRepository('Entity\\Terms')->find($skill);
                 }
                 $exist = $this->doctrine->em->getRepository('Entity\\UserSkills')->findBy(array('term' => $terms->getId(), 'user' => $id));
                 if (!$exist) {
                     //Add Terms in user skills
                     $userSkills = new Entity\UserSkills();
                     $userSkills->setTerm($terms);
                     $userSkills->setUser($actingUser);
                     $this->doctrine->em->persist($userSkills);
                 }
             }
         }
         $this->doctrine->em->flush();
     }
     redirect('user/profile#profile');
 }