2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
|
|
|
*/
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Field for displaying phone numbers. It separates the number, the area code and optionally the country code
|
|
|
|
* and extension.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class PhoneNumberField extends FormField {
|
|
|
|
|
|
|
|
protected $areaCode;
|
|
|
|
protected $countryCode;
|
|
|
|
protected $ext;
|
|
|
|
|
2008-08-06 05:43:48 +02:00
|
|
|
public function __construct( $name, $title = null, $value = '', $extension = null,
|
2007-07-19 12:40:28 +02:00
|
|
|
$areaCode = null, $countryCode = null, $form = null ) {
|
|
|
|
|
|
|
|
$this->areaCode = $areaCode;
|
|
|
|
$this->ext = $extension;
|
|
|
|
$this->countryCode = $countryCode;
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
parent::__construct( $name, $title, $value, $form );
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function Field() {
|
|
|
|
$field = new FieldGroup( $this->name );
|
|
|
|
$field->setID("{$this->name}_Holder");
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
list( $countryCode, $areaCode, $phoneNumber, $extension ) = $this->parseValue();
|
|
|
|
|
|
|
|
$hasTitle = false;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
if ($this->value=="")
|
|
|
|
{
|
|
|
|
$countryCode=$this->countryCode;
|
|
|
|
$areaCode=$this->areaCode;
|
|
|
|
$extension=$this->ext;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
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 = '';
|
2008-12-04 23:38:32 +01:00
|
|
|
if( isset($value['Country']) && $value['Country']!=null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= '+' . $value['Country'];
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( isset($value['Area']) && $value['Area']!=null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= '(' . $value['Area'] . ')';
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$completeNumber .= $value['Number'];
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
if( isset($value['Extension']) && $value['Extension']!=null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= '#' . $value['Extension'];
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $completeNumber;
|
|
|
|
} else
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseValue() {
|
2008-12-04 23:38:32 +01:00
|
|
|
if( !is_array( $this->value ))
|
2007-07-19 12:40:28 +02:00
|
|
|
preg_match( '/^(?:(?:\+(\d+))?\s*\((\d+)\))?\s*([0-9A-Za-z]*)\s*(?:[#]\s*(\d+))?$/', $this->value, $parts );
|
|
|
|
else
|
|
|
|
return array( '', '', $this->value, '' );
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
if(is_array($parts)) array_shift( $parts );
|
|
|
|
|
|
|
|
for ($x=0;$x<=3;$x++) {
|
|
|
|
if (!isset($parts[$x])) $parts[]='';
|
2008-04-18 05:50:29 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
return $parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveInto( $record ) {
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
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;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$record->$fieldName = $completeNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Very basic validation at the moment
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
|
|
|
|
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validatePhoneNumber: function(fieldName) {
|
|
|
|
if(!$(fieldName + "_Holder")) return true;
|
|
|
|
|
|
|
|
// Phonenumbers are split into multiple values, so get the inputs from the form.
|
|
|
|
var parts = $(fieldName + "_Holder").getElementsByTagName('input');
|
|
|
|
var isNull = true;
|
|
|
|
|
|
|
|
// we're not validating empty fields (done by requiredfields)
|
|
|
|
for(i=0; i < parts.length ; i++ ) {
|
|
|
|
isNull = (parts[i].value == null || parts[i].value == "") ? isNull && true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!isNull) {
|
|
|
|
// Concatenate the string values from the parts of the input.
|
|
|
|
var joinedNumber = "";
|
|
|
|
for(i=0; i < parts.length; i++) joinedNumber += parts[i].value;
|
|
|
|
if(!joinedNumber.match(/^[0-9\+\-\(\)\s\#]*\$/)) {
|
|
|
|
// TODO Find a way to mark multiple error fields
|
|
|
|
validationError(
|
|
|
|
fieldName+"-Number",
|
|
|
|
"Please enter a valid phone number",
|
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements :: customScript($jsFunc, 'func_validatePhoneNumber');
|
|
|
|
|
|
|
|
return "\$('$formID').validatePhoneNumber('$this->name');";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Very basic validation at the moment
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function validate($validator){
|
|
|
|
$valid = preg_match(
|
|
|
|
'/^[0-9\+\-\(\)\s\#]*$/',
|
|
|
|
$this->joinPhoneNumber($this->value)
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!$valid){
|
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2007-10-25 04:47:45 +02:00
|
|
|
_t('PhoneNumberField.VALIDATION', "Please enter a valid phone number"),
|
2007-07-19 12:40:28 +02:00
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|