location.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Creates a location context.
  5. */
  6. /**
  7. * Implement hook_[context_name]_ctools_contexts().
  8. */
  9. function location_location_ctools_contexts() {
  10. return array(
  11. 'title' => t('Location'),
  12. 'description' => t('Location'),
  13. 'context' => 'location_context_create_location',
  14. 'context name' => 'location',
  15. 'settings form' => 'location_context_location_settings_form',
  16. 'keyword' => 'location',
  17. 'context name' => 'location',
  18. 'convert list' => 'location_context_location_convert_list',
  19. 'convert' => 'location_context_location_convert',
  20. );
  21. }
  22. /**
  23. * Create a context, either from configuration or an argument on the URL.
  24. *
  25. * @param $empty
  26. * If true, just return an empty context.
  27. * @param $data
  28. * If from settings form, a form values array. If from argument, a string.
  29. * @param $conf
  30. * TRUE if the $data is coming from admin configuration, FALSE if it's
  31. * from a URL arg.
  32. *
  33. * @return
  34. * a Context object
  35. */
  36. function location_context_create_location($empty, $data = NULL, $conf = FALSE) {
  37. $context = new ctools_context('location');
  38. $context->plugin = 'location';
  39. if ($empty) {
  40. return $context;
  41. }
  42. if ($conf) {
  43. $lid = is_array($data) && isset($data['lid']) ? $data['lid'] : (is_object($data) ? $data->lid : 0);
  44. if (is_array($data) || !empty($reload)) {
  45. $nid = $data['nid'];
  46. $data = location_load_location($lid);
  47. $data['nid'] = $nid;
  48. }
  49. }
  50. if (!empty($data)) {
  51. $context->data = $data;
  52. $context->title = $data['city'];
  53. $context->argument = $data['lid'];
  54. return $context;
  55. }
  56. }
  57. function location_settings_form($conf, $external = FALSE) {
  58. return array();
  59. }
  60. /**
  61. * Provide a list of ways that this context can be converted to a string.
  62. */
  63. function location_context_location_convert_list() {
  64. $fields = location_field_names();
  65. $fields['country_name'] = t('Country name');
  66. $fields['province_name'] = t('State/Province name');
  67. return $fields;
  68. }
  69. /**
  70. * Convert a context into a string.
  71. */
  72. function location_context_location_convert($context, $type) {
  73. switch ($type) {
  74. case 'name':
  75. case 'street':
  76. case 'additional':
  77. case 'city':
  78. case 'province':
  79. case 'postal_code':
  80. case 'country':
  81. case 'latitude':
  82. case 'longitude':
  83. case 'province_name':
  84. case 'country_name':
  85. if (isset($context->data[$type])) {
  86. return check_plain($context->data[$type]);
  87. }
  88. default:
  89. return t('Unknown location keyword');
  90. }
  91. }