-
-
Save matthewpoer/6368321 to your computer and use it in GitHub Desktop.
{* 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; | |
} | |
} |
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:
- Change original function include/Smarty/plugins/functions.sugar_phone.php body in the way like this (source adopted to the international phone prefixes 7 and 8, change it if you need):
$replacement_chars = array(' ','-','(',')','x','X','.','+','#','!');
if (!isset($params['value'])){
$smarty->trigger_error("sugar_phone: missing 'value' parameter");
return '';
}
global $system_config;
$value = $params['value'];
$index = 0;
$return = "";
// Return empty phone
if ($value === 'f') {
return '';
}
$value = str_replace($replacement_chars, '', $value);
// 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;
}
elseif($value[$index] == '7'){
$return .= "+{$value[$index++]} ";
}
elseif($value[$index] == '8'){
$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;
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.
When I use this, in my detailview, I get a value from a different row. Thoughts?
My "list" view and "edit" views are working properly.