Skip to content

Instantly share code, notes, and snippets.

@dgrudinin
Forked from matthewpoer/SugarFieldPhone.php
Created February 12, 2016 12:28
Show Gist options
  • Save dgrudinin/591b6f48ea39b8307a72 to your computer and use it in GitHub Desktop.
Save dgrudinin/591b6f48ea39b8307a72 to your computer and use it in GitHub Desktop.
SugarCRM Phone Number Formatting. Store only the phone number integers and use only that in the EditView, but DetailView and ListView show a pretty format like 1 (123) 123-1234. Does not format for Reports.
{* custom/include/SugarFields/Fields/Phone/en_us.DetailView.tpl *}
{if !empty({{sugarvar key='value' string=true}})}
{{$vardef.value_cstm}}
{/if}
{* custom/include/SugarFields/Fields/Phone/en_us.ListView.tpl *}
{$vardef.value_cstm}
<?php
// custom/include/SugarFields/Fields/Phone/SugarFieldPhone.php
require_once('include/SugarFields/Fields/Phone/SugarFieldPhone.php');
class CustomSugarFieldPhone extends SugarFieldPhone{
private $replacement_chars = array(' ','-','(',')','x','X','.','+','#','!');
/**
* Remove any special characters to sanitize input, storing only ints
* @see parent::save
*/
public function save($bean, $params, $field, $properties, $prefix = ''){
parent::save($bean,$params,$field,$properties,$prefix);
$bean->$field = str_replace($this->replacement_chars,'',$bean->$field);
}
/**
* Set a custom key in the $vardef to pass along formatted version of phone number
* @see parent::setup
*/
public function setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass=true){
parent::setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass);
// detailview uses $vardef['value'] but listview doesn't
if(!empty($vardef['value'])){
$vardef['value_cstm'] = $this->format_phone_number($vardef['value']);
$this->ss->assign('vardef', $vardef);
}else{
$vardef['value_cstm'] = $this->format_phone_number($parentFieldArray[$vardef['name']]);
$this->ss->assign('vardef', $vardef);
}
}
/**
* Take the raw int data and format pretty like 1 (123) 123-1234
* @param int $value Phone Number, formatted as integers
*/
private function format_phone_number($value){
$index = 0;
$return = "";
// if we have fewer than 10 chars, just return
if(strlen($value) < 10){
return $value;
}
// if we have fewer than 11 chars and the lead is '1' (country code)
// then just return
if(strlen($value) < 11 && $value[$index] == 1){
return $value;
}
// check for a country code, we only support US, though
if($value[$index] == '1'){
$return .= "{$value[$index++]} ";
}
// lay out the area code
$return .= "({$value[$index++]}{$value[$index++]}{$value[$index++]}) ";
// the prefix and hash
$return .= "{$value[$index++]}{$value[$index++]}{$value[$index++]}-";
// the line number
$return .= "{$value[$index++]}{$value[$index++]}{$value[$index++]}{$value[$index++]}";
// if there are more, it must be an extension
if(strlen($value) > $index){
$return .= " x ";
while($index < strlen($value)){
$return .= "{$value[$index++]}";
}
}
return $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment