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
|
|
|
* This method is accessed by other pages! |
29
|
|
|
* |
30
|
|
|
* @param int $countryCode (e.g. 64) |
|
|
|
|
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function TellLink($countryCode = "") |
35
|
|
|
{ |
36
|
|
|
//remove non digits |
37
|
|
|
if (!$countryCode) { |
38
|
|
|
$countryCode = $this->Config()->default_country_code; |
39
|
|
|
} |
40
|
|
|
//remove non-digits |
41
|
|
|
$phoneNumber = preg_replace('/\D/', '', $this->value); |
42
|
|
|
//remove country code with plus |
43
|
|
|
$phoneNumber = $this->literalLeftTrim($phoneNumber, '+'.$countryCode); |
44
|
|
|
//remove country code |
45
|
|
|
$phoneNumber = $this->literalLeftTrim($phoneNumber, $countryCode); |
46
|
|
|
//remove leading zero |
47
|
|
|
$phoneNumber = $this->literalLeftTrim($phoneNumber, '0'); |
48
|
|
|
//combine |
49
|
|
|
$phoneNumber = 'tel:+'.$countryCode.$phoneNumber; |
50
|
|
|
|
51
|
|
|
return $phoneNumber; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @param countryCode $countryCode (e.g. 64) |
|
|
|
|
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function CallToLink($countryCode = "") |
63
|
|
|
{ |
64
|
|
|
return str_replace('tel:', 'callto:', $this->TellLink($countryCode)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see DBField::scaffoldFormField() |
69
|
|
|
* |
70
|
|
|
* @param string $title (optional) |
|
|
|
|
71
|
|
|
* @param array $params (optional) |
|
|
|
|
72
|
|
|
* |
73
|
|
|
* @return PhoneNumberField | NullableField |
74
|
|
|
*/ |
75
|
|
|
public function scaffoldFormField($title = null, $params = null) |
76
|
|
|
{ |
77
|
|
|
if (!$this->nullifyEmpty) { |
78
|
|
|
return NullableField::create(PhoneNumberField::create($this->name, $title)); |
79
|
|
|
} else { |
80
|
|
|
return PhoneNumberField::create($this->name, $title); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function literalLeftTrim($str, $prefix) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (substr($str, 0, strlen($prefix)) == $prefix) { |
87
|
|
|
$str = substr($str, strlen($prefix)); |
88
|
|
|
} |
89
|
|
|
return $str; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
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.