xmlsitemap_menu.module 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
// $Id$

/**
 * Implements hook_cron().
 *
 * Process old menu links not found in the {xmlsitemap} table.
 */
function xmlsitemap_menu_cron() {
10
11
12
13
14
15
16
  xmlsitemap_menu_xmlsitemap_index_links(xmlsitemap_var('batch_limit'));
}

/**
 * Implements hook_xmlsitemap_index_links().
 */
function xmlsitemap_menu_xmlsitemap_index_links($limit) {
17
18
19
20
  if ($menus = xmlsitemap_menu_get_menus()) {
    // Set the global user variable to the anonymous user.
    xmlsitemap_switch_user(0);

21
    $sql = "SELECT ml.mlid FROM {menu_links} ml LEFT JOIN {xmlsitemap} x ON x.type = 'menu' AND ml.mlid = x.id WHERE x.id IS NULL AND ml.menu_name IN (:menus) ORDER BY ml.mlid DESC";
22
    $mlids = db_query_range($sql, 0, $limit, array(':menus' => $menus));
23

24
    foreach ($mlids as $mlid) {
25
      $menu_item = menu_link_load($mlid);
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
      $link = xmlsitemap_menu_create_link($menu_item);
      xmlsitemap_save_link($link);
    }

    // Set the global user variable back to the original user.
    xmlsitemap_restore_user();
  }
}

/**
 * Implements hook_xmlsitemap_links().
 */
function xmlsitemap_menu_xmlsitemap_links($offset = 0, $limit = 0) {
  $links = array();

  if ($menus = xmlsitemap_menu_get_menus()) {
    // Set the global user variable to the anonymous user.
    xmlsitemap_switch_user(0);

45
46
    $sql = "SELECT ml.mlid FROM {menu_links} ml WHERE ml.mlid > :mlid AND ml.menu_name IN (:menus) ORDER BY ml.mlid";
    $args = array(':mlid' => $offset, ':menus' => $menus);
47
    $mlids = ($limit ? db_query_range($sql, 0, $limit, $args) : db_query($sql, $args));
48

49
    foreach ($mlids as $mlid) {
50
      $menu_item = menu_link_load($mlid);
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      $links[] = xmlsitemap_menu_create_link($menu_item);
    }

    // Set the global user variable back to the original user.
    xmlsitemap_restore_user();
  }

  return $links;
}

/**
 * Implements hook_xmlsitemap_links_batch_info().
 */
function xmlsitemap_menu_xmlsitemap_links_batch_info() {
  $menus = xmlsitemap_menu_get_menus();
  return array(
67
    'max' => $menus ? db_query("SELECT COUNT(ml.mlid) FROM {menu_links} ml WHERE ml.menu_name IN (:menus)", array(':menus' => $menus))->fetchField() : 0,
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  );
}

/**
 * Implements hook_xmlsitemap_link_info().
 */
function xmlsitemap_menu_xmlsitemap_link_info() {
  return array(
    'menu' => array(
      'purge' => TRUE,
      'table' => 'menu_links',
      'id' => 'mlid',
      'subtype' => 'menu_name',
      'subtypes' => xmlsitemap_menu_get_menus(),
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Show a summary of menus on the XML sitemap settings page.
 */
function xmlsitemap_menu_form_xmlsitemap_settings_form_alter(&$form, $form_state) {
  $type = array(
    'type' => 'menu',
    'title' => t('Menus'),
    'item_title' => t('Menu'),
    'access' => user_access('administer menu'),
  );

  $menus = menu_get_menus();
  foreach ($menus as $menu => $name) {
    $menus[$menu] = array(
      'name' => $name,
      'link' => 'admin/build/menu-customize/' . $menu . '/edit',
      'status' => variable_get('xmlsitemap_menu_status_' . $menu, 0),
      'priority' => variable_get('xmlsitemap_menu_priority_' . $menu, 0.5),
    );
  }

  xmlsitemap_add_form_type_summary($form, $type, $menus);
  $form['menu']['#weight'] = 40;
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_edit_menu()
 * @see xmlsitemap_menu_menu_edit_menu_submit()
 */
function xmlsitemap_menu_form_menu_edit_menu_alter(&$form, $form_state) {
  $menu = isset($form['menu_name']['#value']) ? $form['menu_name']['#value'] : '';

  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
  $options = array(
    'status' => variable_get('xmlsitemap_menu_status_' . $menu, 0),
    'priority' => variable_get('xmlsitemap_menu_priority_' . $menu, 0.5),
  );
  xmlsitemap_add_form_type_options($form, 'menu', $options);

  // @todo Enable this feature:
  //$form['xmlsitemap']['xmlsitemap_menu_calculate_priority'] = array(
  //  '#type' => 'checkbox',
  //  '#title' => t('Calculate priority based on menu item depth and weight.'),
  //  '#default_value' => variable_get('xmlsitemap_menu_calculate_priority_' . $menu, FALSE),
  //);

  $form['submit'] += array('#weight' => 50);
  if (isset($form['delete'])) {
    $form['delete'] += array('#weight' => 51);
  }

  $form['#submit'][] = 'xmlsitemap_menu_menu_edit_menu_submit';
}

/**
 * Form submit handler; update settings when a menu is saved.
 */
function xmlsitemap_menu_menu_edit_menu_submit($form, $form_state) {
  $menu = $form_state['values']['menu_name'];
  $new_priority = $form_state['values']['xmlsitemap_menu_priority'];
  $new_status = $form_state['values']['xmlsitemap_menu_status'];

  if ($new_status != variable_get('xmlsitemap_menu_status_' . $menu, 0)) {
    xmlsitemap_update_links(array('status' => $new_status), array('type' => 'menu', 'subtype' => $menu, 'status_override' => 0));
  }
  if ($new_priority != variable_get('xmlsitemap_menu_priority_' . $menu, 0.5)) {
    xmlsitemap_update_links(array('priority' => $new_priority), array('type' => 'menu', 'subtype' => $menu, 'priority_override' => 0));
  }

  variable_set('xmlsitemap_menu_priority_' . $menu, $new_priority);
  variable_set('xmlsitemap_menu_status_' . $menu, $new_status);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_delete_menu_confirm()
 * @see xmlsitemap_menu_form_menu_delete_menu_confirm_submit()
 */
function xmlsitemap_menu_form_menu_delete_menu_confirm_alter(&$form, $form_state) {
  $form['#submit'][] = 'xmlsitemap_menu_form_menu_delete_menu_confirm_submit';
}

/**
 * Form submit handler; delete sitemap links when a menu is deleted.
 */
function xmlsitemap_menu_form_menu_delete_menu_confirm_submit($form, $form_state) {
  $menu = $form['#menu']['menu_name'];
178
  xmlsitemap_link_delete_multiple(array('type' => 'menu', 'subtype' => $menu));
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  variable_del('xmlsitemap_menu_status_' . $menu);
  variable_del('xmlsitemap_menu_priority_ ' . $menu);
}

//function xmlsitemap_menu_form_menu_overview_form_alter(&$form, $form_state) {
//  $form['#submit'][] = 'xmlsitemap_menu_menu_overview_form_submit';
//}
//
//function xmlsitemap_menu_menu_overview_form_submit($form, $form_state) {
//  foreach (element_children($form) as $mlid) {
//    if (isset($form[$mlid]['#item'])) {
//      $menu_item = menu_link_load($form[$mlid]['#item']['mlid'], TRUE);
//      xmlsitemap_menu_item_update($menu_item);
//    }
//  }
//}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_item_delete_form()
 */
function xmlsitemap_menu_form_menu_item_delete_form_alter(&$form, $form_state) {
  $form['#submit'][] = 'xmlsitemap_menu_menu_item_delete_form_submit';
}

/**
 * Form submit callback; delete the sitemap link when a menu item is deleted.
 */
function xmlsitemap_menu_menu_item_delete_form_submit($form, $form_state) {
  xmlsitemap_menu_item_delete($form['#item']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see menu_edit_item()
 */
function xmlsitemap_menu_form_menu_edit_item_alter(&$form, $form_state) {
  $form['#submit'][] = 'xmlsitemap_menu_menu_edit_item_submit';
}

/**
 * Form submit callback; update the sitemap link when a menu item is updated.
 */
function xmlsitemap_menu_menu_edit_item_submit($form, $form_state) {
  xmlsitemap_switch_user(0);
226
  $menu_item = menu_link_load($form_state['values']['menu']['mlid']);
227
228
229
230
231
232
233
234
235
236
  xmlsitemap_restore_user();
  xmlsitemap_menu_item_update($menu_item);
}

function xmlsitemap_menu_item_update(array $menu_item) {
  $link = xmlsitemap_menu_create_link($menu_item);
  xmlsitemap_save_link($link);
}

function xmlsitemap_menu_item_delete(array $menu_item) {
237
  xmlsitemap_link_delete('menu', $menu_item['mlid']);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
}

/**
 * Fetch an array of menus to be included in the sitemap.
 */
function xmlsitemap_menu_get_menus() {
  $menus = array_keys(menu_get_menus());
  foreach ($menus as $index => $menu) {
    if (!variable_get('xmlsitemap_menu_status_' . $menu, 0)) {
      unset($menus[$index]);
    }
  }
  return $menus;
}

/**
 * Create a sitemap link from a menu item.
 *
 * @param $menu_item
 *   A loaded menu item.
 */
function xmlsitemap_menu_create_link(array $menu_item) {
  if (!isset($menu_item['xmlsitemap'])) {
    $menu_item['xmlsitemap'] = array();
262
263
264
    if ($menu_item['mlid'] && $link = xmlsitemap_load_link('menu', $menu_item['mlid'])) {
      $menu_item['xmlsitemap'] = $link;
    }
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  }

  $menu_item['xmlsitemap'] += array(
    'type' => 'menu',
    'id' => $menu_item['mlid'],
    'loc' => $menu_item['link_path'],
    'status' => variable_get('xmlsitemap_menu_status_' . $menu_item['menu_name'], 0),
    'status_default' => variable_get('xmlsitemap_menu_status_' . $menu_item['menu_name'], 0),
    'status_override' => 0,
    'priority' => variable_get('xmlsitemap_menu_priority_' . $menu_item['menu_name'], 0.5),
    'priority_default' => variable_get('xmlsitemap_menu_priority_' . $menu_item['menu_name'], 0.5),
    'priority_override' => 0,
  );

  // The following values must always be checked because they are volatile.
  $menu_item['xmlsitemap']['subtype'] = $menu_item['menu_name'];
  $menu_item['xmlsitemap']['access'] = $menu_item['access'] && !$menu_item['external'] && !$menu_item['hidden'];
282
  $menu_item['xmlsitemap']['language'] = isset($menu_item['options']['langcode']) ? $menu_item['options']['langcode'] : LANGUAGE_NONE;
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

  return $menu_item['xmlsitemap'];
}

/**
 * Calculate the priority of a menu link based on depth and weight.
 */
function xmlsitemap_menu_calculate_priority(array $menu_item) {
  $priority = (MENU_MAX_DEPTH - $menu_item['depth'] + 1) / MENU_MAX_DEPTH;
  $priority -= (50 + $menu_item['weight']) / (100 * (MENU_MAX_DEPTH + 1));
  return $priority;
}

/**
 * Internal default variables for template_var().
 */
function xmlsitemap_menu_variables() {
  $defaults = array(
    // Deprecated variables set to NULL so they are still removed on uninstall.
    'xmlsitemap_menu_menus' => NULL,
    'xmlsitemap_menu_calculate_priority' => NULL,
  );
  $menus = array_keys(menu_get_menus());
  foreach ($menus as $menu) {
    $defaults['xmlsitemap_menu_status_' . $menu] = 0;
    $defaults['xmlsitemap_menu_priority_' . $menu] = 0.5;
    $defaults['xmlsitemap_menu_calculate_priority_' . $menu] = FALSE;
  }
  return $defaults;
}

/**
 * Internal implementation of variable_get().
 */
function xmlsitemap_menu_var($name, $default = NULL) {
  static $defaults = NULL;
  if (!isset($defaults)) {
    $defaults = xmlsitemap_menu_variables();
  }

  $name = 'xmlsitemap_menu_' . $name;

  // @todo Remove when stable.
  if (!isset($defaults[$name])) {
    trigger_error(t('Default variable for %variable not found.', array('%variable' => $name)));
  }

  return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
}