location.views.inc 18 KB

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