forked from HighwayofLife/PyroStreams-Geocoder-Field-Type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.geocoder.php
151 lines (130 loc) · 5.19 KB
/
field.geocoder.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
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* PyroStreams Geocoder Field Type
*
* Generate longitude and latitude from a specified location
*
* @package PyroStreams
* @author David Lewis
* @author Jose Fonseca
* @copyright Copyright (c) 2012, David Lewis
*/
class Field_geocoder {
public $field_type_slug = 'geocoder';
public $db_col_type = 'varchar';
public $version = '1.0.0';
public $author = array('name' => 'David Lewis', 'url' => 'http://jedihorsemanship.com');
// --------------------------------------------------------------------------
/**
* Output form input
*
* @access public
* @param $data array
* @return string
*/
public function form_output($data) {
$value = json_decode($data['value']);
$options = array(
'name' => $data['form_slug'],
'id' => $data['form_slug'],
'value' => $data['value'],
'type' => 'hidden',
);
$html = "";
/** Add assets if not admin * */
if ($this->CI->uri->segment(1) !== "admin") {
$html .= '<script src="//maps.google.com/maps/api/js?sensor=false"></script>';
$html .= '<link rel="stylesheet" href="' . base_url('streams_core/field_asset/css/geocoder/geocoder.css') . '" />';
$html .= '<script type="text/javascript" src="' . base_url('streams_core/field_asset/js/geocoder/geocoder.js') . '"></script>';
$html .= '<script type="text/javascript">$(function() {initialize("' . $data['form_slug'] . '");});</script>';
}
$html .= '<span id="' . $data['form_slug'] . '_msg" class="stream_map_msg"></span>';
$html .= '<div id="' . $data['form_slug'] . '_map" class="stream_map"></div>';
$options_input = array(
'id' => $data['form_slug'] . '_input',
// jhubb81 added name attribute to generated address_input to allow for proper javascript validation
'name' => $data['form_slug'] . '_input',
# Check if addres exists to maintain backward compatibility
'value' => (!empty($value->address)) ? $value->address : $data['value'],
'type' => 'text',
);
return form_input($options) . form_input($options_input) . $html;
}
// --------------------------------------------------------------------------
/**
* Tag output variables
*
* Outputs 'latitude' & 'longitude' variables
*
* @access public
* @param string
* @param array
* @return array
*/
public function pre_output_plugin($input, $data) {
if (!$input)
return null;
$location = json_decode($input);
# Maintain backward compatability
if (!is_object($location)) {
$pieces = explode(',', $input);
if (count($pieces) != 2)
return null;
$array = array(
'lat' => $pieces[0],
'lng' => $pieces[1],
'address' => null,
);
$location = (object) $array;
}
/** HTML to show map * */
$html = '<script src="//maps.google.com/maps/api/js?sensor=false"></script>';
$html .= '<link rel="stylesheet" href="' . base_url('streams_core/field_asset/css/geocoder/geocoder.css') . '" />';
$html .= '<script type="text/javascript" src="' . base_url('streams_core/field_asset/js/geocoder/geocoder.js') . '"></script>';
$html .= '<script type="text/javascript">$(function() {initialize("' . $data['field_slug'] . '");});</script>';
$html .= '<span id="' . $data['field_slug'] . '_msg" class="stream_map_msg"></span>';
$html .= '<div id="' . $data['field_slug'] . '_map" class="stream_map"></div>';
$options_input = array(
'id' => $data['field_slug'] . '_input',
// jhubb81 added name attribute to generated address_input to allow for proper javascript validation
'name' => $data['field_slug'] . '_input',
# Check if addres exists to maintain backward compatibility
'value' => (!empty($location->address)) ? $location->address : "",
'type' => 'hidden',
);
$html .= form_input($options_input);
$data = array(
'latitude' => $location->lat,
'longitude' => $location->lng,
'lat' => $location->lat,
'lng' => $location->lng,
'address' => $location->address,
'html' => $html
);
return $data;
}
/**
* Event
*
* Load assets
*
* @access public
* @param $field object
* @return void
*/
public function event($field) {
$this->CI->type->add_misc('<script src="//maps.google.com/maps/api/js?sensor=false"></script>');
$this->CI->type->add_js('geocoder', 'geocoder.js');
$this->CI->type->add_css('geocoder', 'geocoder.css');
$this->CI->type->add_misc('<script type="text/javascript">
$(document).ready(function() {
initialize("' . $field->field_slug . '");
$(".tabs").bind("tabsshow", function(event, ui) {
google.maps.event.trigger(map, "resize");
mapLocation();
});
});
</script>');
}
}