Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement