From 557b99966d5aae53c143880471b44f5ec3492929 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 12 Sep 2017 08:44:21 +1000 Subject: [PATCH] process names fields to always be Title Case --- booking.helper.inc | 58 +++++++++++++++++++++++++++---------------- booking.regn_form.inc | 9 +++++-- booking.regn_node.inc | 9 +++++-- booking.reports.inc | 6 ++--- 4 files changed, 52 insertions(+), 30 deletions(-) diff --git a/booking.helper.inc b/booking.helper.inc index bfad8ae..49ac41c 100644 --- a/booking.helper.inc +++ b/booking.helper.inc @@ -72,6 +72,21 @@ function _booking_remove_emoji($text) { */ } +/** + * Helper function to convert text of any case to Title Case, specifically for names + * Taken from http://php.net/manual/en/function.ucwords.php + */ +function _booking_ucname($string) { + $string = ucwords(strtolower($string)); + + foreach (array('-', '\'') as $delimiter) { + if (strpos($string, $delimiter)!==false) { + $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string))); + } + } + return $string; +} + /** * Helper function to perform some validity checking of email addresses @@ -107,30 +122,29 @@ function _valid_email_address($email) { */ function _valid_medicare_number($input) { //strip any whitespace - $medicare = preg_replace( '/\s+/', '', $input ); - if (is_numeric($medicare) && strlen($medicare) >= 9 && $medicare > 0) - { - $check_digit = $medicare[0] + (3 * $medicare[1]) + (7 * $medicare[2]) + (9 * $medicare[3]) - + $medicare[4] + (3 * $medicare[5]) + (7 * $medicare[6]) + (9 * $medicare[7]); + $medicare = preg_replace( '/\s+/', '', $input ); - if (($check_digit % 10) == $medicare[8]) - { - watchdog('booking', 'Medicare number (!mca) validates since check digit !check matches remainder from !remainder', - array('!mca' => $input, '!check' => $medicare[8], '!remainder' => $check_digit)); - return TRUE; - } - else - { - watchdog('booking', 'Medicare number (!mca) does not validate since check digit !check does not match remainder from !remainder', - array('!mca' => $input, '!check' => $medicare[8], '!remainder' => $check_digit)); - return FALSE; - } + //for testing, use the fake medicare number 111111110 + + if (is_numeric($medicare) && strlen($medicare) >= 9 && $medicare > 0) { + $check_digit = $medicare[0] + (3 * $medicare[1]) + (7 * $medicare[2]) + (9 * $medicare[3]) + + $medicare[4] + (3 * $medicare[5]) + (7 * $medicare[6]) + (9 * $medicare[7]); + + if (($check_digit % 10) == $medicare[8]) { + watchdog('booking', 'Medicare number (!mca) validates since check digit !check matches remainder from !remainder', + array('!mca' => $input, '!check' => $medicare[8], '!remainder' => $check_digit)); + return TRUE; + } + else { + watchdog('booking', 'Medicare number (!mca) does not validate since check digit !check does not match remainder from !remainder', + array('!mca' => $input, '!check' => $medicare[8], '!remainder' => $check_digit)); + return FALSE; + } } - else - { - watchdog('booking', 'Medicare number (!mca) does not validate since it is either non-numeric or too short', - array('!mca' => $input)); - return FALSE; + else { + watchdog('booking', 'Medicare number (!mca) does not validate since it is either non-numeric or too short', + array('!mca' => $input)); + return FALSE; } } /** diff --git a/booking.regn_form.inc b/booking.regn_form.inc index be9fcad..af10ddf 100644 --- a/booking.regn_form.inc +++ b/booking.regn_form.inc @@ -917,7 +917,7 @@ function booking_form($node, &$form_state, $inserting = FALSE, $early_access_all if (variable_get('booking_enable_roommate', 0) == 1) { $form['misc-areas']['booking_room_mate1'] = array( '#type' => 'textfield', - '#title' => t('List up to two people you would like to share a room with (subject to availability).'), + '#title' => t('List up to two people you would like to share a room with (subject to availability)'), '#maxlength' => 200, '#required' => FALSE, '#default_value' => !empty($data->booking_room_mate1) ? $data->booking_room_mate1 : '' @@ -935,7 +935,7 @@ function booking_form($node, &$form_state, $inserting = FALSE, $early_access_all if (variable_get('booking_enable_songchoice', 0) == 1) { $form['misc-areas']['booking_song_choice'] = array( '#type' => 'textfield', - '#title' => t("Let us know if there's a song you'd love to sing during the week."), + '#title' => t("Let us know if there's a song you'd love to sing during the week"), '#maxlength' => 200, '#required' => TRUE, '#default_value' => !empty($data->booking_song_choice) ? $data->booking_song_choice : '' @@ -1216,6 +1216,11 @@ function booking_form_submit($form, &$form_state) $values = $form_state['input']; + //do some preprocessing on input + //convert names from whatever case they're in to Title Case + $values['booking_firstname'] = _booking_ucname($values['booking_firstname']); + $values['booking_lastname'] = _booking_ucname($values['booking_lastname']); + //strip any emojis from user input if (variable_get('booking_enable_emoji_removal', 1) == 1) { foreach ($values as $key => $value) { diff --git a/booking.regn_node.inc b/booking.regn_node.inc index 0bd1c81..01a04f9 100644 --- a/booking.regn_node.inc +++ b/booking.regn_node.inc @@ -7,7 +7,7 @@ function booking_node_presave($node) { if($node->type == 'booking') { $title = t('!event registration: !name', array( '!event' => $event->booking_eventname, - '!name' => $node->booking_firstname . ' ' . $node->booking_lastname, + '!name' => _booking_ucname($node->booking_firstname . ' ' . $node->booking_lastname), )); //strip any emojis from user input if that feature is enabled @@ -447,7 +447,12 @@ function booking_update($node) { } } } - } + } + + //make sure the name is the correct case + $data['booking_firstname'] = _booking_ucname($data['booking_firstname']); + $data['booking_lastname'] = _booking_ucname($data['booking_lastname']); + watchdog('booking', 'Updating node: @info', array('@info' => var_export($data, TRUE))); db_update('booking_person')->fields($data)->condition('nid', $node->nid)->execute(); /* diff --git a/booking.reports.inc b/booking.reports.inc index f86df64..6b85a04 100644 --- a/booking.reports.inc +++ b/booking.reports.inc @@ -148,12 +148,10 @@ function booking_report_summary() { $amount_owing = _booking_amount_owing($person, 0, FALSE); //calculate the travel link - if ($person->tid > 0) - { + if ($person->tid > 0) { $travel_link = l(t('Travel'), t('node/!id/edit', array('!id' => $person->tid))); } - else - { + else { $travel_link = t('N/A'); }