[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/xmlsitemap/ -> xmlsitemap.admin.inc (source)

   1  <?php
   2  // $Id: xmlsitemap.admin.inc,v 1.1.2.99 2010/04/30 03:31:25 davereid Exp $
   3  
   4  /**
   5   * @file
   6   * Administrative page callbacks for the xmlsitemap module.
   7   *
   8   * @ingroup xmlsitemap
   9   */
  10  
  11  /**
  12   * Render a tableselect list of XML sitemaps for the main admin page.
  13   */
  14  function xmlsitemap_sitemap_list_form() {
  15    xmlsitemap_load_all_includes();
  16    $destination = drupal_get_destination();
  17  
  18    // Add the local actions.
  19    $local_actions = array(
  20      'add' => array(
  21        'title' => t('Add new XML sitemap'),
  22        'href' => 'admin/settings/xmlsitemap/add',
  23        'query' => $destination,
  24      ),
  25    );
  26    $form['actions'] = array(
  27      '#type' => 'markup',
  28      '#value' => theme('links', $local_actions, array('class' => 'item-list action-links')),
  29    );
  30  
  31    // Build the 'Update options' form.
  32    $form['#operations'] = module_invoke_all('xmlsitemap_sitemap_operations');
  33    $operations = array();
  34    foreach ($form['#operations'] as $operation => $operation_info) {
  35      $operations[$operation] = $operation_info['label'];
  36    }
  37    asort($operations);
  38  
  39    $form['operations'] = array(
  40      '#type' => 'fieldset',
  41      '#title' => t('Update options'),
  42      '#prefix' => '<div class="container-inline">',
  43      '#suffix' => '</div>',
  44      '#access' => module_exists('elements'),
  45    );
  46    $form['operations']['operation'] = array(
  47      '#type' => 'select',
  48      '#options' => $operations,
  49      '#default_value' => 'update',
  50    );
  51    $form['operations']['submit'] = array(
  52      '#type' => 'submit',
  53      '#value' => t('Update'),
  54      //'#validate' => array('xmlsitemap_sitemap_list_form_validate'),
  55      //'#submit' => array('xmlsitemap_sitemap_list_form_submit'),
  56    );
  57  
  58    $contexts = xmlsitemap_get_context_info();
  59  
  60    $header = array();
  61    $header['url'] = array('data' => t('URL'));
  62    foreach ($contexts as $context_key => $context_info) {
  63      if (!empty($context_info['summary callback'])) {
  64        $header['context_' . $context_key] = $context_info['label'];
  65      }
  66    }
  67    $header['updated'] = array('data' => t('Last updated'), 'field' => 'updated', 'sort' => 'asc');
  68    $header['links'] = array('data' => t('Links'), 'field' => 'links');
  69    $header['chunks'] = array('data' => t('Pages'), 'field' => 'chunks');
  70    $header['operations'] = array('data' => t('Operations'));
  71  
  72    $smids = array();
  73    $query = db_query("SELECT smid FROM {xmlsitemap_sitemap}" . tablesort_sql($header));
  74    while ($smid = db_result($query)) {
  75      $smids[] = $smid;
  76    }
  77    $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
  78  
  79    $options = array();
  80    foreach ($sitemaps as $smid => $sitemap) {
  81      $sitemap['url'] = url($sitemap['uri']['path'], $sitemap['uri']['options']);
  82  
  83      $options[$smid]['url'] = array(
  84        'data' => l($sitemap['url'], $sitemap['url']),
  85      );
  86  
  87      foreach ($contexts as $context_key => $context_info) {
  88        $options[$smid]['context_' . $context_key] = _xmlsitemap_sitemap_context_summary($sitemap, $context_key, $context_info);
  89      }
  90  
  91      $options[$smid]['updated'] = $sitemap['updated'] ? format_date($sitemap['updated'], 'short') : t('Never');
  92      $options[$smid]['links'] = $sitemap['updated'] ? $sitemap['links'] : '-';
  93      $options[$smid]['chunks'] = $sitemap['updated'] ? $sitemap['chunks'] : '-';
  94  
  95      // @todo Highlight sitemaps that need updating.
  96      //$options[$smid]['#attributes']['class'] = 'warning';
  97  
  98      $operations = array();
  99      $operations['edit'] = array(
 100        'title' => t('Edit'),
 101        'href' => 'admin/settings/xmlsitemap/edit/' . $smid,
 102        'query' => $destination,
 103      );
 104      $operations['delete'] = array(
 105        'title' => t('Delete'),
 106        'href' => 'admin/settings/xmlsitemap/delete/' . $smid,
 107        'query' => $destination,
 108      );
 109      if ($operations) {
 110        $options[$smid]['operations'] = array(
 111          'data' => theme('links', $operations, array('class' => 'links inline')),
 112        );
 113      }
 114      else {
 115        $options[$smid]['operations'] = t('None (sitemap locked)');
 116      }
 117    }
 118  
 119    if (module_exists('elements')) {
 120      $form['sitemaps'] = array(
 121        '#type' => 'tableselect',
 122        '#header' => $header,
 123        '#options' => $options,
 124        '#empty' => t('No XML sitemaps available.') . ' ' . l('Add a new XML sitemap', 'admin/settings/xmlsitemap/add'),
 125      );
 126    }
 127    else {
 128      if (empty($options)) {
 129        $options[] = array(array(
 130          'data' => t('No XML sitemaps available.') . ' ' . l('Add a new XML sitemap', 'admin/settings/xmlsitemap/add'),
 131          'colspan' => count($header),
 132          'class' => 'empty',
 133        ));
 134      }
 135      $form['sitemaps'] = array(
 136        '#type' => 'markup',
 137        '#value' => theme('table', $header, $options),
 138      );
 139    }
 140  
 141    return $form;
 142  }
 143  
 144  /**
 145   * Validate xmlsitemap_sitemap_list_form submissions.
 146   */
 147  function xmlsitemap_sitemap_list_form_validate($form, &$form_state) {
 148    $form_state['values']['sitemaps'] = array_filter($form_state['values']['sitemaps']);
 149  
 150    // Error if there are no items to select.
 151    if (!count($form_state['values']['sitemaps'])) {
 152      form_set_error('', t('No sitemaps selected.'));
 153    }
 154  }
 155  
 156  /**
 157   * Process xmlsitemap_sitemap_list_form submissions.
 158   *
 159   * Execute the chosen 'Update option' on the selected sitemaps.
 160   */
 161  function xmlsitemap_sitemap_list_form_submit($form, &$form_state) {
 162    $operation = $form['#operations'][$form_state['values']['operation']];
 163  
 164    // Filter out unchecked sitemaps.
 165    $sitemaps = array_filter($form_state['values']['sitemaps']);
 166  
 167    if (!empty($operation['confirm']) && empty($form_state['values']['confirm'])) {
 168      // We need to rebuild the form to go to a second step. For example, to
 169      // show the confirmation form for the deletion of redirects.
 170      $form_state['rebuild'] = TRUE;
 171    }
 172    else {
 173      $function = $operation['callback'];
 174  
 175      // Add in callback arguments if present.
 176      if (isset($operation['callback arguments'])) {
 177        $args = array_merge(array($sitemaps), $operation['callback arguments']);
 178      }
 179      else {
 180        $args = array($sitemaps);
 181      }
 182      call_user_func_array($function, $args);
 183  
 184      $count = count($form_state['values']['sitemaps']);
 185      //watchdog('xmlsitemap', '@action @count XML sitemaps.', array('@action' => $operation['action past'], '@count' => $count));
 186      drupal_set_message(format_plural(count($sitemaps), '@action @count XML sitemap.', '@action @count XML sitemaps.', array('@action' => $operation['action past'], '@count' => $count)));
 187      //$form_state['redirect'] = 'admin/settings/xmlsitemap';
 188    }
 189  }
 190  
 191  function xmlsitemap_sitemap_edit_form($form_state, array $sitemap = array()) {
 192    _xmlsitemap_set_breadcrumb();
 193  
 194    $sitemap += array(
 195      'smid' => NULL,
 196      'context' => array(),
 197    );
 198  
 199    $form['#sitemap'] = $sitemap;
 200  
 201    $form['smid'] = array(
 202      '#type' => 'value',
 203      '#value' => $sitemap['smid'],
 204    );
 205    // The context settings should be form_alter'ed by the context modules.
 206    $form['context'] = array(
 207      '#tree' => TRUE,
 208    );
 209  
 210    $form['actions'] = array(
 211      //'#type' => 'container',
 212      //'#attributes' => array('class' => array('form-actions')),
 213      '#weight' => 100,
 214    );
 215    $form['actions']['save'] = array(
 216      '#type' => 'submit',
 217      '#value' => t('Save'),
 218    );
 219    $form['actions']['cancel'] = array(
 220      '#type' => 'markup',
 221      '#value' => l(t('Cancel'), isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/xmlsitemap'),
 222    );
 223  
 224    // Let other modules alter this form with their context settings.
 225    $form['#pre_render'][] = 'xmlsitemap_sitemap_edit_form_pre_render';
 226  
 227    return $form;
 228  }
 229  
 230  function xmlsitemap_sitemap_edit_form_pre_render($form) {
 231    $visible_children = xmlsitemap_element_get_visible_children($form['context']);
 232    if (empty($visible_children)) {
 233      $form['context']['empty'] = array(
 234        '#type' => 'markup',
 235        '#value' => '<p>' . t('There are currently no XML sitemap contexts available.') . '</p>',
 236      );
 237    }
 238    return $form;
 239  }
 240  
 241  function xmlsitemap_sitemap_edit_form_validate($form, &$form_state) {
 242    // If there are no context options, the $form_state['values']['context']
 243    // disappears.
 244    $form_state['values'] += array('context' => array());
 245    $existing = xmlsitemap_sitemap_load_by_context($form_state['values']['context']);
 246    if ($existing && $existing['smid'] != $form_state['values']['smid']) {
 247      form_set_error('context', t('A sitemap with the same context already exists.'));
 248    }
 249  }
 250  
 251  function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
 252    xmlsitemap_sitemap_save($form_state['values']);
 253    drupal_set_message(t('The sitemap has been saved.'));
 254    $form_state['redirect'] = 'admin/settings/xmlsitemap';
 255    // @todo If context was changed, needs to be regenerated.
 256  }
 257  
 258  function xmlsitemap_sitemap_delete_form($form_state, array $sitemap) {
 259    _xmlsitemap_set_breadcrumb();
 260  
 261    $count = (int) db_result(db_query("SELECT COUNT(smid) FROM {xmlsitemap_sitemap}"));
 262    if ($count === 1 && empty($_POST)) {
 263      drupal_set_message('It is not recommended to delete the only XML sitemap.', 'error');
 264    }
 265  
 266    $form['#sitemap'] = $sitemap;
 267    $form['smid'] = array(
 268      '#type' => 'value',
 269      '#value' => $sitemap['smid'],
 270    );
 271    return confirm_form(
 272      $form,
 273      t('Are you sure you want to delete the sitemap?'),
 274      'admin/settings/xmlsitemap',
 275      '',
 276      t('Delete'),
 277      t('Cancel')
 278    );
 279  }
 280  
 281  function xmlsitemap_sitemap_delete_form_submit($form, $form_state) {
 282    xmlsitemap_sitemap_delete($form_state['values']['smid']);
 283    drupal_set_message(t('The sitemap has been deleted.'));
 284    $form_state['redirect'] = 'admin/settings/xmlsitemap';
 285  }
 286  
 287  /**
 288   * Form builder; Administration settings form.
 289   *
 290   * @see system_settings_form()
 291   * @see xmlsitemap_settings_form_validate()
 292   */
 293  function xmlsitemap_settings_form() {
 294    $form['xmlsitemap_minimum_lifetime'] = array(
 295      '#type' => 'select',
 296      '#title' => t('Minimum sitemap lifetime'),
 297      '#options' => array(0 => t('No minimum')) + drupal_map_assoc(array(300, 900, 1800, 3600, 10800, 21600, 43200, 86400, 172800, 259200, 604800), 'format_interval'),
 298      '#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'))),
 299      '#default_value' => variable_get('xmlsitemap_minimum_lifetime', 0),
 300    );
 301    $form['xmlsitemap_xsl'] = array(
 302      '#type' => 'checkbox',
 303      '#title' => t('Include a stylesheet in the sitemaps for humans.'),
 304      '#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.'),
 305      '#default_value' => variable_get('xmlsitemap_xsl', 1),
 306    );
 307    $form['xmlsitemap_prefetch_aliases'] = array(
 308      '#type' => 'checkbox',
 309      '#title' => t('Prefetch URL aliases during sitemap generation.'),
 310      '#description' => t('When enabled, this will fetch all URL alises 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.'),
 311      '#default_value' => variable_get('xmlsitemap_prefetch_aliases', 1),
 312    );
 313  
 314    $form['advanced'] = array(
 315      '#type' => 'fieldset',
 316      '#title' => t('Advanced settings'),
 317      '#collapsible' => TRUE,
 318      '#collapsed' => !variable_get('xmlsitemap_developer_mode', 0),
 319      '#weight' => 10,
 320      '#group' => FALSE,
 321    );
 322    //$form['advanced']['xmlsitemap_gz'] = array(
 323    //  '#type' => 'checkbox',
 324    //  '#title' => t('Generate additional compressed sitemaps using gzip.'),
 325    //  '#default_value' => xmlsitemap_var('gz'),
 326    //  '#disabled' => !function_exists('gzencode'),
 327    //);
 328    $form['advanced']['xmlsitemap_chunk_size'] = array(
 329      '#type' => 'select',
 330      '#title' => t('Number of links in each sitemap page'),
 331      '#options' => array('auto' => t('Automatic (recommended)')) + drupal_map_assoc(array(100, 500, 1000, 2500, 5000, 10000, 25000, XMLSITEMAP_MAX_SITEMAP_LINKS)),
 332      '#default_value' => xmlsitemap_var('chunk_size'),
 333      // @todo This description is not clear.
 334      '#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)),
 335    );
 336    $form['advanced']['xmlsitemap_batch_limit'] = array(
 337      '#type' => 'select',
 338      '#title' => t('Maximum number of sitemap links to process at once'),
 339      '#options' => drupal_map_assoc(array(5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000)),
 340      '#default_value' => xmlsitemap_var('batch_limit'),
 341      '#description' => t('If you have problems running cron or rebuilding the sitemap, you may want to lower this value.'),
 342    );
 343    file_check_directory(xmlsitemap_get_directory(), FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'xmlsitemap_path');
 344    $form['advanced']['xmlsitemap_path'] = array(
 345      '#type' => 'textfield',
 346      '#title' => t('Sitemap cache directory'),
 347      '#default_value' => variable_get('xmlsitemap_path', 'xmlsitemap'),
 348      '#size' => 30,
 349      '#maxlength' => 255,
 350      '#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.'),
 351      '#field_prefix' =>  file_directory_path() . '/',
 352      '#required' => TRUE,
 353    );
 354    $form['advanced']['xmlsitemap_base_url'] = array(
 355      '#type' => 'textfield',
 356      '#title' => t('Default base URL'),
 357      '#default_value' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
 358      '#size' => 30,
 359      '#description' => t('This is the default base URL used for sitemaps and sitemap links.'),
 360      '#required' => TRUE,
 361    );
 362    $form['advanced']['xmlsitemap_lastmod_format'] = array(
 363      '#type' => 'select',
 364      '#title' => t('Last modification date format'),
 365      '#options' => array(
 366        XMLSITEMAP_LASTMOD_SHORT => t('Short'),
 367        XMLSITEMAP_LASTMOD_MEDIUM => t('Medium'),
 368        XMLSITEMAP_LASTMOD_LONG => t('Long'),
 369      ),
 370      '#default_value' => variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM),
 371    );
 372    foreach ($form['advanced']['xmlsitemap_lastmod_format']['#options'] as $key => &$label) {
 373      $label .= ' (' . gmdate($key, REQUEST_TIME) . ')';
 374    }
 375    $form['advanced']['xmlsitemap_developer_mode'] = array(
 376      '#type' => 'checkbox',
 377      '#title' => t('Enable developer mode to expose additional settings.'),
 378      '#default_value' => variable_get('xmlsitemap_developer_mode', 0),
 379    );
 380  
 381    $entities = xmlsitemap_get_link_info(NULL, TRUE);
 382    foreach ($entities as $entity => $entity_info) {
 383      $form[$entity] = array(
 384        '#type' => 'fieldset',
 385        '#title' => $entity_info['label'],
 386        '#collapsible' => TRUE,
 387        '#collapsed' => TRUE,
 388        '#group' => 'xmlsitemap_settings',
 389        '#weight' => 20,
 390      );
 391  
 392      if (!empty($entity_info['bundles'])) {
 393        // If this entity has bundles, show a bundle setting summary.
 394        xmlsitemap_add_form_entity_summary($form[$entity], $entity, $entity_info);
 395      }
 396  
 397      if (!empty($entity_info['xmlsitemap']['settings callback'])) {
 398        // Add any entity-specific settings.
 399        $entity_info['xmlsitemap']['settings callback']($form[$entity]);
 400      }
 401  
 402      // Ensure that the entity fieldset is not shown if there are no accessible
 403      // sub-elements.
 404      $form[$entity]['#access'] = (bool) xmlsitemap_element_get_visible_children($form[$entity]);
 405    }
 406  
 407    $form['#pre_render'][] = 'vertical_tabs_form_pre_render';
 408    $form['#validate'][] = 'xmlsitemap_settings_form_validate';
 409    $form['#submit'][] = 'xmlsitemap_settings_form_submit';
 410    $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
 411  
 412    $form = system_settings_form($form);
 413    $form['buttons']['#weight'] = 100;
 414    return $form;
 415  }
 416  
 417  /**
 418   * Form validator; Check the sitemap files directory.
 419   *
 420   * @see xmlsitemap_settings_form()
 421   */
 422  function xmlsitemap_settings_form_validate($form, &$form_state) {
 423    // Check that the chunk size will not create more than 1000 chunks.
 424    $chunk_size = $form_state['values']['xmlsitemap_chunk_size'];
 425    if ($chunk_size != 'auto' && $chunk_size != 50000 && (xmlsitemap_get_link_count() / $chunk_size) > 1000) {
 426      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)));
 427    }
 428  
 429    $base_url = &$form_state['values']['xmlsitemap_base_url'];
 430    $base_url = rtrim($base_url, '/');
 431    if ($base_url != '' && !valid_url($base_url, TRUE)) {
 432      form_set_error('xmlsitemap_base_url', t('Invalid base URL.'));
 433    }
 434  }
 435  
 436  /**
 437   * Submit handler;
 438   *
 439   * @see xmlsitemap_settings_form()
 440   */
 441  function xmlsitemap_settings_form_submit($form, $form_state) {
 442    // Save any changes to the frontpage link.
 443    xmlsitemap_link_save(array('type' => 'frontpage', 'id' => 0, 'loc' => ''));
 444  }
 445  
 446  /**
 447   * Menu callback; Confirm rebuilding of the sitemap.
 448   *
 449   * @see xmlsitemap_rebuild_form_submit()
 450   */
 451  function xmlsitemap_rebuild_form() {
 452    if (!$_POST && !variable_get('xmlsitemap_rebuild_needed', FALSE)) {
 453      if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) {
 454        drupal_set_message(t('Your sitemap is up to date and does not need to be rebuilt.'), 'error');
 455      }
 456      else {
 457        $_REQUEST += array('destination' => 'admin/settings/xmlsitemap');
 458        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');
 459      }
 460    }
 461  
 462    // Build a list of rebuildable link types.
 463    module_load_include('generate.inc', 'xmlsitemap');
 464    $rebuild_types = xmlsitemap_get_rebuildable_link_types();
 465  
 466    $form['entities'] = array(
 467      '#type' => 'select',
 468      '#title' => t("Select which link types you would like to rebuild"),
 469      '#description' => t('If no link types are selected, the sitemap files will just be regenerated.'),
 470      '#multiple' => TRUE,
 471      '#options' => drupal_map_assoc($rebuild_types),
 472      '#default_value' => variable_get('xmlsitemap_rebuild_needed', FALSE) || !variable_get('xmlsitemap_developer_mode', 0) ? $rebuild_types : array(),
 473      '#access' => variable_get('xmlsitemap_developer_mode', 0),
 474    );
 475    $form['save_custom'] = array(
 476      '#type' => 'checkbox',
 477      '#title' => t('Save and restore any custom inclusion and priority links.'),
 478      '#default_value' => TRUE,
 479    );
 480  
 481    return confirm_form(
 482      $form,
 483      t('Are you sure you want to rebuild the sitemap?'),
 484      'admin/settings/xmlsitemap',
 485      '',
 486      t('Rebuild sitemap'),
 487      t('Cancel')
 488    );
 489  }
 490  
 491  /**
 492   * Submit handler; Starts the sitemap rebuild batch.
 493   *
 494   * @see xmlsitemap_rebuild_form()
 495   * @see xmlsitemap_rebuild_batch()
 496   */
 497  function xmlsitemap_rebuild_form_submit($form, &$form_state) {
 498    module_load_include('generate.inc', 'xmlsitemap');
 499    $batch = xmlsitemap_rebuild_batch($form_state['values']['entities'], $form_state['values']['save_custom']);
 500    batch_set($batch);
 501    $form_state['redirect'] = 'admin/settings/xmlsitemap';
 502  }
 503  
 504  /**
 505   * Add a table summary for an entity and its bundles.
 506   */
 507  function xmlsitemap_add_form_entity_summary(&$form, $entity, array $entity_info) {
 508    $priorities = xmlsitemap_get_priority_options(NULL, FALSE);
 509    $statuses = xmlsitemap_get_status_options(NULL);
 510    $destination = drupal_get_destination();
 511  
 512    $rows = array();
 513    $totals = array('total' => 0, 'indexed' => 0, 'visible' => 0);
 514    foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
 515      // Fetch current per-bundle link total and indexed counts.
 516      $status = xmlsitemap_get_link_type_indexed_status($entity, $bundle);
 517      $totals['total'] += $status['total'];
 518      $totals['indexed'] += $status['indexed'];
 519      $totals['visible'] += $status['visible'];
 520  
 521      $row = array();
 522      if (xmlsitemap_can_admin_bundle($entity, $bundle) && $path = xmlsitemap_get_bundle_path($entity, $bundle)) {
 523        $options = array('query' => $destination);
 524        if (module_exists('ctools') && module_exists('dialog')) {
 525          dialog_add_js();
 526          $path = "admin/settings/xmlsitemap/settings/$entity/$bundle/nojs";
 527          $options['attributes'] = array('class' => 'ctools-use-dialog');
 528        }
 529        $row[] = l($bundle_info['label'], $path, $options);
 530      }
 531      else {
 532        $row[] = $bundle_info['label'];
 533      }
 534      $row[] = $statuses[$bundle_info['xmlsitemap']['status'] ? 1 : 0];
 535      $row[] = $priorities[number_format($bundle_info['xmlsitemap']['priority'], 1)];
 536      $row[] = $status['total'];
 537      $row[] = $status['indexed'];
 538      $row[] = $status['visible'];
 539      $rows[] = $row;
 540    }
 541  
 542    if ($rows) {
 543      $header = array(
 544        $entity_info['bundle label'],
 545        t('Inclusion'),
 546        t('Priority'),
 547        t('Available'),
 548        t('Indexed'),
 549        t('Visible'),
 550      );
 551      $rows[] = array(
 552        array(
 553          'data' => t('Totals'),
 554          'colspan' => 3,
 555          'header' => TRUE,
 556        ),
 557        array(
 558          'data' => $totals['total'],
 559          'header' => TRUE,
 560        ),
 561        array(
 562          'data' => $totals['indexed'],
 563          'header' => TRUE,
 564        ),
 565        array(
 566          'data' => $totals['visible'],
 567          'header' => TRUE,
 568        ),
 569      );
 570      $form['summary'] = array(
 571        '#value' => theme('table', $header, $rows),
 572      );
 573    }
 574  }
 575  
 576  /**
 577   * Add the link type XML sitemap options to the link type's form.
 578   *
 579   * Caller is responsible for ensuring xmlsitemap_link_bundle_settings_save()
 580   * is called during submission.
 581   */
 582  function xmlsitemap_add_link_bundle_settings(array &$form, array &$form_state, $entity, $bundle) {
 583    $form += xmlsitemap_link_bundle_settings_form($form_state, $entity, $bundle, FALSE);
 584  
 585    // Manually call drupal_alter() on the $options form since it was not called
 586    // using drupal_get_form().
 587    $form_id = 'xmlsitemap_link_bundle_settings_form';
 588    $data = &$form;
 589    $data['__drupal_alter_by_ref'] = array(&$form_state);
 590    drupal_alter('form_'. $form_id, $data);
 591    $data['__drupal_alter_by_ref'] = array(&$form_state);
 592    drupal_alter('form', $data, $form_id);
 593  
 594    // Make the base element a fieldset.
 595    $form['xmlsitemap'] += array(
 596      '#type' => 'fieldset',
 597      '#title' => t('XML sitemap'),
 598      '#collapsible' => TRUE,
 599      '#collapsed' => TRUE,
 600      '#access' => user_access('administer xmlsitemap'),
 601      '#group' => 'additional_settings',
 602      '#attached' => array(
 603        'js' => array(
 604          'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
 605        ),
 606      ),
 607    );
 608  }
 609  
 610  function xmlsitemap_link_bundle_settings_form(array $form_state, $entity, $bundle, $buttons = TRUE) {
 611    $settings = xmlsitemap_link_bundle_load($entity, $bundle);
 612  
 613    $form['xmlsitemap'] = array(
 614      '#tree' => TRUE,
 615      '#entity' => $entity,
 616      '#bundle' => $bundle,
 617    );
 618    $form['xmlsitemap']['description'] = array(
 619      '#prefix' => '<div class="description">',
 620      '#suffix' => '</div>',
 621      '#value' => t('Changing these type settings will affect any items of this type that have either inclusion or priority set to default.'),
 622    );
 623    $form['xmlsitemap']['status'] = array(
 624      '#type' => 'select',
 625      '#title' => t('Inclusion'),
 626      '#options' => xmlsitemap_get_status_options(),
 627      '#default_value' => $settings['status'],
 628    );
 629    $form['xmlsitemap']['priority'] = array(
 630      '#type' => 'select',
 631      '#title' => t('Default priority'),
 632      '#options' => xmlsitemap_get_priority_options(),
 633      '#default_value' => $settings['priority'],
 634      '#process' => array('form_expand_ahah', 'ctools_dependent_process'),
 635      '#dependency' => array('edit-xmlsitemap-status' => array(1)),
 636    );
 637  
 638    // Hide the priority field if the link is excluded.
 639    if (module_exists('ctools')) {
 640      ctools_include('dependent');
 641    }
 642  
 643    if ($buttons) {
 644      $form['actions'] = array(
 645        //'#type' => 'container',
 646        //'#attributes' => array('class' => array('form-actions')),
 647        '#weight' => 100,
 648      );
 649      $form['actions']['save'] = array(
 650        '#type' => 'submit',
 651        '#value' => t('Save'),
 652      );
 653      if (!empty($form_state['ajax'])) {
 654        $form['actions']['submit']['#attributes']['class'] = 'ctools-dialog-button';
 655      }
 656    }
 657  
 658    return $form;
 659  }
 660  
 661  function xmlsitemap_link_bundle_settings_form_submit($form, $form_state) {
 662    $entity = $form['xmlsitemap']['#entity'];
 663    $bundle = $form['xmlsitemap']['#bundle'];
 664    $info = xmlsitemap_get_link_info($entity);
 665    xmlsitemap_link_bundle_settings_save($entity, $bundle, $form_state['values']['xmlsitemap']);
 666    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'])));
 667  }
 668  
 669  function xmlsitemap_bundle_settings_dialog($entity, $bundle, $ajax = FALSE) {
 670    $info = xmlsitemap_get_link_info($entity);
 671  
 672    if (!$ajax) {
 673      if ($path = xmlsitemap_get_bundle_path($entity, $bundle)) {
 674        $destination = drupal_get_destination();
 675        unset($_REQUEST['destination']);
 676        drupal_goto($path, array('query' => $destination));
 677      }
 678      return drupal_not_found();
 679    }
 680  
 681    ctools_include('ajax');
 682    if (!isset($info['bundles'][$bundle]) || !xmlsitemap_can_admin_bundle($entity, $bundle)) {
 683      $commands = array();
 684      $commands[] = dialog_command_display(t('Access denied'), t('You are not authorized to access this page.'));
 685      ctools_ajax_render($commands);
 686    }
 687  
 688    $form_state = array(
 689      'ajax' => TRUE,
 690      'title' => t('@bundle XML sitemap settings', array('@bundle' => $info['bundles'][$bundle]['label'])),
 691      'args' => array($entity, $bundle),
 692    );
 693    $output = dialog_form_wrapper('xmlsitemap_link_bundle_settings_form', $form_state);
 694  
 695    if (empty($output)) {
 696      $output[] = dialog_command_display(t('Settings saved'), t('Reloading...'));
 697      $output[] = ctools_ajax_command_reload();
 698    }
 699    ctools_ajax_render($output);
 700  }
 701  
 702  /**
 703   * Add a link's XML sitemap options to the link's form.
 704   *
 705   * @todo Add changefreq overridability.
 706   */
 707  function xmlsitemap_add_form_link_options(&$form, $entity, $bundle, $id) {
 708    $info = xmlsitemap_get_link_info($entity);
 709  
 710    if (!$info || empty($info['bundles'][$bundle])) {
 711      return;
 712    }
 713  
 714    if (!$link = xmlsitemap_link_load($entity, $id)) {
 715      $link = array();
 716    }
 717  
 718    $settings = xmlsitemap_link_bundle_load($entity, $bundle);
 719    $link += array(
 720      'access' => 1,
 721      'status' => $settings['status'],
 722      'status_default' => $settings['status'],
 723      'status_override' => 0,
 724      'priority' => $settings['priority'],
 725      'priority_default' => $settings['priority'],
 726      'priority_override' => 0,
 727    );
 728  
 729    $form['xmlsitemap'] = array(
 730      '#type' => 'fieldset',
 731      '#tree' => TRUE,
 732      '#title' => t('XML sitemap'),
 733      '#collapsible' => TRUE,
 734      '#collapsed' => !$link['status_override'] && !$link['priority_override'],
 735      '#access' => user_access('administer xmlsitemap') || xmlsitemap_can_admin_bundle($entity, $bundle),
 736      '#group' => 'additional_settings',
 737      '#attached' => array(
 738        'js' => array(
 739          'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
 740        ),
 741      ),
 742    );
 743  
 744    $bundle_info = $info['bundles'][$bundle];
 745    if (xmlsitemap_can_admin_bundle($entity, $bundle) && $path = xmlsitemap_get_bundle_path($entity, $bundle)) {
 746      $form['xmlsitemap']['description'] = array(
 747        '#prefix' => '<div class="description">',
 748        '#suffix' => '</div>',
 749        '#value' => 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())))),
 750      );
 751    }
 752  
 753    // Show a warning if the link is not accessible and will not be included in
 754    // the sitemap.
 755    if ($id && !$link['access']) {
 756      $form['xmlsitemap']['warning'] = array(
 757        '#type' => 'markup',
 758        '#prefix' => '<p><strong>',
 759        '#suffix' => '</strong></p>',
 760        '#value' => ('This item is not currently visible to anonymous users, so it will not be included in the sitemap.'),
 761      );
 762    }
 763  
 764    // Status field (inclusion/exclusion)
 765    $form['xmlsitemap']['status'] = array(
 766      '#type' => 'select',
 767      '#title' => t('Inclusion'),
 768      '#options' => xmlsitemap_get_status_options($link['status_default']),
 769      '#default_value' => $link['status_override'] ? $link['status'] : 'default',
 770    );
 771    $form['xmlsitemap']['status_default'] = array(
 772      '#type' => 'value',
 773      '#value' => $link['status_default'],
 774    );
 775    $form['xmlsitemap']['status_override'] = array(
 776      '#type' => 'value',
 777      '#value' => $link['status_override'],
 778    );
 779  
 780    // Priority field
 781    $form['xmlsitemap']['priority'] = array(
 782      '#type' => 'select',
 783      '#title' => t('Priority'),
 784      '#options' => xmlsitemap_get_priority_options($link['priority_default']),
 785      '#default_value' => $link['priority_override'] ? number_format($link['priority'], 1) : 'default',
 786      '#description' => t('The priority of this URL relative to other URLs on your site.'),
 787      '#process' => array('form_expand_ahah', 'ctools_dependent_process'),
 788      '#dependency' => array('edit-xmlsitemap-status' => array(1)),
 789    );
 790    if ($link['status_default']) {
 791      $form['xmlsitemap']['priority']['#dependency']['edit-xmlsitemap-status'][] = 'default';
 792    }
 793    $form['xmlsitemap']['priority_default'] = array(
 794      '#type' => 'value',
 795      '#value' => $link['priority_default'],
 796    );
 797    $form['xmlsitemap']['priority_override'] = array(
 798      '#type' => 'value',
 799      '#value' => $link['priority_override'],
 800    );
 801  
 802    // Other persistent fields.
 803    //$form['xmlsitemap']['lastmod'] = array(
 804    //  '#type' => 'value',
 805    //  '#value' => $node->xmlsitemap['lastmod'],
 806    //);
 807    //$form['xmlsitemap']['changefreq'] = array(
 808    //  '#type' => 'value',
 809    //  '#value' => $node->xmlsitemap['changefreq'],
 810    //);
 811    //$form['xmlsitemap']['changecount'] = array(
 812    //  '#type' => 'value',
 813    //  '#value' => $node->xmlsitemap['changecount'],
 814    //);
 815  
 816    // Hide the priority field if the link is excluded.
 817    if (module_exists('ctools')) {
 818      ctools_include('dependent');
 819    }
 820  
 821    // Add the submit handler to adjust the default values if selected.
 822    if (!in_array('xmlsitemap_process_form_link_options', $form['#submit'])) {
 823      array_unshift($form['#submit'], 'xmlsitemap_process_form_link_options');
 824    }
 825  }
 826  
 827  /**
 828   * Get a list of priority options.
 829   *
 830   * @param $default
 831   *   Include a 'default' option.
 832   * @param $guides
 833   *   Add helpful indicators for the highest, middle and lowest values.
 834   * @return
 835   *   An array of options.
 836   */
 837  function xmlsitemap_get_priority_options($default = NULL, $guides = TRUE) {
 838    $options = array();
 839    $priorities = array(
 840      '1.0' => t('1.0'),
 841      '0.9' => t('0.9'),
 842      '0.8' => t('0.8'),
 843      '0.7' => t('0.7'),
 844      '0.6' => t('0.6'),
 845      '0.5' => t('0.5'),
 846      '0.4' => t('0.4'),
 847      '0.3' => t('0.3'),
 848      '0.2' => t('0.2'),
 849      '0.1' => t('0.1'),
 850      '0.0' => t('0.0'),
 851    );
 852  
 853    if (isset($default)) {
 854      $default = number_format($default, 1);
 855      $options['default'] = t('Default (@value)', array('@value' => $priorities[$default]));
 856    }
 857  
 858    // Add the rest of the options.
 859    $options += $priorities;
 860  
 861    if ($guides) {
 862      $options['1.0'] .= ' ' . t('(highest)');
 863      $options['0.5'] .= ' ' . t('(normal)');
 864      $options['0.0'] .= ' ' . t('(lowest)');
 865    }
 866  
 867    return $options;
 868  }
 869  
 870  /**
 871   * Get a list of priority options.
 872   *
 873   * @param $default
 874   *   Include a 'default' option.
 875   * @return
 876   *   An array of options.
 877   *
 878   * @see _xmlsitemap_translation_strings()
 879   */
 880  function xmlsitemap_get_status_options($default = NULL) {
 881    $options = array();
 882    $statuses = array(
 883      1 => t('Included'),
 884      0 => t('Excluded'),
 885    );
 886  
 887    if (isset($default)) {
 888      $default = $default ? 1 : 0;
 889      $options['default'] = t('Default (@value)', array('@value' => drupal_strtolower($statuses[$default])));
 890    }
 891  
 892    $options += $statuses;
 893  
 894    return $options;
 895  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7