|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Represents a decimal field from 0-1 containing a percentage value. |
|
4
|
|
|
* |
|
5
|
|
|
* Example instantiation in {@link DataObject::$db}: |
|
6
|
|
|
* <code> |
|
7
|
|
|
* static $db = array( |
|
8
|
|
|
* "SuccessRatio" => "Percentage", |
|
9
|
|
|
* "ReallyAccurate" => "Percentage(6)", |
|
10
|
|
|
* ); |
|
11
|
|
|
* </code> |
|
12
|
|
|
* |
|
13
|
|
|
* @package framework |
|
14
|
|
|
* @subpackage model |
|
15
|
|
|
*/ |
|
16
|
|
|
class USPhoneNumber extends Varchar |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return string e.g. (555) 555-5555 |
|
22
|
|
|
*/ |
|
23
|
|
|
public function Nice() |
|
24
|
|
|
{ |
|
25
|
|
|
$val = $this->cleanInput($this->value); |
|
26
|
|
|
|
|
27
|
|
|
return "(".substr($val, 0, 3).") ".substr($val, 3, 3)."-".substr($val, 6); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @param boolean $includeIntprefix (includes +1 at the beginning of the number) |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function TelLink($includeIntprefix = false) |
|
37
|
|
|
{ |
|
38
|
|
|
$str = 'tel:' . $this->initializePhoneString($includeIntprefix); |
|
39
|
|
|
return $str . $this->removeNonNumbericChars($this->value); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param boolean $includeIntprefix (includes +1 at the beginning of the number) |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function CallToLink($includeIntprefix = false) |
|
48
|
|
|
{ |
|
49
|
|
|
$str = 'callto:' . $this->initializePhoneString($includeIntprefix); |
|
50
|
|
|
return $str . $this->removeNonNumbericChars($this->value); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param boolean $includeIntprefix (includes +1 at the beginning of the number) |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function initializePhoneString($includeIntprefix){ |
|
59
|
|
|
$str = ''; |
|
60
|
|
|
if($includeIntprefix){ |
|
61
|
|
|
$str = '+1'; |
|
62
|
|
|
} |
|
63
|
|
|
return $str; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $str |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function removeNonNumbericChars($str){ |
|
72
|
|
|
return preg_replace('/[^0-9]/', '', $str); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* |
|
76
|
|
|
* @param DataObject $dataObject |
|
77
|
|
|
*/ |
|
78
|
|
|
public function saveInto($dataObject) |
|
79
|
|
|
{ |
|
80
|
|
|
parent::saveInto($dataObject); |
|
81
|
|
|
$fieldName = $this->name; |
|
82
|
|
|
$dataObject->$fieldName = $this->cleanInput($dataObject->$fieldName); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function prepValueForDB($value) |
|
86
|
|
|
{ |
|
87
|
|
|
$value = $this->cleanInput($value); |
|
88
|
|
|
return parent::prepValueForDB($value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $val |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function cleanInput($val) |
|
97
|
|
|
{ |
|
98
|
|
|
return preg_replace("/[^0-9]/", "", $val); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* (non-PHPdoc) |
|
104
|
|
|
* @see DBField::scaffoldFormField() |
|
105
|
|
|
*/ |
|
106
|
|
|
public function scaffoldFormField($title = null, $params = null) |
|
107
|
|
|
{ |
|
108
|
|
|
if (!$this->nullifyEmpty) { |
|
109
|
|
|
// Allow the user to select if it's null instead of automatically assuming empty string is |
|
110
|
|
|
return new NullableField(new USPhoneNumberField($this->name, $title)); |
|
111
|
|
|
} else { |
|
112
|
|
|
// Automatically determine null (empty string) |
|
113
|
|
|
return new USPhoneNumberField($this->name, $title); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|