/[drupal]/contributions/modules/geonames_cck/geonames_cck.views.inc
ViewVC logotype

Contents of /contributions/modules/geonames_cck/geonames_cck.views.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Oct 28 14:16:55 2009 UTC (3 weeks, 6 days ago) by thegreenman
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +290 -293 lines
File MIME type: text/x-php
Some new updates. There are going to be a few bits of School of Everything only code in here.
But it should get things going.
1 <?php
2 // $Id$
3
4 define ('DEBUG', FALSE);
5
6 function geonames_cck_views_tables() {
7 //get a list of geonames cck fields
8 $geo_cck = geonames_cck_get_all_fields();
9 $geo_tables = array();
10 //get the table info for each field
11 foreach ($geo_cck as $geo_field) {
12 $geo_cck[$geo_field['field_name']]['table_info'] = content_database_info($geo_field);
13 }
14
15 //create the table array for Views
16 foreach ($geo_cck as $geo_field) {
17 //print $geo_field['table_info']['table']."<br/>\n";
18 $table = array(
19 'name' => $geo_field['table_info']['table'],
20 'join' => array(
21 'left' => array(
22 'table' => 'node',
23 'field' => 'nid'
24 ),
25 'right' => array(
26 'field' => 'nid'
27 ),
28 ),
29 'fields' => array(
30 //location name field
31 $geo_field['table_info']['columns']['name']['column'] => array(
32 'name' => t('Geonames CCK: Location name (@field)', array('@field' => $geo_field['field_name'])),
33 'sortable' => TRUE,
34 ),
35 //country field
36 $geo_field['table_info']['columns']['country']['column'] => array(
37 'name' => t('Geonames CCK: Country (@field)', array('@field' => $geo_field['field_name'])),
38 'sortable' => TRUE,
39 ),
40 ),
41 'filters' => array(
42 //filter for location name (freetext)
43 $geo_field['table_info']['columns']['name']['column'] => array(
44 'name' => t('Geonames CCK: Location name for @s', array('@s' => $geo_field['field_name'])),
45 'help' => 'Enter a name of city you want to search',
46 //by omitting 'value', a textfield is automatically added by Views
47 //'value'=> array (geonames_cck_views_textfield()),
48 'operator' => 'views_handler_operator_like',
49 'handler' => 'views_handler_filter_like'
50 ),
51 //filter for country (select)
52 $geo_field['table_info']['columns']['country']['column'] => array(
53 'name' => t('Geonames CCK: country for @s', array('@s' => $geo_field['field_name'])),
54 'help' => t("This allows you to filter nodes with geonames CCK field @s by country", array('@s' => $geo_field['field_name'])),
55 'list' => array(geoapi_countries()),
56 'list-type' => 'select',
57 'operator' => 'views_handler_operator_eqneq',
58 ),
59 ),
60 );
61
62 $tables[$geo_field['table_info']['table']] = $table;
63 }
64 return $tables;
65
66 }
67
68
69 function geonames_cck_views_arguments() {
70 $arguments = array(
71 'country' => array(
72 'name' => t('geonames cck: Country'),
73 'handler' => 'geonames_cck_handler_country',
74 'help' => t('Limit nodes to a specific country. Select which field to use in the options'),
75 'option' => array('#type' => 'select', '#options' => geonames_cck_get_all_fields_options(), '#multiple' => false ),
76 ),
77 'proximity' => array(
78 'name' => t('geonames cck: proximity'),
79 'handler' => 'geonames_cck_handler_proximity',
80 'help' => t('Show nodes near a geoname id. '),
81 'option' => array('#type' => 'select', '#options' => geonames_cck_get_all_fields_options(), '#multiple' => false ),
82 ),
83 'node_proximity' => array(
84 'name' => t('geonames cck: proximity to node'),
85 'handler' => 'geonames_cck_handler_proximity_node',
86 'help' => t('Show nodes near a node. '),
87 'option' => array('#type' => 'select', '#options' => geonames_cck_get_all_fields_options(), '#multiple' => false ),
88 ),
89 'bounding_box' => array(
90 'name' => t('geonames cck: bounding box'),
91 'handler' => 'geonames_cck_handler_bounding box',
92 'help' => t('Show nodes near a geoname id. '),
93 'option' => array('#type' => 'select', '#options' => geonames_cck_get_all_fields_options(), '#multiple' => false ),
94 ),
95 'bounding_box_node' => array(
96 'name' => t('geonames cck: bounding box, centered on node'),
97 'handler' => 'geonames_cck_handler_bounding_box_node',
98 'help' => t('Show nodes near a node. '),
99 'option' => array('#type' => 'select', '#options' => geonames_cck_get_all_fields_options(), '#multiple' => false ),
100 ),
101 );
102 if (module_exists('nodeprofile')) {
103 // lets allow some nodeprofile extensions
104
105 }
106 return $arguments;
107 }
108
109
110 function geonames_cck_handler_country($op, &$query, $a1, $a2 = '') {
111
112 switch ($op) {
113 case 'summary':
114
115
116 break;
117 case 'filter':
118 $field = content_fields($a1['options']);
119 $db_info = content_database_info($field);
120 $table = $db_info['table'];
121 $country = $a2;
122 $country_field = $db_info['columns']['country']['column'];
123
124 $joininfo = array( 'right' => array( 'table' => $table, 'field' => "nid" ),
125 'left' => array( 'table' => 'node', 'field' => 'nid'),
126 'type' => 'inner',
127 );
128 $query->add_where( $table .'.'. $country_field ." = '%s'" , $country);
129 $query->add_table($table, false, 1, $joininfo );
130
131 break;
132 case 'link':
133 //$node = node_load( $query->field_related_content_nid );
134 //return l( $node->title, $a2. '/'. intval( $query->field_related_content_nid ) );
135 break;
136 case 'title':
137 //TODO: This is not working. I am not getting any titles out
138 if ( $query ) {
139 //$node=node_load( $query );
140 //return $node->title;
141 }
142 break;
143 }
144 }
145
146 function geonames_cck_handler_proximity($op, &$query, $a1, $a2 = '') {
147 switch ($op) {
148 case 'summary':
149
150
151 break;
152 case 'filter':
153 $geonameid = geoapi_get_geonameid_for_alias($a2);
154 $geodata = _geoapi_get_data_for_geonameid($geonameid);
155 $field = content_fields($a1['options']);
156 geonames_cck_proximity_query(&$query, $field, $geodata);
157 break;
158 case 'link':
159 break;
160 case 'title':
161 if ( $query ) {
162 $geodata = _geoapi_get_data_for_geonameid(geoapi_get_geonameid_for_alias($query));
163 return $geodata['name'];
164 }
165 break;
166 }
167 }
168
169
170 function geonames_cck_handler_proximity_node($op, &$query, $a1, $a2 = '') {
171 switch ($op) {
172 case 'summary':
173
174
175 break;
176 case 'filter':
177 if ($a2) {
178 $field = content_fields($a1['options']);
179 $nid= $a2;
180 $node = node_load($nid);
181 $geodata = $node->{$field['field_name']}[0];
182 if ($geodata['lat'] && $geodata['lng']) {
183 geonames_cck_proximity_query(&$query, $field, $geodata);
184 }
185 }
186 break;
187 case 'link':
188 //$node = node_load( $query->field_related_content_nid );
189 //return l( $node->title, $a2. '/'. intval( $query->field_related_content_nid ) );
190 break;
191 case 'title':
192 if ( $query ) {
193 $node = node_load($query);
194 return $node->title;
195 }
196 break;
197 }
198 }
199
200
201 function geonames_cck_handler_bounding_box($op, &$query, $a1, $a2 = '') {
202 switch ($op) {
203 case 'summary':
204
205
206 break;
207 case 'filter':
208 if ($a2) {
209 $geonameid = $a2;
210 $geodata = _geoapi_get_data_for_geonameid($geonameid);
211 $field = content_fields($a1['options']);
212 geonames_cck_bounding_box_query(&$query, $field, $geodata);
213 }
214 break;
215 case 'link':
216 break;
217 case 'title':
218 if ( $query ) {
219 $geodata = _geoapi_get_data_for_geonameid($query);
220 return $geodata['name'];
221 }
222 break;
223 }
224 }
225 /**
226 * Views handler to limit nodes to bounding box lat and lng
227 *
228 * @param unknown_type $op
229 * @param unknown_type $query
230 * @param unknown_type $a1
231 * @param unknown_type $a2
232 * @return unknown
233 */
234 function geonames_cck_handler_bounding_box_node($op, &$query, $a1, $a2 = '') {
235 switch ($op) {
236 case 'summary':
237
238
239 break;
240 case 'filter':
241 if ($a2) {
242 $field = content_fields($a1['options']);
243 $nid= $a2;
244 $node = node_load($nid);
245 $geodata = $node->{$field['field_name']}[0];
246 if ($geodata['lat'] && $geodata['lng']) {
247 geonames_cck_bounding_box_query(&$query, $field, $geodata);
248 }
249 }
250 break;
251 case 'link':
252 //$node = node_load( $query->field_related_content_nid );
253 //return l( $node->title, $a2. '/'. intval( $query->field_related_content_nid ) );
254 break;
255 case 'title':
256 if ( $query ) {
257 $node = node_load($query);
258 return $node->title;
259 }
260 break;
261 }
262 }
263
264
265 /**
266 * Alters a views query object with proximity sort data
267 *
268 * @param unknown_type $query
269 * @param unknown_type $field
270 * @param unknown_type $geodata
271 */
272 function geonames_cck_proximity_query(&$query, $field, $geodata, $left_table = 'node', $distance = 50) {
273 include_once(drupal_get_path('module', 'geonames_cck') .'/geonames_cck_earth.inc');
274 $db_info = content_database_info($field);
275 $table = $db_info['table'];
276 $latcol = $db_info['columns']['lat']['column'];
277 $lngcol = $db_info['columns']['lng']['column'];
278 $lat = $geodata['lat'];
279 $lon = $geodata['lng'];
280 if (!$lat || !$lon) return;
281 $divisor = 1609.347;
282 $latrange = earth_latitude_range($lon, $lat, ($distance * $divisor));
283 $lonrange = earth_longitude_range($lon, $lat, ($distance * $divisor));
284 $joininfo = array( 'right' => array( 'table' => $table, 'field' => "nid" ),
285 'left' => array( 'table' => $left_table, 'field' => 'nid'),
286 'type' => 'inner',
287 );
288 $query->add_table($table, false, 1, $joininfo );
289 $query->add_orderby(NULL, earth_distancesqlared_sql($lon, $lat, $table, $latcol, $lngcol), 'ASC', 'distanceSquared');
290 $query->add_where("$table.$lngcol IS NOT NULL");
291 $query->add_where("$table.$latcol > %f AND $table.$latcol < %f AND $table.$lngcol > %f AND $table.$lngcol < %f", $latrange[0], $latrange[1], $lonrange[0], $lonrange[1]);
292 }
293
294 /**
295 * Alters a views query object with proximity sort data
296 *
297 * @param unknown_type $query
298 * @param unknown_type $field
299 * @param unknown_type $geodata
300 */
301 function geonames_cck_bounding_box_query(&$query, $field, $geodata, $left_table = 'node') {
302 include_once(drupal_get_path('module', 'geonames_cck') .'/geonames_cck_earth.inc');
303 $db_info = content_database_info($field);
304 $table = $db_info['table'];
305 $latcol = $db_info['columns']['lat']['column'];
306 $lngcol = $db_info['columns']['lng']['column'];
307 $lat = $geodata['lat'];
308 $lon = $geodata['lng'];
309 if (!$lat || !$lon) return;
310 $distance = 50;
311 $divisor = 1609.347;
312 $latrange = earth_latitude_range($lon, $lat, ($distance * $divisor));
313 $lonrange = earth_longitude_range($lon, $lat, ($distance * $divisor));
314 $joininfo = array( 'right' => array( 'table' => $table, 'field' => "nid" ),
315 'left' => array( 'table' => $left_table, 'field' => 'nid'),
316 'type' => 'inner',
317 );
318 $query->add_table($table, false, 1, $joininfo );
319 $query->add_where("$table.$lngcol IS NOT NULL");
320 $query->add_where("$table.$latcol > %f AND $table.$latcol < %f AND $table.$lngcol > %f AND $table.$lngcol < %f", $latrange[0], $latrange[1], $lonrange[0], $lonrange[1]);
321 }
322
323
324
325 function geonames_cck_views_textfield() {
326 return array(
327 $form['value'] = array(
328 '#type' => 'textfield',
329 '#title' => t('Location name'),
330 '#default_value' => '',
331 '#size' => 40,
332 '#maxlength' => 40,
333 '#description' => t('Enter the name of location'),
334 )
335 );
336
337 }
338
339 /**
340 * returns all geonames_cck fields with detailed information
341 */
342 function geonames_cck_get_all_fields2() {
343 //1. get a list of content types
344 //2. create a list of geonames cck field
345
346 $field_list = content_types();
347 $geo_cck = array();
348 $geo_cck_fieldname = array();
349 foreach ($field_list as $content_types => $content_info) {
350 foreach ($content_info['fields'] as $field_info => $field_detail) {
351 //keep the field info if not found previously
352 if (!in_array($field_detail['field_name'], $geo_cck_fieldname)) {
353 $geo_cck[] = $field_detail;
354 $geo_cck_fieldname[] = $field_detail['field_name'];
355 }
356 }
357 }
358 return $geo_cck;
359 }

  ViewVC Help
Powered by ViewVC 1.1.2