| 1 |
<?php
|
| 2 |
// $Id: gmap_plugin_style_gmap.inc,v 1.8 2009/03/11 17:39:20 bdragon Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* GMap style plugin.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Style plugin to render a map.
|
| 11 |
*
|
| 12 |
* @ingroup views_style_plugins
|
| 13 |
*/
|
| 14 |
class gmap_plugin_style_gmap extends views_plugin_style {
|
| 15 |
/**
|
| 16 |
* Set default options
|
| 17 |
*/
|
| 18 |
function option_definition() {
|
| 19 |
$options = parent::option_definition();
|
| 20 |
|
| 21 |
$options['macro'] = array(
|
| 22 |
'default' => '[gmap ]',
|
| 23 |
);
|
| 24 |
|
| 25 |
$options['datasource'] = array(
|
| 26 |
'default' => 'location',
|
| 27 |
);
|
| 28 |
|
| 29 |
$options['markers'] = array('default' => 'static');
|
| 30 |
$options['markertype'] = array('default' => 'drupal');
|
| 31 |
|
| 32 |
$options['latfield'] = array('default' => '');
|
| 33 |
$options['lonfield'] = array('default' => '');
|
| 34 |
$options['markerfield'] = array('default' => '');
|
| 35 |
|
| 36 |
return $options;
|
| 37 |
}
|
| 38 |
|
| 39 |
function query() {
|
| 40 |
if ($this->options['datasource'] == 'location') {
|
| 41 |
$table = $this->view->query->ensure_table('location');
|
| 42 |
$this->view->query->add_field($table, 'latitude', 'gmap_lat');
|
| 43 |
$this->view->query->add_field($table, 'longitude', 'gmap_lon');
|
| 44 |
}
|
| 45 |
|
| 46 |
if ($this->options['markers'] == 'nodetype') {
|
| 47 |
$this->view->query->add_field('node', 'type', 'gmap_node_type');
|
| 48 |
}
|
| 49 |
else if ($this->options['markers'] == 'taxonomy') {
|
| 50 |
$this->view->query->add_field('gmap_taxonomy_node', 'marker', 'gmap_node_marker');
|
| 51 |
}
|
| 52 |
else if ($this->options['markers'] == 'userrole') {
|
| 53 |
$this->view->query->add_field('users_roles', 'rid', 'gmap_role_marker');
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
function render() {
|
| 58 |
if (isset($this->view->live_preview) && $this->view->live_preview) {
|
| 59 |
return t('GMap views are not compatible with live preview.');
|
| 60 |
}
|
| 61 |
|
| 62 |
if (empty($this->row_plugin)) {
|
| 63 |
vpr('gmap_plugin_style_gmap: Missing row plugin');
|
| 64 |
return;
|
| 65 |
}
|
| 66 |
|
| 67 |
$lat_field = 'gmap_lat';
|
| 68 |
$lon_field = 'gmap_lon';
|
| 69 |
|
| 70 |
// Determine fieldname for latitude and longitude fields.
|
| 71 |
if ($this->options['datasource'] == 'fields') {
|
| 72 |
$lat_field = $this->view->display_handler->get_handler('field', $this->options['latfield'])->field_alias;
|
| 73 |
$lon_field = $this->view->display_handler->get_handler('field', $this->options['lonfield'])->field_alias;
|
| 74 |
}
|
| 75 |
|
| 76 |
// Determine fieldname for marker field.
|
| 77 |
if ($this->options['markers'] == 'field') {
|
| 78 |
$marker_field = $this->view->display_handler->get_handler('field', $this->options['markerfield'])->field_alias;
|
| 79 |
}
|
| 80 |
|
| 81 |
$markername = isset($this->options['markertype']) ? $this->options['markertype'] : 'drupal';
|
| 82 |
|
| 83 |
$markertypes = variable_get('gmap_node_markers', array());
|
| 84 |
if ($this->options['markers'] == 'nodetype') {
|
| 85 |
$markertypes = variable_get('gmap_node_markers', array());
|
| 86 |
}
|
| 87 |
else if ($this->options['markers'] == 'userrole') {
|
| 88 |
$markertypes = variable_get('gmap_role_markers', array(DRUPAL_AUTHENTICATED_RID => 'drupal'));
|
| 89 |
}
|
| 90 |
|
| 91 |
// Group the rows according to the grouping field, if specified.
|
| 92 |
$sets = $this->render_grouping($this->view->result, $this->options['grouping']);
|
| 93 |
|
| 94 |
// Render each group separately and concatenate. Plugins may override this
|
| 95 |
// method if they wish some other way of handling grouping.
|
| 96 |
$output = '';
|
| 97 |
foreach ($sets as $title => $records) {
|
| 98 |
$markers = array();
|
| 99 |
$offsets = array();
|
| 100 |
foreach ($records as $label => $row) {
|
| 101 |
$lat = (float)$row->{$lat_field};
|
| 102 |
$lon = (float)$row->{$lon_field};
|
| 103 |
if (!empty($lat) && !empty($lon)) {
|
| 104 |
if ($this->options['markers'] == 'nodetype') {
|
| 105 |
if (isset($markertypes[$row->gmap_node_type])) {
|
| 106 |
$markername = $markertypes[$row->gmap_node_type];
|
| 107 |
}
|
| 108 |
}
|
| 109 |
else if ($this->options['markers'] == 'taxonomy') {
|
| 110 |
if (!empty($row->gmap_node_marker)) {
|
| 111 |
$markername = $row->gmap_node_marker;
|
| 112 |
}
|
| 113 |
}
|
| 114 |
else if ($this->options['markers'] == 'userrole') {
|
| 115 |
if (!empty($row->gmap_role_marker)) {
|
| 116 |
$markername = $markertypes[DRUPAL_AUTHENTICATED_RID];
|
| 117 |
if (isset($markertypes[$row->gmap_role_marker])) {
|
| 118 |
$markername = $markertypes[$row->gmap_role_marker];
|
| 119 |
}
|
| 120 |
}
|
| 121 |
}
|
| 122 |
else if ($this->options['markers'] == 'field') {
|
| 123 |
if (!empty($row->{$marker_field})) {
|
| 124 |
$markername = $row->{$marker_field};
|
| 125 |
}
|
| 126 |
}
|
| 127 |
if (!isset($offsets[$markername])) {
|
| 128 |
$offsets[$markername] = 0;
|
| 129 |
}
|
| 130 |
$markers[] = array(
|
| 131 |
'latitude' => $lat,
|
| 132 |
'longitude' => $lon,
|
| 133 |
'markername' => $markername,
|
| 134 |
'offset' => $offsets[$markername],
|
| 135 |
'text' => $this->row_plugin->render($row),
|
| 136 |
);
|
| 137 |
$offsets[$markername]++;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
if (!empty($markers)) { // Don't draw empty maps.
|
| 141 |
$map = gmap_parse_macro($this->options['macro']);
|
| 142 |
$map['markers'] = $markers;
|
| 143 |
$output .= theme($this->theme_functions(), $this->view, $this->options, $map, $title);
|
| 144 |
}
|
| 145 |
}
|
| 146 |
return $output;
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Render the given style.
|
| 151 |
*/
|
| 152 |
function options_form(&$form, &$form_state) {
|
| 153 |
parent::options_form($form, $form_state);
|
| 154 |
$form['macro'] = array(
|
| 155 |
'#type' => 'textfield',
|
| 156 |
'#title' => t('Macro'),
|
| 157 |
'#size' => 1000,
|
| 158 |
'#default_value' => $this->options['macro'],
|
| 159 |
);
|
| 160 |
|
| 161 |
$form['datasource'] = array(
|
| 162 |
'#type' => 'select',
|
| 163 |
'#title' => t('Data Source'),
|
| 164 |
'#options' => array(
|
| 165 |
'location' => t('Location.module'),
|
| 166 |
'fields' => t('Choose latitude and longitude fields'),
|
| 167 |
//'geocode' => t('Just-in-time geocoding on field named "address"'),
|
| 168 |
),
|
| 169 |
'#default_value' => $this->options['datasource'],
|
| 170 |
'#multiple' => FALSE,
|
| 171 |
);
|
| 172 |
|
| 173 |
$options = array();
|
| 174 |
$fields = $this->display->handler->get_handlers('field');
|
| 175 |
|
| 176 |
foreach ($fields as $id => $handler) {
|
| 177 |
$options[$id] = $handler->ui_name(FALSE);
|
| 178 |
}
|
| 179 |
|
| 180 |
$form['latfield'] = array(
|
| 181 |
'#title' => t('Latitude field'),
|
| 182 |
'#description' => t('Format must be degrees decimal.'),
|
| 183 |
'#type' => 'select',
|
| 184 |
'#options' => $options,
|
| 185 |
'#default_value' => $this->options['latfield'],
|
| 186 |
'#process' => array('views_process_dependency'),
|
| 187 |
'#dependency' => array('edit-style-options-datasource' => array('fields')),
|
| 188 |
);
|
| 189 |
$form['lonfield'] = array(
|
| 190 |
'#title' => t('Longitude field'),
|
| 191 |
'#description' => t('Format must be degrees decimal.'),
|
| 192 |
'#type' => 'select',
|
| 193 |
'#options' => $options,
|
| 194 |
'#default_value' => $this->options['lonfield'],
|
| 195 |
'#process' => array('views_process_dependency'),
|
| 196 |
'#dependency' => array('edit-style-options-datasource' => array('fields')),
|
| 197 |
);
|
| 198 |
|
| 199 |
$form['markers'] = array(
|
| 200 |
'#type' => 'select',
|
| 201 |
'#title' => t('Marker handling'),
|
| 202 |
// @@@ Detect view type automatically?
|
| 203 |
'#options' => array(
|
| 204 |
'nodetype' => t('By content type (for node views)'),
|
| 205 |
'taxonomy' => t('By term (for node views)'),
|
| 206 |
'userrole' => t('By user role (for user views)'),
|
| 207 |
'field' => t('Use marker field'),
|
| 208 |
'static' => t('Use single marker type'),
|
| 209 |
),
|
| 210 |
'#default_value' => $this->options['markers'],
|
| 211 |
);
|
| 212 |
|
| 213 |
$form['markerfield'] = array(
|
| 214 |
'#type' => 'select',
|
| 215 |
'#title' => t('Marker field'),
|
| 216 |
'#description' => t('You can use a views field to set the <em>markername</em> property of the markers.'),
|
| 217 |
'#options' => $options,
|
| 218 |
'#default_value' => $this->options['markerfield'],
|
| 219 |
'#process' => array('views_process_dependency'),
|
| 220 |
'#dependency' => array('edit-style-options-markers' => array('field')),
|
| 221 |
);
|
| 222 |
|
| 223 |
// Hide the taxonomy handling if gmap_taxonomy.module isn't installed.
|
| 224 |
if (!module_exists('gmap_taxonomy')) {
|
| 225 |
unset($form['markers']['#options']['taxonomy']);
|
| 226 |
}
|
| 227 |
|
| 228 |
$form['markertype'] = array(
|
| 229 |
'#type' => 'gmap_markerchooser',
|
| 230 |
'#title' => t('Marker / fallback marker to use'),
|
| 231 |
'#default_value' => $this->options['markertype'],
|
| 232 |
);
|
| 233 |
}
|
| 234 |
}
|