|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* you can now use the following in your silverstripe templates |
|
4
|
|
|
* $MyPhoneField.TellLink |
|
5
|
|
|
* which then removes the first 0 |
|
6
|
|
|
* adds country code at the end |
|
7
|
|
|
* and adds + and country code |
|
8
|
|
|
* |
|
9
|
|
|
* e.g |
|
10
|
|
|
* 09 5556789 |
|
11
|
|
|
* becomes |
|
12
|
|
|
* tel:+649555789 |
|
13
|
|
|
* |
|
14
|
|
|
* if you would like a different country code then use: |
|
15
|
|
|
* $MyPhoneField.TellLink(55) |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
class PhoneField extends Varchar |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
private static $default_country_code = "64"; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
private static $casting = array( |
|
|
|
|
|
|
23
|
|
|
"TellLink" => "Varchar", |
|
24
|
|
|
"CallToLink" => "Varchar" |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* This method is accessed by other pages! |
|
30
|
|
|
* |
|
31
|
|
|
* @param int $countryCode (e.g. 64) |
|
|
|
|
|
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function IntlFormat($countryCode = "") |
|
36
|
|
|
{ |
|
37
|
|
|
//remove non digits |
|
38
|
|
|
if (!$countryCode) { |
|
39
|
|
|
$countryCode = $this->Config()->default_country_code; |
|
40
|
|
|
} |
|
41
|
|
|
//remove non-digits |
|
42
|
|
|
$phoneNumber = preg_replace('/\D/', '', $this->value); |
|
43
|
|
|
//remove country code with plus - NOT NECESSARY |
|
44
|
|
|
//$phoneNumber = $this->literalLeftTrim($phoneNumber, '+'.$countryCode); |
|
|
|
|
|
|
45
|
|
|
//remove country code |
|
46
|
|
|
$phoneNumber = $this->literalLeftTrim($phoneNumber, $countryCode); |
|
|
|
|
|
|
47
|
|
|
//remove leading zero |
|
48
|
|
|
$phoneNumber = $this->literalLeftTrim($phoneNumber, '0'); |
|
49
|
|
|
|
|
50
|
|
|
return '+'.$countryCode.$phoneNumber; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* This method is accessed by other pages! |
|
55
|
|
|
* |
|
56
|
|
|
* @param int $countryCode (e.g. 64) |
|
|
|
|
|
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function TellLink($countryCode = "") |
|
61
|
|
|
{ |
|
62
|
|
|
return 'tel:'.$this->IntlFormat($countryCode); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @param countryCode $countryCode (e.g. 64) |
|
|
|
|
|
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function CallToLink($countryCode = "") |
|
74
|
|
|
{ |
|
75
|
|
|
return 'callto:'.$this->IntlFormat($countryCode); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @see DBField::scaffoldFormField() |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $title (optional) |
|
|
|
|
|
|
82
|
|
|
* @param array $params (optional) |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return PhoneNumberField | NullableField |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
|
|
public function scaffoldFormField($title = null, $params = null) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!$this->nullifyEmpty) { |
|
89
|
|
|
return NullableField::create(TextField::create($this->name, $title)); |
|
90
|
|
|
} else { |
|
91
|
|
|
return TextField::create($this->name, $title); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* removes a string at the start of a string, if present... |
|
97
|
|
|
* @param string $str - the haystack |
|
98
|
|
|
* @param string $prefix - the needle |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function literalLeftTrim($str, $prefix) |
|
103
|
|
|
{ |
|
104
|
|
|
if (substr($str, 0, strlen($prefix)) == $prefix) { |
|
105
|
|
|
$str = substr($str, strlen($prefix)); |
|
106
|
|
|
} |
|
107
|
|
|
return $str; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.