Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. <?php
  2. // custom/include/SugarFields/Fields/Phone/SugarFieldPhone.php
  3. require_once('include/SugarFields/Fields/Phone/SugarFieldPhone.php');
  4. class CustomSugarFieldPhone extends SugarFieldPhone{
  5.  
  6. private $replacement_chars = array(' ','-','(',')','x','X','.','+','#','!');
  7.  
  8. /**
  9. * Remove any special characters to sanitize input, storing only ints
  10. * @see parent::save
  11. */
  12. public function save($bean, $params, $field, $properties, $prefix = ''){
  13. parent::save($bean,$params,$field,$properties,$prefix);
  14. $bean->$field = str_replace($this->replacement_chars,'',$bean->$field);
  15. }
  16.  
  17. /**
  18. * Set a custom key in the $vardef to pass along formatted version of phone number
  19. * @see parent::setup
  20. */
  21. public function setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass=true){
  22. parent::setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass);
  23.  
  24. // detailview uses $vardef['value'] but listview doesn't
  25. if(!empty($vardef['value'])){
  26. $vardef['value_cstm'] = $this->format_phone_number($vardef['value']);
  27. $this->ss->assign('vardef', $vardef);
  28. }else{
  29. $vardef['value_cstm'] = $this->format_phone_number($parentFieldArray[$vardef['name']]);
  30. $this->ss->assign('vardef', $vardef);
  31. }
  32. }
  33.  
  34. /**
  35. * Take the raw int data and format pretty like 1 (123) 123-1234
  36. * @param int $value Phone Number, formatted as integers
  37. */
  38. private function format_phone_number($value){
  39. $index = 0;
  40. $return = "";
  41.  
  42. // if we have fewer than 10 chars, just return
  43. if(strlen($value) < 10){
  44. return $value;
  45. }
  46.  
  47. // if we have fewer than 11 chars and the lead is '1' (country code)
  48. // then just return
  49. if(strlen($value) < 11 && $value[$index] == 1){
  50. return $value;
  51. }
  52.  
  53. // check for a country code, we only support US, though
  54. if($value[$index] == '1'){
  55. $return .= "{$value[$index++]} ";
  56. }
  57.  
  58. // lay out the area code
  59. $return .= "({$value[$index++]}{$value[$index++]}{$value[$index++]}) ";
  60.  
  61. // the prefix and hash
  62. $return .= "{$value[$index++]}{$value[$index++]}{$value[$index++]}-";
  63.  
  64. // the line number
  65. $return .= "{$value[$index++]}{$value[$index++]}{$value[$index++]}{$value[$index++]}";
  66.  
  67. // if there are more, it must be an extension
  68. if(strlen($value) > $index){
  69. $return .= " x ";
  70. while($index < strlen($value)){
  71. $return .= "{$value[$index++]}";
  72. }
  73. }
  74.  
  75. return $return;
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement