source: trunk/workflow/inc/local/classes/class.wf_location.php @ 7515

Revision 7515, 5.3 KB checked in by rafaelgobara, 11 years ago (diff)

Ticket #3168 - Ajuste para cidades que possuem apostofre no nome.

  • Property svn:executable set to *
Line 
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
9class 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                if (is_object($result)) {
164                while ($row = $result->fetchRow())
165                        $output[] = $row;
166
167                $this->citiesFromState[$state_id] = $output;
168                }
169                return $output;
170        }
171    /**
172         * Busca as cidades por parte do nome, sem considerar maiúsculas e/ou minúsculas e nem acentuação (retorna 10 resultados)
173         * @param string Parte do nome da cidade
174         * @param int $state_id Numero ID do estado
175         * @param bool $include_districts True, busca cidades e distritos. False, busca apenas cidades.
176         * @return mixed (array ou boolean)
177         * @access public
178         */
179        function getCitiesByKey($key, $state_id = 0, $include_districts = true, $hasAccent = true)
180        {
181                if (!is_string($key) || !is_numeric($state_id) || !is_bool($include_districts))
182                        return false;
183
184                $where = "";
185                if($state_id > 0){
186                        $where = " AND c.id_state = " . $state_id;
187                }
188
189                if(!$include_districts){
190                        $where = " AND c.is_district = 'F' ";
191                }
192
193                $city_name = 'c.city_name AS city_name,';
194
195                if(!$hasAccent)
196                {
197                        $city_name = 'TO_ASCII(c.city_name) AS city_name,';
198                }
199
200                $sql =
201                "SELECT
202                        c.id_city AS id_city,
203                        $city_name
204                        s.id_state AS id_state,
205                        s.state_name AS state_name,
206                        s.state_symbol AS state_symbol
207                FROM
208                        phpgw_cc_state s,
209                        phpgw_cc_city c
210                WHERE
211                        c.id_state = s.id_state AND
212                        s.id_country = 'BR' AND
213                        TO_ASCII(c.city_name) ILIKE TO_ASCII('" . pg_escape_string($key) . "%')
214                        " . $where . "
215                ORDER BY
216                        city_name
217                LIMIT 10";
218
219                $result = $this->db->query($sql);
220                $output = array();
221                while ($row = $result->fetchRow())
222                        $output[] = $row;
223
224                return $output;
225        }
226        /**
227         * Busca os estados brasileiros
228         * @return mixed (array ou boolean)
229         * @access public
230         */
231        function getStates()
232        {
233                $sql =
234                "SELECT
235                        id_state,
236                        state_name
237                FROM
238                        phpgw_cc_state
239                WHERE
240                        id_country = 'BR'
241                ORDER BY
242                        state_name";
243
244                $result = $this->db->query($sql);
245                $output = array();
246                while ($row = $result->fetchRow())
247                        $output[] = $row;
248
249                return $output;
250        }
251}
252?>
Note: See TracBrowser for help on using the repository browser.