Skip to content

Commit

Permalink
feat: standardise phone number format while importing vCard (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dagolin authored and djaiss committed Oct 27, 2018
1 parent a98b762 commit de6f705
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ UNRELEASED CHANGES:

* Add ability to archive a contact
* Add right-click support on contact list
* Standardize phonenumber format while importing vCard
* Set currency and timezone for new users
* Fix settings' sidebar links and change security icon
* Fix CSV import
Expand Down
29 changes: 29 additions & 0 deletions app/Helpers/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Matriphe\ISO639\ISO639;
use Illuminate\Support\Facades\Auth;
use libphonenumber\PhoneNumberFormat;

class LocaleHelper
{
Expand Down Expand Up @@ -104,4 +105,32 @@ public static function getLocaleAlpha($locale)

return $lang;
}

/**
* Format phone number by country.
*
* @param string $tel
* @param $iso
* @param int $format
*
* @return null | string
*/
public static function formatTelephoneNumberByISO(string $tel, $iso, int $format = PhoneNumberFormat::INTERNATIONAL)
{
if (empty($iso)) {
return $tel;
}

try {
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();

$phoneInstance = $phoneUtil->parse($tel, strtoupper($iso));

$tel = $phoneUtil->format($phoneInstance, $format);
} catch (\libphonenumber\NumberParseException $e) {
// Do nothing if the number cannot be parsed successfully
}

return $tel;
}
}
18 changes: 18 additions & 0 deletions app/Helpers/VCardHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,22 @@ public static function addAddressToVCard(Contact $contact, VCard $vCard)

return $vCard;
}

/**
* Get country model object from given VCard file.
*
* @param \Sabre\VObject\Component\VCard $VCard
*
* @return null | string
*/
public static function getCountryISOFromSabreVCard(\Sabre\VObject\Component\VCard $VCard)
{
$VCardAddress = $VCard->ADR;

if (empty($VCardAddress)) {
return;
}

return CountriesHelper::find($VCardAddress->getParts()[6]);
}
}
8 changes: 8 additions & 0 deletions app/Models/Account/ImportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Exception;
use App\Models\User\User;
use Sabre\VObject\Reader;
use App\Helpers\VCardHelper;
use App\Helpers\LocaleHelper;
use App\Models\Contact\Gender;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
Expand Down Expand Up @@ -537,6 +539,12 @@ public function importTel(Contact $contact): void
}

foreach ($this->currentEntry->TEL as $tel) {
$tel = (string) $this->currentEntry->TEL;

$countryISO = VCardHelper::getCountryISOFromSabreVCard($this->currentEntry);

$tel = LocaleHelper::formatTelephoneNumberByISO($tel, $countryISO);

ContactField::firstOrCreate([
'account_id' => $contact->account_id,
'contact_id' => $contact->id,
Expand Down
9 changes: 8 additions & 1 deletion app/Traits/VCardImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Traits;

use Sabre\VObject\Reader;
use App\Helpers\VCardHelper;
use App\Helpers\LocaleHelper;
use App\Models\Contact\Gender;
use App\Models\Contact\Address;
use App\Models\Contact\Contact;
Expand Down Expand Up @@ -126,11 +128,16 @@ private function vCardToContact($vcard, $account_id, $gender_id)
}

if (! is_null($this->formatValue($vcard->TEL))) {
$tel = (string) $vcard->TEL;

$countryISO = VCardHelper::getCountryISOFromSabreVCard($vcard);
$tel = LocaleHelper::formatTelephoneNumberByISO($tel, $countryISO);

// Saves the phone number
$contactField = new ContactField;
$contactField->contact_id = $contact->id;
$contactField->account_id = $contact->account_id;
$contactField->data = $this->formatValue($vcard->TEL);
$contactField->data = $this->formatValue($tel);
$contactField->contact_field_type_id = $this->contactFieldPhoneId();
$contactField->save();
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"doctrine/dbal": "^2.5",
"erusev/parsedown": "~1.7",
"fideloper/proxy": "^4.0",
"giggsey/libphonenumber-for-php": "^8.9",
"guzzlehttp/guzzle": "^6.2",
"intervention/image": "^2.3",
"ircop/antiflood": "^0.1.4",
Expand Down
Loading

0 comments on commit de6f705

Please sign in to comment.