From ac92c16c0a3cc1d4f321dc20bd0d86a4286353d2 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sat, 29 Apr 2017 15:55:03 +0100 Subject: [PATCH] Always reformat first and last name, and lower case emails --- application/controllers/Customers.php | 53 ++++++++++++------------ application/controllers/Employees.php | 58 +++++++++++++++------------ application/controllers/Persons.php | 37 ++++++++++++++++- application/controllers/Suppliers.php | 32 ++++++++++----- 4 files changed, 116 insertions(+), 64 deletions(-) diff --git a/application/controllers/Customers.php b/application/controllers/Customers.php index b442dcb50..c57c351ed 100644 --- a/application/controllers/Customers.php +++ b/application/controllers/Customers.php @@ -41,7 +41,7 @@ class Customers extends Persons echo json_encode(array('total' => $total_rows, 'rows' => $data_rows)); } - + /* Gives search suggestions based on what is being searched for */ @@ -58,7 +58,7 @@ class Customers extends Persons echo json_encode($suggestions); } - + /* Loads the customer edit form */ @@ -142,7 +142,7 @@ class Customers extends Persons } ++$click; } - + ++$total; } @@ -155,10 +155,10 @@ class Customers extends Persons } } } - + $this->load->view("customers/form", $data); } - + /* Inserts/updates a customer */ @@ -167,7 +167,11 @@ class Customers extends Persons $first_name = $this->xss_clean($this->input->post('first_name')); $last_name = $this->xss_clean($this->input->post('last_name')); $email = $this->xss_clean(strtolower($this->input->post('email'))); - + + // format first and last name properly + $first_name = $this->nameize($first_name); + $last_name = $this->nameize($last_name); + $person_data = array( 'first_name' => $first_name, 'last_name' => $last_name, @@ -209,23 +213,20 @@ class Customers extends Persons // New customer if($customer_id == -1) { - echo json_encode(array( - 'success' => TRUE, + echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_successful_adding') . ' ' . $first_name . ' ' . $last_name, 'id' => $this->xss_clean($customer_data['person_id']))); } else // Existing customer { - echo json_encode(array( - 'success' => TRUE, + echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_successful_updating') . ' ' . $first_name . ' ' . $last_name, 'id' => $customer_id)); } } else // Failure { - echo json_encode(array( - 'success' => FALSE, + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_error_adding_updating') . ' ' . $first_name . ' ' . $last_name, 'id' => -1)); } @@ -250,7 +251,7 @@ class Customers extends Persons echo !$exists ? 'true' : 'false'; } - + /* This deletes customers from the customers table */ @@ -285,7 +286,7 @@ class Customers extends Persons $data = file_get_contents('../' . $name); force_download($name, $data); } - + public function excel_import() { $this->load->view('customers/form_excel_import', NULL); @@ -301,13 +302,13 @@ class Customers extends Persons { if(($handle = fopen($_FILES['file_path']['tmp_name'], 'r')) !== FALSE) { - // Skip the first row as it's the table description + // Skip the first row as it's the table description fgetcsv($handle); $i = 1; $failCodes = array(); - while(($data = fgetcsv($handle)) !== FALSE) + while(($data = fgetcsv($handle)) !== FALSE) { // XSS file data sanity check $data = $this->xss_clean($data); @@ -329,7 +330,7 @@ class Customers extends Persons 'country' => $data[10], 'comments' => $data[11] ); - + $customer_data = array( 'company_name' => $data[12], 'discount_percent' => $data[14], @@ -340,19 +341,19 @@ class Customers extends Persons // don't duplicate people with same email $invalidated = $this->Customer->check_email_exists($email); - if($account_number != '') + if($account_number != '') { $customer_data['account_number'] = $account_number; $invalidated &= $this->Customer->check_account_number_exists($account_number); } } - else + else { $invalidated = TRUE; } if($invalidated) - { + { $failCodes[] = $i; } elseif($this->Customer->save_customer($person_data, $customer_data)) @@ -361,17 +362,17 @@ class Customers extends Persons $this->mailchimp_lib->addOrUpdateMember($this->_list_id, $person_data['email'], $person_data['first_name'], '', $person_data['last_name']); } else - { + { $failCodes[] = $i; } - + ++$i; } - + if(count($failCodes) > 0) { $message = $this->lang->line('customers_excel_import_partially_failed') . ' (' . count($failCodes) . '): ' . implode(', ', $failCodes); - + echo json_encode(array('success' => FALSE, 'message' => $message)); } else @@ -379,9 +380,9 @@ class Customers extends Persons echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('customers_excel_import_success'))); } } - else + else { - echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_excel_import_nodata_wrongformat'))); + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('customers_excel_import_nodata_wrongformat'))); } } } diff --git a/application/controllers/Employees.php b/application/controllers/Employees.php index 26d8e92da..533f334b2 100644 --- a/application/controllers/Employees.php +++ b/application/controllers/Employees.php @@ -33,7 +33,7 @@ class Employees extends Persons echo json_encode(array('total' => $total_rows, 'rows' => $data_rows)); } - + /* Gives search suggestions based on what is being searched for */ @@ -43,7 +43,7 @@ class Employees extends Persons echo json_encode($suggestions); } - + /* Loads the employee edit form */ @@ -61,7 +61,7 @@ class Employees extends Persons { $module->module_id = $this->xss_clean($module->module_id); $module->grant = $this->xss_clean($this->Employee->has_grant($module->module_id, $person_info->person_id)); - + $modules[] = $module; } $data['all_modules'] = $modules; @@ -72,14 +72,14 @@ class Employees extends Persons $permission->module_id = $this->xss_clean($permission->module_id); $permission->permission_id = $this->xss_clean($permission->permission_id); $permission->grant = $this->xss_clean($this->Employee->has_grant($permission->permission_id, $person_info->person_id)); - + $permissions[] = $permission; } $data['all_subpermissions'] = $permissions; $this->load->view('employees/form', $data); } - + /* Inserts/updates an employee */ @@ -94,7 +94,7 @@ class Employees extends Persons 'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT), 'hash_version' => 2 ); - + if($this->Employee->change_password($employee_data, $employee_id)) { echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('employees_successful_change_password'), 'id' => $employee_id)); @@ -111,11 +111,19 @@ class Employees extends Persons } else { + $first_name = $this->xss_clean($this->input->post('first_name')); + $last_name = $this->xss_clean($this->input->post('last_name')); + $email = $this->xss_clean(strtolower($this->input->post('email'))); + + // format first and last name properly + $first_name = $this->nameize($first_name); + $last_name = $this->nameize($last_name); + $person_data = array( - 'first_name' => $this->input->post('first_name'), - 'last_name' => $this->input->post('last_name'), + 'first_name' => $first_name, + 'last_name' => $last_name, 'gender' => $this->input->post('gender'), - 'email' => $this->input->post('email'), + 'email' => $email, 'phone_number' => $this->input->post('phone_number'), 'address_1' => $this->input->post('address_1'), 'address_2' => $this->input->post('address_2'), @@ -126,7 +134,7 @@ class Employees extends Persons 'comments' => $this->input->post('comments'), ); $grants_data = $this->input->post('grants') != NULL ? $this->input->post('grants') : array(); - + //Password has been changed OR first time password set if($this->input->post('password') != '') { @@ -140,34 +148,32 @@ class Employees extends Persons { $employee_data = array('username' => $this->input->post('username')); } - + if($this->Employee->save_employee($person_data, $employee_data, $grants_data, $employee_id)) { - $person_data = $this->xss_clean($person_data); - $employee_data = $this->xss_clean($employee_data); - - //New employee + // New employee if($employee_id == -1) { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('employees_successful_adding') . ' ' . - $person_data['first_name'] . ' ' . $person_data['last_name'], 'id' => $employee_data['person_id'])); + echo json_encode(array('success' => TRUE, + 'message' => $this->lang->line('employees_successful_adding') . ' ' . $first_name . ' ' . $last_name, + 'id' => $this->xss_clean($employee_data['person_id']))); } - else //Existing employee + else // Existing employee { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('employees_successful_updating') . ' ' . - $person_data['first_name'] . ' ' . $person_data['last_name'], 'id' => $employee_id)); + echo json_encode(array('success' => TRUE, + 'message' => $this->lang->line('employees_successful_updating') . ' ' . $first_name . ' ' . $last_name, + 'id' => $employee_id)); } } - else//failure + else // Failure { - $person_data = $this->xss_clean($person_data); - - echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_error_adding_updating') . ' ' . - $person_data['first_name'] . ' ' . $person_data['last_name'], 'id' => -1)); + echo json_encode(array('success' => FALSE, + 'message' => $this->lang->line('employees_error_adding_updating') . ' ' . $first_name . ' ' . $last_name, + 'id' => -1)); } } } - + /* This deletes employees from the employees table */ diff --git a/application/controllers/Persons.php b/application/controllers/Persons.php index a8f3e7985..07cc9d626 100644 --- a/application/controllers/Persons.php +++ b/application/controllers/Persons.php @@ -6,7 +6,7 @@ abstract class Persons extends Secure_Controller { public function __construct($module_id = NULL) { - parent::__construct($module_id); + parent::__construct($module_id); } public function index() @@ -35,5 +35,38 @@ abstract class Persons extends Secure_Controller echo json_encode($data_row); } + + /* + Capitalize segments of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters. + + i.e. + + returns John O'Grady-Smith + */ + protected function nameize($str, $a_char = array("'", "-", " ")) + { + // $str contains the complete raw name string + // $a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default. + $string = strtolower($str); + + foreach($a_char as $temp) + { + $pos = strpos($string, $temp); + if($pos) + { + // we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one. + $mend = ''; + $a_split = explode($temp, $string); + foreach($a_split as $temp2) + { + // capitalize each portion of the string which was separated at a special character + $mend .= ucfirst($temp2).$temp; + } + $string = substr($mend, 0, -1); + } + } + + return ucfirst($string); + } } -?> \ No newline at end of file +?> diff --git a/application/controllers/Suppliers.php b/application/controllers/Suppliers.php index f9e1be56c..02b1d1f71 100644 --- a/application/controllers/Suppliers.php +++ b/application/controllers/Suppliers.php @@ -88,11 +88,19 @@ class Suppliers extends Persons */ public function save($supplier_id = -1) { + $first_name = $this->xss_clean($this->input->post('first_name')); + $last_name = $this->xss_clean($this->input->post('last_name')); + $email = $this->xss_clean(strtolower($this->input->post('email'))); + + // format first and last name properly + $first_name = $this->nameize($first_name); + $last_name = $this->nameize($last_name); + $person_data = array( - 'first_name' => $this->input->post('first_name'), - 'last_name' => $this->input->post('last_name'), + 'first_name' => $first_name, + 'last_name' => $last_name, 'gender' => $this->input->post('gender'), - 'email' => $this->input->post('email'), + 'email' => $email, 'phone_number' => $this->input->post('phone_number'), 'address_1' => $this->input->post('address_1'), 'address_2' => $this->input->post('address_2'), @@ -102,6 +110,7 @@ class Suppliers extends Persons 'country' => $this->input->post('country'), 'comments' => $this->input->post('comments') ); + $supplier_data = array( 'company_name' => $this->input->post('company_name'), 'agency_name' => $this->input->post('agency_name'), @@ -115,21 +124,24 @@ class Suppliers extends Persons //New supplier if($supplier_id == -1) { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('suppliers_successful_adding').' '. - $supplier_data['company_name'], 'id' => $supplier_data['person_id'])); + echo json_encode(array('success' => TRUE, + 'message' => $this->lang->line('suppliers_successful_adding') . ' ' . $supplier_data['company_name'], + 'id' => $supplier_data['person_id'])); } else //Existing supplier { - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('suppliers_successful_updating').' '. - $supplier_data['company_name'], 'id' => $supplier_id)); + echo json_encode(array('success' => TRUE, + 'message' => $this->lang->line('suppliers_successful_updating') . ' ' . $supplier_data['company_name'], + 'id' => $supplier_id)); } } else//failure { $supplier_data = $this->xss_clean($supplier_data); - echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('suppliers_error_adding_updating').' '. - $supplier_data['company_name'], 'id' => -1)); + echo json_encode(array('success' => FALSE, + 'message' => $this->lang->line('suppliers_error_adding_updating') . ' ' . $supplier_data['company_name'], + 'id' => -1)); } } @@ -152,4 +164,4 @@ class Suppliers extends Persons } } -?> \ No newline at end of file +?>