Last active
April 17, 2021 19:06
-
-
Save matthewpoer/6368321 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{* custom/include/SugarFields/Fields/Phone/en_us.DetailView.tpl *} | |
{if !empty({{sugarvar key='value' string=true}})} | |
{{$vardef.value_cstm}} | |
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{* custom/include/SugarFields/Fields/Phone/en_us.ListView.tpl *} | |
{$vardef.value_cstm} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
openjack, it seems to be a real bug in this solution. I've tried to fix it using this repo files and SugarCRM CE source, but it still alive. I think the problem is connected with different $parentFieldArray loading & handling in different views (DetailView vs ListView).
I've found other way to solve this problem:
Architecturally it is better solution than placed in this repo, because Smarty plugins are used to control formatting and output.
Or you can define a new function in Smarty plugins directory (for example, function.sugar_custom_phone.php) and redefine basic include/SugarFields/Phone/... (copy-paste) to custom/include/SugarFields/Phone/..., where replace {sugar_phone... } calls by {sugar_custom_phone ...}.
Or you can define a new copy of include/SugarFields/Phone/..., place it to custom/SugarFields..., name it something like a 'CustomPhone' and place there {sugar_custom_phone} calls as defined in the 2. But you will need to replace all metadata fields calls 'type' => 'phone' by 'type' => 'CustomPhone'.
I think the 1st and 2nd are the best solutions of phone numbers formatting problem.