public function add_staff_member() { if (!empty($this->posts)) { //all the fields that want validation $this->fields = array('title', 'first_name', 'last_name', 'gender', 'dob[]', 'evening_telephone', 'address_line_1[]', 'city[]', 'postcode[]', 'date[]'); //Correspoding errors $this->errors = array('required', 'required', 'required', 'required', 'required', 'required', 'required', 'required', 'required', 'required'); /* * Created a function to return the loop */ $this->runValidator($this->fields, $this->errors); //Check for errors if ($this->form_validation->run() == FALSE) { //Are we dealing with ajax? if ($this->input->is_ajax_request()) { //Run the warning function $this->warning('staff_warning'); } else { //show the form and send the parameters of the failed fields $this->staff_form($this->form_validation->error_array()); } } else { //Create the correct date of birth structure for insertion into db $this->posts['dob'] = strtotime($this->posts['dob'][2] . '-' . sprintf('%02s', $this->posts['dob'][1] + 1) . '-' . sprintf('%02s', $this->posts['dob'][0] + 1)); //Get the id from the date table for the dob $dob_id = $this->Registration_Model->insert_return(array('date' => date('Y-m-d', $this->posts['dob'])), 'date'); //When did we add the entry to the system $date_id = $this->Registration_Model->insert_return(array('date' => $this->time), 'date'); /* * Address Data to be inserted into the database * * We need to have a count of keys in case we need * * to add doctors address!! */ $address_ids = $this->staff_model->my_array(array_slice($this->posts, 10, 6), 'address', NULL, NULL); //Count array length.... $a_id = count($address_ids); /* * Contact Data to be inserted into the database * * We need to have a count of keys in case we need * * to add doctors address!! * */ $contact_ids = $this->staff_model->my_array(array_slice($this->posts, 5, 5), 'contact', NULL, NULL); //Count array length.... $c_id = count($contact_ids); /* * We now need to create the reference for the user profiles * */ $ref = array('reference' => createReference($this->posts['last_name'] . $this->posts['first_name'], $dob_id)); /* * Insert the above array into the table "reference" so provide a unique reference that we can use to search from. * * Get the id back for the user table */ $ref_id = $this->staff_model->insert_return($ref, 'reference', true); $user_array = array('address_id' => $address_ids[0], 'contact_id' => $contact_ids[0], 'company_id' => $this->data['data']->master_id, 'dob_id' => (int) $dob_id, 'date_id' => (int) $date_id, 'reference_id' => (int) $ref_id, 'type' => (int) $this->config->item('staff'), 'master_id' => $this->data['data']->master_id); /* * Create user array so we can trck them * * Slice the post vars and merge above array * * Get the user id returned.. */ $user_id = $this->staff_model->insert_return(array_merge(array_slice($this->posts, 0, 4), $user_array), 'users', true); /* * Create a start date for the staff member * * we have a datepicker in case they started before or * * going to start in the future and pre-entered * */ $startDate = $this->staff_model->insert_return(array('date' => isset($this->posts['startToday']) ? $this->time : date("Y-m-d", strtotime($this->posts['date'][0]))), 'date'); /* * We are dealing with an array that changes size dependant on user input * * so we need to find its numeric position in the current post vars, * * this class has private function to find the array position, then slice from the that offset! */ $staff_key = $this->arrayMarker('job_title'); /* * Create an array of additonal attributes for staff table * */ $staffArray = array('user_id' => $user_id, 'master_id' => $this->data['data']->master_id, 'start_from' => $startDate, 'type' => false); /* * Merge above array with a slice of the post vars, insert and return the correct id * */ $staff_id = $this->staff_model->insert_return(array_merge($staffArray, array_slice($this->posts, $staff_key, 10)), 'staff'); /* * Insert the workwear data into the system using @param staff_id * */ $this->staff_model->insert(array_merge(array('staff_id' => $staff_id), array_slice($this->posts, $staff_key + 10, 2)), 'workwear'); /* * Use the above offset, count the correct position in array from that key * * Check if convictions is true and move the counter on the slice to facilitate * * the additional post vars ready to be inserted. * */ $this->staff_model->insert(array_merge(array_slice($this->posts, $staff_key + 12, $this->posts['conviction'] == true ? 3 : 1), array('staff_id' => $staff_id)), 'convictions'); /* * Have this user been dbs checked? * * if so add the check to their records, * * we are using certifications table to hold these records! * */ $this->staff_model->insert(array('staff_id' => $staff_id, 'type' => (int) $this->config->item('dbs'), 'dbs' => $this->posts['dbs'], 'dbs' => $this->posts['dbs'], 'signiture' => $this->posts['signiture'], 'discrepencies' => $this->posts['discrepencies']), 'certifications'); /* * Has the user decided to add payroll? * * If yes, we slice the array using the arrayMarker function to get the array key * * Add the staff id, merge arrays and insert.... */ if (array_key_exists('payroll_number', $this->posts)) { $this->staff_model->insert(array_merge(array('staff_id' => $staff_id), array_slice($this->posts, $this->arrayMarker('payroll_number'), 7)), 'finance'); } /* * Count how many contact vars are being posted */ $c = count($this->posts['daytime_telephone']); /* * Are we posting more than 1? * * If yes we need to insert these records into the contact table and return the id * * This id will be used for medical join, doctors details * */ if ($c > 1) { $cont_id = $this->staff_model->insert_return(array('daytime_telephone' => $this->posts['daytime_telephone'][1], 'evening_telephone' => $this->posts['evening_telephone'][1]), 'contact'); } /* * Insert the medical records based upon certian conditions * * 1.) has it got contact details? yes - insert id / no - insert 0 * * 2.) User the counter from the address id array? have we got 2 entries? insert the second, if not 0 * * 3.) As this array can also change size depending on selection we need to create a double offset by you the arrayMarker function * * on 2 periters, this allows us to calculate the array length so we insert the correct data * */ $this->staff_model->insert(array_merge(array('staff_id' => $staff_id, 'contact_id' => $c > 1 ? $cont_id : 0, 'address_id' => $a_id > 1 ? $address_ids[1] : 0), array_slice($this->posts, $this->arrayMarker('dermatitis'), $this->arrayMarker(array_key_exists('doctors_name', $this->posts) ? 'doctors_name' : 'contact_doc'))), 'medical'); //Give the user a success message to say all well! $this->data['message'] = $this->lang->line('staff_added'); //Encode view data and message into json format for ajax purposes echo json_encode(array('message' => $this->load->view('/clientarea/modules/flashmessage', $this->data, true))); //Return false so the user does not get double view load.... return false; } return false; } $this->data['arr'] = true; $this->staff_form(NULL); $this->view($this->master, $this->prefix, $this->file_ext); }
public function add($contacts = NULL) { //Are we posting if (!empty($this->posts)) { //all the fields that want validation $this->fields = array('type', 'title', 'first_name', 'last_name', 'email_address[]', 'daytime_telephone[]', 'address_line_1[]', 'city[]', 'postcode[]'); //Correspoding errors $this->errors = array('required', 'required', 'required', 'required', 'required|valid_email', 'required', 'required', 'required', 'required'); /* * Customers can be an individual or company, so for validation * * purposes we run an extra check on what is posting * */ if ($this->posts['type'] != 0) { /* * We are dealing with a company so we need to add * * some rules to validation * * Extra fields have been unlocked so * * They need validating too. * */ array_push($this->fields, 'company_name[]'); array_push($this->errors, 'required'); } /* * Parent control function to * * automatically run the validator * */ $this->runValidator($this->fields, $this->errors); } //Check for errors if ($this->form_validation->run() == FALSE) { //Are we transmitting through ajax if ($this->input->is_ajax_request()) { //Generate a warning $this->warning('customer_warning'); return false; } else { //show the form and send the parameters of the failed fields $this->customer_form($this->form_validation->error_array()); } } else { /* * We are dealing with a company * * so we need to create some keys for the multidimansional arrays * */ if ($this->posts['type'] == true) { /* * Generate company array * */ $companyArray = array_merge(array('active' => '1'), array_slice($this->posts, 9, 4)); //Invoice Keys to join onto multidimensional post array $invoice = array(0, 1, 0); //Which is the invoice address out of the 2 vars $addInvoice = array(1, 0); } else { //We are dealing with an individual, so we need a different array $companyArray = array('active' => '1'); //Smaller array so less keys needed $invoice = array(1, 0); //The second var will will be primary $addInvoice = array(0, 1); } //Insert the company data into the database and get the returned keys $company_id = $this->customer_model->insert_return($companyArray, 'company'); //Insert the date for the logs and so we can identify when the customer was added $date_id = $this->customer_model->insert_return(array('date' => $this->time), 'date'); /* * We need to identify the array key position of address data so * * a slice of the post array can be taken to be formatted through the batch * * functions within the core model. * */ $add_key = array_search('address_line_1', array_keys($this->posts)); $address_ids = $this->customer_model->my_array(array_slice($this->posts, $add_key, 6), 'address', NULL, array('company_id' => $company_id, 'invoice' => $addInvoice = array(1, 0))); /* * The site data is mostly composed of returned data and we need to build the array accordingly * * to the post vars, we have multiple ways in which the data can be inserted into the database * * depending on how the user has posted the form * * We also create a unique reference through the createReference helper function */ $a_id = count($address_ids) - 1; $sites = array('site_name' => 'Company Head Quarters', 'site_reference' => createReference($this->name(), $company_id), 'address_id' => $address_ids[$a_id], 'company_id' => $company_id, 'domain' => true); //Insert the site array and return the id accordingly $site_id = $this->customer_model->insert_return($sites, 'sites'); $site_ids = $this->posts['type'] == true ? array(0, 0, $site_id) : array(0, $site_id); /* * customer array function calls the core model functions to insert formatted batch data * * additional parameters can be set such as adding custom arrays to the method which will * * be attached and formated alongside the post array * */ $contact_ids = $this->customer_model->my_array(array_slice($this->posts, 4, 5), 'contact', NULL, array('user' => $this->data['data']->user_id, 'company_id' => $company_id, 'site_id' => $site_ids, 'invoice' => $invoice)); /* * Create an array of customer data with all the corect foriegn * * keys and type. * */ $userData = array('company_id' => $company_id, 'contact_id' => $contact_ids[0], 'address_id' => $a_id == 1 ? $address_ids[1] : $address_ids[0], 'type' => (int) $this->config->item($this->prefix), 'master_id' => $this->data['data']->master_id); //Insert the customer into the database, merge the array above with a slice of the post array. $this->customer_model->insert(array_merge($userData, array_slice($this->posts, 1, 3)), 'users'); //array_slice($this->posts, 1, 3) //Create a customer array and customer reference through the createReference helper function $customerArray = array('reference' => createReference($this->name(), $company_id), 'company_id' => $company_id, 'date_id' => $date_id, 'master_id' => $this->data['data']->master_id, 'individual' => $this->posts['type']); //Insert the customer data into the customer table $this->customer_model->insert($customerArray, 'customers'); //Give the user a success message to say all well! $this->data['message'] = $this->lang->line('customer_added'); //Encode view data and message into json format for ajax purposes echo json_encode(array('message' => $this->load->view('/clientarea/modules/flashmessage', $this->data, true))); //Return false so the user does not get double view load.... return false; } //This arg opens up the form to be multidimensional array $this->data['arr'] = true; //Generate the form $this->customer_form(NULL); //Load the view file $this->view($this->master, $this->prefix, $this->file_ext); }
function update_sites($company_id, $siteId = NULL, $addressId = NULL, $bypass = false) { /* * Are we adding or editing data, if we are adding a new site we need contact data. * * If not we only edit site name or address because the site could have multiple contacts * * and would make the form to big and cumbersome... * */ $this->data['hideContact'] = $siteId == NULL ? NULL : 'NOT NULL'; /* * Run a check for ajax posting * */ if ($this->input->is_ajax_request()) { /* * Are we posting data * */ if (!empty($this->posts)) { //Set the validation for the add/edit site form, adding will require additional fields $this->fields = array('site_name', 'address_line_1', 'city', 'postcode'); $this->errors = array('required', 'required', 'required', 'required'); /* * If we dont have a site id sent as param we are adding * * So we need some additional rules to validate because we are using * * So we push some additonal data onto the array... * */ if ($siteId === NULL) { /* * If we are adding data we need to validate * * addional fields wich are not present during edit * */ array_push($this->fields, 'email_address', 'daytime_telephone', 'first_name'); array_push($this->errors, 'required|valid_email', 'required|validate_uk_phone', 'required'); } /* * Run the validator in the parent class * */ $this->runValidator($this->fields, $this->errors); //Is it Valid? if ($this->form_validation->run() == FALSE) { //show the form and send the parameters of the failed fields echo json_encode($this->form_validation->error_array()); return false; } else { /* * Again are adding or editing * * If it equals NULL we are adding data so * * we need to run different functions to add */ if ($siteId == NULL) { /* * Insert the site address into the system * * */ $address_id = $this->sites_model->insert_return(array_slice($this->posts, 1, 6), 'address'); /* * Create an array to insert the correct site data * * */ $siteData = array('company_id' => $company_id, 'address_id' => $address_id, 'domain' => 0, 'site_name' => $this->posts['site_name'], 'site_reference' => createReference($this->posts['site_name'], $company_id)); //Insert the site data $site_id = $this->sites_model->insert_return($siteData, 'sites'); //Insert the contact data $contact_id = $this->sites_model->insert_return(array_merge(array('site_id' => $site_id), array_slice($this->posts, 10)), 'contact'); $this->sites_model->insert(array_merge(array('contact_id' => $contact_id, 'master_id' => $this->data['data']->master_id), array_slice($this->posts, 7, 3)), 'users'); } else { /* * We are editing!!! * * Update address and sites table with the correct * * Params sent through url * */ $this->sites_model->update_row($addressId, 'id', 'address', array_slice($this->posts, 1, 6)); $this->sites_model->update_row($siteId, 'id', 'sites', array('site_name' => $this->posts['site_name'])); } //Return json success message echo json_encode(array('message' => 'Site Address Changes Saved')); return false; } } if ($siteId == NULL) { //Load company form blank with validation fields $this->form_data($this->fields, '', 1); } else { //We are editing so we need to get the data from the database $this->data['row_data'] = $this->customer_model->get_modal_site_edit($siteId); /* * When editing site data from customers page we use a get request * * so we need to detect from the server what the request is.. * * If its GET just return a json array */ if ($this->input->server('REQUEST_METHOD') == 'GET' && $bypass == true) { echo json_encode($this->data['row_data']); return false; } } $this->title($this->fields, $siteId == NULL ? NULL : $this->data['row_data']); $this->form_data($this->fields, $siteId == NULL ? NULL : (!empty($this->data['row_data']->last_name) ? $this->data['row_data'] : NULL), 0); //Run the address form with data if needed... $this->address($this->fields, $siteId == NULL ? NULL : $this->data['row_data']); //Load the company data $this->form_data($this->fields, $siteId == NULL ? NULL : $this->data['row_data'], 4); //Load the view... $this->load->view('clientarea/sites/modules/sites', $this->data); } else { //No JS enabled, alert user to switch it back on echo "Please Enable Javascript To use These Features"; } }