| 1 |
<?php //$Id
|
| 2 |
$yahoo_appID = 'P0X_MvPV34G1GH8DRzmzcUq_JFGz90muOAtqQYCSj2VFwBOwPoJylo_V2XUw69_2PA--';
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*
|
| 7 |
* @todo: make gmap address input it's own element.
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
function geolocation_gmap_help($section) {
|
| 12 |
}
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
function geolocation_gmap_widget_info() {
|
| 17 |
return array(
|
| 18 |
'geolocation_gmap_address' => array(
|
| 19 |
'label' => 'Google Geocoded Address',
|
| 20 |
'field types' => array('geolocation'),
|
| 21 |
),
|
| 22 |
/*
|
| 23 |
'geolocation_gmap_map' => array(
|
| 24 |
'label' => 'Google Map',
|
| 25 |
'field types' => array('geolocation'),
|
| 26 |
),
|
| 27 |
*/
|
| 28 |
);
|
| 29 |
}
|
| 30 |
|
| 31 |
function geolocation_gmap_widget_settings($op, $widget) {
|
| 32 |
switch($op) {
|
| 33 |
case 'callback':
|
| 34 |
return array('default value' => CONTENT_CALLBACK_CUSTOM);
|
| 35 |
|
| 36 |
case 'form':
|
| 37 |
$form = array();
|
| 38 |
return $form;
|
| 39 |
|
| 40 |
case 'validate':
|
| 41 |
break;
|
| 42 |
|
| 43 |
case 'save':
|
| 44 |
return array('');
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
function geolocation_gmap_widget($op, $node, $field, &$items) {
|
| 49 |
$fieldname = $field['field_name'];
|
| 50 |
$op = $field['widget']['type'] .'/'. $op;
|
| 51 |
switch($op) {
|
| 52 |
case 'geolocation_gmap_address/form':
|
| 53 |
$form[$fieldname] = array(
|
| 54 |
'#type' => 'fieldset',
|
| 55 |
'#title' => $field['widget']['label'],
|
| 56 |
'#weight' => $field['widget']['weight'],
|
| 57 |
'#collapsible' => TRUE,
|
| 58 |
'#collapsed' => FALSE,
|
| 59 |
'#tree' => TRUE,
|
| 60 |
);
|
| 61 |
|
| 62 |
// if we got items, put em in the form.
|
| 63 |
foreach($items as $delta => $item) {
|
| 64 |
$form[$fieldname][$delta] = geolocation_gmap_address_form($item);
|
| 65 |
}
|
| 66 |
// if we're a multiple field add an extra element, if we're a single value add a new form.
|
| 67 |
if ($field['multiple'] || !isset($form[$fieldname][0])) {
|
| 68 |
$form[$fieldname][count($items)] = geolocation_gmap_address_form();
|
| 69 |
}
|
| 70 |
return $form;
|
| 71 |
|
| 72 |
case 'geolocation_gmap_address/validate':
|
| 73 |
foreach ($items as $delta => $item) {
|
| 74 |
if (!empty($item['source'])) {
|
| 75 |
$geo = gmap_geocode($item['source']);
|
| 76 |
if ($geo['status'] != 200) {
|
| 77 |
form_set_error($field['field_name'] .']['. $delta .'][source', t('Unable to geocode address.'));
|
| 78 |
}
|
| 79 |
else {
|
| 80 |
form_set_value(array('#parents' => array($field['field_name'], $delta, 'longitude')), $geo['longitude']);
|
| 81 |
form_set_value(array('#parents' => array($field['field_name'], $delta, 'latitude')), $geo['latitude']);
|
| 82 |
}
|
| 83 |
}
|
| 84 |
}
|
| 85 |
//drupal_set_message('validate');
|
| 86 |
break;
|
| 87 |
|
| 88 |
case 'geolocation_gmap_address/process form values':
|
| 89 |
foreach ($items as $delta => $item) {
|
| 90 |
// Don't save empty values, beyond the first value.
|
| 91 |
if ($item['source'] == '' && $item['longitude'] == '' && $item['latitude'] == '' && $delta > 0) {
|
| 92 |
unset($items[$delta]);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
break;
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
function geolocation_gmap_address_form($item = array()) {
|
| 100 |
$form = array();
|
| 101 |
$form['source'] = array(
|
| 102 |
'#type' => 'textfield',
|
| 103 |
'#title' => t('Location'),
|
| 104 |
'#description' => t('A free-form address input.'),
|
| 105 |
'#default_value' => $item['source'],
|
| 106 |
);
|
| 107 |
|
| 108 |
if (isset($item['latitude'])) {
|
| 109 |
$form['latitude'] = array(
|
| 110 |
'#type' => 'value',
|
| 111 |
'#value' => $item['latitude'],
|
| 112 |
);
|
| 113 |
}
|
| 114 |
if (isset($item['longitude'])) {
|
| 115 |
$form['longitude'] = array(
|
| 116 |
'#type' => 'value',
|
| 117 |
'#value' => $item['longitude'],
|
| 118 |
);
|
| 119 |
}
|
| 120 |
return $form;
|
| 121 |
}
|
| 122 |
|
| 123 |
function geolocation_gmap_field_formatter_info() {
|
| 124 |
$formatters = array(
|
| 125 |
'geolocation_gmap' => array(
|
| 126 |
'label' => 'Google Map',
|
| 127 |
'field types' => array('geolocation'),
|
| 128 |
),
|
| 129 |
);
|
| 130 |
return $formatters;
|
| 131 |
}
|
| 132 |
|
| 133 |
function geolocation_gmap_field_formatter($field, $item, $formatter) {
|
| 134 |
switch($formatter) {
|
| 135 |
case 'geolocation_gmap':
|
| 136 |
return theme('geolocation_gmap', $item, $field);
|
| 137 |
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
function theme_geolocation_gmap($item = array(), $field) {
|
| 142 |
|
| 143 |
$settings['id'] = gmap_get_auto_mapid();
|
| 144 |
$settings['longitude'] = $item['longitude'];
|
| 145 |
$settings['latitude'] = $item['latitude'];
|
| 146 |
$settings['zoom'] = 'auto';
|
| 147 |
|
| 148 |
$settings['markers'][] = array(
|
| 149 |
'latitude' => $item['latitude'],
|
| 150 |
'longitude' => $item['longitude'],
|
| 151 |
'offset' => $delta,
|
| 152 |
);
|
| 153 |
|
| 154 |
return theme('gmap', array('#settings' => $settings));
|
| 155 |
}
|
| 156 |
|