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

Revision 1326, 2.6 kB (checked in by Torben Brodt, 2 years ago)

fixes for custom gmap input

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        /**
43         * @var array<string>
44         */
45        public function getFields() {
46                if(defined('GMAP_CUSTOMINPUT') && $const = GMAP_CUSTOMINPUT && !empty($const)) {
47                        $tmp = explode(",", GMAP_CUSTOMINPUT);
48                        $cols = array();
49                        foreach($tmp as $field) {
50                                $col = User::getUserOptionID($field);
51                                if($col) {
52                                        $cols[] = $field;
53                                }
54                        }
55                } else {
56                        $cols = array('location');
57                }
58                return $cols;
59        }
60       
61        /**
62         * @var string
63         */
64        public function getColumn() {
65                $cols = array();
66                foreach($this->getFields() as $field) {
67                        $cols[] = User::getUserOptionID($field);
68                }
69
70                $col = array();
71                foreach($cols as $id) {
72                        $col[] = 'CONCAT(userOption'.$id.', IF(userOption'.$id.' = "", "", " "))';
73                }
74                $col = 'CONCAT('.implode(',', $col).')';
75               
76                return $col;
77        }
78       
79        /**
80         * ask google for geopositions
81         *
82         * @param       $location               string
83         * @return                              array<float>    keys are lat and lon
84         */
85        public function search($location) {
86
87                if(!$this->isActive()) {
88                        return;
89                }
90
91                $lookupstring = urlencode(StringUtil::trim($location));
92                if(isset($this->cache_search[$lookupstring])) {
93                        return $this->cache_search[$lookupstring];
94                }
95               
96                $url = "http://maps.google.com/maps/geo?q=".$lookupstring."&key=".$this->apikey."&output=csv";
97               
98                require_once(WCF_DIR.'lib/util/FileUtil.class.php');
99                $res = array();
100                try {
101                        $tmp = FileUtil::downloadFileFromHttp($url, 'gmap.search');
102                        foreach(file($tmp) as $row) {
103                                if (preg_match('/^200,[^,]+,([^,]+),([^,]+)$/', $row, $hits)) {
104                                        $res = array(
105                                                'lat' => trim($hits[1]),
106                                                'lon' => trim($hits[2])
107                                        );
108                                        break;
109                                }
110                        }
111                        @unlink($tmp);
112                } catch(Exception $e) {
113                        error_log($e->getMessage());
114                }
115
116                return $this->cache_search[$lookupstring] = $res;
117        }
118}
Note: See TracBrowser for help on using the browser.