From 66859a49409b37a16d17daad46056af20b4df164 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Nov 2014 11:08:59 +0000 Subject: [PATCH] pushPhoneNumberToCache. This permit to fix problems with phone numbers with dashes and parenthesis --- appinfo/ocsmsapp.php | 58 ++++++++++++++++++++++++++++++++++-- controller/smscontroller.php | 14 ++++----- 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 9cea17e..a7b7cfb 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -134,19 +134,73 @@ class OcSmsApp extends App { } private function pushPhoneNumberToCache($rawPhone, $contactName) { + // We try to add many combinaisons $phoneNb = preg_replace("#[ ]#", "/", $rawPhone); - $phoneNbNoSpaces = preg_replace("#[ ]#", "", $rawPhone); + + /* + * At this point, spaces are slashes. + */ + + // Spaces removed + $phoneNbNoSpaces = preg_replace("#[/]#", "", $phoneNb); + // Parenthesis removed + $phoneNbNoParenthesis = preg_replace("#[(]|[)]#", "", $phoneNb); + // Dashes removed + $phoneNbNoDashes = preg_replace("#[-]#", "", $phoneNb); + + // Spaces and parenthesis + $phoneNbNoSpacesParenthesis = preg_replace("#[/]|[(]|[)]#", "", $phoneNb); + // Spaces and dashes + $phoneNbNoSpacesDashes = preg_replace("#[/]|[-]#", "", $phoneNb); + // parenthesis and dashes + $phoneNbNoDashesParenthesis = preg_replace("#[-]|[(]|[)]#", "", $phoneNb); + + // Nothing + $phoneNbNothing = preg_replace("#[/]|[(]|[)]|[-]#", "", $phoneNb); + // Contacts self::$contacts[$phoneNb] = $contactName; self::$contacts[$phoneNbNoSpaces] = $contactName; + self::$contacts[$phoneNbNoParenthesis] = $contactName; + self::$contacts[$phoneNbNoDashes] = $contactName; + self::$contacts[$phoneNbNoSpacesParenthesis] = $contactName; + self::$contacts[$phoneNbNoSpacesDashes] = $contactName; + self::$contacts[$phoneNbNoDashesParenthesis] = $contactName; + self::$contacts[$phoneNbNothing] = $contactName; + // Inverted contacts if (!isset(self::$contactsInverted[$contactName])) { self::$contactsInverted[$contactName] = array(); } + array_push(self::$contactsInverted[$contactName], $phoneNb); - if ($phoneNb != $phoneNbNoSpaces) { + if (!in_array($phoneNbNoSpaces, self::$contactsInverted[$contactName])) { array_push(self::$contactsInverted[$contactName], $phoneNbNoSpaces); } + + if (!in_array($phoneNbNoParenthesis, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNoParenthesis); + } + + if (!in_array($phoneNbNoDashes, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNoDashes); + } + + if (!in_array($phoneNbNoSpacesParenthesis, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNoSpacesParenthesis); + } + + if (!in_array($phoneNbNoSpacesDashes, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNoSpacesDashes); + } + + if (!in_array($phoneNbNoDashesParenthesis, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNoDashesParenthesis); + } + + if (!in_array($phoneNbNothing, self::$contactsInverted[$contactName])) { + array_push(self::$contactsInverted[$contactName], $phoneNbNothing); + } } } diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 78b8a7d..1bb0f7f 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -127,7 +127,7 @@ class SmsController extends Controller { $msgCount = 0; // This table will be used to avoid duplicates - $noSpacesPhones = array(); + $cleanedPhones = array(); // Contact resolved if ($contactName != "" && isset($iContacts[$contactName])) { @@ -143,10 +143,10 @@ class SmsController extends Controller { $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - $fmtPNNoSpaces = preg_replace("#[ ]#","", $fmtPN); - if (!in_array($fmtPNNoSpaces, $noSpacesPhones)) { + $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); + if (!in_array($fmtPNCleaned, $cleanedPhones)) { $phoneNumbers[] = $fmtPN; - $noSpacesPhones[] = $fmtPNNoSpaces; + $cleanedPhones[] = $fmtPNCleaned; } } } @@ -157,10 +157,10 @@ class SmsController extends Controller { $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $fmtPN, $lastDate); $msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $fmtPN); - $fmtPNNoSpaces = preg_replace("#[ ]#","", $fmtPN); - if (!in_array($fmtPNNoSpaces, $noSpacesPhones)) { + $fmtPNCleaned = preg_replace("#[ ]|-|\(|\)]#","", $fmtPN); + if (!in_array($fmtPNCleaned, $cleanedPhones)) { $phoneNumbers[] = $fmtPN; - $noSpacesPhones[] = $fmtPNNoSpaces; + $cleanedPhones[] = $fmtPNCleaned; } }