/
PhoneNumberField.php
147 lines (114 loc) · 3.56 KB
/
PhoneNumberField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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 {
protected $areaCode;
protected $countryCode;
protected $ext;
public function __construct( $name, $title = null, $value = '', $extension = null, $areaCode = null, $countryCode = null) {
$this->areaCode = $areaCode;
$this->ext = $extension;
$this->countryCode = $countryCode;
parent::__construct($name, $title, $value);
}
public function Field() {
$fields = new FieldGroup( $this->name );
$fields->setID("{$this->name}_Holder");
list($countryCode, $areaCode, $phoneNumber, $extension) = $this->parseValue();
$hasTitle = false;
if ($this->value=="") {
$countryCode=$this->countryCode;
$areaCode=$this->areaCode;
$extension=$this->ext;
}
if($this->countryCode !== null) {
$fields->push(new NumericField($this->name.'[Country]', '+', $countryCode, 4));
}
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));
}
if($this->ext !== null) {
$field->push(new NumericField( $this->name.'[Extension]', 'ext', $extension, 6));
}
foreach($fields as $field) {
$field->setDisabled($this->isDisabled());
$field->setReadonly($this->isReadonly());
}
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']) && $value['Country']!=null) {
$completeNumber .= '+' . $value['Country'];
}
if( isset($value['Area']) && $value['Area']!=null) {
$completeNumber .= '(' . $value['Area'] . ')';
}
$completeNumber .= $value['Number'];
if( isset($value['Extension']) && $value['Extension']!=null) {
$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 );
for ($x=0;$x<=3;$x++) {
if (!isset($parts[$x])) $parts[]='';
}
return $parts;
}
public function saveInto(DataObjectInterface $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 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;
}
}