1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Divine CMS - Open source CMS for widespread use. |
4
|
|
|
Copyright (c) 2019 Mykola Burakov ([email protected]) |
5
|
|
|
|
6
|
|
|
See SOURCE.txt for other and additional information. |
7
|
|
|
|
8
|
|
|
This file is part of Divine CMS. |
9
|
|
|
|
10
|
|
|
This program is free software: you can redistribute it and/or modify |
11
|
|
|
it under the terms of the GNU General Public License as published by |
12
|
|
|
the Free Software Foundation, either version 3 of the License, or |
13
|
|
|
(at your option) any later version. |
14
|
|
|
|
15
|
|
|
This program is distributed in the hope that it will be useful, |
16
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
GNU General Public License for more details. |
19
|
|
|
|
20
|
|
|
You should have received a copy of the GNU General Public License |
21
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
22
|
|
|
|
23
|
|
|
class ModelLocalisationCountry extends \Divine\Engine\Core\Model |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
public function addCountry($data) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$this->db->query(" |
28
|
|
|
INSERT INTO country |
29
|
|
|
SET name = '" . $this->db->escape($data['name']) . "', |
30
|
|
|
iso_code_2 = '" . $this->db->escape($data['iso_code_2']) . "', |
31
|
|
|
iso_code_3 = '" . $this->db->escape($data['iso_code_3']) . "', |
32
|
|
|
address_format = '" . $this->db->escape($data['address_format']) . "', |
33
|
|
|
postcode_required = '" . (int)$data['postcode_required'] . "', |
34
|
|
|
status = '" . (int)$data['status'] . "' |
35
|
|
|
"); |
36
|
|
|
|
37
|
|
|
$this->cache->delete('country'); |
38
|
|
|
|
39
|
|
|
return $this->db->getLastId(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function editCountry($country_id, $data) |
43
|
|
|
{ |
44
|
|
|
$this->db->query(" |
45
|
|
|
UPDATE country |
46
|
|
|
SET name = '" . $this->db->escape($data['name']) . "', |
47
|
|
|
iso_code_2 = '" . $this->db->escape($data['iso_code_2']) . "', |
48
|
|
|
iso_code_3 = '" . $this->db->escape($data['iso_code_3']) . "', |
49
|
|
|
address_format = '" . $this->db->escape($data['address_format']) . "', |
50
|
|
|
postcode_required = '" . (int)$data['postcode_required'] . "', |
51
|
|
|
status = '" . (int)$data['status'] . "' |
52
|
|
|
WHERE country_id = '" . (int)$country_id . "' |
53
|
|
|
"); |
54
|
|
|
|
55
|
|
|
$this->cache->delete('country'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function deleteCountry($country_id) |
59
|
|
|
{ |
60
|
|
|
$this->db->query(" |
61
|
|
|
DELETE |
62
|
|
|
FROM country |
63
|
|
|
WHERE country_id = '" . (int)$country_id . "' |
64
|
|
|
"); |
65
|
|
|
|
66
|
|
|
$this->cache->delete('country'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getCountry($country_id) |
70
|
|
|
{ |
71
|
|
|
$query = $this->db->query(" |
72
|
|
|
SELECT DISTINCT * |
73
|
|
|
FROM country |
74
|
|
|
WHERE country_id = '" . (int)$country_id . "' |
75
|
|
|
"); |
76
|
|
|
|
77
|
|
|
return $query->row; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getCountries($data = array()) |
81
|
|
|
{ |
82
|
|
|
if ($data) { |
83
|
|
|
$sql = " |
|
|
|
|
84
|
|
|
SELECT * |
85
|
|
|
FROM country |
86
|
|
|
"; |
87
|
|
|
|
88
|
|
|
$sort_data = array( |
89
|
|
|
'name', |
90
|
|
|
'iso_code_2', |
91
|
|
|
'iso_code_3' |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { |
95
|
|
|
$sql .= " ORDER BY " . $data['sort']; |
|
|
|
|
96
|
|
|
} else { |
97
|
|
|
$sql .= " ORDER BY name"; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (isset($data['order']) && ($data['order'] == 'DESC')) { |
101
|
|
|
$sql .= " DESC"; |
|
|
|
|
102
|
|
|
} else { |
103
|
|
|
$sql .= " ASC"; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (isset($data['start']) || isset($data['limit'])) { |
107
|
|
|
if ($data['start'] < 0) { |
108
|
|
|
$data['start'] = 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($data['limit'] < 1) { |
112
|
|
|
$data['limit'] = 20; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$query = $this->db->query($sql); |
119
|
|
|
|
120
|
|
|
return $query->rows; |
121
|
|
|
} else { |
122
|
|
|
$country_data = $this->cache->get('country.admin'); |
123
|
|
|
|
124
|
|
|
if (!$country_data) { |
125
|
|
|
$query = $this->db->query(" |
|
|
|
|
126
|
|
|
SELECT * |
127
|
|
|
FROM country |
128
|
|
|
ORDER BY name ASC |
129
|
|
|
"); |
130
|
|
|
|
131
|
|
|
$country_data = $query->rows; |
132
|
|
|
|
133
|
|
|
$this->cache->set( |
134
|
|
|
'country.admin', |
135
|
|
|
$country_data |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $country_data; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getTotalCountries() |
144
|
|
|
{ |
145
|
|
|
$query = $this->db->query(" |
|
|
|
|
146
|
|
|
SELECT COUNT(*) AS total |
147
|
|
|
FROM country |
148
|
|
|
"); |
149
|
|
|
|
150
|
|
|
return $query->row['total']; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
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.