1 | <?php |
---|
2 | /** |
---|
3 | * Class for getting city/state information |
---|
4 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
5 | * @package Workflow |
---|
6 | * @subpackage local |
---|
7 | */ |
---|
8 | |
---|
9 | class wf_location |
---|
10 | { /** |
---|
11 | * @var object $db banco de dados |
---|
12 | * @access private |
---|
13 | */ |
---|
14 | var $db; |
---|
15 | /** |
---|
16 | * @var array $cityInfo Armazena informações sobre a cidade |
---|
17 | * @access public |
---|
18 | */ |
---|
19 | var $cityInfo; |
---|
20 | /** |
---|
21 | * @var array $stateInfo Armazena as informações sobre o Estado |
---|
22 | * @access public |
---|
23 | */ |
---|
24 | var $stateInfo; |
---|
25 | /** |
---|
26 | * @var array $citiesFromState Armazena as cidades de um estado |
---|
27 | * @access public |
---|
28 | */ |
---|
29 | var $citiesFromState; |
---|
30 | |
---|
31 | /** |
---|
32 | * Inicializa os arrays da classe |
---|
33 | * return void |
---|
34 | * @access private |
---|
35 | */ |
---|
36 | function initialize() |
---|
37 | { |
---|
38 | $this->db = &Factory::getInstance('WorkflowObjects')->getDBExpresso()->Link_ID; |
---|
39 | $this->db->setFetchMode(ADODB_FETCH_ASSOC); |
---|
40 | $this->cityInfo = array(); |
---|
41 | $this->stateInfo = array(); |
---|
42 | $this->citiesFromState = array(); |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Construtor da classe |
---|
47 | * return object |
---|
48 | * @access public |
---|
49 | */ |
---|
50 | function wf_location() |
---|
51 | { |
---|
52 | $this->initialize(); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Busca as informações da cidade pelo numero id passado |
---|
57 | * @param int $city_id Numero ID da cidade |
---|
58 | * @return mixed (array ou boolean) |
---|
59 | * @access public |
---|
60 | */ |
---|
61 | function getCityById($city_id) |
---|
62 | { |
---|
63 | if (!is_numeric($city_id)) |
---|
64 | return false; |
---|
65 | |
---|
66 | $city_id = (int) $city_id; |
---|
67 | if (isset($this->cityInfo[$city_id])) |
---|
68 | return $this->cityInfo[$city_id]; |
---|
69 | |
---|
70 | $sql = |
---|
71 | "SELECT |
---|
72 | c.id_city AS id_city, |
---|
73 | c.city_name AS city_name, |
---|
74 | c.is_district, |
---|
75 | s.id_state AS id_state, |
---|
76 | s.state_name AS state_name, |
---|
77 | s.state_symbol AS state_symbol |
---|
78 | FROM |
---|
79 | phpgw_cc_state s, |
---|
80 | phpgw_cc_city c |
---|
81 | WHERE |
---|
82 | c.id_state = s.id_state AND |
---|
83 | s.id_country = 'BR' AND |
---|
84 | c.id_city = ?"; |
---|
85 | |
---|
86 | $result = $this->db->query($sql, array($city_id)); |
---|
87 | $output = $result->fetchRow(); |
---|
88 | |
---|
89 | $this->cityInfo[$city_id] = $output; |
---|
90 | return $output; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Busca as informações do estado pelo id passado |
---|
95 | * @param int $state_id Numero ID do estado |
---|
96 | * @return mixed (array ou boolean) |
---|
97 | * @access public |
---|
98 | */ |
---|
99 | function getStateById($state_id) |
---|
100 | { |
---|
101 | if (!is_numeric($state_id)) |
---|
102 | return false; |
---|
103 | |
---|
104 | $state_id = (int) $state_id; |
---|
105 | if (isset($this->stateInfo[$state_id])) |
---|
106 | return $this->stateInfo[$state_id]; |
---|
107 | |
---|
108 | $sql = |
---|
109 | "SELECT |
---|
110 | id_state, |
---|
111 | state_name, |
---|
112 | state_symbol |
---|
113 | FROM |
---|
114 | phpgw_cc_state |
---|
115 | WHERE |
---|
116 | id_country = 'BR' AND |
---|
117 | id_state = ?"; |
---|
118 | |
---|
119 | $result = $this->db->query($sql, array($state_id)); |
---|
120 | $output = $result->fetchRow(); |
---|
121 | |
---|
122 | $this->stateInfo[$city_id] = $output; |
---|
123 | return $output; |
---|
124 | } |
---|
125 | /** |
---|
126 | * Busca as cidades de um estado |
---|
127 | * @param int $state_id Numero ID do estado |
---|
128 | * @param bool $include_districts True, busca cidades e distritos. False, busca apenas cidades. |
---|
129 | * @return mixed (array ou boolean) |
---|
130 | * @access public |
---|
131 | */ |
---|
132 | function getCitiesFromState($state_id, $include_districts = true) |
---|
133 | { |
---|
134 | if (!is_numeric($state_id) || !is_bool($include_districts)) |
---|
135 | return false; |
---|
136 | |
---|
137 | $state_id = (int) $state_id; |
---|
138 | if (isset($this->citiesFromState[$state_id])) |
---|
139 | return $this->citiesFromState[$state_id]; |
---|
140 | |
---|
141 | $where = ""; |
---|
142 | if(!$include_districts){ |
---|
143 | $where = " AND c.is_district = 'F' "; |
---|
144 | } |
---|
145 | |
---|
146 | $sql = |
---|
147 | "SELECT |
---|
148 | c.id_city AS id_city, |
---|
149 | c.city_name AS city_name, |
---|
150 | c.is_district |
---|
151 | FROM |
---|
152 | phpgw_cc_state s, |
---|
153 | phpgw_cc_city c |
---|
154 | WHERE |
---|
155 | c.id_state = s.id_state AND |
---|
156 | s.id_country = 'BR' AND |
---|
157 | c.id_state = ? " . $where . " |
---|
158 | ORDER BY |
---|
159 | city_name"; |
---|
160 | |
---|
161 | $result = $this->db->query($sql, array($state_id)); |
---|
162 | $output = array(); |
---|
163 | while ($row = $result->fetchRow()) |
---|
164 | $output[] = $row; |
---|
165 | |
---|
166 | $this->citiesFromState[$state_id] = $output; |
---|
167 | return $output; |
---|
168 | } |
---|
169 | /** |
---|
170 | * Busca as cidades por parte do nome, sem considerar maiúsculas e/ou minúsculas e nem acentuação (retorna 10 resultados) |
---|
171 | * @param string Parte do nome da cidade |
---|
172 | * @param int $state_id Numero ID do estado |
---|
173 | * @param bool $include_districts True, busca cidades e distritos. False, busca apenas cidades. |
---|
174 | * @return mixed (array ou boolean) |
---|
175 | * @access public |
---|
176 | */ |
---|
177 | function getCitiesByKey($key, $state_id = 0, $include_districts = true) |
---|
178 | { |
---|
179 | if (!is_string($key) || !is_numeric($state_id) || !is_bool($include_districts)) |
---|
180 | return false; |
---|
181 | |
---|
182 | $where = ""; |
---|
183 | if($state_id > 0){ |
---|
184 | $where = " AND c.id_state = " . $state_id; |
---|
185 | } |
---|
186 | |
---|
187 | if(!$include_districts){ |
---|
188 | $where = " AND c.is_district = 'F' "; |
---|
189 | } |
---|
190 | |
---|
191 | $sql = |
---|
192 | "SELECT |
---|
193 | c.id_city AS id_city, |
---|
194 | c.city_name AS city_name, |
---|
195 | s.id_state AS id_state, |
---|
196 | s.state_name AS state_name, |
---|
197 | s.state_symbol AS state_symbol |
---|
198 | FROM |
---|
199 | phpgw_cc_state s, |
---|
200 | phpgw_cc_city c |
---|
201 | WHERE |
---|
202 | c.id_state = s.id_state AND |
---|
203 | s.id_country = 'BR' AND |
---|
204 | TO_ASCII(c.city_name) ILIKE TO_ASCII('" . $key . "%') |
---|
205 | " . $where . " |
---|
206 | ORDER BY |
---|
207 | city_name |
---|
208 | LIMIT 10"; |
---|
209 | |
---|
210 | $result = $this->db->query($sql); |
---|
211 | $output = array(); |
---|
212 | while ($row = $result->fetchRow()) |
---|
213 | $output[] = $row; |
---|
214 | |
---|
215 | return $output; |
---|
216 | } |
---|
217 | /** |
---|
218 | * Busca os estados brasileiros |
---|
219 | * @return mixed (array ou boolean) |
---|
220 | * @access public |
---|
221 | */ |
---|
222 | function getStates() |
---|
223 | { |
---|
224 | $sql = |
---|
225 | "SELECT |
---|
226 | id_state, |
---|
227 | state_name |
---|
228 | FROM |
---|
229 | phpgw_cc_state |
---|
230 | WHERE |
---|
231 | id_country = 'BR' |
---|
232 | ORDER BY |
---|
233 | state_name"; |
---|
234 | |
---|
235 | $result = $this->db->query($sql); |
---|
236 | $output = array(); |
---|
237 | while ($row = $result->fetchRow()) |
---|
238 | $output[] = $row; |
---|
239 | |
---|
240 | return $output; |
---|
241 | } |
---|
242 | } |
---|
243 | ?> |
---|