Results 1 to 3 of 3

Thread: Formating the Phone Number Field

  1. #1
    dhavey is offline Member
    Join Date
    Sep 2008
    Posts
    8

    Default Formating the Phone Number Field

    Hello again everyone,

    Is there a way to format all the telephone number fields (accounts and contacts) so that they all display the number the same way? i.e. by just typing or pasting 10 digits it will always show as xxx.xxx.xxxx this way we don't have (xxx) xxx-xxxx or xxx-xxx-xxxx or 1-xxx-xxx-xxxx.

    Thanks for any help you can give.

  2. #2
    bickart68 is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    111

    Default Re: Formating the Phone Number Field

    With a combination of logic hooks and utility functions this can be done.

    The first file is custom/include/utils.php

    <?php
    /*
    Copyright 2009 Jeff Bickart
    jeff.bickart@gmail.com
    */
    /**
    * Take an existing phone number in any format and strip off any or all of the extra
    * characters and then format for either lookup (type == 0) or outbound dial (type==1)
    *
    * @param String $phone
    * @param int $type
    * @return String phone number
    */
    function getPhoneNumber($phone, $type = 0) {

    //remove all of the extra characters from the phone number
    //leave an x for the extension
    # $phone = ereg_replace("[ ()-.a-wy-z]", "", $phone);
    $phone = ereg_replace("[ !-/:-wy-~]", "", $phone);

    //determine if they have an extension
    $index = strpos($phone, 'x');

    //if the phone number has an extesion
    if ($index > 0) {
    //strip off the extension
    if ($type == 0) {
    $phone = substr($phone, 0, $index);
    } else {
    //replace the extension with commas
    $phone = ereg_replace("[x]", ",,", $phone);
    }
    }

    $length = strlen($phone);
    if ($type == 0) {
    //strip off the leading one
    if (($length > 10) && (substr($phone, 0, 1) == 1)) {
    $phone = substr($phone, 1);
    }
    }

    return $phone;
    }


    function getFormattedPhoneNumber(&$phone) {

    if (! isset($phone))
    return;

    $ret = "";
    $phone = getPhoneNumber($phone);
    switch (strlen($phone)) {
    case 7 :
    $ret = substr($phone, 0, 3)."-".substr($phone, 3);
    break;
    case 8 :
    $ret = substr($phone, 0, 4)."-".substr($phone, 4);
    break;
    case 10 :
    $ret = "(".substr($phone, 0, 3).") ".substr($phone, 3, 3)."-".substr($phone, 6, 4);
    break;
    default :
    $ret = $phone;
    }

    $phone = $ret;
    return $ret;
    }
    ?>


    Then add a logic_hook for each of the modules Accounts, Contacts, Leads.
    This is an example accounts logic_hook
    logic_hook.php

    <?php
    // Do not store anything in this file that is not part of the array or the hook version. This file will
    // be automatically rebuilt in the future.

    // array(hook execute order, 'hook name', 'hook code location', 'hook code class name', 'hook code function to execute')

    $hook_version = 1;
    $hook_array = Array();
    // position, file, function

    $hook_array['before_save'] = array();
    $hook_array['before_save'][] = array(1, 'phone number formatting', 'custom/modules/Accounts/AccountFormatter.php', 'AccountFormatter', 'setPhoneNumbers');
    ?>

    AccountFormatter.php
    <?php
    /*
    * Created on Jan 29, 2009
    *
    * Copyright 2009 - Jeff Bickart
    * jeff.bickart@gmail.com
    */
    class AccountFormatter {

    function setPhoneNumbers(& $bean, $event, $arguments) {
    require_once('custom/include/utils.php');

    getFormattedPhoneNumber($bean->phone_fax);
    getFormattedPhoneNumber($bean->phone_office);
    getFormattedPhoneNumber($bean->phone_alternate);
    }
    }
    ?>

  3. #3
    ptemplin7 is offline Sugar Community Member
    Join Date
    Jun 2007
    Location
    Puget Sound Washington
    Posts
    380

    Default Re: Formating the Phone Number Field

    I have a set of script that does this slightly different and can also work.

    Interested in the files?

    Paris

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Importing compares duplicate against phone number field
    By rampar in forum Installation and Upgrade Help
    Replies: 0
    Last Post: 2009-05-15, 01:33 AM
  2. Phone Number Search Help!
    By rfenn in forum General Discussion
    Replies: 5
    Last Post: 2009-02-21, 04:29 PM
  3. Add link on a phone number
    By Orbiplanax in forum Developer Help
    Replies: 5
    Last Post: 2007-11-29, 09:26 AM
  4. Phone Number(Text Field) in Contact Module
    By jin828sin in forum Help
    Replies: 1
    Last Post: 2007-07-18, 05:29 PM
  5. Fix for the Phone Number Search bug
    By kbrill in forum Downloads
    Replies: 7
    Last Post: 2007-06-27, 05:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •