I'm using URIs to direct a function in a library:
$id = $this->CI->session->userdata('id');
$URI = $this->CI->uri->uri_string();
$new = "new";
if(strpos($URI, $new) === FALSE){
$method = "update";
}
elseif(strpos($URI, $new) !== FALSE){
$method = "create";
}
So I have two if statements directing what information to
if ($method === 'update') {
// Modify form, first load
$this->CI->db->from('be_survey');
$this->CI->db->where('user_id' , $id);
$survey = $this->CI->db->get();
$user = array_merge($user->row_array(),$survey->row_array());
$this->CI->validation->set_default_value($user);
// Display page
$data['user'] = $user;
}
$this->CI->validation->set_rules($rules);
if ( $this->CI->validation->run() === FALSE )
{
// Output any errors
$this->CI->validation->output_errors();
}
else
{
// Submit form
$this->_submit($method);
}
Submit function:
function _submit($method)
{
//Submit and Update for current User
$id = $this->CI->session->userdata('id');
$this->CI->db->select('users.id, users.username, users.email, profiles.firstname, profiles.manager_id');
$this->CI->db->from('be_users' . " users");
$this->CI->db->join('be_user_profiles' . " profiles",'users.id=profiles.user_id');
$this->CI->db->having('id', $id);
$email_data['user'] = $this->CI->db->get();
$email_data['user'] = $email_data['user']->row();
$manager_id = $email_data['user']->manager_id;
$this->CI->db->select('firstname','email')->from('be_user_profiles')->where('user_id', $manager_id);
$email_data['manager'] = $this->CI->db->get();
$email_data['manager'] = $email_data['manager']->row();
// Fetch what they entered in the form
for($i=1;$i<18;$i++){
$survey["a_".$i]= $this->CI->input->post('a_'.$i);
}
for($i=1;$i<15;$i++){
$survey["b_".$i]= $this->CI->input->post('b_'.$i);
}
for($i=1;$i<12;$i++){
$survey["c_".$i]= $this->CI->input->post('c_'.$i);
}
$profile['firstname'] = $this->CI->input->post('firstname');
$profile['lastname'] = $this->CI->input->post('lastname');
$profile['test_date'] = date ("Y-m-d H:i:s");
$profile['company_name'] = $this->CI->input->post('company_name');
$profile['company_address'] = $this->CI->input->post('company_address');
$profile['company_city'] = $this->CI->input->post('company_city');
$profile['company_phone'] = $this->CI->input->post('company_phone');
$profile['company_state'] = $this->CI->input->post('company_state');
$profile['company_zip'] = $this->CI->input->post('company_zip');
$profile['job_title'] = $this->CI->input->post('job_title');
$profile['job_type'] = $this->CI->input->post('job_type');
$profile['job_time'] = $this->CI->input->post('job_time');
$profile['department'] = $this->CI->input->post('department');
$profile['vision'] = $this->CI->input->post('vision');
$profile['height'] = $this->CI->input->post('height');
$profile['weight'] = $this->CI->input->post('weight');
$profile['hand_dominance'] = $this->CI->input->post('hand_dominance');
$profile['areas_of_fatigue'] = $this->CI->input->post('areas_of_fatigue');
$profile['job_description'] = $this->CI->input->post('job_description');
$profile['injury_review'] = $this->CI->input->post('injury_review');
$profile['job_positive'] = $this->CI->input->post('job_positive');
$profile['risk_factors'] = $this->CI->input->post('risk_factors');
$profile['job_improvement_short'] = $this->CI->input->post('job_improvement_short');
$profile['job_improvement_long'] = $this->CI->input->post('job_improvement_long');
if ($method == "update") {
//Begin db transmission
$this->CI->db->trans_begin();
$this->CI->home_model->update('Survey',$survey, array('user_id' => $id));
$this->CI->db->update('be_user_profiles',$profile, array('user_id' => $id));
if ($this->CI->db->trans_status() === FALSE)
{
flashMsg('error','There was a problem entering your test! Please contact an administrator.');
redirect('survey','location');
}
else
{
//Get credits of user and subtract 1
$this->CI->db->set('credits', 'credits -1', FALSE);
$this->CI->db->update('be_user_profiles',$profile, array('user_id' => $manager_id));
//Mark the form completed.
$this->CI->db->set('test_complete', '1');
$this->CI->db->where('user_id', $id)->update('be_user_profiles');
// Stuff worked...
$this->CI->db->trans_commit();
//Get Manager Information
$this->CI->db->select('users.id, users.username, users.email, profiles.firstname');
$this->CI->db->from('be_users' . " users");
$this->CI->db->join('be_user_profiles' . " profiles",'users.id=profiles.user_id');
$this->CI->db->having('id', $email_data['user']->manager_id);
$email_data['manager'] = $this->CI->db->get();
$email_data['manager'] = $email_data['manager']->row();
//Email User
$this->CI->load->library('User_email');
$data_user = array(
'firstname'=>$email_data['user']->firstname,
'email'=> $email_data['user']->email,
'user_completed'=>$email_data['user']->firstname,
'site_name'=>$this->CI->preference->item('site_name'),
'site_url'=>base_url()
);
//Email Manager
$data_manager = array(
'firstname'=>$email_data['manager']->firstname,
'email'=> $email_data['manager']->email,
'user_completed'=>$email_data['user']->firstname,
'site_name'=>$this->CI->preference->item('site_name'),
'site_url'=>base_url()
);
$this->CI->user_email->send($email_data['manager']->email,'Completed the Assessment Tool','public/email_manager_complete',$data_manager);
$this->CI->user_email->send($email_data['user']->email,'Completed the Assessment Tool','public/email_user_complete',$data_user);
flashMsg('success','You finished the assessment successfully!');
redirect('home','location');
}
}
//Create New User
elseif ($method == "create") {
// Build
$profile['user_id'] = $id;
$profile['manager_id'] = $manager_id;
$profile['test_complete'] = '1';
$survey['user_id'] = $id;
$this->CI->db->trans_begin();
// Add user_profile details to DB
$this->CI->db->insert('be_user_profiles',$profile);
$this->CI->db->insert('be_survey',$survey);
if ($this->CI->db->trans_status() === FALSE)
{
// Registration failed
$this->CI->db->trans_rollback();
flashMsg('error',$this->CI->lang->line('userlib_registration_failed'));
redirect('auth/register','location');
}
else
{
// User registered
$this->CI->db->trans_commit();
flashMsg('success',$this->CI->lang->line('userlib_registration_success'));
redirect($this->CI->config->item('userlib_action_register'),'location');
}
}
}
The submit function is similar, updating the db if $method == "update", and inserting if the method == "create".
The problem is, when the form is submitted, it doesn't take into account the url b/c the form submits to the function "survey", which passes data to the lib function, so things are always updated, never created.
*How can I pass $method to the _submit() function correctly?!*