location.views.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * @file
  4. * Views 3 support for Location.
  5. */
  6. /**
  7. * @TODO:
  8. * Finish porting!
  9. * Write "relationships" -- see book.views.inc, upload.views.inc, nodequeue...
  10. */
  11. /**
  12. * Implementation of hook_views_handlers().
  13. */
  14. function location_views_handlers() {
  15. return array(
  16. 'info' => array(
  17. 'path' => drupal_get_path('module', 'location') . '/handlers',
  18. ),
  19. 'handlers' => array(
  20. 'location_views_handler_field_latitude' => array(
  21. 'parent' => 'views_handler_field',
  22. ),
  23. 'location_views_handler_field_longitude' => array(
  24. 'parent' => 'location_views_handler_field_latitude',
  25. ),
  26. 'location_views_handler_field_coordinates' => array(
  27. 'parent' => 'location_views_handler_field_latitude',
  28. ),
  29. 'location_handler_field_location_country' => array(
  30. 'parent' => 'views_handler_field',
  31. ),
  32. 'location_handler_filter_location_country' => array(
  33. 'parent' => 'views_handler_filter_in_operator',
  34. ),
  35. 'location_handler_argument_location_country' => array(
  36. 'parent' => 'views_handler_argument',
  37. ),
  38. 'location_handler_field_location_province' => array(
  39. 'parent' => 'views_handler_field',
  40. ),
  41. 'location_handler_filter_location_province' => array(
  42. 'parent' => 'views_handler_filter',
  43. ),
  44. 'location_handler_argument_location_province' => array(
  45. 'parent' => 'views_handler_argument',
  46. ),
  47. 'location_handler_argument_location_proximity' => array(
  48. 'parent' => 'views_handler_argument',
  49. ),
  50. 'location_handler_field_location_address' => array(
  51. 'parent' => 'views_handler_field',
  52. ),
  53. 'location_handler_field_location_street' => array(
  54. 'parent' => 'views_handler_field',
  55. ),
  56. 'location_handler_field_location_additional' => array(
  57. 'parent' => 'views_handler_field',
  58. ),
  59. 'location_views_handler_filter_proximity' => array(
  60. 'parent' => 'views_handler_filter',
  61. ),
  62. 'location_handler_field_location_distance' => array(
  63. 'parent' => 'views_handler_field',
  64. ),
  65. 'location_handler_sort_location_distance' => array(
  66. 'parent' => 'views_handler_sort',
  67. ),
  68. 'location_handler_sort_location_country' => array(
  69. 'parent' => 'views_handler_sort',
  70. ),
  71. ),
  72. );
  73. }
  74. /**
  75. * Implementation of hook_views_data().
  76. */
  77. function location_views_data() {
  78. // Location table -- basic table information.
  79. // Define the base group of this table. Fields that don't
  80. // have a group defined will go into this field by default.
  81. $data['location']['table']['group'] = t('Location', array(), array('context' => 'geolocation'));
  82. // Advertise this table as a possible base table.
  83. $data['location']['table']['base'] = array(
  84. 'field' => 'lid',
  85. 'title' => t('Location', array(), array('context' => 'geolocation')),
  86. 'help' => t('Locations are addresses and map coordinates.'),
  87. 'weight' => -10,
  88. );
  89. $data['location']['table']['join'] = array(
  90. // Location links to node through location_instance via lid.
  91. 'node' => array(
  92. 'left_table' => 'location_instance',
  93. 'left_field' => 'lid',
  94. 'field' => 'lid',
  95. ),
  96. // Location links to node_revision through location_instance via lid.
  97. 'node_revision' => array(
  98. 'left_table' => 'location_instance',
  99. 'left_field' => 'lid',
  100. 'field' => 'lid',
  101. ),
  102. // Location links to taxonomy_term_data through location_instance via lid.
  103. 'taxonomy_term_data' => array(
  104. 'left_table' => 'location_instance',
  105. 'left_field' => 'lid',
  106. 'field' => 'lid',
  107. ),
  108. // Location links to users through location_instance via lid.
  109. 'users' => array(
  110. 'left_table' => 'location_instance',
  111. 'left_field' => 'lid',
  112. 'field' => 'lid',
  113. ),
  114. );
  115. // Location table -- fields.
  116. $data['location']['lid'] = array(
  117. 'title' => t('Lid'),
  118. // The help that appears on the UI.
  119. 'help' => t('The location ID of the location.'),
  120. // Information for displaying the lid.
  121. 'field' => array(
  122. 'handler' => 'views_handler_field',
  123. 'click sortable' => TRUE,
  124. ),
  125. // Information for accepting a lid as an argument
  126. /*
  127. 'argument' => array(
  128. 'handler' => 'views_handler_argument_node_nid',
  129. 'name field' => 'title', // the field to display in the summary.
  130. 'numeric' => TRUE,
  131. 'validate type' => 'nid',
  132. ),
  133. */
  134. // Information for accepting a lid as a filter.
  135. 'filter' => array(
  136. 'handler' => 'views_handler_filter_numeric',
  137. 'allow empty' => TRUE,
  138. ),
  139. // Information for sorting on a lid.
  140. 'sort' => array(
  141. 'handler' => 'views_handler_sort',
  142. ),
  143. );
  144. $data['location']['name'] = array(
  145. 'title' => t('Name'),
  146. 'help' => t('The name of the selected location.'),
  147. 'field' => array(
  148. 'click sortable' => TRUE,
  149. ),
  150. 'filter' => array(
  151. 'handler' => 'views_handler_filter_string',
  152. ),
  153. 'sort' => array(
  154. 'handler' => 'views_handler_sort',
  155. ),
  156. );
  157. // @@@ 1.x Conversion -- 'additional' => 'street', style 'additional'
  158. $data['location']['street'] = array(
  159. 'title' => t('Street'),
  160. 'help' => t('The street address of the selected location.'),
  161. 'field' => array(
  162. 'handler' => 'location_handler_field_location_street',
  163. 'click sortable' => TRUE,
  164. ),
  165. 'filter' => array(
  166. 'handler' => 'views_handler_filter_string',
  167. ),
  168. 'sort' => array(
  169. 'handler' => 'views_handler_sort',
  170. ),
  171. );
  172. $data['location']['additional'] = array(
  173. 'title' => t('Additional'),
  174. 'help' => t('Additional street address of the selected location.'),
  175. 'field' => array(
  176. 'click sortable' => TRUE,
  177. ),
  178. 'argument' => array(
  179. 'handler' => 'views_handler_argument_string',
  180. // 'empty field name' => t('Unknown'),
  181. ),
  182. 'filter' => array(
  183. 'handler' => 'views_handler_filter_string',
  184. ),
  185. 'sort' => array(
  186. 'handler' => 'views_handler_sort',
  187. ),
  188. );
  189. $data['location']['city'] = array(
  190. 'title' => t('City'),
  191. 'help' => t('The city of the selected location.'),
  192. 'field' => array(
  193. 'click sortable' => TRUE,
  194. ),
  195. 'argument' => array(
  196. 'handler' => 'views_handler_argument_string',
  197. 'empty field name' => t('Unknown'),
  198. ),
  199. 'filter' => array(
  200. 'handler' => 'views_handler_filter_string',
  201. ),
  202. 'sort' => array(
  203. 'handler' => 'views_handler_sort',
  204. ),
  205. );
  206. // @@@ 1.x Conversion -- 'province' => 'province', style 'name'; 'province_code' => 'province', style 'code'
  207. $data['location']['province'] = array(
  208. 'title' => t('Province'),
  209. 'help' => t('The province of the selected location.'),
  210. 'field' => array(
  211. 'handler' => 'location_handler_field_location_province',
  212. 'click sortable' => TRUE,
  213. ),
  214. 'argument' => array(
  215. 'handler' => 'location_handler_argument_location_province',
  216. ),
  217. 'filter' => array(
  218. 'handler' => 'location_handler_filter_location_province',
  219. ),
  220. 'sort' => array(
  221. 'handler' => 'views_handler_sort',
  222. // TODO: needs handler to sort by name, not code.
  223. ),
  224. );
  225. $data['location']['postal_code'] = array(
  226. 'title' => t('Postal Code'),
  227. 'help' => t('The postal code of the selected location.'),
  228. 'field' => array(
  229. 'click sortable' => TRUE,
  230. ),
  231. 'filter' => array(
  232. 'handler' => 'views_handler_filter_string',
  233. ),
  234. 'sort' => array(
  235. 'handler' => 'views_handler_sort',
  236. ),
  237. );
  238. // @@@ 1.x Conversion -- 'country' => 'country', style 'name'; 'country_code' => 'country', style 'code'.
  239. $data['location']['country'] = array(
  240. 'title' => t('Country'),
  241. 'help' => t('The country of the selected location.'),
  242. 'field' => array(
  243. 'handler' => 'location_handler_field_location_country',
  244. 'click sortable' => TRUE,
  245. ),
  246. 'argument' => array(
  247. 'handler' => 'location_handler_argument_location_country',
  248. ),
  249. 'filter' => array(
  250. 'handler' => 'location_handler_filter_location_country',
  251. ),
  252. 'sort' => array(
  253. 'handler' => 'location_handler_sort_location_country',
  254. ),
  255. );
  256. $data['location']['latitude'] = array(
  257. 'title' => t('Latitude'),
  258. 'help' => t('The latitude of the selected location.'),
  259. 'field' => array(
  260. 'handler' => 'location_views_handler_field_latitude',
  261. 'click sortable' => TRUE,
  262. ),
  263. 'filter' => array(
  264. 'handler' => 'views_handler_filter_numeric',
  265. ),
  266. 'sort' => array(
  267. 'handler' => 'views_handler_sort',
  268. ),
  269. );
  270. $data['location']['longitude'] = array(
  271. 'title' => t('Longitude'),
  272. 'help' => t('The longitude of the selected location.'),
  273. 'field' => array(
  274. 'handler' => 'location_views_handler_field_longitude',
  275. 'click sortable' => TRUE,
  276. ),
  277. 'filter' => array(
  278. 'handler' => 'views_handler_filter_numeric',
  279. ),
  280. 'sort' => array(
  281. 'handler' => 'views_handler_sort',
  282. ),
  283. );
  284. $data['location']['coordinates'] = array(
  285. 'title' => t('Coordinates'),
  286. 'help' => t("The coordinates of the selected location in 'lat, long' format."),
  287. 'field' => array(
  288. // The handler adds the longitude.
  289. 'field' => 'latitude',
  290. 'handler' => 'location_views_handler_field_coordinates',
  291. 'click sortable' => FALSE,
  292. ),
  293. );
  294. $data['location']['distance'] = array(
  295. 'title' => t('Distance / Proximity'),
  296. 'help' => t("The distance from the selected location and either the current user or a specific location."),
  297. 'field' => array(
  298. 'handler' => 'location_handler_field_location_distance',
  299. 'click sortable' => TRUE,
  300. ),
  301. 'sort' => array(
  302. 'handler' => 'location_handler_sort_location_distance',
  303. ),
  304. 'argument' => array(
  305. 'handler' => 'location_handler_argument_location_proximity',
  306. ),
  307. 'filter' => array(
  308. 'handler' => 'location_views_handler_filter_proximity',
  309. ),
  310. );
  311. $data['location']['address'] = array(
  312. 'title' => t('Address'),
  313. 'help' => t('The entire address block for the location.'),
  314. 'field' => array(
  315. 'field' => 'lid',
  316. 'handler' => 'location_handler_field_location_address',
  317. 'element type' => 'div',
  318. ),
  319. );
  320. $data['location_instance']['table']['group'] = t('Location', array(), array('context' => 'geolocation'));
  321. $data['location_instance']['table']['join'] = array(
  322. 'location' => array(
  323. 'left_field' => 'lid',
  324. 'field' => 'lid',
  325. ),
  326. 'users' => array(
  327. 'left_field' => 'uid',
  328. 'field' => 'uid',
  329. ),
  330. 'node' => array(
  331. 'left_field' => 'vid',
  332. 'field' => 'vid',
  333. ),
  334. 'node_revision' => array(
  335. 'left_field' => 'vid',
  336. 'field' => 'vid',
  337. ),
  338. 'taxonomy_term_data' => array(
  339. 'left_field' => 'tid',
  340. 'field' => 'vid',
  341. ),
  342. );
  343. // Tell the base tables about us.
  344. $data['node']['table']['join']['location'] = array(
  345. 'left_table' => 'location_instance',
  346. 'left_field' => 'vid',
  347. 'field' => 'vid',
  348. );
  349. $data['node_revision']['table']['join']['location'] = array(
  350. 'left_table' => 'location_instance',
  351. 'left_field' => 'vid',
  352. 'field' => 'vid',
  353. );
  354. $data['taxonomy_term_data']['table']['join']['location'] = array(
  355. 'left_table' => 'location_instance',
  356. 'left_field' => 'vid',
  357. 'field' => 'tid',
  358. );
  359. $data['users']['table']['join']['location'] = array(
  360. 'left_table' => 'location_instance',
  361. 'left_field' => 'uid',
  362. 'field' => 'uid',
  363. );
  364. return $data;
  365. }
  366. /**
  367. * Helper function for proximity handlers.
  368. *
  369. * Gets a list of available node location and cck location fields.
  370. *
  371. * @return array
  372. * An array of the location field options.
  373. */
  374. function location_views_proximity_get_location_field_options() {
  375. // Get the CCK location field options.
  376. $field_options = array('node' => t('Node location'));
  377. if (module_exists('location_cck')) {
  378. $fields = field_info_fields();
  379. foreach ($fields as $field) {
  380. if ($field['module'] == 'location_cck') {
  381. $field_options[$field['field_name']] = t('CCK Location: @name', array('@name' => $field['field_name']));
  382. }
  383. }
  384. }
  385. return $field_options;
  386. }
  387. /**
  388. * Helper function for proximity handlers.
  389. *
  390. * Gets available arguments for argument related handler options.
  391. *
  392. * @param object $view
  393. * The view object.
  394. *
  395. * @return array
  396. * An array containing arrays of the nid arguments and the uid arguments.
  397. */
  398. function location_views_proximity_get_argument_options($view) {
  399. // Get the arguments for this view so we can use nid, uid or Global: Null arguments.
  400. $uid_argument_options = array();
  401. $nid_argument_options = array();
  402. $arguments = $view->display_handler->get_handlers('argument');
  403. foreach ($arguments as $id => $handler) {
  404. if ($handler->field == 'null') {
  405. $uid_argument_options[$id] = $handler->ui_name();
  406. $nid_argument_options[$id] = $handler->ui_name();
  407. }
  408. else {
  409. if ($handler->field == 'nid') {
  410. $nid_argument_options[$id] = $handler->ui_name();
  411. }
  412. else {
  413. if ($handler->field == 'uid') {
  414. $uid_argument_options[$id] = $handler->ui_name();
  415. }
  416. }
  417. }
  418. }
  419. return array($nid_argument_options, $uid_argument_options);
  420. }
  421. /**
  422. * Helper function for proximity handlers.
  423. *
  424. * Retrieves the coordinates of the location that this field measures distances against.
  425. *
  426. * @param object $view
  427. * The view object.
  428. *
  429. * @param array $options
  430. * An array of the options (or values in the case of the filter) of the handler.
  431. *
  432. * @return array
  433. * An array with keys 'latitude' and 'longitude' or an empty array.
  434. */
  435. function location_views_proximity_get_reference_location($view, $options) {
  436. $coordinates = array();
  437. switch ($options['origin']) {
  438. case 'user':
  439. case 'hybrid':
  440. global $user;
  441. $user_locations = isset($user->locations) ? $user->locations : location_load_locations($user->uid, 'uid');
  442. // This user_location_delta will only possibly be set if we are dealing with the filter.
  443. $i = (isset($options['user_location_delta']) && !empty($options['user_location_delta'])) ? $options['user_location_delta'] : 0;
  444. if (isset($user_locations[$i]['latitude']) || !empty($user_locations[$i]['latitude'])) {
  445. $coordinates['latitude'] = (float) $user_locations[$i]['latitude'];
  446. $coordinates['longitude'] = (float) $user_locations[$i]['longitude'];
  447. }
  448. else {
  449. if ($options['origin'] == 'hybrid') {
  450. $coordinates['latitude'] = (float) $options['latitude'];
  451. $coordinates['longitude'] = (float) $options['longitude'];
  452. }
  453. }
  454. break;
  455. case 'static':
  456. case 'latlon_gmap':
  457. // Validate actual values.
  458. if (!empty($options['latitude']) && !empty($options['longitude'])) {
  459. $coordinates['latitude'] = (float) $options['latitude'];
  460. $coordinates['longitude'] = (float) $options['longitude'];
  461. }
  462. break;
  463. case 'tied':
  464. if (!empty($view->filter)) {
  465. foreach ($view->filter as $filter) {
  466. if ($filter->table == 'location' && $filter->field == 'distance' && $filter->options['relationship'] == $options['relationship']) {
  467. $filter_options = array_merge($filter->options, $filter->options['value'], $filter->value);
  468. if ($coords = location_views_proximity_get_reference_location($view, $filter_options)) {
  469. $coordinates['latitude'] = (float) $coords['latitude'];
  470. $coordinates['longitude'] = (float) $coords['longitude'];
  471. }
  472. }
  473. }
  474. }
  475. // If at this stage we don't have coordinates then check for a distance contextual filter.
  476. if (!count(
  477. $coordinates
  478. ) && !empty($view->argument) && !empty($view->argument->value['latitude']) && !empty($view->argument->value['longitude'])
  479. ) {
  480. foreach ($view->argument as $argument) {
  481. if ($argument->table == 'location' && $argument->field == 'distance') {
  482. $coordinates['latitude'] = (float) $argument->value['latitude'];
  483. $coordinates['longitude'] = (float) $argument->value['longitude'];
  484. }
  485. }
  486. }
  487. break;
  488. case 'postal':
  489. case 'postal_default':
  490. // Force default for country.
  491. if ($options['origin'] == 'postal_default') {
  492. $options['country'] = variable_get('location_default_country', 'us');
  493. }
  494. // Zip code lookup.
  495. if (!empty($options['postal_code']) && !empty($options['country'])) {
  496. $coords = location_latlon_rough($options);
  497. if ($coords) {
  498. $coordinates['latitude'] = (float) $coords['lat'];
  499. $coordinates['longitude'] = (float) $coords['lon'];
  500. }
  501. }
  502. break;
  503. case 'php':
  504. ob_start();
  505. $result = eval($options['php_code']);
  506. ob_end_clean();
  507. if ($result && $result['latitude'] && $result['longitude']) {
  508. $coordinates['latitude'] = (float) $result['latitude'];
  509. $coordinates['longitude'] = (float) $result['longitude'];
  510. }
  511. break;
  512. case 'nid_arg':
  513. if ($nodehandler = $view->display_handler->get_handler('argument', $options['nid_arg'])) {
  514. $nid = $nodehandler->get_value(NULL);
  515. if ($nid && is_numeric($nid) && $tempnode = node_load($nid)) {
  516. $field_name = $options['nid_loc_field'];
  517. if ($field_name == 'node') {
  518. $coordinates['latitude'] = (float) $tempnode->location['latitude'];
  519. $coordinates['longitude'] = (float) $tempnode->location['longitude'];
  520. }
  521. else {
  522. if (isset($tempnode->$field_name)) {
  523. $cck_location = $tempnode->$field_name;
  524. if (isset($cck_location[LANGUAGE_NONE][0]['longitude']) && isset($cck_location[LANGUAGE_NONE][0]['latitude'])) {
  525. $coordinates['latitude'] = (float) $cck_location[LANGUAGE_NONE][0]['latitude'];
  526. $coordinates['longitude'] = (float) $cck_location[LANGUAGE_NONE][0]['longitude'];
  527. }
  528. else {
  529. if (isset($cck_location[0]['longitude']) && isset($cck_location[0]['latitude'])) {
  530. $coordinates['latitude'] = (float) $cck_location[0]['latitude'];
  531. $coordinates['longitude'] = (float) $cck_location[0]['longitude'];
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. break;
  539. case 'uid_arg':
  540. if ($userhandler = $view->display_handler->get_handler('argument', $options['uid_arg'])) {
  541. $uid = $userhandler->get_value();
  542. if ($uid && is_numeric($uid) && $tempuser = user_load($uid)) {
  543. $coordinates['latitude'] = (float) $tempuser->location['latitude'];
  544. $coordinates['longitude'] = (float) $tempuser->location['longitude'];
  545. }
  546. }
  547. break;
  548. case 'distance_arg':
  549. foreach ($view->argument as $argument) {
  550. if ($argument->field == 'distance') {
  551. list($coords, $search_distance) = explode('_', $view->args[$argument->position]);
  552. list($lat, $lon) = explode(',', $coords);
  553. break;
  554. }
  555. }
  556. $coordinates['latitude'] = (float) $lat;
  557. $coordinates['longitude'] = (float) $lon;
  558. break;
  559. case 'geoip':
  560. $coordinates['latitude'] = (float) $options['latitude'];
  561. $coordinates['longitude'] = (float) $options['longitude'];
  562. if (module_exists('smart_ip')) {
  563. $smart_ip_session = smart_ip_session_get('smart_ip');
  564. if (isset($smart_ip_session['location']['latitude']) &&
  565. isset($smart_ip_session['location']['longitude'])
  566. ) {
  567. $coordinates['latitude'] = (float) $smart_ip_session['location']['latitude'];
  568. $coordinates['longitude'] = (float) $smart_ip_session['location']['longitude'];
  569. }
  570. }
  571. elseif (module_exists('geoip')) {
  572. // Intentional assignment of $geoip.
  573. if ($geoip_location = geoip_city()) {
  574. $coordinates['latitude'] = $geoip_location->latitude;
  575. $coordinates['longitude'] = $geoip_location->longitude;
  576. }
  577. }
  578. break;
  579. }
  580. return $coordinates;
  581. }