/**
  * [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');
 }
 /**
  * [add description]
  */
 public function add()
 {
     $this->is_logged_in();
     if (!$this->require_min_level(9)) {
         show_error("You are not authorized to access this page", '403');
     }
     $data['title'] = 'Add User';
     $em = $this->doctrine->em;
     $this->load->model('user_model');
     $view['department'] = $this->user_model->getMasterTerms(array('department'))['department'];
     //Validate
     $this->form_validation->set_rules('username', 'Username', 'required|callback__username_check');
     $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback__email_check');
     $this->form_validation->set_rules('password', 'Password', 'trim|required|external_callbacks[model,formval_callbacks,_check_password_strength,TRUE]');
     $this->form_validation->set_rules('confirm_pass', 'Password Confirmation', 'required|matches[password]');
     $this->form_validation->set_rules('department', 'Department', 'required');
     if (!$this->form_validation->run()) {
         $data['content'] = $this->load->view('admin/add_user', $view, TRUE);
     } else {
         //Save user
         $user = new Entity\Users();
         $user->setUserName($this->input->post('username'));
         $user->setUserEmail($this->input->post('email'));
         $user->setUserLevel($this->input->post('role'));
         $salt = $this->authentication->random_salt();
         $pass = $this->authentication->hash_passwd($this->input->post('password'), $salt);
         $now = date_create(date('Y-m-d H:i:s'));
         $user->setUserPass($pass);
         $user->setUserSalt($salt);
         $user->setUserDate($now);
         $user->setUserModified($now);
         $user->setUserBanned(0);
         try {
             //save to database
             $em->persist($user);
             //Add user department
             $profile = new Entity\Profiles();
             $department = $em->getReference('Entity\\Terms', $this->input->post('department'));
             $profile->setUser($user);
             $profile->setDepartment($department);
             $profile->setCreatedOn($now);
             $em->persist($profile);
             //Subscribe user to the public groups
             $groups = $em->getRepository('Entity\\Groups')->findBy(array('type' => 'organic', 'visibility' => 'public'));
             foreach ($groups as $group) {
                 $subscription = new Entity\Subscription();
                 $subscription->setGroup($group);
                 $subscription->setUser($user);
                 $subscription->setCreatedOn($now);
                 $em->persist($subscription);
             }
             $em->flush();
             if ($this->input->post('notify')) {
                 //Notify user of account creation
                 $this->email->from('*****@*****.**', 'No Reply');
                 $this->email->subject('[' . $this->config->item('site_name') . '] - Account created');
                 $this->email->to($this->input->post('email'));
                 $data['name'] = $this->input->post('username');
                 $data['header'] = 'User account created on portal';
                 //Send mail to the assigned users
                 $data['text'] = sprintf('A user account has been created for you on the portal. You can now login using the login credentials created for you. It is advisable to update your password on your first login, you can find the password update page in your profile page.<br/><br/><b>Username:</b>%s<br/><b>Password:</b> %s', $this->input->post('username'), $this->input->post('password'));
                 $data['cta_url'] = base_url();
                 $data['call_to_action'] = "Login Now";
                 $message = $this->load->view('emails/mail', $data, TRUE);
                 $this->email->message($message);
                 $this->email->send();
                 $this->email->clear();
             }
             $this->session->set_flashdata('success', 'User account has been successfully created.');
             redirect('admin/users');
         } catch (Exception $err) {
             die($err->getMessage());
         }
     }
     return $this->load->view('html', $data);
 }