1 | <?php |
||
2 | |||
3 | namespace LeKoala\GeoTools; |
||
4 | |||
5 | use SilverStripe\ORM\DataExtension; |
||
6 | |||
7 | /** |
||
8 | * An extension that make use of our geo tools |
||
9 | * |
||
10 | * @property \SilverStripe\ORM\DataObject&\LeKoala\GeoTools\GeoExtension $owner |
||
11 | * @property float $Latitude |
||
12 | * @property float $Longitude |
||
13 | * @property string $StreetNumber |
||
14 | * @property string $StreetName |
||
15 | * @property string $StreetExtended |
||
16 | * @property string $PostalCode |
||
17 | * @property string $Locality |
||
18 | * @property string $CountryCode |
||
19 | */ |
||
20 | class GeoExtension extends DataExtension |
||
21 | { |
||
22 | /** |
||
23 | * @var array<string, string> |
||
24 | */ |
||
25 | private static $db = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
26 | // Coordinates |
||
27 | 'Latitude' => 'Float(10,6)', |
||
28 | 'Longitude' => 'Float(10,6)', |
||
29 | // Address |
||
30 | 'StreetNumber' => 'Varchar(50)', |
||
31 | 'StreetName' => 'Varchar(255)', |
||
32 | 'StreetExtended' => 'Varchar(255)', |
||
33 | 'PostalCode' => 'Varchar(32)', |
||
34 | 'Locality' => 'Varchar(255)', |
||
35 | 'CountryCode' => 'Country', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @var boolean |
||
40 | */ |
||
41 | public static $disable_auto_geocode = false; |
||
42 | |||
43 | /** |
||
44 | * @link http://microformats.org/wiki/adr |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getHTMLAddress() |
||
48 | { |
||
49 | $html = ''; |
||
50 | $html .= '<div class="adr">'; |
||
51 | $html .= '<div class="street-address">' . $this->owner->StreetNumber . ' ' . $this->owner->StreetName . '</div>'; |
||
52 | if ($this->owner->StreetExtended) { |
||
53 | $html .= '<div class="extended-address">' . $this->owner->StreetExtended . '</div>'; |
||
54 | } |
||
55 | $html .= '<span class="locality">' . $this->owner->Locality . '</span>,'; |
||
56 | $html .= '<span class="postal-code">' . $this->owner->PostalCode . '</span>'; |
||
57 | $html .= '<div class="country-name">' . $this->getCountryName() . '</div>'; |
||
58 | $html .= '</div>'; |
||
59 | |||
60 | return $html; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $code |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function IsCountry($code) |
||
68 | { |
||
69 | return $this->owner->CountryCode == $code; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getCountryName() |
||
76 | { |
||
77 | return $this->owner->dbObject('CountryCode')->getCountryName(); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get location (number street) |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getStreet() |
||
86 | { |
||
87 | return trim($this->owner->StreetNumber . ' ' . $this->owner->StreetName); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get location (city, country) |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getLocation() |
||
96 | { |
||
97 | return trim($this->owner->PostalCode . ' ' . $this->owner->Locality . ', ' . $this->getCountryName(), ' ,'); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get location (city, country) on multiple lines |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getLocationLines() |
||
106 | { |
||
107 | return trim($this->owner->PostalCode . ' ' . $this->owner->Locality . "\n" . $this->getCountryName(), ' ,'); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get location (city, country) with br as line return |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getLocationBr() |
||
116 | { |
||
117 | return trim($this->owner->PostalCode . ' ' . $this->owner->Locality . "<br/>" . $this->getCountryName(), ' ,'); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Full address on 1 line |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getAddress() |
||
126 | { |
||
127 | return trim($this->getStreet() . ', ' . $this->getLocation()); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Full address on multiple lines |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getAddressLines() |
||
136 | { |
||
137 | return trim($this->getStreet() . "\n" . $this->getLocationLines()); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Full address with br as line return |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getAddressBr() |
||
146 | { |
||
147 | return trim($this->getStreet() . "<br/>" . $this->getLocationBr()); |
||
148 | } |
||
149 | } |
||
150 |