areaCode = $areaCode; $this->ext = $extension; $this->countryCode = $countryCode; parent::__construct( $name, $title, $value, $form ); } public function Field() { $field = new FieldGroup( $this->name ); $field->setID("{$this->name}_Holder"); list( $countryCode, $areaCode, $phoneNumber, $extension ) = $this->parseValue(); $hasTitle = false; if( $this->countryCode !== null ) $field->push( new NumericField( $this->name.'[Country]', '+', $countryCode, 4 ) ); if( $this->areaCode !== null ){ $field->push( new NumericField( $this->name.'[Area]', '(', $areaCode, 4 ) ); $field->push( new NumericField( $this->name.'[Number]', ')', $phoneNumber, 10 ) ); }else{ $field->push( new NumericField( $this->name.'[Number]', '', $phoneNumber, 10 ) ); } if( $this->ext !== null ) $field->push( new NumericField( $this->name.'[Extension]', 'ext', $extension, 6 ) ); return $field; } public function setValue( $value ) { $this->value = self::joinPhoneNumber( $value ); return $this; } public static function joinPhoneNumber( $value ) { if( is_array( $value ) ) { $completeNumber = ''; if( isset($value['Country']) ) $completeNumber .= '+' . $value['Country']; if( isset($value['Area']) ) $completeNumber .= '(' . $value['Area'] . ')'; $completeNumber .= $value['Number']; if( isset($value['Extension']) ) $completeNumber .= '#' . $value['Extension']; return $completeNumber; } else return $value; } protected function parseValue() { if( !is_array( $this->value ) ) preg_match( '/^(?:(?:\+(\d+))?\s*\((\d+)\))?\s*([0-9A-Za-z]*)\s*(?:[#]\s*(\d+))?$/', $this->value, $parts ); else return array( '', '', $this->value, '' ); if( is_array( $parts ) ) { array_shift( $parts ); $parts = array_pad($parts, 4, false); } return $parts; } public function saveInto( $record ) { list( $countryCode, $areaCode, $phoneNumber, $extension ) = $this->parseValue(); $fieldName = $this->name; $completeNumber = ''; if( $countryCode ) $completeNumber .= '+' . $countryCode; if( $areaCode ) $completeNumber .= '(' . $areaCode . ')'; $completeNumber .= $phoneNumber; if( $extension ) $completeNumber .= '#' . $extension; $record->$fieldName = $completeNumber; } /** * @todo Very basic validation at the moment */ function jsValidation() { $formID = $this->form->FormName(); $jsFunc =<<name');"; } /** * @todo Very basic validation at the moment */ function validate($validator){ $valid = preg_match( '/^[0-9\+\-\(\)\s\#]*$/', $this->joinPhoneNumber($this->value) ); if(!$valid){ $validator->validationError( $this->name, _t('PhoneNumberField.VALIDATION', "Please enter a valid phone number"), "validation", false ); return false; } return true; } } ?>