root/g-map/files/lib/data/gmap/GmapApi.class.php @ 1306

Revision 1306, 2.4 kB (checked in by Torben Brodt, 2 years ago)

working on custom input fields for g-map

Line 
1<?php
2
3/**
4 * gets several positions and returns a clustered array
5 *
6 * @author      Torben Brodt
7 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
8 */
9class GmapApi extends DatabaseObject {
10        protected $apikey = null;
11        protected $cache_search = array();
12
13        /**
14         * creates new instance
15         */
16        public function __construct() {
17                $apikey = GMAP_API_KEY;
18                $apikey = explode("\n", StringUtil::unifyNewlines($apikey));
19                $apikey = $apikey[0];
20                $apikey = explode(":", $apikey);
21               
22                // this is the way, woltlab wants the users to enter their api key
23                if(count($apikey) == 2) {
24                        $this->apikey = $apikey[1];
25                }
26
27                // this is the way, almost all of the woltlab users really enter their api key
28                else if(!empty($apikey)) {
29                        $this->apikey = $apikey[0];
30                }
31        }
32
33        /**
34         * is active? api key existent?
35         *
36         * @return      boolean
37         */
38        public function isActive() {
39                return !empty($this->apikey);
40        }
41       
42        public function getFields() {
43                if(defined('GMAP_CUSTOMINPUT') && !empty(GMAP_CUSTOMINPUT)) {
44                        $cols = explode(",", GMAP_CUSTOMINPUT);
45                } else {
46                        $cols = array('location');
47                }
48                return $cols;
49        }
50       
51        public function getColumn() {
52                $cols = array();
53                foreach($this->getFields() as $field) {
54                        $cols[] = User::getUserOptionID($field);
55                }
56
57                $col = array();
58                foreach($cols as $id) {
59                        $col[] = 'CONCAT(userOption'.$id.', IF(userOption'.$id.' = '', '', ' '))';
60                }
61                $col = 'CONCAT('.implode(',', $col).')';
62               
63                return $col;
64        }
65       
66        /**
67         * ask google for geopositions
68         *
69         * @param       $location               string
70         * @return                              array<float>    keys are lat and lon
71         */
72        public function search($location) {
73
74                if(!$this->isActive()) {
75                        return;
76                }
77
78                $lookupstring = urlencode(StringUtil::trim($location));
79                if(isset($this->cache_search[$lookupstring])) {
80                        return $this->cache_search[$lookupstring];
81                }
82               
83                $url = "http://maps.google.com/maps/geo?q=".$lookupstring."&key=".$this->apikey."&output=csv";
84               
85                require_once(WCF_DIR.'lib/util/FileUtil.class.php');
86                $res = array();
87                try {
88                        $tmp = FileUtil::downloadFileFromHttp($url, 'gmap.search');
89                        foreach(file($tmp) as $row) {
90                                if (preg_match('/^200,[^,]+,([^,]+),([^,]+)$/', $row, $hits)) {
91                                        $res = array(
92                                                'lat' => trim($hits[1]),
93                                                'lon' => trim($hits[2])
94                                        );
95                                        break;
96                                }
97                        }
98                        @unlink($tmp);
99                } catch(Exception $e) {
100                        error_log($e->getMessage());
101                }
102
103                return $this->cache_search[$lookupstring] = $res;
104        }
105}
Note: See TracBrowser for help on using the browser.