2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
|
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 {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $areaCode;
|
|
|
|
protected $countryCode;
|
|
|
|
protected $ext;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title = null, $value = '', $extension = null, $areaCode = null,
|
|
|
|
$countryCode = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->areaCode = $areaCode;
|
|
|
|
$this->ext = $extension;
|
|
|
|
$this->countryCode = $countryCode;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 17:09:30 +01:00
|
|
|
parent::__construct($name, $title, $value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 12:11:08 +02:00
|
|
|
/**
|
|
|
|
* @param array $properties
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2015-06-20 12:11:08 +02:00
|
|
|
*/
|
2012-04-11 07:33:36 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-01-02 16:19:22 +01:00
|
|
|
$fields = new FieldGroup( $this->name );
|
|
|
|
list($countryCode, $areaCode, $phoneNumber, $extension) = $this->parseValue();
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
if ($this->value=="") {
|
|
|
|
$countryCode=$this->countryCode;
|
|
|
|
$areaCode=$this->areaCode;
|
|
|
|
$extension=$this->ext;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
if($this->countryCode !== null) {
|
|
|
|
$fields->push(new NumericField($this->name.'[Country]', '+', $countryCode, 4));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 16:19:22 +01: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));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
if($this->ext !== null) {
|
2013-04-12 05:13:58 +02:00
|
|
|
$fields->push(new NumericField( $this->name.'[Extension]', 'ext', $extension, 6));
|
2012-01-02 16:19:22 +01:00
|
|
|
}
|
|
|
|
|
2012-11-30 12:10:09 +01:00
|
|
|
$description = $this->getDescription();
|
2016-06-03 10:51:02 +02:00
|
|
|
if($description) {
|
|
|
|
$fields->getChildren()->first()->setDescription($description);
|
|
|
|
}
|
2012-11-30 12:10:09 +01:00
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
foreach($fields as $field) {
|
2016-06-03 10:51:02 +02:00
|
|
|
/** @var FormField $field */
|
2012-01-02 16:19:22 +01:00
|
|
|
$field->setDisabled($this->isDisabled());
|
|
|
|
$field->setReadonly($this->isReadonly());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
return $fields->Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
public function setValue( $value ) {
|
|
|
|
$this->value = self::joinPhoneNumber( $value );
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= $value['Number'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $completeNumber;
|
|
|
|
} else
|
|
|
|
return $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function parseValue() {
|
2014-08-15 08:53:05 +02:00
|
|
|
if( !is_array( $this->value ))
|
2012-09-26 23:34:00 +02:00
|
|
|
preg_match( '/^(?:(?:\+(\d+))?\s*\((\d+)\))?\s*([0-9A-Za-z]*)\s*(?:[#]\s*(\d+))?$/', $this->value, $parts);
|
2007-07-19 12:40:28 +02:00
|
|
|
else
|
|
|
|
return array( '', '', $this->value, '' );
|
2012-12-08 12:20:20 +01:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $parts;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-11 07:00:57 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2007-07-19 12:40:28 +02:00
|
|
|
list( $countryCode, $areaCode, $phoneNumber, $extension ) = $this->parseValue();
|
|
|
|
$fieldName = $this->name;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber = '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $countryCode )
|
|
|
|
$completeNumber .= '+' . $countryCode;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $areaCode )
|
|
|
|
$completeNumber .= '(' . $areaCode . ')';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= $phoneNumber;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if( $extension )
|
|
|
|
$completeNumber .= '#' . $extension;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$record->$fieldName = $completeNumber;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-11-12 03:19:12 +01:00
|
|
|
* Validate this field
|
|
|
|
*
|
2008-01-11 02:49:50 +01:00
|
|
|
* @todo Very basic validation at the moment
|
2014-11-12 03:19:12 +01:00
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator){
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = preg_match(
|
|
|
|
'/^[0-9\+\-\(\)\s\#]*$/',
|
|
|
|
$this->joinPhoneNumber($this->value)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$valid){
|
|
|
|
$validator->validationError(
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->name,
|
2007-10-25 04:47:45 +02:00
|
|
|
_t('PhoneNumberField.VALIDATION', "Please enter a valid phone number"),
|
2016-06-03 10:51:02 +02:00
|
|
|
"validation"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|