2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
2008-12-04 23:38:32 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
2016-07-25 07:24:26 +02:00
|
|
|
class PhoneNumberField extends FieldGroup {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* Default area code
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $areaCode;
|
2016-07-25 07:24:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default country code
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $countryCode;
|
2016-07-25 07:24:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default extension
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $ext;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* @return NumericField
|
|
|
|
*/
|
|
|
|
public function getCountryField() {
|
|
|
|
return $this->getChildField('Country');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* @return NumericField
|
|
|
|
*/
|
|
|
|
public function getAreaField() {
|
|
|
|
return $this->getChildField('Area');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* @return NumericField
|
|
|
|
*/
|
|
|
|
public function getNumberField() {
|
|
|
|
return $this->getChildField('Number');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-20 12:11:08 +02:00
|
|
|
/**
|
2016-07-25 07:24:26 +02:00
|
|
|
* @return NumericField
|
2015-06-20 12:11:08 +02:00
|
|
|
*/
|
2016-07-25 07:24:26 +02:00
|
|
|
public function getExtensionField() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-25 07:24:26 +02:00
|
|
|
return $this->getChildField('Extension');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getChildField($name) {
|
|
|
|
$endsWith = "[{$name}]";
|
|
|
|
foreach($this->getChildren() as $child) {
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/** @var Formfield $child */
|
|
|
|
if(substr($child->getName(), 0 - strlen($endsWith)) === $endsWith) {
|
|
|
|
return $child;
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
public function __construct(
|
|
|
|
$name, $title = null, $value = '', $extension = null, $areaCode = null, $countryCode = null
|
|
|
|
) {
|
|
|
|
$this->areaCode = $areaCode;
|
|
|
|
$this->ext = $extension;
|
|
|
|
$this->countryCode = $countryCode;
|
|
|
|
|
|
|
|
// Build fields
|
|
|
|
$fields = new FieldList();
|
2012-01-02 16:19:22 +01:00
|
|
|
if($this->countryCode !== null) {
|
2016-07-25 07:24:26 +02:00
|
|
|
$countryField = NumericField::create($name.'[Country]', false, $countryCode, 4)
|
|
|
|
->addExtraClass('phonenumber-field__country');
|
|
|
|
$fields->push($countryField);
|
2012-01-02 16:19:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
if($this->areaCode !== null) {
|
2016-07-25 07:24:26 +02:00
|
|
|
$areaField = NumericField::create($name.'[Area]', false, $areaCode, 4)
|
|
|
|
->addExtraClass('phonenumber-field__area');
|
|
|
|
$fields->push($areaField);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
$numberField = NumericField::create($name.'[Number]', false, null, 10)
|
|
|
|
->addExtraClass('phonenumber-field__number');
|
|
|
|
$fields->push($numberField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-02 16:19:22 +01:00
|
|
|
if($this->ext !== null) {
|
2016-07-25 07:24:26 +02:00
|
|
|
$extensionField = NumericField::create( $name.'[Extension]', false, $extension, 6)
|
|
|
|
->addExtraClass('phonenumber-field__extension');
|
|
|
|
$fields->push($extensionField);
|
2012-01-02 16:19:22 +01:00
|
|
|
}
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
parent::__construct($title, $fields);
|
|
|
|
|
|
|
|
$this->setName($name);
|
|
|
|
if (isset($value)) {
|
|
|
|
$this->setValue($value);
|
2016-06-03 10:51:02 +02:00
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setName($name) {
|
|
|
|
parent::setName($name);
|
|
|
|
foreach($this->getChildren() as $child) {
|
|
|
|
/** @var FormField $child */
|
|
|
|
$thisName = $child->getName();
|
|
|
|
$thisName = preg_replace('/^.*(\[\\w+\\])$/', $name . '\\1', $thisName);
|
|
|
|
$child->setName($thisName);
|
|
|
|
}
|
|
|
|
}
|
2012-11-30 12:10:09 +01:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
public function hasData() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $properties
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Field($properties = array()) {
|
|
|
|
foreach($this->getChildren() 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());
|
2016-07-25 07:24:26 +02:00
|
|
|
$field->setReadonly($this->IsReadonly());
|
|
|
|
$field->setForm($this->getForm());
|
2012-01-02 16:19:22 +01:00
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
return parent::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 );
|
2016-07-25 07:24:26 +02:00
|
|
|
$parts = $this->parseValue();
|
|
|
|
if($countryField = $this->getCountryField()) {
|
|
|
|
$countryField->setValue($parts['Country']);
|
|
|
|
}
|
|
|
|
if($areaField = $this->getAreaField()) {
|
|
|
|
$areaField->setValue($parts['Area']);
|
|
|
|
}
|
|
|
|
$this->getNumberField()->setValue($parts['Number']);
|
|
|
|
if ($extensionField = $this->getExtensionField()) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-25 07:24:26 +02:00
|
|
|
$extensionField->setValue($parts['Extension']);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* Join phone number into a string
|
|
|
|
*
|
|
|
|
* @param array|string $value Input
|
|
|
|
* @return string
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public static function joinPhoneNumber( $value ) {
|
|
|
|
if( is_array( $value ) ) {
|
|
|
|
$completeNumber = '';
|
2016-07-25 07:24:26 +02:00
|
|
|
if( !empty($value['Country'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$completeNumber .= '+' . $value['Country'];
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
if( !empty($value['Area'])) {
|
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
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-25 07:24:26 +02:00
|
|
|
if( !empty($value['Extension']) ) {
|
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;
|
2016-07-25 07:24:26 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return $value;
|
2016-07-25 07:24:26 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-25 07:24:26 +02:00
|
|
|
/**
|
|
|
|
* Returns array with parsed phone format
|
|
|
|
*
|
|
|
|
* @return array Array with Country, Area, Number, and Extension keys (in order)
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function parseValue() {
|
2016-07-25 07:24:26 +02:00
|
|
|
if (is_array($this->value)) {
|
|
|
|
return $this->value;
|
2008-04-18 05:50:29 +02:00
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
// Parse value in form "+ countrycode (areacode) phone # extension"
|
|
|
|
$valid = preg_match(
|
|
|
|
'/^(?:(?:\+(?<Country>\d+))?\s*\((?<Area>\d+)\))?\s*(?<Number>[0-9A-Za-z]*)\s*(?:[#]\s*(?<Extension>\d+))?$/',
|
|
|
|
$this->value,
|
|
|
|
$parts
|
|
|
|
);
|
|
|
|
if(!$valid) {
|
|
|
|
$parts = [];
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-25 07:24:26 +02:00
|
|
|
return array(
|
|
|
|
'Country' => isset($parts['Country']) ? $parts['Country'] : '',
|
|
|
|
'Area' => isset($parts['Area']) ? $parts['Area'] : '',
|
|
|
|
'Number' => isset($parts['Number']) ? $parts['Number'] : '',
|
|
|
|
'Extension' => isset($parts['Extension']) ? $parts['Extension'] : '',
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-11 07:00:57 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2016-07-25 07:24:26 +02:00
|
|
|
$completeNumber = static::joinPhoneNumber($this->parseValue());
|
|
|
|
$record->setCastedField($this->getName(), $completeNumber);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
2016-07-25 07:24:26 +02:00
|
|
|
|
|
|
|
public function performReadonlyTransformation()
|
|
|
|
{
|
|
|
|
// Just setReadonly without casting to NumericField_Readonly
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setReadonly(true);
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function performDisabledTransformation()
|
|
|
|
{
|
|
|
|
// Just setDisabled without casting to NumericField_Disabled
|
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setDisabled(true);
|
|
|
|
return $clone;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|