location_from_node.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for location data from node.
  5. */
  6. /**
  7. * Implement hook_[relationship_name]_ctools_relationships().
  8. */
  9. function location_location_from_node_ctools_relationships() {
  10. return array(
  11. 'title' => t('Node location'),
  12. 'keyword' => 'location',
  13. 'description' => t('Creates a location context from a node.'),
  14. 'required context' => new ctools_context_required(t('Node'), 'node'),
  15. 'context' => 'location_location_from_node_context',
  16. 'settings form' => 'location_location_from_node_settings_form',
  17. );
  18. }
  19. /**
  20. * Return a new location context based on a node context.
  21. *
  22. * This does all the magic from the relationship settings to select if we're
  23. * dealing with a location_node field or a location_cck field, what field
  24. * offset to use, etc.
  25. *
  26. * @param $context
  27. * The node context we're creating a relationship context out of.
  28. * @param $conf
  29. * The configuration settings for this relationship.
  30. *
  31. * @return
  32. * Either a valid location context object based on the given node and the
  33. * currently active configuration settings, an empty context object
  34. * (e.g. for configuration in the Panels admin UI), or NULL in case the
  35. * given node context does not contain valid location data matching the
  36. * current relationship settings.
  37. */
  38. function location_location_from_node_context($context, $conf) {
  39. // If unset it wants a generic, unfilled context, which is just NULL.
  40. if (empty($context->data)) {
  41. $new_context = ctools_context_create_empty('location', NULL);
  42. }
  43. // Grab the right location data based on the relationship settings.
  44. if (isset($conf['location_type'])) {
  45. if ($conf['location_type'] == 'location_node' && isset($context->data->locations)) {
  46. $location_offset = isset($conf['location_node_offset']) ? $conf['location_node_offset'] : 0;
  47. $location = $context->data->locations[$location_offset];
  48. }
  49. elseif ($conf['location_type'] == 'location_cck' && isset($conf['location_cck_field_name'])) {
  50. $field_name = $conf['location_cck_field_name'];
  51. $field_offset = isset($conf['location_cck_field_offset']) ? $conf['location_cck_field_offset'] : 0;
  52. if (isset($context->data->{$field_name}[$field_offset]['lid'])) {
  53. $location = $context->data->{$field_name}[$field_offset];
  54. }
  55. }
  56. }
  57. if (!empty($location)) {
  58. $location['nid'] = $context->data->nid;
  59. $new_context = ctools_context_create('location', $location);
  60. }
  61. if (!empty($new_context)) {
  62. return $new_context;
  63. }
  64. }
  65. /**
  66. * Settings form for the location relationship.
  67. *
  68. * Before we can create a CTools context for the relationship attached to a
  69. * node, we need to know what kind of location we're looking for. So, the
  70. * relationship settings form tries to figure out if the site is using
  71. * location_node or location_cck.
  72. *
  73. * If only location_node is enabled, the only thing we have to handle is the
  74. * case where there are multiple locations attached to nodes, so the only
  75. * settings form element is the location offset to use (which defaults to 0
  76. * for the first location in the node).
  77. *
  78. * If only location_cck is enabled, we see if there are any Location CCK
  79. * fields defined on any content types and present all of those as options for
  80. * the admin to select which Location CCK field should be used for the
  81. * relationship. Again, to handle multi-valued fields, we need to ask for an
  82. * offset.
  83. *
  84. * If both location_node and location_cck are enabled, the form presents two
  85. * radio buttons so the user selects which kind of location field they want to
  86. * use for the relationship, and we use the magic of CTools #dependency to
  87. * conditionally show/hide the appropriate sub-settings based on the currently
  88. * selected radio.
  89. *
  90. * @param $conf
  91. * Array of existing configuration options for this relationship.
  92. *
  93. * @return
  94. * FormAPI array of settings form elements.
  95. */
  96. function location_location_from_node_settings_form($conf) {
  97. ctools_include('dependent');
  98. $location_type_options = array();
  99. $offset_options = array();
  100. for ($i = 0; $i < 10; $i++) {
  101. $offset_options[$i] = $i;
  102. }
  103. if (module_exists('location_node')) {
  104. $location_type_options['location_node'] = t('Use location directly saved in the node for this relationship');
  105. $form['location_node_offset'] = array(
  106. '#title' => t('Node location offset'),
  107. '#type' => 'select',
  108. '#options' => $offset_options,
  109. '#default_value' => isset($conf['location_node_offset']) ? $conf['location_node_offset'] : 0,
  110. '#description' => t('If the nodes that will be used for this relationship have multiple locations, select which location you want to use.'),
  111. '#process' => array('ctools_dependent_process'),
  112. '#dependency' => array('radio:relationship[relationship_settings][location_type]' => array('location_node')),
  113. '#weight' => 4,
  114. );
  115. }
  116. if (module_exists('location_cck')) {
  117. $cck_options = array();
  118. foreach (content_fields() as $field) {
  119. if ($field['type'] == 'location') {
  120. $cck_options[$field['field_name']] = t($field['widget']['label']);
  121. }
  122. }
  123. if (!empty($cck_options)) {
  124. $form['location_cck_field_name'] = array(
  125. '#title' => t('Location CCK field to use for this relationship'),
  126. '#type' => 'select',
  127. '#options' => $cck_options,
  128. '#default_value' => isset($conf['location_cck_field_name']) ? $conf['location_cck_field_name'] : '',
  129. '#process' => array('ctools_dependent_process'),
  130. '#dependency' => array('radio:relationship[relationship_settings][location_type]' => array('location_cck')),
  131. '#prefix' => '<div class="clear-block">',
  132. '#suffix' => '</div>',
  133. '#weight' => 6,
  134. );
  135. $form['location_cck_field_offset'] = array(
  136. '#title' => t('Location CCK field offset'),
  137. '#type' => 'select',
  138. '#options' => $offset_options,
  139. '#default_value' => isset($conf['location_cck_field_offset']) ? $conf['location_cck_field_offset'] : 0,
  140. '#description' => t('If the nodes that will be used for this relationship have multiple locations, select which location you want to use.'),
  141. '#process' => array('ctools_dependent_process'),
  142. '#dependency' => array('radio:relationship[relationship_settings][location_type]' => array('location_cck')),
  143. '#weight' => 7,
  144. '#prefix' => '<div class="clear-block">',
  145. '#suffix' => '</div>',
  146. );
  147. // Since we have valid Location CCK fields on the site, present this as
  148. // an option for the location relationship.
  149. $location_type_options['location_cck'] = t('Use location CCK fields for this relationship');
  150. }
  151. }
  152. if (isset($location_type_options)) {
  153. if (count($location_type_options) > 1) {
  154. $form['location_type'] = array(
  155. '#title' => t('Location type'),
  156. '#type' => 'radios',
  157. '#options' => $location_type_options,
  158. '#default_value' => isset($conf['location_type']) ? $conf['location_type'] : '',
  159. '#weight' => 2,
  160. );
  161. }
  162. else {
  163. $form['location_type'] = array(
  164. '#type' => 'value',
  165. '#value' => reset(array_keys($location_type_options)),
  166. );
  167. }
  168. }
  169. return $form;
  170. }