silverstripe-framework/forms/PhoneNumberField.php

156 lines
3.7 KiB
PHP
Raw Normal View History

<?php
/**
* @package forms
* @subpackage fields-formattedinput
*/
/**
* Field for displaying phone numbers. It separates the number, the area code and optionally the country code
* and extension.
* @package forms
* @subpackage fields-formattedinput
*/
class PhoneNumberField extends FormField {
2014-08-15 08:53:05 +02:00
protected $areaCode;
protected $countryCode;
protected $ext;
2014-08-15 08:53:05 +02:00
public function __construct($name, $title = null, $value = '', $extension = null, $areaCode = null,
$countryCode = null) {
2014-08-15 08:53:05 +02:00
$this->areaCode = $areaCode;
$this->ext = $extension;
$this->countryCode = $countryCode;
2014-08-15 08:53:05 +02:00
parent::__construct($name, $title, $value);
}
2014-08-15 08:53:05 +02:00
public function Field($properties = array()) {
$fields = new FieldGroup( $this->name );
$fields->setID("{$this->name}_Holder");
list($countryCode, $areaCode, $phoneNumber, $extension) = $this->parseValue();
if ($this->value=="") {
$countryCode=$this->countryCode;
$areaCode=$this->areaCode;
$extension=$this->ext;
}
2014-08-15 08:53:05 +02:00
if($this->countryCode !== null) {
$fields->push(new NumericField($this->name.'[Country]', '+', $countryCode, 4));
}
2014-08-15 08:53:05 +02:00
if($this->areaCode !== null) {
$fields->push(new NumericField($this->name.'[Area]', '(', $areaCode, 4));
$fields->push(new NumericField($this->name.'[Number]', ')', $phoneNumber, 10));
} else {
$fields->push(new NumericField($this->name.'[Number]', '', $phoneNumber, 10));
}
2014-08-15 08:53:05 +02:00
if($this->ext !== null) {
$fields->push(new NumericField( $this->name.'[Extension]', 'ext', $extension, 6));
}
$description = $this->getDescription();
if($description) $fields->getChildren()->First()->setDescription($description);
foreach($fields as $field) {
$field->setDisabled($this->isDisabled());
$field->setReadonly($this->isReadonly());
}
2014-08-15 08:53:05 +02:00
return $fields;
}
2014-08-15 08:53:05 +02:00
public function setValue( $value ) {
$this->value = self::joinPhoneNumber( $value );
return $this;
}
2014-08-15 08:53:05 +02:00
public static function joinPhoneNumber( $value ) {
if( is_array( $value ) ) {
$completeNumber = '';
if( isset($value['Country']) && $value['Country']!=null) {
$completeNumber .= '+' . $value['Country'];
}
if( isset($value['Area']) && $value['Area']!=null) {
$completeNumber .= '(' . $value['Area'] . ')';
}
2014-08-15 08:53:05 +02:00
$completeNumber .= $value['Number'];
2014-08-15 08:53:05 +02:00
if( isset($value['Extension']) && $value['Extension']!=null) {
$completeNumber .= '#' . $value['Extension'];
}
2014-08-15 08:53:05 +02:00
return $completeNumber;
} else
return $value;
}
2014-08-15 08:53:05 +02:00
protected function parseValue() {
2014-08-15 08:53:05 +02:00
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 );
for ($x=0;$x<=3;$x++) {
if (!isset($parts[$x])) $parts[]='';
}
2014-08-15 08:53:05 +02:00
return $parts;
}
2014-08-15 08:53:05 +02:00
public function saveInto(DataObjectInterface $record) {
list( $countryCode, $areaCode, $phoneNumber, $extension ) = $this->parseValue();
$fieldName = $this->name;
2014-08-15 08:53:05 +02:00
$completeNumber = '';
2014-08-15 08:53:05 +02:00
if( $countryCode )
$completeNumber .= '+' . $countryCode;
2014-08-15 08:53:05 +02:00
if( $areaCode )
$completeNumber .= '(' . $areaCode . ')';
2014-08-15 08:53:05 +02:00
$completeNumber .= $phoneNumber;
2014-08-15 08:53:05 +02:00
if( $extension )
$completeNumber .= '#' . $extension;
$record->$fieldName = $completeNumber;
}
2014-08-15 08:53:05 +02:00
/**
* Validate this field
*
* @todo Very basic validation at the moment
*
* @param Validator $validator
* @return bool
*/
public function validate(Validator $validator){
$valid = preg_match(
'/^[0-9\+\-\(\)\s\#]*$/',
$this->joinPhoneNumber($this->value)
);
2014-08-15 08:53:05 +02:00
if(!$valid){
$validator->validationError(
2014-08-15 08:53:05 +02:00
$this->name,
_t('PhoneNumberField.VALIDATION', "Please enter a valid phone number"),
2014-08-15 08:53:05 +02:00
"validation",
false
);
return false;
}
2014-08-15 08:53:05 +02:00
return true;
}
}