|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\PhoneNumber; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Forms\FieldGroup; |
|
7
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
8
|
|
|
use SilverStripe\Forms\DropdownField; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A country/phone combo field |
|
12
|
|
|
*/ |
|
13
|
|
|
class CountryPhoneField extends FieldGroup |
|
14
|
|
|
{ |
|
15
|
|
|
protected $dataformat = null; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($name, $title = null, $value = null) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$country = new DropdownField($name . "[CountryCode]", ""); |
|
20
|
|
|
$country->setSource(PhoneHelper::getCountriesList()); |
|
21
|
|
|
$country->setHasEmptyDefault(true); |
|
22
|
|
|
$country->setAttribute('style', 'max-width:166px'); // Match FieldGroup min width |
|
23
|
|
|
$country->setAttribute('size', 1); // fix some weird sizing issue in cms |
|
24
|
|
|
|
|
25
|
|
|
$number = new PhoneField($name . "[Number]", ""); |
|
26
|
|
|
|
|
27
|
|
|
parent::__construct($title, $country, $number); |
|
28
|
|
|
|
|
29
|
|
|
$this->name = $name; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function hasData() |
|
34
|
|
|
{ |
|
35
|
|
|
// Turn this into a datafield |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return DropdownField |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getCountryField() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->fieldByName($this->name . "[CountryCode]"); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return PhoneField |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getPhoneField() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->fieldByName($this->name . "[Number]"); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDataFormat() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->dataformat; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setDataFormat($v) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->dataformat = $v; |
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function setValue($value, $data = null) |
|
67
|
|
|
{ |
|
68
|
|
|
// An array of value to assign to sub fields |
|
69
|
|
|
if (is_array($value)) { |
|
70
|
|
|
foreach ($value as $k => $v) { |
|
71
|
|
|
$this->fieldByName($this->name . "[$k]")->setValue($v); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
// It's an international number |
|
76
|
|
|
if (strpos((string)$value, '+') === 0) { |
|
77
|
|
|
$util = PhoneHelper::getPhoneNumberUtil(); |
|
78
|
|
|
try { |
|
79
|
|
|
$number = $util->parse($value); |
|
80
|
|
|
$regionCode = $util->getRegionCodeForNumber($number); |
|
81
|
|
|
$this->getCountryField()->setValue($regionCode); |
|
82
|
|
|
$phone = $util->format($number, PhoneNumberFormat::NATIONAL); |
|
83
|
|
|
$this->getPhoneField()->setValue($phone); |
|
84
|
|
|
} catch (Exception $ex) { |
|
85
|
|
|
// We were unable to parse, simply set the value as is |
|
86
|
|
|
$this->getPhoneField()->setValue($phone); |
|
87
|
|
|
} |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->getPhoneField()->setValue($value); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Value in E164 format (no formatting) |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function dataValue() |
|
99
|
|
|
{ |
|
100
|
|
|
$countryValue = $this->getCountryField()->Value(); |
|
101
|
|
|
$phoneValue = $this->getPhoneField()->Value(); |
|
102
|
|
|
if (!$phoneValue) { |
|
103
|
|
|
return ''; |
|
104
|
|
|
} |
|
105
|
|
|
if (!$countryValue && strpos((string)$phoneValue, '+') !== 0) { |
|
106
|
|
|
return $phoneValue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$util = PhoneHelper::getPhoneNumberUtil(); |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
$number = $util->parse($phoneValue, $countryValue); |
|
113
|
|
|
$format = $this->dataformat ?? PhoneNumberFormat::E164; |
|
114
|
|
|
return $util->format($number, $format); |
|
115
|
|
|
} catch (Exception $ex) { |
|
116
|
|
|
// We were unable to parse, simply return the value as is |
|
117
|
|
|
return $phoneValue; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.