forked from graphis/openclassifieds2
/
map.php
executable file
·179 lines (142 loc) · 5.98 KB
/
map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Map extends Controller {
public function action_index()
{
$this->before('/pages/maps');
$this->template->title = __('Map');
$this->template->height = Core::get('height','100%');
$this->template->width = Core::get('width','100%');
$this->template->zoom = Core::get('zoom',core::config('advertisement.map_zoom'));
$this->template->height_thumb = Core::config('image.height_thumb')/4;
$this->template->width_thumb = Core::config('image.width_thumb')/4;
if (Model_User::get_userlatlng())
{
$this->template->center_lon = $_COOKIE['mylng'];
$this->template->center_lat = $_COOKIE['mylat'];
}
else
{
$this->template->center_lon = Core::get('lon',core::config('advertisement.center_lon'));
$this->template->center_lat = Core::get('lat',core::config('advertisement.center_lat'));
}
$ads = new Model_Ad();
$ads->where('status','=',Model_Ad::STATUS_PUBLISHED)
->where('address','IS NOT',NULL)
->where('latitude','IS NOT',NULL)
->where('longitude','IS NOT',NULL);
//filter by category
if (core::get('category')!==NULL)
{
$category = new Model_Category();
$category->where('seoname','=',core::get('category'))->cached()->limit(1)->find();
if ($category->loaded())
$ads->where('id_category', 'IN', $category->get_siblings_ids());
}
//filter by location
if (core::get('location')!==NULL)
{
$location = new Model_location();
$location->where('seoname','=',core::get('location'))->cached()->limit(1)->find();
if ($location->loaded())
$ads->where('id_location', 'IN', $location->get_siblings_ids());
}
//if only 1 ad
if (is_numeric(core::get('id_ad')))
$ads = $ads->where('id_ad','=',core::get('id_ad'));
$ads = $ads->order_by('published','desc')
->limit(Core::config('advertisement.map_elements'))
->find_all();
$this->template->ads = $ads;
}
/**
* get geocode lat/lon points for given address from google
*
* @param string $address
* @return bool|array false if can't be geocoded, array or geocdoes if successful
*/
public static function address_coords($address)
{
$url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address='.rawurlencode($address);
//try to get the json from the cache
$coords = Core::cache($url);
//not cached :(
if ($coords === NULL)
{
$coords = FALSE;
//get contents from google
if($result = core::curl_get_contents($url))
{
$result = json_decode($result);
//not found :()
if($result->status!="OK")
$coords = FALSE;
else
{
$coords['lat'] = $result->results[0]->geometry->location->lat;
$coords['lon'] = $result->results[0]->geometry->location->lng;
}
}
//save the json
Core::cache($url,$coords,strtotime('+7 day'));
}
return $coords;
}
public function action_index2()
{
require_once Kohana::find_file('vendor', 'php-googlemap/GoogleMap','php');
$this->before('/pages/maps');
$this->template->title = __('Map');
$height = Core::get('height','100%');
$width = Core::get('width','100%');
$map = new GoogleMapAPI();
$map->setWidth($width);
$map->setHeight($height);
$map->disableSidebar();
$map->setMapType('map');
$map->setZoomLevel(Core::get('zoom',core::config('advertisement.map_zoom')));
//$map->mobile = TRUE;
$atributes = array("target='_blank'");
if ( core::get('controls')==0 )
{
$map->disableMapControls();
$map->disableTypeControls();
$map->disableScaleControl();
$map->disableZoomEncompass();
$map->disableStreetViewControls();
$map->disableOverviewControl();
}
//only 1 marker
if ( core::get('address')!='' )
{
$map->addMarkerByAddress(core::get('address'), core::get('address'));
}
else
{
//last ads, you can modify this value at: advertisement.feed_elements
$ads = DB::select('a.seotitle')
->select(array('c.seoname','category'),'a.title','a.published','a.address')
->from(array('ads', 'a'))
->join(array('categories', 'c'),'INNER')
->on('a.id_category','=','c.id_category')
->where('a.status','=',Model_Ad::STATUS_PUBLISHED)
->where('a.address','IS NOT',NULL)
->order_by('published','desc')
->limit(Core::config('advertisement.map_elements'))
->as_object()
->cached()
->execute();
foreach($ads as $a)
{
//d($a);
if (strlen($a->address)>3)
{
$url= Route::url('ad', array('category'=>$a->category,'seotitle'=>$a->seotitle));
$map->addMarkerByAddress($a->address, $a->title, HTML::anchor($url, $a->title, $atributes) );
}
}
//only center if not a single ad
$map->setCenterCoords(Core::get('lon',core::config('advertisement.center_lon')),Core::get('lat',core::config('advertisement.center_lat')));
}
$this->template->map = $map;
}
} // End map