location_node.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @file
  4. * Associate locations with nodes.
  5. */
  6. /**
  7. * Implements hook_form_BASE_FORM_ID_alter().
  8. * Alter the node forms.
  9. */
  10. function location_node_form_node_form_alter(&$form, &$form_state, $form_id) {
  11. // Add the Location fields on the node edit form.
  12. $node = $form['#node'];
  13. $settings = variable_get('location_settings_node_' . $node->type, array());
  14. if (isset($settings['multiple']['max']) && $settings['multiple']['max'] > 0) {
  15. if (!empty($form_state['rebuild']) && !empty($form_state['values']['locations'])) {
  16. $locations = $form_state['values']['locations'];
  17. }
  18. else {
  19. $locations = isset($node->locations) ? $node->locations : array();
  20. }
  21. $form['locations'] = location_form($settings, $locations);
  22. $form['locations']['#group'] = 'additional_settings';
  23. unset($form['locations']['collapsible']);
  24. unset($form['locations']['collapsed']);
  25. }
  26. }
  27. /**
  28. * Implements hook_node_revision_delete().
  29. */
  30. function location_node_node_revision_delete($node) {
  31. if (location_node_node_locations_enabled($node->type)) {
  32. $locations = array();
  33. location_save_locations($locations, array('vid' => $node->vid));
  34. }
  35. }
  36. /**
  37. * Implements hook_node_delete().
  38. */
  39. function location_node_node_delete($node) {
  40. if (location_node_node_locations_enabled($node->type)) {
  41. $locations = array();
  42. location_save_locations($locations, array('vid' => $node->vid));
  43. }
  44. }
  45. /**
  46. * Implements hook_node_load().
  47. *
  48. * @todo
  49. * Make this load all locations at once instead of running separate queries
  50. * for each node to enhance performance.
  51. * location_load_locations() and location_load_location() will need
  52. * changing to make this happen.
  53. */
  54. function location_node_node_load($nodes, $types) {
  55. foreach ($nodes as $nid => $node) {
  56. $nodes[$nid]->locations = location_load_locations($node->vid);
  57. $nodes[$nid]->location = count($nodes[$nid]->locations) ? $nodes[$nid]->locations[0] : array();
  58. }
  59. }
  60. /**
  61. * Implements hook_node_insert().
  62. */
  63. function location_node_node_insert($node) {
  64. if (location_node_node_locations_enabled($node->type)) {
  65. if (!empty($node->locations)) {
  66. location_save_locations($node->locations, array('nid' => $node->nid, 'vid' => $node->vid));
  67. }
  68. }
  69. }
  70. /**
  71. * Implements hook_node_update().
  72. */
  73. function location_node_node_update($node) {
  74. if (location_node_node_locations_enabled($node->type)) {
  75. if (!empty($node->locations)) {
  76. location_save_locations($node->locations, array('nid' => $node->nid, 'vid' => $node->vid));
  77. }
  78. }
  79. }
  80. /**
  81. * Implements hook_node_view().
  82. */
  83. function location_node_node_view($node, $view_mode) {
  84. $settings = variable_get('location_settings_node_' . $node->type, array());
  85. if ($view_mode == 'rss') {
  86. $mode = isset($settings['rss']['mode']) ? $settings['rss']['mode'] : 'simple';
  87. if ($mode != 'none') {
  88. if (is_array($node->locations)) {
  89. foreach ($node->locations as $location) {
  90. if (($item = location_rss_item($location, $mode))) {
  91. $node->rss_elements[] = $item;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. else {
  98. if (variable_get('location_display_location', 1) && !empty($node->locations)) {
  99. if (isset($settings['display']['teaser']) && isset($settings['display']['full'])) {
  100. if (($view_mode == 'teaser' && $settings['display']['teaser']) || ($view_mode == 'full' && $settings['display']['full'])) {
  101. $node->content['locations'] = location_display($settings, $node->locations);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * Implements hook_locationapi().
  109. */
  110. function location_node_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  111. switch ($op) {
  112. case 'instance_links':
  113. foreach ($obj as $k => $v) {
  114. if ($v['nid'] != 0) {
  115. $node = node_load($v['nid']);
  116. $obj[$k]['href'] = 'node/' . $v['nid'];
  117. $obj[$k]['title'] = $node->title;
  118. $obj[$k]['type'] = t('Node location');
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * Implements hook_form_FORM_ID_alter().
  125. * Alter the node_type_form form.
  126. */
  127. function location_node_form_node_type_form_alter(&$form, &$form_state, $form_id) {
  128. $type = $form['#node_type']->type;
  129. // Hook the form handlers so we can correctly extract our information;
  130. // the node type form doesn't handle nested values correctly.
  131. array_unshift($form['#validate'], 'location_node_settings_validate');
  132. //array_unshift($form['#submit'], 'node_type_form_submit');
  133. array_unshift($form['#submit'], '_location_node_type_save_submit');
  134. $settings = variable_get('location_settings_node_'. $type, array());
  135. $form['location_settings'] = location_settings($settings);
  136. $form['location_settings']['#group'] = 'additional_settings';
  137. // Tack on customizations for node settings.
  138. $form['location_settings']['display']['teaser'] = array(
  139. '#type' => 'checkbox',
  140. '#title' => t('Display location in teaser view'),
  141. '#default_value' => isset($settings['display']['teaser']) ? $settings['display']['teaser'] : TRUE,
  142. '#weight' => -2,
  143. );
  144. $form['location_settings']['display']['full'] = array(
  145. '#type' => 'checkbox',
  146. '#title' => t('Display location in full view'),
  147. '#default_value' => isset($settings['display']['full']) ? $settings['display']['full'] : TRUE,
  148. '#weight' => -1,
  149. );
  150. $form['location_settings']['rss'] = array(
  151. '#type' => 'fieldset',
  152. '#title' => t('RSS Settings'),
  153. '#description' => t('Here, you can change how locative data affects RSS feeds on nodes.'),
  154. '#tree' => TRUE,
  155. '#weight' => 5,
  156. );
  157. $form['location_settings']['rss']['mode'] = array(
  158. '#type' => 'select',
  159. '#title' => t('RSS mode'),
  160. '#description' => t('Select how to use locations in RSS feeds for this content type.'),
  161. '#options' => array(
  162. 'none' => t('None (Do not put locational data in RSS feeds)'),
  163. 'w3c' => t('W3C Geo (deprecated)'),
  164. 'w3c_bugcompat' => t('Location 1.x-2.x compatible (buggy W3C)'),
  165. 'simple' => t('GeoRSS-Simple'),
  166. 'gml' => t('GeoRSS GML'),
  167. ),
  168. '#default_value' => isset($settings['rss']['mode']) ? $settings['rss']['mode'] : 'simple',
  169. );
  170. // Make the weights inaccessible if CCK is being used.
  171. if (module_exists('content')) {
  172. $form['location_settings']['form']['weight']['#type'] = 'value';
  173. $form['location_settings']['form']['weight']['#value'] = $form['location_settings']['form']['weight']['#default_value'];
  174. $form['location_settings']['display']['weight']['#type'] = 'value';
  175. $form['location_settings']['display']['weight']['#value'] = $form['location_settings']['display']['weight']['#default_value'];
  176. }
  177. // @@@ THIS IS NOT GOOD. --Bdragon
  178. // clear the views cache in case anything was changed
  179. if (function_exists('views_invalidate_cache')) {
  180. views_invalidate_cache();
  181. }
  182. }
  183. /**
  184. * Validation function for node settings form.
  185. * Logically, the default number of locations per node cannot
  186. * be bigger than the max locations.
  187. *
  188. * @ingroup $form
  189. */
  190. function location_node_settings_validate($form, &$form_state) {
  191. if (!empty($form_state['values']['location_settings']['multiple']['max']) && empty($form_state['values']['location_settings']['multiple']['add'])) {
  192. form_error($form['location_settings']['multiple']['add'], t("You must have at least one empty location form enabled if you are going to allow locations to be submitted for nodes of this content type. If you don't intend to allow locations to be submitted for nodes of this content type, set the maximum number of locations allowed for this content type to 0."));
  193. }
  194. elseif ($form_state['values']['location_settings']['multiple']['max'] > 0) {
  195. if ($form_state['values']['location_settings']['multiple']['add'] > $form_state['values']['location_settings']['multiple']['max']) {
  196. form_error($form['location_settings']['multiple']['add'], t("You can't show more empty location forms than the maximum number of locations allowed for this content type."));
  197. }
  198. }
  199. }
  200. /**
  201. * Custom submit function to save location settings properly.
  202. */
  203. function _location_node_type_save_submit($form, &$form_state) {
  204. variable_set('location_settings_node_' . $form_state['values']['type'], $form_state['values']['location_settings']);
  205. // @@@ Backwards compatibility variables.
  206. // There are a few places in contrib where these variables are checked.
  207. variable_set('location_maxnum_' . $form_state['values']['type'], $form_state['values']['location_settings']['multiple']['max']);
  208. variable_set('location_defaultnum_' . $form_state['values']['type'], $form_state['values']['location_settings']['multiple']['add']);
  209. // Prevent the "normal" submit handler from stomping our variable.
  210. unset($form_state['values']['location_settings']);
  211. }
  212. /**
  213. * Alter the content_field_overview_form form.
  214. */
  215. function location_node_form_content_field_overview_form_alter(&$form, &$form_state) {
  216. $form['#submit'][] = 'location_node_content_extra_save';
  217. }
  218. /**
  219. * Implements hook_content_extra_fields().
  220. *
  221. * For being able to edit location weight in node forms and node view.
  222. */
  223. function location_node_content_extra_fields($type_name) {
  224. $settings = variable_get('location_settings_node_' . $type_name, array());
  225. if (isset($settings['multiple']) && $settings['multiple']['max'] > 0) {
  226. $weight = isset($settings['form']['weight']) ? $settings['form']['weight'] : 0;
  227. $extra['locations'] = array(
  228. 'label' => t('Locations'),
  229. 'description' => t('Node Locations module form.'),
  230. 'weight' => $weight,
  231. );
  232. return $extra;
  233. }
  234. }
  235. /**
  236. * Saving the fields management's weight for location node data in forms and node view
  237. *
  238. * @param $form
  239. * @param $form_state
  240. */
  241. function location_node_content_extra_save($form, &$form_state) {
  242. $type = $form['#type_name'];
  243. // We need to stay in sync with CCK's idea of the weight.
  244. $settings = variable_get('location_settings_node_' . $type, array());
  245. if (isset($form_state['values']['locations'])) {
  246. $settings['form']['weight'] = $form_state['values']['locations']['weight'];
  247. $settings['display']['weight'] = $form_state['values']['locations']['weight'];
  248. }
  249. variable_set('location_settings_node_' . $type, $settings);
  250. }
  251. /**
  252. * Determine whether or not node locations are enabled for a given node type.
  253. *
  254. * This is needed to stop location_node from deleting/inserting/updating
  255. * location_cck locations. This is not used for loading and viewing locations
  256. * because it is expected that cck locations will be loaded into
  257. * $node->locations.
  258. *
  259. * @param $type
  260. * The node type.
  261. *
  262. * @return
  263. * Boolean depending on whether or not node locations are enabled.
  264. */
  265. function location_node_node_locations_enabled($type) {
  266. $settings = variable_get('location_settings_node_' . $type, array());
  267. // If max number of locations is zero it is disabled.
  268. if (!empty($settings) && isset($settings['multiple']['max']) && !empty($settings['multiple']['max'])) {
  269. return TRUE;
  270. }
  271. return FALSE;
  272. }