xmlsitemap.admin.inc 28.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
// $Id$

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

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 178 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
function xmlsitemap_sitemap_list_form() {
  // Build the 'Update options' form.
  $operations = array();
  foreach (module_invoke_all('xmlsitemap_sitemap_operations') as $operation => $operation_info) {
    $operations[$operation] = $operation_info['label'];
  }
  asort($operations);

  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#options' => $operations,
    '#default_value' => 'update',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
    '#validate' => array('xmlsitemap_sitemap_list_validate'),
    '#submit' => array('xmlsitemap_sitemap_list_submit'),
  );

  $query = db_select('xmlsitemap_sitemap');
  $query->fields('xmlsitemap_sitemap', array('smid'));
  $smids = $query->execute()->fetchCol();

  $contexts = xmlsitemap_get_context_info();

  $header = array();
  $header['url'] = array('data' => t('URL'), 'field' => 'url');
  foreach ($contexts as $context_key => $context_info) {
    if (!empty($context_info['summary callback'])) {
      $header['context_' . $context_key] = $context_info['label'];
    }
  }
  $header['updated'] = array('data' => t('Last updated'), 'field' => 'updated');
  $header['links'] = array('data' => t('Links'), 'field' => 'links');
  $header['chunks'] = array('data' => t('Pages'), 'field' => 'chunks');
  $header['operations'] = array('data' => t('Operations'));

  $options = array();
  $destination = drupal_get_destination();
  foreach ($smids as $smid) {
    $sitemap = xmlsitemap_sitemap_load($smid);
    $sitemap['uri'] = xmlsitemap_sitemap_uri($sitemap);

    $options[$smid]['url'] = array(
      'data' => array(
        '#type' => 'link',
        '#title' => url($sitemap['uri']['path'], $sitemap['uri']['options']),
        '#href' =>  $sitemap['uri']['path'],
        '#options' =>  $sitemap['uri']['options'],
      ),
    );

    foreach ($contexts as $context_key => $context_info) {
      $options[$smid]['context_' . $context_key] = _xmlsitemap_sitemap_context_summary($sitemap, $context_key, $context_info);
    }

    $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'] : '-';

    $operations = array();
    //if (!empty($sitemap['context'])) {
      $operations['edit'] = array(
        'title' => t('Edit'),
        'href' => 'admin/config/search/xmlsitemap/edit/' . $smid,
        'query' => $destination,
      );
    //}
    $operations['delete'] = array(
      'title' => t('Delete'),
      'href' => 'admin/config/search/xmlsitemap/delete/' . $smid,
      'query' => $destination,
    );
    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,
    '#empty' => t('No XML sitemaps available.') . ' ' . l('Add a new XML sitemap', 'admin/config/search/xmlsitemap/add'),
  );
  return $form;
}

function xmlsitemap_sitemap_edit_form(&$form, $form_state, array $sitemap = array()) {
  $sitemap += array(
    'smid' => NULL,
    'context' => array(),
  );

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

  $form['smid'] = array(
    '#type' => 'value',
    '#value' => $sitemap['smid'],
  );
  // The context settings should be form_alter'ed by the context modules.
  $form['context'] = array(
    '#tree' => TRUE,
  );

  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('form-actions')),
    '#weight' => 100,
  );
  $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;
}

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;
}

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']);
  if ($existing && $existing['smid'] != $form_state['values']['smid']) {
    form_set_error('context', t('A sitemap with the same context already exists.'));
  }
}

function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
  form_state_values_clean($form_state);
  xmlsitemap_sitemap_save($form_state['values']);
  $uri = xmlsitemap_sitemap_uri($form_state['values']);
  drupal_set_message(t('The sitemap %sitemap was saved.', array('%sitemap' => url($uri['path'], $uri['options']))));
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
}

function xmlsitemap_sitemap_delete_form(&$form, $form_state, array $sitemap) {
  $count = (int) db_query("SELECT COUNT(smid) FROM {xmlsitemap_sitemap}")->fetchField();
  if ($count === 1 && empty($_POST)) {
    drupal_set_message('It is not recommended to delete the only XML sitemap.', 'error');
  }

  $form['#sitemap'] = $sitemap;
  $form['smid'] = array(
    '#type' => 'value',
    '#value' => $sitemap['smid'],
  );
  return confirm_form(
    $form,
    t('Are you sure you want to delete the sitemap?'),
    'admin/config/search/xmlsitemap',
    '',
    t('Delete'),
    t('Cancel')
  );
}

function xmlsitemap_sitemap_delete_form_submit($form, $form_state) {
  xmlsitemap_sitemap_delete($form_state['values']['smid']);
  $uri = xmlsitemap_sitemap_uri($form['#sitemap']);
  drupal_set_message(t('The sitemap %sitemap was deleted.', array('%sitemap' => url($uri['path'], $uri['options']))));
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
/**
 * Form builder; Administration settings form.
 *
 * @see system_settings_form()
 * @see xmlsitemap_settings_form_validate()
 */
function xmlsitemap_settings_form() {
  $form['xmlsitemap'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['xmlsitemap']['xmlsitemap_minimum_lifetime'] = array(
    '#type' => 'select',
    '#title' => t('Minimum sitemap lifetime'),
225
    '#options' => array(0 => t('No minimum')) + drupal_map_assoc(array(300, 900, 1800, 3600, 10800, 21600, 43200, 86400, 172800, 259200, 604800), 'format_interval'),
226 227 228 229 230 231 232 233 234 235 236 237 238
    '#default_value' => xmlsitemap_var('minimum_lifetime')
  );
  $form['xmlsitemap']['xmlsitemap_xsl'] = array(
    '#type' => 'checkbox',
    '#title' => t('Include a stylesheet in the sitemaps for humans.'),
    '#default_value' => xmlsitemap_var('xsl'),
    '#description' => t('Using the stylesheet 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 any formatting.')
  );

  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
239
    '#collapsed' => !variable_get('xmlsitemap_developer_mode', 0),
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    '#weight' => 10,
  );
  //$form['advanced']['xmlsitemap_gz'] = array(
  //  '#type' => 'checkbox',
  //  '#title' => t('Generate additional compressed sitemaps using gzip.'),
  //  '#default_value' => xmlsitemap_var('gz'),
  //  '#disabled' => !function_exists('gzencode'),
  //);
  $form['advanced']['xmlsitemap_chunk_size'] = array(
    '#type' => 'select',
    '#title' => t('Number of links in each sitemap page'),
    '#options' => array('auto' => t('Automatic (recommended)')) + drupal_map_assoc(array(100, 500, 1000, 2500, 5000, 10000, 25000, 50000)),
    '#default_value' => xmlsitemap_var('chunk_size'),
    // @todo This description is not clear.
    '#description' => t('If there are problems with rebuilding the sitemap, you may want to manually set this value. If you have more than 50,000 links, an index with multiple sitemap pages will be generated. There is a maximum of 1000 sitemap pages.'),
  );
256 257 258 259 260 261
  $form['advanced']['xmlsitemap_prefetch_aliases'] = array(
    '#type' => 'checkbox',
    '#title' => t('Prefetch URL aliases during sitemap generation.'),
    '#description' => t('This will help increase performance for sitemap generation on sites with a small to medium number of URL alises.'),
    '#default_value' => TRUE,
  );
262 263 264 265 266 267 268
  $form['advanced']['xmlsitemap_batch_limit'] = array(
    '#type' => 'select',
    '#title' => t('Maximum number of sitemap links to process at once'),
    '#options' => drupal_map_assoc(array(5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000)),
    '#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.'),
  );
269 270
  $path = variable_get('xmlsitemap_path', 'xmlsitemap');
  if (!xmlsitemap_check_directory()) {
271
    form_set_error('xmlsitemap_path', t('The directory %directory does not exist or is not writable.', array('%directory' => xmlsitemap_get_directory())));
272
  }
273 274 275
  $form['advanced']['xmlsitemap_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Sitemap cache directory'),
276
    '#default_value' => $path,
277 278
    '#size' => 30,
    '#maxlength' => 255,
279 280
    '#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.'),
    '#field_prefix' =>  file_directory_path() . '/',
281 282 283 284
    '#required' => TRUE,
  );
  $form['advanced']['xmlsitemap_base_url'] = array(
    '#type' => 'textfield',
285
    '#title' => t('Default base URL'),
286 287
    '#default_value' => xmlsitemap_var('base_url'),
    '#size' => 30,
288
    '#description' => t('This is the default base URL used for sitemaps and sitemap links.'),
289 290
    '#required' => TRUE,
  );
291 292 293 294
  $form['advanced']['xmlsitemap_lastmod_format'] = array(
    '#type' => 'select',
    '#title' => t('Last modification date format'),
    '#options' => array(
295 296 297
      'Y-m-d' => t('Short'),
      'Y-m-d\TH:i\Z' => t('Medium'),
      'c' => t('Long'),
298
    ),
299
    '#default_value' => 'Y-m-d\TH:i\Z',
300 301 302 303
  );
  foreach ($form['advanced']['xmlsitemap_lastmod_format']['#options'] as $key => &$label) {
    $label .= ' (' . gmdate($key, REQUEST_TIME) . ')';
  }
304 305
  $form['advanced']['xmlsitemap_developer_mode'] = array(
    '#type' => 'checkbox',
306 307
    '#title' => t('Enable developer mode to expose additional settings.'),
    '#default_value' => variable_get('xmlsitemap_developer_mode', 0),
308 309
  );

310 311 312 313 314
  $form['xmlsitemap_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 20,
  );

315 316
  $entities = xmlsitemap_get_link_info(NULL, TRUE);
  foreach ($entities as $entity => $entity_info) {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    $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]);
  }
339 340 341

  $form['#validate'][] = 'xmlsitemap_settings_form_validate';
  $form['#submit'][] = 'xmlsitemap_settings_form_submit';
342
  array_unshift($form['#submit'], 'xmlsitemap_form_submit_flag_regenerate');
343
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  $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.'));
  }
}

/**
 * Submit handler;
 *
 * @see xmlsitemap_settings_form()
 */
function xmlsitemap_settings_form_submit($form, $form_state) {
  // Save any changes to the frontpage link.
  xmlsitemap_save_link(array('type' => 'frontpage', 'id' => 0, 'loc' => ''));
}

/**
 * Menu callback; Confirm rebuilding of the sitemap.
 *
 * @see xmlsitemap_rebuild_form_submit()
 */
function xmlsitemap_rebuild_form() {
384 385
  if (!$_POST && !variable_get('xmlsitemap_rebuild_needed', FALSE)) {
    if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) {
386 387 388
      drupal_set_message(t('Your sitemap is up to date and does not need to be rebuilt.'), 'error');
    }
    else {
389
      $_REQUEST += array('destination' => 'admin/config/search/xmlsitemap');
390 391 392 393
      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');
    }
  }

394
  // Build the list of rebuildable entities.
395
  $entities = xmlsitemap_get_link_info();
396
  $callbacks = $options = array();
397
  foreach ($entities as $entity => $info) {
398
    if (empty($info['xmlsitemap']['rebuild callback'])) {
399
      // If the entity is missing a rebuild callback, skip.
400 401
      continue;
    }
402
    if (!empty($info['entity keys']['bundle']) && !xmlsitemap_get_link_type_enabled_bundles($entity)) {
403 404
      // If the entity has bundles, but no enabled bundles, skip since
      // rebuilding wouldn't get any links.
405 406 407
      continue;
    }
    else {
408 409
      // Build the list of callbacks and options for the form and batch
      // processing.
410 411
      $callbacks[$entity] = $info['xmlsitemap']['rebuild callback'];
      $options[$entity] = $info['label'];
412 413
    }
  }
414 415 416 417 418

  $form['callbacks'] = array(
    '#type' => 'value',
    '#value' => $callbacks,
  );
419
  $form['entities'] = array(
420
    '#type' => 'select',
421 422
    '#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.'),
423
    '#multiple' => TRUE,
424
    '#options' => $options,
425 426
    '#default_value' => variable_get('xmlsitemap_rebuild_needed', FALSE) || !variable_get('xmlsitemap_developer_mode', 0) ? array_keys($options) : array(),
    '#access' => variable_get('xmlsitemap_developer_mode', 0),
427 428 429 430 431 432 433 434 435 436
  );
  $form['save_custom'] = array(
    '#type' => 'checkbox',
    '#title' => t('Save and restore any custom inclusion and priority links.'),
    '#default_value' => TRUE,
  );

  return confirm_form(
    $form,
    t('Are you sure you want to rebuild the sitemap?'),
437
    'admin/config/search/xmlsitemap',
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    '',
    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) {
  // Set the rebuild flag incase something fails during the rebuild.
  variable_set('xmlsitemap_rebuild_needed', TRUE);

454
  module_load_include('inc', 'xmlsitemap', 'xmlsitemap.generate');
455
  $batch = xmlsitemap_rebuild_batch($form_state['values']['entities'], $form_state['values']['callbacks'], $form_state['values']['save_custom']);
456
  batch_set($batch);
457
  $form_state['redirect'] = 'admin/config/search/xmlsitemap';
458 459 460
}

/**
461
 * Add a table summary for an entity and its bundles.
462
 */
463
function xmlsitemap_add_form_entity_summary(&$form, $entity, array $entity_info) {
464 465
  $priorities = xmlsitemap_get_priority_options(NULL, FALSE);
  $statuses = xmlsitemap_get_status_options(NULL);
466
  $destination = drupal_get_destination();
467 468

  $rows = array();
469 470 471 472 473 474 475 476 477
  $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();
478
    if (xmlsitemap_can_admin_bundle($entity, $bundle) && $path = xmlsitemap_get_bundle_path($entity, $bundle)) {
479 480 481 482 483 484 485
      $options = array('query' => $destination);
      if (module_exists('dialog')) {
        $form['#attached']['library']['dialog'] = array('dialog', 'dialog');
        $path = "admin/config/search/xmlsitemap/settings/$entity/$bundle/nojs";
        $options['attributes']['class'] = array('use-dialog', 'use-ajax');
      }
      $row[] = l($bundle_info['label'], $path, $options);
486 487 488 489 490 491 492 493 494 495 496 497 498
    }
    else {
      $row[] = $bundle_info['label'];
    }
    $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) {
499 500 501 502 503 504 505 506
    $header = array(
      $entity_info['bundle label'],
      t('Inclusion'),
      t('Priority'),
      t('Available'),
      t('Indexed'),
      t('Visible'),
    );
507
    $rows[] = array(
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
      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,
      ),
    );
526
    $form['summary'] = array(
527 528 529
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
530 531 532 533 534 535 536
    );
  }
}

/**
 * Add the link type XML sitemap options to the link type's form.
 *
537 538
 * Caller is responsible for ensuring xmlsitemap_link_bundle_settings_save()
 * is called during submission.
539
 */
540
function xmlsitemap_add_link_bundle_settings(array &$form, array &$form_state, $entity, $bundle) {
541 542
  xmlsitemap_link_bundle_settings_form($form, $form_state, $entity, $bundle, FALSE);

543 544 545 546 547 548
  // Manually call drupal_alter() on the $options form since it was not called
  // using drupal_get_form().
  $form_id = 'xmlsitemap_link_bundle_settings_form';
  drupal_alter('form_' . $form_id, $form, $form_state);
  drupal_alter('form', $form, $form_state, $form_id);

549 550
  // Make the base element a fieldset.
  $form['xmlsitemap'] += array(
551 552 553 554 555 556
    '#type' => 'fieldset',
    '#title' => t('XML sitemap'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => user_access('administer xmlsitemap'),
    '#group' => 'additional_settings',
557 558 559 560 561 562 563 564 565 566 567 568 569 570
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
      ),
    ),
  );

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

function xmlsitemap_link_bundle_settings_form(array &$form, array $form_state, $entity, $bundle, $buttons = TRUE) {
571
  $settings = xmlsitemap_link_bundle_load($entity, $bundle);
572 573 574 575 576 577 578 579 580 581

  $form['xmlsitemap'] = array(
    '#tree' => TRUE,
    '#entity' => $entity,
    '#bundle' => $bundle,
  );
  $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.'),
582
  );
583
  $form['xmlsitemap']['status'] = array(
584 585 586
    '#type' => 'select',
    '#title' => t('Inclusion'),
    '#options' => xmlsitemap_get_status_options(),
587
    '#default_value' => $settings['status'],
588
  );
589
  $form['xmlsitemap']['priority'] = array(
590 591 592
    '#type' => 'select',
    '#title' => t('Default priority'),
    '#options' => xmlsitemap_get_priority_options(),
593
    '#default_value' => $settings['priority'],
594 595
    '#states' => array(
      'invisible' => array(
596
        'select[name="xmlsitemap[status]"]' => array('value' => '0'),
597 598
      ),
    ),
599
  );
600 601 602 603

  if ($buttons) {
    $form['actions'] = array(
      '#type' => 'container',
604 605
      '#attributes' => array('class' => array('form-actions')),
      '#weight' => 100,
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    );
    $form['actions']['save'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );
  }

  return $form;
}

function xmlsitemap_link_bundle_settings_form_submit($form, $form_state) {
  $entity = $form['xmlsitemap']['#entity'];
  $bundle = $form['xmlsitemap']['#bundle'];
  $info = xmlsitemap_get_link_info($entity);
  xmlsitemap_link_bundle_settings_save($entity, $bundle, $form_state['values']['xmlsitemap']);
  drupal_set_message(t('Settings for the @bundle-label %bundle have been saved.', array('@bundle-label' => drupal_strtolower($info['bundle label']), '%bundle' => $info['bundles'][$bundle]['label'])));
622 623
}

624 625 626 627
function xmlsitemap_bundle_settings_dialog($entity, $bundle, $ajax = FALSE) {
  $info = xmlsitemap_get_link_info($entity);

  if (!$ajax) {
628
    if ($path = xmlsitemap_get_bundle_path($entity, $bundle)) {
629 630
      $destination = drupal_get_destination();
      unset($_GET['destination']);
631
      drupal_goto($path, array('query' => $destination));
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    }
    return MENU_NOT_FOUND;
  }

  if (!isset($info['bundles'][$bundle]) || !xmlsitemap_can_admin_bundle($entity, $bundle)) {
    $output = array();
    $output[] = dialog_command_display(t('You are not authorized to access this page.'), array('title' => t('Access denied')));
    ajax_render($output);
  }

  $content = drupal_get_form('xmlsitemap_link_bundle_settings_form', $entity, $bundle);
  $options = array(
    'title' => t('@bundle XML sitemap settings', array('@bundle' => $info['bundles'][$bundle]['label'])),
  );
  $output[] = dialog_command_display($content, $options);
  ajax_render($output);
}

650 651
/**
 * Add a link's XML sitemap options to the link's form.
652 653
 *
 * @todo Add changefreq overridability.
654
 */
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
function xmlsitemap_add_form_link_options(&$form, $entity, $bundle, $id) {
  $info = xmlsitemap_get_link_info($entity);

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

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

  $settings = xmlsitemap_link_bundle_load($entity, $bundle);
  $link += array(
    'access' => 1,
    'status' => $settings['status'],
    'status_default' => $settings['status'],
    'status_override' => 0,
    'priority' => $settings['priority'],
    'priority_default' => $settings['priority'],
    'priority_override' => 0,
  );
676

677 678 679 680 681 682
  $form['xmlsitemap'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('XML sitemap'),
    '#collapsible' => TRUE,
    '#collapsed' => !$link['status_override'] && !$link['priority_override'],
683
    '#access' => user_access('administer xmlsitemap') || xmlsitemap_can_admin_bundle($entity, $bundle),
684 685 686 687 688 689 690 691
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
      ),
    ),
  );

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

697 698
  $bundle_info = $info['bundles'][$bundle];
  if (xmlsitemap_can_admin_bundle($entity, $bundle) && $path = xmlsitemap_get_bundle_path($entity, $bundle)) {
699 700 701
    $form['xmlsitemap']['description'] = array(
      '#prefix' => '<div class="description">',
      '#suffix' => '</div>',
702
      '#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())))),
703 704 705
    );
  }

706 707
  // Show a warning if the link is not accessible and will not be included in
  // the sitemap.
708
  if ($id && !$link['access']) {
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
    $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'],
  );

  // Priority field
  $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.'),
740 741
    '#states' => array(
      'invisible' => array(
742
        'select[name="xmlsitemap[status]"]' => array('value' => '0'),
743 744
      ),
    ),
745
  );
746 747 748 749 750 751 752
  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'),
    );
  }
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
  $form['xmlsitemap']['priority_default'] = array(
    '#type' => 'value',
    '#value' => $link['priority_default'],
  );
  $form['xmlsitemap']['priority_override'] = array(
    '#type' => 'value',
    '#value' => $link['priority_override'],
  );

  // Other persistent fields.
  //$form['xmlsitemap']['lastmod'] = array(
  //  '#type' => 'value',
  //  '#value' => $node->xmlsitemap['lastmod'],
  //);
  //$form['xmlsitemap']['changefreq'] = array(
  //  '#type' => 'value',
  //  '#value' => $node->xmlsitemap['changefreq'],
  //);
  //$form['xmlsitemap']['changecount'] = array(
  //  '#type' => 'value',
  //  '#value' => $node->xmlsitemap['changecount'],
  //);

  // Add the submit handler to adjust the default values if selected.
  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.
 *
 * @param $default
 *   Include a 'default' option.
 * @param $guides
 *   Add helpful indicators for the highest, middle and lowest values.
 * @return
 *   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.
 *
 * @param $default
 *   Include a 'default' option.
 * @return
 *   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;
}