| 1 |
<?php //$Id
|
| 2 |
$yahoo_appID = 'P0X_MvPV34G1GH8DRzmzcUq_JFGz90muOAtqQYCSj2VFwBOwPoJylo_V2XUw69_2PA--';
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*
|
| 7 |
* a extras widgets and formatters for geolocation field.
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
function geolocation_extras_help($section) {
|
| 12 |
}
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
function geolocation_extras_widget_info() {
|
| 17 |
return array(
|
| 18 |
'geolocation_yahoo_addressfield' => array(
|
| 19 |
'label' => 'Yahoo Address Field.',
|
| 20 |
'field types' => array('geolocation'),
|
| 21 |
),
|
| 22 |
/*
|
| 23 |
'geolocation_yahoo_map' => array(
|
| 24 |
'label' => 'Yahoo Map Field',
|
| 25 |
'field types' => array('geolocation'),
|
| 26 |
),
|
| 27 |
*/
|
| 28 |
);
|
| 29 |
}
|
| 30 |
|
| 31 |
function geolocation_extras_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 |
$form['appID'] = array(
|
| 39 |
'#type' => 'textfield',
|
| 40 |
'#title' => t('Yahoo Application ID'),
|
| 41 |
'#description' => t('You can apply for a Yahoo Application ID at %url', array('%url' => l('http://developer.yahoo.com/', 'Yahoo\'s Developer Site'))),
|
| 42 |
'#default_value' => $widget['appID'],
|
| 43 |
);
|
| 44 |
return $form;
|
| 45 |
|
| 46 |
case 'validate':
|
| 47 |
// test yahoo application ID.
|
| 48 |
if (!$widget['appID']) {
|
| 49 |
form_set_error('appID', 'You need to enter a proper appID.' );
|
| 50 |
}
|
| 51 |
break;
|
| 52 |
|
| 53 |
case 'save':
|
| 54 |
return array('appID');
|
| 55 |
}
|
| 56 |
}
|
| 57 |
|
| 58 |
function geolocation_extras_widget($op, $node, $field, &$items) {
|
| 59 |
$fieldname = $field['field_name'];
|
| 60 |
$op = $field['widget']['type'] .'/'. $op;
|
| 61 |
switch($op) {
|
| 62 |
case 'geolocation_yahoo_addressfield/form':
|
| 63 |
$form[$fieldname] = array(
|
| 64 |
'#type' => 'fieldset',
|
| 65 |
'#title' => $field['widget']['label'],
|
| 66 |
'#weight' => $field['widget']['weight'],
|
| 67 |
'#collapsible' => TRUE,
|
| 68 |
'#collapsed' => FALSE,
|
| 69 |
'#tree' => TRUE,
|
| 70 |
);
|
| 71 |
|
| 72 |
// if we got items, put em in the form.
|
| 73 |
if (count($items)) {
|
| 74 |
foreach($items as $delta => $item) {
|
| 75 |
//drupal_set_message('form: <pre>'. print_r($item, TRUE) .'</pre>');
|
| 76 |
// store the location field data in the source column.
|
| 77 |
$form[$fieldname][$delta] = geolocation_extras_widget_geolocation_yahoo_addressfield_form($item);
|
| 78 |
//drupal_set_message('form: <pre>'. print_r($item, TRUE) .'</pre>');
|
| 79 |
}
|
| 80 |
}
|
| 81 |
// if we're a multiple field add a few extra elements, if we're a single value add a new form.
|
| 82 |
if ($field['multiple'] || !isset($form[$fieldname][0])) {
|
| 83 |
$form[$fieldname][0] = geolocation_extras_widget_geolocation_yahoo_addressfield_form();
|
| 84 |
// for later when we support both yahoo & google.
|
| 85 |
// $form[$fieldname][0] = $form_function();
|
| 86 |
}
|
| 87 |
//drupal_set_message('form out: <pre>'. print_r($form, TRUE) .'</pre>');
|
| 88 |
return $form;
|
| 89 |
|
| 90 |
case 'geolocation_yahoo_addressfield/prepare form values':
|
| 91 |
// I would unserialize $item['source'] here if I were storing complex
|
| 92 |
// data types in it.
|
| 93 |
break;
|
| 94 |
|
| 95 |
case 'geolocation_yahoo_addressfield/process form values':
|
| 96 |
foreach ($items as $delta => $item) {
|
| 97 |
if ($item['source']) {
|
| 98 |
$url = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid='. urlencode($field['widget']['appID']) .'&location='. urlencode($item['source']) .'&output=php';
|
| 99 |
$geoinfo = unserialize(file_get_contents($url));
|
| 100 |
$items[$delta]['latitude'] = $geoinfo['ResultSet']['Result']['Latitude'];
|
| 101 |
$items[$delta]['longitude'] = $geoinfo['ResultSet']['Result']['Longitude'];
|
| 102 |
}
|
| 103 |
// Don't save empty values, beyond the first value.
|
| 104 |
if ($item['source'] == '' && $item['longitude'] == '' && $item['latitude'] == '' && $delta > 0) {
|
| 105 |
unset($items[$delta]);
|
| 106 |
}
|
| 107 |
}
|
| 108 |
break;
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
function geolocation_extras_widget_geolocation_yahoo_addressfield_form($item = array()) {
|
| 113 |
$form = array();
|
| 114 |
$form['source'] = array(
|
| 115 |
'#type' => 'textfield',
|
| 116 |
'#title' => t('Location'),
|
| 117 |
'#description' => t('A free-form address input.'),
|
| 118 |
'#default_value' => $item['source'],
|
| 119 |
);
|
| 120 |
|
| 121 |
if (isset($item['latitude'])) {
|
| 122 |
$form['latitude'] = array(
|
| 123 |
'#type' => 'value',
|
| 124 |
'#value' => $item['latitude'],
|
| 125 |
);
|
| 126 |
}
|
| 127 |
if (isset($item['longitude'])) {
|
| 128 |
$form['longitude'] = array(
|
| 129 |
'#type' => 'value',
|
| 130 |
'#value' => $item['longitude'],
|
| 131 |
);
|
| 132 |
}
|
| 133 |
return $form;
|
| 134 |
}
|
| 135 |
|
| 136 |
function geolocation_extras_field_formatter_info() {
|
| 137 |
$formatters = array(
|
| 138 |
'geolocation_extras_static_yahoo_map' => array(
|
| 139 |
'label' => 'Static Yahoo Map',
|
| 140 |
'field types' => array('geolocation'),
|
| 141 |
),
|
| 142 |
'geolocation_extras_ajax_yahoo_map' => array(
|
| 143 |
'label' => 'Interactive Yahoo Map',
|
| 144 |
'field types' => array('geolocation'),
|
| 145 |
),
|
| 146 |
);
|
| 147 |
return $formatters;
|
| 148 |
}
|
| 149 |
|
| 150 |
function geolocation_extras_field_formatter($field, $item, $formatter) {
|
| 151 |
switch($formatter) {
|
| 152 |
case 'geolocation_extras_static_yahoo_map':
|
| 153 |
$result = file_get_contents('http://api.local.yahoo.com/MapsService/V1/mapImage?appid='. urlencode($field['widget']['appID']) .'&longitude='. urlencode($item['longitude']) .'&latitude='. urlencode($item['latitude']) .'&output=php');
|
| 154 |
$result = unserialize($result);
|
| 155 |
return theme('geolocation_extras_static_yahoo_map', $result['Result'], $item, $field);
|
| 156 |
|
| 157 |
case 'geolocation_extras_ajax_yahoo_map':
|
| 158 |
drupal_set_html_head('<script type="text/javascript" src="http://api.maps.yahoo.com/ajaxymap?v=3.0&appid='. urlencode($field['widget']['appID']) .'"></script>');
|
| 159 |
return theme('geolocation_extras_ajax_yahoo_map', $item);
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
function theme_geolocation_extras_static_yahoo_map($url, $item) {
|
| 164 |
$output = '';
|
| 165 |
//$output = '<pre>'. print_r($item, TRUE) .'</pre>';
|
| 166 |
return $output . '<img src="'. $url .'" />';
|
| 167 |
}
|
| 168 |
|
| 169 |
function theme_geolocation_extras_ajax_yahoo_map($item = array(), $field = array()) {
|
| 170 |
$output = '<div id="mapContainer"></div>';
|
| 171 |
$output .= '
|
| 172 |
<script type="text/javascript">
|
| 173 |
// Create a lat/lon object
|
| 174 |
var myPoint = new YGeoPoint('. check_plain($item['latitude']) .','. check_plain($item['longitude']) .');
|
| 175 |
|
| 176 |
// Create a marker for our location.
|
| 177 |
var marker = new YMarker(myPoint);
|
| 178 |
marker.addLabel("*");
|
| 179 |
var _txt = \'<div style="width:160px;height:50px;"><b>'. check_plain($item['source']) .'</b></div>\';
|
| 180 |
//marker.addAutoExpand("'. check_plain($item['source']) .'");
|
| 181 |
marker.addAutoExpand(_txt);
|
| 182 |
|
| 183 |
|
| 184 |
// Create a map object
|
| 185 |
var map = new YMap(document.getElementById(\'mapContainer\'));
|
| 186 |
// Display the map centered on a latitude and longitude
|
| 187 |
map.drawZoomAndCenter(myPoint, 3);
|
| 188 |
|
| 189 |
// Add map type control
|
| 190 |
map.addTypeControl();
|
| 191 |
map.addZoomLong();
|
| 192 |
|
| 193 |
// Set map type to either of: YAHOO_MAP_SAT YAHOO_MAP_HYB YAHOO_MAP_REG
|
| 194 |
map.setMapType(YAHOO_MAP_HYB);
|
| 195 |
|
| 196 |
//Get valid map types, returns array [YAHOO_MAP_REG, YAHOO_MAP_SAT, YAHOO_MAP_HYB]
|
| 197 |
var myMapTypes = map.getMapTypes();
|
| 198 |
|
| 199 |
map.addOverlay(marker);
|
| 200 |
</script>
|
| 201 |
';
|
| 202 |
return $output;
|
| 203 |
}
|