Jump to content

HelperPhone


valan
 Share

Recommended Posts

At our site we use both email and phone authorizations at frontend. To make life easier, I've developed HelperPhone pack that handles phone numbers. This pack includes following modules for ProcessWire CMS/CMF:

  • FieldtypePhoneNumber: module that stores phone numbers
  • InputfieldPhoneNumber: module that renders inputfield for phone numbers
  • HelperPhone: module that loads PhoneNumber and PhoneNumberConst classes, and 'libphonenumber' namespace

All these modules require included PW WireData-derived class PhoneNumber and PhoneNumberConst.

  • PhoneNumber class is a thin wrapper over giggsey/libphonenumber-for-php, itself is port of Google's libphonenumber.
  • PhoneNumberConst class stores constants, used by PhoneNumber class

Usage: PhoneNumber class

$phone = '8 (916) 318-07-29 ext 1234'; // input string could be in any phone-recognizable format
$phoneNumber = new PhoneNumber($phone, 'RU');
// or wire('modules')->get('HelperPhone')->makePhoneNumber($phone, 'RU');

echo ($phoneNumber->isValidNumber() ? 'Yes':'No');
// Yes

echo ($phoneNumber->isValidNumberForRegion($regionCode) ? 'Yes':'No');
// Yes

echo $phoneNumber->getNumberTypeTitle();
// Mobile

echo $phoneNumber->getCountryCode();
// 7

echo $phoneNumber->getRegionCode();
// RU

echo $phoneNumber->getNationalNumber();
// 9163180729

echo $phoneNumber->getExtension();
// 1234

echo $phoneNumber->formatForCallingFrom('US')
// 011 7 916 318-07-28

echo $phoneNumber->formatForCallingFrom('GE')
// 00 7 916 318-07-28


For more methods and properties please refer to PhoneNumber and PhoneNumberConst source files. Need more? Check giggsey/libphonenumber-for-php and use it by accessing $phoneNumber->phoneNumber property - it is instance of \libphonenumber\PhoneNumber or null (if empty).

Usage: field

Note: on field creation, make sure that you've configured field settings

Default region: assumed region if input phone number string is not in international format (starts with '+', etc)
Enabled/disabled phone extentions: if disabled, phone extension will be removed on field save.
Phone field settings in example below: default region code 'RU', phone extensions are enabled

echo $page->phone;
// +79163180729
// Note1: $page->phone stores instance of PhoneNumber and renders to string in E164 format.
// Note2: E164 format does not include extension.

echo $page->getFormatted('phone');
// +7 916 318-07-29 ext. 1234

echo $page->getUnformatted('phone');
// +79163180729

echo $page->phone->format(PhoneNumberConst::RFC3966);
// tel:+7-916-318-07-29;ext=1234

echo $page->phone->getNationalNumber();
// 9163180729

Usage: PW selectors

FieldtypePhoneNumber is instance of FieldtypeText. It stores phone numbers and extensions as string in E164 format with #extention (if provided by user and enabled in settings) E.g. in db it looks like this: '+79163180729#1234'. This makes it easy to query fields as any text field.

echo $pages->find([
    'template' => 'my_temlate',
    'phone^=' => '+79163180729',
]);
// will echo page ids where phone starts with '+79163180729'

Finally

I've decided to put it here first and later to Modules directory (based on your feedbacks).

GitHub: https://github.com/valieand/HelperPhone

Enjoy :)

  • Like 12
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...