Example #1
0
 public function test_phone_without_formatting()
 {
     $sugarField = new SugarFieldPhone('Phone');
     $bean = new Contact();
     $params = array('phone_work' => '+1(800)999-2222');
     $field = 'phone_work';
     $properties = array('name' => 'phone_work', 'vname' => 'LBL_OFFICE_PHONE', 'type' => 'phone', 'dbType' => 'varchar', 'len' => 100, 'audited' => 1, 'unified_search' => 1, 'comment' => 'Work phone number of the contact', 'merge_filter' => 'enabled', 'calculated' => '');
     $sugarField->save($bean, $params, $field, $properties);
     $this->assertEquals($bean->phone_work, '+1(800)999-2222', "Assert that '+1(800)999-2222' is not formatted");
     $params = array('phone_work' => '');
     $sugarField->save($bean, $params, $field, $properties);
     $this->assertEquals($bean->phone_work, '', "Assert that saving empty value works as expected");
 }
 /**
  * This should be called when the bean is saved. The bean itself will be passed by reference
  * 
  * @param SugarBean bean - the bean performing the save
  * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
  */
 public function save($bean, $params, $field, $properties, $prefix = '')
 {
     parent::save($bean, $params, $field, $properties, $prefix);
     if (isset($params[$prefix . $field])) {
         $bean->{$field} = $params[$prefix . $field];
     }
     /*
      * Additional code for phone number format to E.164
      * add by xye@sugarcrm.com begin
      */
     $phoneNumberStr = $bean->{$field};
     /*
      * ISO 3166-1 two-letter country code
      * http://www.iso.org/iso/country_names_and_code_elements
      */
     global $sugar_config;
     $default_region = "US";
     if (isset($sugar_config['phonenumber_default_region']) && !empty($sugar_config['phonenumber_default_region'])) {
         $default_region = $sugar_config['phonenumber_default_region'];
     }
     $phoneUtil = PhoneNumberUtil::getInstance();
     //_ppl('phone number--->'.$phoneNumberStr);
     try {
         $phoneNumberProto = $phoneUtil->parseAndKeepRawInput($phoneNumberStr, $default_region);
         $isValid = $phoneUtil->isValidNumber($phoneNumberProto);
         //_ppl(array($phoneNumberStr, $isValid, $phoneNumberProto, $phoneUtil->format($phoneNumberProto, PhoneNumberFormat::E164)));
         if ($isValid) {
             if ($phoneNumberProto->hasExtension()) {
                 $bean->{$field} = $phoneUtil->format($phoneNumberProto, PhoneNumberFormat::E164) . ' ext. ' . $phoneNumberProto->getExtension();
             } else {
                 $bean->{$field} = $phoneUtil->format($phoneNumberProto, PhoneNumberFormat::E164);
             }
         }
     } catch (NumberParseException $e) {
         _ppl($e->__toString());
         //                         echo $e;
     }
     /*
      * Additional code for phone number format to E.164
      * add by xye@sugarcrm.com end
      */
 }