xmlsitemap.admin.inc 31 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Administrative page callbacks for the xmlsitemap module.
 *
 * @ingroup xmlsitemap
 */

10 11 12
/**
 * Render a tableselect list of XML sitemaps for the main admin page.
 */
13 14
function xmlsitemap_sitemap_list_form() {
  // Build the 'Update options' form.
15
  $form['#operations'] = module_invoke_all('xmlsitemap_sitemap_operations');
16
  $operations = array();
17
  foreach ($form['#operations'] as $operation => $operation_info) {
18 19 20 21
    $operations[$operation] = $operation_info['label'];
  }
  asort($operations);

22
  $form['operations'] = array(
23 24 25 26 27
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
28
  $form['operations']['operation'] = array(
29 30 31 32
    '#type' => 'select',
    '#options' => $operations,
    '#default_value' => 'update',
  );
33
  $form['operations']['submit'] = array(
34 35 36 37 38 39 40
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  $contexts = xmlsitemap_get_context_info();

  $header = array();
41
  $header['url'] = array('data' => t('URL'));
42 43 44 45 46
  foreach ($contexts as $context_key => $context_info) {
    if (!empty($context_info['summary callback'])) {
      $header['context_' . $context_key] = $context_info['label'];
    }
  }
47 48 49 50 51
  $header['updated'] = array(
    'data' => t('Last updated'),
    'field' => 'updated',
    'sort' => 'asc',
  );
52 53 54 55
  $header['links'] = array('data' => t('Links'), 'field' => 'links');
  $header['chunks'] = array('data' => t('Pages'), 'field' => 'chunks');
  $header['operations'] = array('data' => t('Operations'));

56 57 58 59
  $query = db_select('xmlsitemap_sitemap');
  $query->fields('xmlsitemap_sitemap', array('smid'));
  $query->extend('TableSort')->orderByHeader($header);
  $smids = $query->execute()->fetchCol();
60
  $sitemaps = $smids ? xmlsitemap_sitemap_load_multiple($smids) : array();
61

62
  $options = array();
63
  foreach ($sitemaps as $smid => $sitemap) {
64
    $sitemap->url = url($sitemap->uri['path'], $sitemap->uri['options']);
65

66 67 68
    $options[$smid]['url'] = array(
      'data' => array(
        '#type' => 'link',
69 70
        '#title' => $sitemap->url,
        '#href' => $sitemap->url,
71 72 73 74
      ),
    );

    foreach ($contexts as $context_key => $context_info) {
75 76 77
      if (!empty($context_info['summary callback'])) {
        $options[$smid]['context_' . $context_key] = _xmlsitemap_sitemap_context_summary($sitemap, $context_key, $context_info);
      }
78 79
    }

80 81 82
    $options[$smid]['updated'] = $sitemap->updated ? format_date($sitemap->updated, 'short') : t('Never');
    $options[$smid]['links'] = $sitemap->updated ? $sitemap->links : '-';
    $options[$smid]['chunks'] = $sitemap->updated ? $sitemap->chunks : '-';
83

84
    // @todo Highlight sitemaps that need updating.
85
    // $options[$smid]['#attributes']['class'][] = 'warning';
86
    $operations = array();
87 88
    $operations['edit'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/edit/' . $smid, array('title' => t('Edit'), 'modal' => TRUE));
    $operations['delete'] = xmlsitemap_get_operation_link('admin/config/search/xmlsitemap/delete/' . $smid, array('title' => t('Delete'), 'modal' => TRUE));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    if ($operations) {
      $options[$smid]['operations'] = array(
        'data' => array(
          '#theme' => 'links',
          '#links' => $operations,
          '#attributes' => array('class' => array('links', 'inline')),
        ),
      );
    }
    else {
      $options[$smid]['operations'] = t('None (sitemap locked)');
    }
  }

  $form['sitemaps'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
107
    '#empty' => t('No XML sitemaps available.') . ' ' . l(t('Add a new XML sitemap'), 'admin/config/search/xmlsitemap/add'),
108 109 110 111
  );
  return $form;
}

112
/**
113
 * Validate xmlsitemap_sitemap_list_form submissions.
114
 */
115
function xmlsitemap_sitemap_list_form_validate($form, &$form_state) {
116 117 118 119 120 121 122 123 124
  $form_state['values']['sitemaps'] = array_filter($form_state['values']['sitemaps']);

  // Error if there are no items to select.
  if (!count($form_state['values']['sitemaps'])) {
    form_set_error('', t('No sitemaps selected.'));
  }
}

/**
125
 * Process xmlsitemap_sitemap_list_form submissions.
126 127 128
 *
 * Execute the chosen 'Update option' on the selected sitemaps.
 */
129
function xmlsitemap_sitemap_list_form_submit($form, &$form_state) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  $operation = $form['#operations'][$form_state['values']['operation']];

  // Filter out unchecked sitemaps.
  $sitemaps = array_filter($form_state['values']['sitemaps']);

  if (!empty($operation['confirm']) && empty($form_state['values']['confirm'])) {
    // We need to rebuild the form to go to a second step. For example, to
    // show the confirmation form for the deletion of redirects.
    $form_state['rebuild'] = TRUE;
  }
  else {
    $function = $operation['callback'];

    // Add in callback arguments if present.
    if (isset($operation['callback arguments'])) {
      $args = array_merge(array($sitemaps), $operation['callback arguments']);
    }
    else {
      $args = array($sitemaps);
    }
    call_user_func_array($function, $args);

    $count = count($form_state['values']['sitemaps']);
153 154 155 156 157 158
    drupal_set_message(
      format_plural(
        count($sitemaps), '@action @count XML sitemap.', '@action @count XML sitemaps.',
        array('@action' => $operation['action past'], '@count' => $count)
      )
    );
159 160 161
  }
}

162
/**
163
 * Edit Form.
164
 */
165
function xmlsitemap_sitemap_edit_form(array $form, array &$form_state, stdClass $sitemap = NULL) {
166 167
  _xmlsitemap_set_breadcrumb();

168 169 170 171 172
  if (!isset($sitemap)) {
    $sitemap = new stdClass();
    $sitemap->smid = NULL;
    $sitemap->context = array();
  }
173 174 175 176 177

  $form['#sitemap'] = $sitemap;

  $form['smid'] = array(
    '#type' => 'value',
178
    '#value' => $sitemap->smid,
179 180 181 182 183 184 185
  );
  // The context settings should be form_alter'ed by the context modules.
  $form['context'] = array(
    '#tree' => TRUE,
  );

  $form['actions'] = array(
186
    '#type' => 'actions',
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#href' => isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/xmlsitemap',
    '#title' => t('Cancel'),
  );

  // Let other modules alter this form with their context settings.
  $form['#pre_render'][] = 'xmlsitemap_sitemap_edit_form_pre_render';

  return $form;
}

204
/**
205
 * Edit Form Pre Render.
206
 */
207 208 209 210 211 212 213 214 215 216 217
function xmlsitemap_sitemap_edit_form_pre_render($form) {
  $visible_children = element_get_visible_children($form['context']);
  if (empty($visible_children)) {
    $form['context']['empty'] = array(
      '#type' => 'markup',
      '#markup' => '<p>' . t('There are currently no XML sitemap contexts available.') . '</p>',
    );
  }
  return $form;
}

218
/**
219
 * Edit form validate.
220
 */
221 222 223 224 225
function xmlsitemap_sitemap_edit_form_validate($form, &$form_state) {
  // If there are no context options, the $form_state['values']['context']
  // disappears.
  $form_state['values'] += array('context' => array());
  $existing = xmlsitemap_sitemap_load_by_context($form_state['values']['context']);
226
  if ($existing && $existing->smid != $form_state['values']['smid']) {
227 228 229 230
    form_set_error('context', t('A sitemap with the same context already exists.'));
  }
}

231
/**
232
 * Edit Form Submit.
233
 */
234 235
function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
  form_state_values_clean($form_state);
236 237
  $sitemap = (object) $form_state['values'];
  xmlsitemap_sitemap_save($sitemap);
238
  drupal_set_message(t('The sitemap has been saved.'));
239 240 241
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
}

242
/**
243
 * Delete form.
244
 */
245
function xmlsitemap_sitemap_delete_form(array $form, array &$form_state, stdClass $sitemap) {
246 247
  _xmlsitemap_set_breadcrumb();

248 249
  $count = (int) db_query("SELECT COUNT(smid) FROM {xmlsitemap_sitemap}")->fetchField();
  if ($count === 1 && empty($_POST)) {
250
    drupal_set_message(t('It is not recommended to delete the only XML sitemap.'), 'error');
251 252 253 254 255
  }

  $form['#sitemap'] = $sitemap;
  $form['smid'] = array(
    '#type' => 'value',
256
    '#value' => $sitemap->smid,
257 258 259
  );
  return confirm_form(
    $form,
260
    t('Are you sure you want to delete the XML sitemap?'),
261 262 263 264 265 266 267
    'admin/config/search/xmlsitemap',
    '',
    t('Delete'),
    t('Cancel')
  );
}

268
/**
269
 * Delete form submit.
270
 */
271
function xmlsitemap_sitemap_delete_form_submit($form, $form_state) {
272
  xmlsitemap_sitemap_delete($form_state['values']['smid']);
273
  drupal_set_message(t('The sitemap has been deleted.'));
274 275 276
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
}

277 278 279 280 281 282
/**
 * Form builder; Administration settings form.
 *
 * @see system_settings_form()
 * @see xmlsitemap_settings_form_validate()
 */
283
function xmlsitemap_settings_form($form, &$form_state) {
284
  global $base_url;
285
  $form['xmlsitemap_minimum_lifetime'] = array(
286 287
    '#type' => 'select',
    '#title' => t('Minimum sitemap lifetime'),
288 289 290 291 292 293 294 295 296 297 298 299
    '#options' => array(0 => t('No minimum')) + drupal_map_assoc(array(300,
      900,
      1800,
      3600,
      10800,
      21600,
      43200,
      86400,
      172800,
      259200,
      604800,
    ), 'format_interval'),
300
    '#description' => t('The minimum amount of time that will elapse before the sitemaps are regenerated. The sitemaps will also only be regenerated on cron if any links have been added, updated, or deleted.') . '<br />' . t('Recommended value: %value.', array('%value' => t('1 day'))),
301
    '#default_value' => variable_get('xmlsitemap_minimum_lifetime', 0),
302
  );
303
  $form['xmlsitemap_xsl'] = array(
304 305
    '#type' => 'checkbox',
    '#title' => t('Include a stylesheet in the sitemaps for humans.'),
306
    '#description' => t('When enabled, this will add formatting and tables with sorting to make it easier to view the XML sitemap data instead of viewing raw XML output. Search engines will ignore this.'),
307
    '#default_value' => variable_get('xmlsitemap_xsl', 1),
308 309 310 311
  );
  $form['xmlsitemap_prefetch_aliases'] = array(
    '#type' => 'checkbox',
    '#title' => t('Prefetch URL aliases during sitemap generation.'),
312
    '#description' => t('When enabled, this will fetch all URL aliases at once instead of one at a time during sitemap generation. For medium or large sites, it is recommended to disable this feature as it uses a lot of memory.'),
313
    '#default_value' => variable_get('xmlsitemap_prefetch_aliases', 1),
314
  );
315 316 317 318 319 320
  $form['xmlsitemap_robotstxt'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable RobotsTxt support'),
    '#description' => t('When enabled, this will add all sitemaps to the robots.txt generated by <a href="@drupal-robotstxt">the RobotsTxt</a>.', array('@drupal-robotstxt' => 'https://www.drupal.org/project/robotstxt')),
    '#default_value' => variable_get('xmlsitemap_robotstxt', 1),
  );
321 322 323 324 325 326 327 328 329
  // Allow to skip pages that might have 301 redirect set.
  if (module_exists('redirect')) {
    $form['xmlsitemap_redirect'] = array(
      '#type' => 'checkbox',
      '#title' => t('Skip pages that have 301 redirects set.'),
      '#description' => t('Prevents pages which are 301 redirects to be saved in sitemap.'),
      '#default_value' => variable_get('xmlsitemap_redirect', 0),
      );
  }
330 331 332 333
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
334
    '#collapsed' => !variable_get('xmlsitemap_developer_mode', 0),
335 336 337 338 339
    '#weight' => 10,
  );
  $form['advanced']['xmlsitemap_chunk_size'] = array(
    '#type' => 'select',
    '#title' => t('Number of links in each sitemap page'),
340 341 342 343 344 345 346 347 348 349
    '#options' => array('auto' => t('Automatic (recommended)')) + drupal_map_assoc(array(
      100,
      500,
      1000,
      2500,
      5000,
      10000,
      25000,
      XMLSITEMAP_MAX_SITEMAP_LINKS,
    )),
350 351
    '#default_value' => xmlsitemap_var('chunk_size'),
    // @todo This description is not clear.
352
    '#description' => t('If there are problems with rebuilding the sitemap, you may want to manually set this value. If you have more than @max links, an index with multiple sitemap pages will be generated. There is a maximum of @max sitemap pages.', array('@max' => XMLSITEMAP_MAX_SITEMAP_LINKS)),
353
  );
354 355 356
  $form['advanced']['xmlsitemap_batch_limit'] = array(
    '#type' => 'select',
    '#title' => t('Maximum number of sitemap links to process at once'),
357 358 359 360 361 362 363 364 365 366 367 368
    '#options' => drupal_map_assoc(array(
      5,
      10,
      25,
      50,
      100,
      250,
      500,
      1000,
      2500,
      5000,
    )),
369 370 371
    '#default_value' => xmlsitemap_var('batch_limit'),
    '#description' => t('If you have problems running cron or rebuilding the sitemap, you may want to lower this value.'),
  );
372
  if (!xmlsitemap_check_directory()) {
373
    form_set_error('xmlsitemap_path', t('The directory %directory does not exist or is not writable.', array('%directory' => xmlsitemap_get_directory())));
374
  }
375 376 377
  $form['advanced']['xmlsitemap_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Sitemap cache directory'),
378
    '#default_value' => variable_get('xmlsitemap_path', 'xmlsitemap'),
379 380
    '#size' => 30,
    '#maxlength' => 255,
381
    '#description' => t('Subdirectory where the sitemap data will be stored. This folder <strong>must not be shared</strong> with any other Drupal site or install using XML sitemap.'),
382
    '#field_prefix' => file_build_uri(''),
383 384 385 386
    '#required' => TRUE,
  );
  $form['advanced']['xmlsitemap_base_url'] = array(
    '#type' => 'textfield',
387
    '#title' => t('Default base URL'),
388
    '#default_value' => variable_get('xmlsitemap_base_url', $base_url),
389
    '#size' => 30,
390
    '#description' => t('This is the default base URL used for sitemaps and sitemap links.'),
391 392
    '#required' => TRUE,
  );
393 394 395 396
  $form['advanced']['xmlsitemap_lastmod_format'] = array(
    '#type' => 'select',
    '#title' => t('Last modification date format'),
    '#options' => array(
397 398 399
      XMLSITEMAP_LASTMOD_SHORT => t('Short'),
      XMLSITEMAP_LASTMOD_MEDIUM => t('Medium'),
      XMLSITEMAP_LASTMOD_LONG => t('Long'),
400
    ),
401
    '#default_value' => variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM),
402 403 404 405
  );
  foreach ($form['advanced']['xmlsitemap_lastmod_format']['#options'] as $key => &$label) {
    $label .= ' (' . gmdate($key, REQUEST_TIME) . ')';
  }
406 407
  $form['advanced']['xmlsitemap_developer_mode'] = array(
    '#type' => 'checkbox',
408 409
    '#title' => t('Enable developer mode to expose additional settings.'),
    '#default_value' => variable_get('xmlsitemap_developer_mode', 0),
410
  );
411 412 413 414 415 416
  $form['advanced']['xmlsitemap_disable_cron_regeneration'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable cron generation of sitemap files.'),
    '#default_value' => variable_get('xmlsitemap_disable_cron_regeneration', 0),
    '#description' => t('This can be disabled if other methods are being used to generate the sitemap files, like <em>drush xmlsitemap-regenerate</em>.'),
  );
417 418 419 420 421 422 423 424
  $form['advanced']['xmlsitemap_output_elements'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enable or disable the individual @loc elements from output', array('@loc' => '<loc>')),
    '#options' => array(
      'lastmod' => t('Last modification date: @lastmod', array('@lastmod' => '<lastmod>')),
      'changefreq' => t('Change frequency: @changfreq', array('@changfreq' => '<changefreq>')),
      'priority' => t('Priority: @priority', array('@priority' => '<priority>')),
    ),
425 426 427 428 429
    '#default_value' => drupal_map_assoc(variable_get('xmlsitemap_output_elements', array(
      'lastmod',
      'changefreq',
      'priority',
    ))),
430
  );
431

432 433 434 435 436
  $form['xmlsitemap_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 20,
  );

437
  $entities = xmlsitemap_get_link_info(NULL, TRUE);
438
  module_load_all_includes('xmlsitemap.inc');
439
  foreach ($entities as $entity => $entity_info) {
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    $form[$entity] = array(
      '#type' => 'fieldset',
      '#title' => $entity_info['label'],
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'xmlsitemap_settings',
    );

    if (!empty($entity_info['bundles'])) {
      // If this entity has bundles, show a bundle setting summary.
      xmlsitemap_add_form_entity_summary($form[$entity], $entity, $entity_info);
    }

    if (!empty($entity_info['xmlsitemap']['settings callback'])) {
      // Add any entity-specific settings.
      $entity_info['xmlsitemap']['settings callback']($form[$entity]);
    }

    // Ensure that the entity fieldset is not shown if there are no accessible
    // sub-elements.
    $form[$entity]['#access'] = (bool) element_get_visible_children($form[$entity]);
  }
462 463 464

  $form['#validate'][] = 'xmlsitemap_settings_form_validate';
  $form['#submit'][] = 'xmlsitemap_settings_form_submit';
465
  array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
466
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  $form = system_settings_form($form);
  return $form;
}

/**
 * Form validator; Check the sitemap files directory.
 *
 * @see xmlsitemap_settings_form()
 */
function xmlsitemap_settings_form_validate($form, &$form_state) {
  // Check that the chunk size will not create more than 1000 chunks.
  $chunk_size = $form_state['values']['xmlsitemap_chunk_size'];
  if ($chunk_size != 'auto' && $chunk_size != 50000 && (xmlsitemap_get_link_count() / $chunk_size) > 1000) {
    form_set_error('xmlsitemap_chunk_size', t('The sitemap page link count of @size will create more than 1,000 sitemap pages. Please increase the link count.', array('@size' => $chunk_size)));
  }

  $base_url = &$form_state['values']['xmlsitemap_base_url'];
  $base_url = rtrim($base_url, '/');
  if ($base_url != '' && !valid_url($base_url, TRUE)) {
    form_set_error('xmlsitemap_base_url', t('Invalid base URL.'));
  }
}

/**
492
 * Submit handler;.
493 494 495 496 497
 *
 * @see xmlsitemap_settings_form()
 */
function xmlsitemap_settings_form_submit($form, $form_state) {
  // Save any changes to the frontpage link.
498
  xmlsitemap_link_save(array('type' => 'frontpage', 'id' => 0, 'loc' => ''));
499 500 501 502 503 504 505 506
}

/**
 * Menu callback; Confirm rebuilding of the sitemap.
 *
 * @see xmlsitemap_rebuild_form_submit()
 */
function xmlsitemap_rebuild_form() {
507 508
  if (!$_POST && !variable_get('xmlsitemap_rebuild_needed', FALSE)) {
    if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) {
509 510 511
      drupal_set_message(t('Your sitemap is up to date and does not need to be rebuilt.'), 'error');
    }
    else {
512
      $_REQUEST += array('destination' => 'admin/config/search/xmlsitemap');
513 514 515 516
      drupal_set_message(t('A rebuild is not necessary. If you are just wanting to regenerate the XML sitemap files, you can <a href="@link-cron">run cron manually</a>.', array('@link-cron' => url('admin/reports/status/run-cron', array('query' => drupal_get_destination())))), 'warning');
    }
  }

517 518 519
  // Build a list of rebuildable link types.
  module_load_include('generate.inc', 'xmlsitemap');
  $rebuild_types = xmlsitemap_get_rebuildable_link_types();
520

521
  $form['entities'] = array(
522
    '#type' => 'select',
523 524
    '#title' => t("Select which link types you would like to rebuild"),
    '#description' => t('If no link types are selected, the sitemap files will just be regenerated.'),
525
    '#multiple' => TRUE,
526 527
    '#options' => drupal_map_assoc($rebuild_types),
    '#default_value' => variable_get('xmlsitemap_rebuild_needed', FALSE) || !variable_get('xmlsitemap_developer_mode', 0) ? $rebuild_types : array(),
528
    '#access' => variable_get('xmlsitemap_developer_mode', 0),
529 530 531 532 533 534 535 536 537
  );
  $form['save_custom'] = array(
    '#type' => 'checkbox',
    '#title' => t('Save and restore any custom inclusion and priority links.'),
    '#default_value' => TRUE,
  );

  return confirm_form(
    $form,
538
    t('Are you sure you want to rebuild the XML sitemap?'),
539
    'admin/config/search/xmlsitemap',
540 541 542 543 544 545 546 547 548 549 550 551 552
    '',
    t('Rebuild sitemap'),
    t('Cancel')
  );
}

/**
 * Submit handler; Starts the sitemap rebuild batch.
 *
 * @see xmlsitemap_rebuild_form()
 * @see xmlsitemap_rebuild_batch()
 */
function xmlsitemap_rebuild_form_submit($form, &$form_state) {
553 554
  module_load_include('generate.inc', 'xmlsitemap');
  $batch = xmlsitemap_rebuild_batch($form_state['values']['entities'], $form_state['values']['save_custom']);
555
  batch_set($batch);
556
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
557 558 559
}

/**
560
 * Add a table summary for an entity and its bundles.
561
 */
562
function xmlsitemap_add_form_entity_summary(&$form, $entity, array $entity_info) {
563 564 565
  $priorities = xmlsitemap_get_priority_options(NULL, FALSE);
  $statuses = xmlsitemap_get_status_options(NULL);
  $rows = array();
566 567 568 569 570 571 572 573 574
  $totals = array('total' => 0, 'indexed' => 0, 'visible' => 0);
  foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
    // Fetch current per-bundle link total and indexed counts.
    $status = xmlsitemap_get_link_type_indexed_status($entity, $bundle);
    $totals['total'] += $status['total'];
    $totals['indexed'] += $status['indexed'];
    $totals['visible'] += $status['visible'];

    $row = array();
575 576 577
    if (drupal_valid_path("admin/config/search/xmlsitemap/settings/$entity/$bundle")) {
      $edit_link = xmlsitemap_get_operation_link("admin/config/search/xmlsitemap/settings/$entity/$bundle", array('title' => $bundle_info['label'], 'modal' => TRUE));
      $row[] = l($edit_link['title'], $edit_link['href'], $edit_link);
578 579
    }
    else {
580 581
      // Bundle labels are assumed to be un-escaped input.
      $row[] = check_plain($bundle_info['label']);
582 583 584 585 586 587 588 589 590 591
    }
    $row[] = $statuses[$bundle_info['xmlsitemap']['status'] ? 1 : 0];
    $row[] = $priorities[number_format($bundle_info['xmlsitemap']['priority'], 1)];
    $row[] = $status['total'];
    $row[] = $status['indexed'];
    $row[] = $status['visible'];
    $rows[] = $row;
  }

  if ($rows) {
592
    $header = array(
593
      isset($entity_info['bundle label']) ? $entity_info['bundle label'] : '',
594 595 596 597 598 599
      t('Inclusion'),
      t('Priority'),
      t('Available'),
      t('Indexed'),
      t('Visible'),
    );
600
    $rows[] = array(
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
      array(
        'data' => t('Totals'),
        'colspan' => 3,
        'header' => TRUE,
      ),
      array(
        'data' => $totals['total'],
        'header' => TRUE,
      ),
      array(
        'data' => $totals['indexed'],
        'header' => TRUE,
      ),
      array(
        'data' => $totals['visible'],
        'header' => TRUE,
      ),
    );
619
    $form['summary'] = array(
620 621 622
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
623 624 625 626 627 628 629
    );
  }
}

/**
 * Add the link type XML sitemap options to the link type's form.
 *
630 631
 * Caller is responsible for ensuring xmlsitemap_link_bundle_settings_save()
 * is called during submission.
632
 */
633
function xmlsitemap_add_link_bundle_settings(array &$form, array &$form_state, $entity, $bundle) {
634 635
  $entity_info = xmlsitemap_get_link_info($entity);
  $bundle_info = xmlsitemap_link_bundle_load($entity, $bundle);
636

637
  $form['xmlsitemap'] = array(
638 639 640 641 642 643
    '#type' => 'fieldset',
    '#title' => t('XML sitemap'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => user_access('administer xmlsitemap'),
    '#group' => 'additional_settings',
644 645 646 647 648
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
      ),
    ),
649 650 651 652 653
    '#tree' => TRUE,
    '#entity' => $entity,
    '#bundle' => $bundle,
    '#entity_info' => $entity_info,
    '#bundle_info' => $bundle_info,
654 655 656 657 658 659 660 661 662 663 664
  );

  // Hack to remove fieldset summary if Vertical tabs is not enabled.
  if (!isset($form['additional_settings'])) {
    unset($form['xmlsitemap']['#attached']['js']['vertical-tabs']);
  }

  $form['xmlsitemap']['description'] = array(
    '#prefix' => '<div class="description">',
    '#suffix' => '</div>',
    '#markup' => t('Changing these type settings will affect any items of this type that have either inclusion or priority set to default.'),
665
  );
666
  $form['xmlsitemap']['status'] = array(
667 668 669
    '#type' => 'select',
    '#title' => t('Inclusion'),
    '#options' => xmlsitemap_get_status_options(),
670
    '#default_value' => $bundle_info['status'],
671
  );
672
  $form['xmlsitemap']['priority'] = array(
673 674 675
    '#type' => 'select',
    '#title' => t('Default priority'),
    '#options' => xmlsitemap_get_priority_options(),
676
    '#default_value' => $bundle_info['priority'],
677 678
    '#states' => array(
      'invisible' => array(
679
        'select[name="xmlsitemap[status]"]' => array('value' => '0'),
680 681
      ),
    ),
682
  );
683

684 685 686 687 688 689 690 691 692 693 694
  $form += array('#submit' => array());
  array_unshift($form['#submit'], 'xmlsitemap_link_bundle_settings_form_submit');

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

695
/**
696
 * Link bundle settings form.
697
 */
698
function xmlsitemap_link_bundle_settings_form(array $form, array &$form_state, array $bundle) {
699
  if (empty($form_state['ajax']) && $admin_path = xmlsitemap_get_bundle_path($bundle['entity'], $bundle['bundle'])) {
700 701 702
    // If this is a non-ajax form, redirect to the bundle administration page.
    $destination = drupal_get_destination();
    unset($_GET['destination']);
703
    drupal_goto($admin_path, array('query' => $destination));
704
  }
705
  else {
706
    drupal_set_title(t('@bundle XML sitemap settings', array('@bundle' => $bundle['info']['label'])));
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
  }

  $form = array();
  xmlsitemap_add_link_bundle_settings($form, $form_state, $bundle['entity'], $bundle['bundle']);
  $form['xmlsitemap']['#type'] = 'markup';
  $form['xmlsitemap']['#value'] = '';
  $form['xmlsitemap']['#access'] = TRUE;
  $form['xmlsitemap']['#show_message'] = TRUE;

  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  $form['actions']['cancel'] = array(
    '#value' => l(t('Cancel'), isset($_GET['destination']) ? $_GET['destination'] : 'admin/config/search/xmlsitemap/settings'),
    '#weight' => 10,
  );
727 728 729 730

  return $form;
}

731 732
/**
 * Add a link's XML sitemap options to the link's form.
733 734
 *
 * @todo Add changefreq overridability.
735
 */
736
function xmlsitemap_add_form_link_options(array &$form, $entity, $bundle, $id) {
737 738 739 740 741 742 743 744 745 746
  $info = xmlsitemap_get_link_info($entity);

  if (!$info || empty($info['bundles'][$bundle])) {
    return;
  }

  if (!$link = xmlsitemap_link_load($entity, $id)) {
    $link = array();
  }

747
  $bundle_info = xmlsitemap_link_bundle_load($entity, $bundle);
748 749
  $link += array(
    'access' => 1,
750 751
    'status' => $bundle_info['status'],
    'status_default' => $bundle_info['status'],
752
    'status_override' => 0,
753 754
    'priority' => $bundle_info['priority'],
    'priority_default' => $bundle_info['priority'],
755 756
    'priority_override' => 0,
  );
757

758 759 760 761 762 763
  $form['xmlsitemap'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('XML sitemap'),
    '#collapsible' => TRUE,
    '#collapsed' => !$link['status_override'] && !$link['priority_override'],
764
    '#access' => user_access('administer xmlsitemap') || user_access('use xmlsitemap') || xmlsitemap_link_bundle_access($bundle_info),
765 766 767 768 769 770 771 772
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
      ),
    ),
  );

773 774 775 776 777
  // Hack to remove fieldset summary if Vertical tabs is not enabled.
  if (!isset($form['additional_settings'])) {
    unset($form['xmlsitemap']['#attached']['js']['vertical-tabs']);
  }

778
  if (xmlsitemap_link_bundle_access($bundle_info) && $path = xmlsitemap_get_bundle_path($entity, $bundle)) {
779 780 781
    $form['xmlsitemap']['description'] = array(
      '#prefix' => '<div class="description">',
      '#suffix' => '</div>',
782 783 784 785 786 787
      '#markup' => t('The default XML sitemap settings for this @bundle can be changed <a href="@link-type">here</a>.', array(
        '@bundle' => drupal_strtolower($info['bundle label']),
        '@link-type' => url($path, array(
          'query' => drupal_get_destination(),
        )),
      )),
788 789 790
    );
  }

791 792
  // Show a warning if the link is not accessible and will not be included in
  // the sitemap.
793
  if ($id && !$link['access']) {
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    $form['xmlsitemap']['warning'] = array(
      '#type' => 'markup',
      '#prefix' => '<p><strong>',
      '#suffix' => '</strong></p>',
      '#value' => ('This item is not currently visible to anonymous users, so it will not be included in the sitemap.'),
    );
  }

  // Status field (inclusion/exclusion)
  $form['xmlsitemap']['status'] = array(
    '#type' => 'select',
    '#title' => t('Inclusion'),
    '#options' => xmlsitemap_get_status_options($link['status_default']),
    '#default_value' => $link['status_override'] ? $link['status'] : 'default',
  );
  $form['xmlsitemap']['status_default'] = array(
    '#type' => 'value',
    '#value' => $link['status_default'],
  );
  $form['xmlsitemap']['status_override'] = array(
    '#type' => 'value',
    '#value' => $link['status_override'],
  );

818
  // Priority field.
819 820 821 822 823 824
  $form['xmlsitemap']['priority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#options' => xmlsitemap_get_priority_options($link['priority_default']),
    '#default_value' => $link['priority_override'] ? number_format($link['priority'], 1) : 'default',
    '#description' => t('The priority of this URL relative to other URLs on your site.'),
825 826
    '#states' => array(
      'invisible' => array(
827
        'select[name="xmlsitemap[status]"]' => array('value' => '0'),
828 829
      ),
    ),
830
  );
831 832 833 834 835 836 837
  if (!$link['status_default']) {
    // If the default status is excluded, add a visible state on the include
    // override option.
    $form['xmlsitemap']['priority']['#states']['visible'] = array(
      'select[name="xmlsitemap[status]"]' => array('value' => '1'),
    );
  }
838 839 840 841 842 843 844 845 846
  $form['xmlsitemap']['priority_default'] = array(
    '#type' => 'value',
    '#value' => $link['priority_default'],
  );
  $form['xmlsitemap']['priority_override'] = array(
    '#type' => 'value',
    '#value' => $link['priority_override'],
  );
  // Add the submit handler to adjust the default values if selected.
847
  $form += array('#submit' => array());
848 849 850 851 852 853 854 855
  if (!in_array('xmlsitemap_process_form_link_options', $form['#submit'])) {
    array_unshift($form['#submit'], 'xmlsitemap_process_form_link_options');
  }
}

/**
 * Get a list of priority options.
 *
856
 * @param string $default
857
 *   Include a 'default' option.
858
 * @param string $guides
859
 *   Add helpful indicators for the highest, middle and lowest values.
860
 *
861
 * @return array
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
 *   An array of options.
 */
function xmlsitemap_get_priority_options($default = NULL, $guides = TRUE) {
  $options = array();
  $priorities = array(
    '1.0' => t('1.0'),
    '0.9' => t('0.9'),
    '0.8' => t('0.8'),
    '0.7' => t('0.7'),
    '0.6' => t('0.6'),
    '0.5' => t('0.5'),
    '0.4' => t('0.4'),
    '0.3' => t('0.3'),
    '0.2' => t('0.2'),
    '0.1' => t('0.1'),
    '0.0' => t('0.0'),
  );

  if (isset($default)) {
    $default = number_format($default, 1);
    $options['default'] = t('Default (@value)', array('@value' => $priorities[$default]));
  }

  // Add the rest of the options.
  $options += $priorities;

  if ($guides) {
    $options['1.0'] .= ' ' . t('(highest)');
    $options['0.5'] .= ' ' . t('(normal)');
    $options['0.0'] .= ' ' . t('(lowest)');
  }

  return $options;
}

/**
 * Get a list of priority options.
 *
900
 * @param string $default
901
 *   Include a 'default' option.
902
 *
903
 * @return array
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
 *   An array of options.
 *
 * @see _xmlsitemap_translation_strings()
 */
function xmlsitemap_get_status_options($default = NULL) {
  $options = array();
  $statuses = array(
    1 => t('Included'),
    0 => t('Excluded'),
  );

  if (isset($default)) {
    $default = $default ? 1 : 0;
    $options['default'] = t('Default (@value)', array('@value' => drupal_strtolower($statuses[$default])));
  }

  $options += $statuses;

  return $options;
}