[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Admin page callbacks for the Pathauto module.
   6   *
   7   * @ingroup pathauto
   8   */
   9  
  10  /**
  11   * Form builder; Configure the Pathauto system.
  12   *
  13   * @ingroup forms
  14   * @see system_settings_form()
  15   */
  16  function pathauto_admin_settings() {
  17    _pathauto_include();
  18  
  19    // Generate the form - settings applying to all patterns first
  20    $form['general'] = array(
  21      '#type' => 'fieldset',
  22      '#weight' => -20,
  23      '#title' => t('General settings'),
  24      '#collapsible' => TRUE,
  25      '#collapsed' => TRUE,
  26    );
  27  
  28    $form['general']['pathauto_verbose'] = array(
  29      '#type' => 'checkbox',
  30      '#title' => t('Verbose'),
  31      '#default_value' => variable_get('pathauto_verbose', FALSE),
  32      '#description' => t('Display alias changes (except during bulk updates).'),
  33    );
  34  
  35    $form['general']['pathauto_separator'] = array(
  36      '#type' => 'textfield',
  37      '#title' => t('Separator'),
  38      '#size' => 1,
  39      '#maxlength' => 1,
  40      '#default_value' => variable_get('pathauto_separator', '-'),
  41      '#description' => t('Character used to separate words in titles. This will replace any spaces and punctuation characters. Using a space or + character can cause unexpected results.'),
  42    );
  43  
  44    $form['general']['pathauto_case'] = array(
  45      '#type' => 'radios',
  46      '#title' => t('Character case'),
  47      '#default_value' => variable_get('pathauto_case', 1),
  48      '#options' => array(t('Leave case the same as source token values.'), t('Change to lower case')),
  49    );
  50  
  51    $max_length = _pathauto_get_schema_alias_maxlength();
  52  
  53    $form['general']['pathauto_max_length'] = array(
  54      '#type' => 'textfield',
  55      '#title' => t('Maximum alias length'),
  56      '#size' => 3,
  57      '#maxlength' => 3,
  58      '#default_value' => variable_get('pathauto_max_length', 100),
  59      '#min_value' => 1,
  60      '#max_value' => $max_length,
  61      '#description' => t('Maximum length of aliases to generate. 100 is recommended. See <a href="@pathauto-help">Pathauto help</a> for details.', array('@pathauto-help' => url('admin/help/pathauto'))),
  62      '#element_validate' => array('_pathauto_validate_numeric_element'),
  63    );
  64  
  65    $form['general']['pathauto_max_component_length'] = array(
  66      '#type' => 'textfield',
  67      '#title' => t('Maximum component length'),
  68      '#size' => 3,
  69      '#maxlength' => 3,
  70      '#default_value' => variable_get('pathauto_max_component_length', 100),
  71      '#min_value' => 1,
  72      '#max_value' => $max_length,
  73      '#description' => t('Maximum text length of any component in the alias (e.g., [title]). 100 is recommended. See <a href="@pathauto-help">Pathauto help</a> for details.', array('@pathauto-help' => url('admin/help/pathauto'))),
  74      '#element_validate' => array('_pathauto_validate_numeric_element'),
  75    );
  76  
  77    $form['general']['pathauto_max_bulk_update'] = array(
  78      '#type' => 'textfield',
  79      '#title' => t('Maximum number of objects to alias in a bulk update'),
  80      '#size' => 4,
  81      '#maxlength' => 4,
  82      '#default_value' => variable_get('pathauto_max_bulk_update', 50),
  83      '#min_value' => 0,
  84      '#description' => t('Maximum number of objects of a given type which should be aliased during a bulk update. The default is 50 and the recommended number depends on the speed of your server. If bulk updates "time out" or result in a "white screen" then reduce the number.'),
  85      '#element_validate' => array('_pathauto_validate_numeric_element'),
  86    );
  87  
  88    $actions = array(
  89      t('Do nothing. Leave the old alias intact.'),
  90      t('Create a new alias. Leave the existing alias functioning.'),
  91      t('Create a new alias. Delete the old alias.'));
  92  
  93    if (function_exists('path_redirect_save')) {
  94      $actions[] = t('Create a new alias. Redirect from old alias.');
  95    }
  96    elseif (variable_get('pathauto_update_action', 2) == 3) {
  97      // the redirect action is selected, but path_redirect is not enabled
  98      // let's set the variable back to the default
  99      variable_set('pathauto_update_action', 2);
 100    }
 101  
 102    $form['general']['pathauto_update_action'] = array(
 103      '#type' => 'radios',
 104      '#title' => t('Update action'),
 105      '#default_value' => variable_get('pathauto_update_action', 2),
 106      '#options' => $actions,
 107      '#description' => t('What should pathauto do when updating an existing content item which already has an alias?'),
 108    );
 109  
 110    $form['general']['pathauto_transliterate'] = array(
 111      '#type' => 'checkbox',
 112      '#title' => t('Transliterate prior to creating alias'),
 113      '#default_value' => variable_get('pathauto_transliterate', FALSE),
 114      '#description' => t('When a pattern includes certain characters (such as those with accents) should Pathauto attempt to transliterate them into the ASCII-96 alphabet?'),
 115      '#disabled' => !_pathauto_get_i18n_file(),
 116    );
 117    $i18n_locations = _pathauto_get_i18n_possible_files();
 118    foreach ($i18n_locations as $key => $file) {
 119      $i18n_locations[$key] = check_plain($file);
 120      if (file_exists($file)) {
 121        $i18n_locations[$key] .= ' - <span class="admin-enabled">' . t('File exists') . '</span>';
 122      }
 123      else {
 124        $i18n_locations[$key] .= ' - <span class="admin-disabled">'  . t('Does not exist') . '</span>';
 125      }
 126    }
 127    $form['general']['pathauto_transliterate']['#description'] .= ' ' . t('Transliteration is determined by the i18n-ascii.txt file in the following possible locations, in order of precedence: !locations', array('!locations' => theme('item_list', $i18n_locations)));
 128    if ($form['general']['pathauto_transliterate']['#disabled']) {
 129      // Perhaps they've removed the file, set the transliterate option to FALSE
 130      variable_set('pathauto_transliterate', FALSE);
 131    }
 132  
 133    $form['general']['pathauto_reduce_ascii'] = array(
 134      '#type' => 'checkbox',
 135      '#title' => t('Reduce strings to letters and numbers from ASCII-96'),
 136      '#default_value' => variable_get('pathauto_reduce_ascii', FALSE),
 137      '#description' => t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
 138    );
 139  
 140    // Default words to ignore
 141    $ignore_words = array(
 142      'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from', 'is', 'in',
 143      'into', 'like', 'of', 'off', 'on', 'onto', 'per', 'since', 'than', 'the',
 144      'this', 'that', 'to', 'up', 'via', 'with',
 145    );
 146    $form['general']['pathauto_ignore_words'] = array(
 147      '#type' => 'textarea',
 148      '#title' => t('Strings to Remove'),
 149      '#default_value' => variable_get('pathauto_ignore_words', implode(',', $ignore_words)),
 150      '#description' => t('Words to strip out of the URL alias, separated by commas. Do not place punctuation in here and do not use WYSIWYG editors on this field.'),
 151      '#wysiwyg' => FALSE,
 152    );
 153  
 154    $form['punctuation'] = array(
 155      '#type' => 'fieldset',
 156      '#weight' => -10,
 157      '#title' => t('Punctuation settings'), '#collapsible' => TRUE,
 158      '#collapsed' => TRUE,
 159    );
 160  
 161    $punctuation = pathauto_punctuation_chars();
 162    foreach ($punctuation as $name => $details) {
 163      $details['default'] = 0;
 164      if ($details['value'] == variable_get('pathauto_separator', '-')) {
 165        $details['default'] = 1;
 166      }
 167      $form['punctuation']['pathauto_punctuation_'. $name] = array(
 168        '#type' => 'select',
 169        '#title' => $details['name'],
 170        '#default_value' => variable_get('pathauto_punctuation_'. $name, $details['default']),
 171        '#options' => array(
 172          '0' => t('Remove'),
 173          '1' => t('Replace by separator'),
 174          '2' => t('No action (do not replace)'),
 175        ),
 176      );
 177    }
 178  
 179    // Call the hook on all modules - an array of 'settings' objects is returned
 180    $all_settings = module_invoke_all('pathauto', 'settings');
 181    foreach ($all_settings as $settings) {
 182      $module = $settings->module;
 183      $patterndescr = $settings->patterndescr;
 184      $patterndefault = $settings->patterndefault;
 185      $groupheader = $settings->groupheader;
 186      $supportsfeeds = isset($settings->supportsfeeds) ? $settings->supportsfeeds : NULL;
 187      variable_set('pathauto_' . $module . '_supportsfeeds', $supportsfeeds);
 188  
 189      $form[$module] = array(
 190        '#type' => 'fieldset',
 191        '#title' => $groupheader,
 192        '#collapsible' => TRUE,
 193        '#collapsed' => TRUE,
 194      );
 195  
 196      // Prompt for the default pattern for this module
 197      $variable = 'pathauto_' . $module . '_pattern';
 198      $form[$module][$variable] = array(
 199        '#type' => 'textfield',
 200        '#title' => $patterndescr,
 201        '#default_value' => variable_get($variable, $patterndefault),
 202        '#size' => 65,
 203        '#maxlength' => 1280,
 204        '#element_validate' => array('_pathauto_validate_pattern_element'),
 205        '#after_build' => array('_pathauto_validate_pattern_element'),
 206        '#token_types' => array($settings->token_type),
 207        '#parents' => array($variable),
 208      );
 209  
 210      // If the module supports a set of specialized patterns, set
 211      // them up here
 212      if (isset($settings->patternitems)) {
 213        foreach ($settings->patternitems as $itemname => $itemlabel) {
 214          $variable = 'pathauto_' . $module . '_' . $itemname . '_pattern';
 215          $form[$module][$variable] = array(
 216            '#type' => 'textfield',
 217            '#title' => $itemlabel,
 218            '#default_value' => variable_get($variable, ''),
 219            '#size' => 65,
 220            '#maxlength' => 1280,
 221            '#element_validate' => array('_pathauto_validate_pattern_element'),
 222            '#after_build' => array('_pathauto_validate_pattern_element'),
 223            '#token_types' => array($settings->token_type),
 224            '#parents' => array($variable),
 225          );
 226        }
 227      }
 228  
 229      // Display the user documentation of placeholders supported by
 230      // this module, as a description on the last pattern
 231      $form[$module]['token_help'] = array(
 232        '#title' => t('Replacement patterns'),
 233        '#type' => 'fieldset',
 234        '#collapsible' => TRUE,
 235        '#collapsed' => TRUE,
 236        '#description' => t('Use -raw replacements for text to avoid problems with HTML entities.'),
 237      );
 238  
 239      // Use the token tree if available.
 240      $doc = theme('token_tree', array($settings->token_type), FALSE);
 241      if (empty($doc)) {
 242        $doc = "<dl>\n";
 243        foreach ($settings->placeholders as $name => $description) {
 244          $doc .= '<dt>'. $name .'</dt>';
 245          $doc .= '<dd>'. $description .'</dd>';
 246        }
 247        $doc .= "</dl>\n";
 248      }
 249      $form[$module]['token_help']['help'] = array(
 250        '#type' => 'markup',
 251        '#value' => $doc,
 252      );
 253  
 254      // If the module supports bulk updates, offer the update action here
 255      if ($settings->bulkname) {
 256        $variable = 'pathauto_' . $module . '_bulkupdate';
 257        if (variable_get($variable, FALSE)) {
 258          variable_set($variable, FALSE);
 259          $function = $module . '_pathauto_bulkupdate';
 260          call_user_func($function);
 261        }
 262        $form[$module][$variable] = array(
 263          '#type' => 'checkbox',
 264          '#title' => $settings->bulkname,
 265          '#default_value' => FALSE,
 266          '#description' => $settings->bulkdescr,
 267        );
 268      }
 269  
 270      // If the module supports feeds, offer to generate aliases for them
 271      if ($supportsfeeds) {
 272        $variable = 'pathauto_' . $module . '_applytofeeds';
 273        $current = variable_get($variable, $supportsfeeds);
 274        // This checks for the old style from 2.0 and earlier. TODO: At some point we can drop that.
 275        if (is_numeric($current)) {
 276          $current = $supportsfeeds;
 277        }
 278  
 279        $form[$module][$variable] = array(
 280          '#type' => 'textfield',
 281          '#title' => t('Internal feed alias text (leave blank to disable)'),
 282          '#size' => 65,
 283          '#maxlength' => 1280,
 284          '#default_value' => $current,
 285          '#description' => t('The text to use for aliases for RSS feeds. Examples are "0/feed" (used throughout Drupal core) and "feed" (used by some contributed Drupal modules, like Views).'),
 286        );
 287      }
 288    }
 289  
 290    if (isset($do_index_bulkupdate) && $do_index_bulkupdate) {
 291      drupal_set_message(format_plural($indexcount,
 292        'Bulk generation of index aliases completed, one alias generated.',
 293        'Bulk generation of index aliases completed, @count aliases generated.'));
 294    }
 295  
 296    return system_settings_form($form);
 297  }
 298  
 299  /**
 300   * Element validation callback for URL alias patterns.
 301   *
 302   * This function performs the following validations:
 303   * - Checks if the pattern has at least one token.
 304   * - Checks if any tokens with raw companions are being used and recommends
 305   *   use of the raw tokens.
 306   */
 307  function _pathauto_validate_pattern_element(&$element, &$form_state) {
 308    // Get the current value of the element (since this can be used during both
 309    // form display and validation).
 310    $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
 311  
 312    // Empty patterns need no further validation.
 313    if (!drupal_strlen($value)) {
 314      return $element;
 315    }
 316  
 317    // Check to see if the required token functions are available.
 318    if (!function_exists('token_scan') || !function_exists('token_element_validate_token_context')) {
 319      drupal_set_message(t('Please make sure you are using the latest version of the Token module.'), 'warning', FALSE);
 320      return $element;
 321    }
 322  
 323    // Check for at least one token.
 324    $tokens = token_scan($value);
 325    if (empty($tokens)) {
 326      form_error($element, t('The %name should contain at least one token to ensure unique URL aliases are created.', array('%name' => $element['#title'])));
 327    }
 328    else {
 329      token_element_validate_token_context($element, $form_state);
 330    }
 331  
 332    // Find any non-raw tokens that do have a raw companion token and warn.
 333    module_load_include('inc', 'pathauto');
 334    $not_raw_tokens = array();
 335    $raw_tokens = _pathauto_get_raw_tokens();
 336    foreach ($tokens as $token) {
 337      if (substr($token, -4) === '-raw') {
 338        // Skip raw tokens.
 339        continue;
 340      }
 341      elseif (in_array($token . '-raw', $raw_tokens)) {
 342        drupal_set_message(t('You are using the token [%token] which has a raw companion token [%raw_token]. For Pathauto patterns you should use the -raw version of tokens unless you really know what you are doing. See the <a href="@pathauto-help">Pathauto help</a> for more details.', array('%token' => $token, '%raw_token' => $token . '-raw', '@pathauto-help' => url('admin/help/pathauto'))), 'error', FALSE);
 343      }
 344    }
 345  
 346    return $element;
 347  }
 348  
 349  /**
 350   * Validate a form element that should have an numeric value.
 351   */
 352  function _pathauto_validate_numeric_element($element, &$form_state) {
 353    $value = $element['#value'];
 354  
 355    if (!is_numeric($value)) {
 356      form_error($element, t('The field %name is not a valid number.', array('%name' => $element['#title'])));
 357    }
 358    elseif (isset($element['#max_value']) && $value > $element['#max_value']) {
 359      form_error($element, t('The field %name cannot be greater than @max.', array('%name' => $element['#title'], '@max' => $element['#max_value'])));
 360    }
 361    elseif (isset($element['#min_value']) && $value < $element['#min_value']) {
 362      form_error($element, t('The field %name cannot be less than @min.', array('%name' => $element['#title'], '@min' => $element['#min_value'])));
 363    }
 364  }
 365  
 366  /**
 367   * Validate pathauto_admin_settings form submissions.
 368   */
 369  function pathauto_admin_settings_validate($form, &$form_state) {
 370    module_load_include('inc', 'pathauto');
 371  
 372    // Perform a basic check for HTML characters in the strings to remove field.
 373    if (strip_tags($form_state['values']['pathauto_ignore_words']) != $form_state['values']['pathauto_ignore_words']) {
 374      form_set_error('pathauto_ignore_words', t('The <em>Strings to remove</em> field must not contain HTML. Make sure to disable any WYSIWYG editors for this field.'));
 375    }
 376  
 377    // Validate that the separator is not set to be removed per http://drupal.org/node/184119
 378    // This isn't really all that bad so warn, but still allow them to save the value.
 379    $separator = $form_state['values']['pathauto_separator'];
 380    $punctuation = pathauto_punctuation_chars();
 381    foreach ($punctuation as $name => $details) {
 382      if ($details['value'] == $separator) {
 383        $action = $form_state['values']['pathauto_punctuation_' . $name];
 384        if ($action == 0) {
 385          drupal_set_message(t('You have configured the @name to be the separator and to be removed when encountered in strings. This can cause problems with your patterns and especially with the catpath and termpath patterns. You should probably set the action for @name to be "replace by separator"', array('@name' => $details['name'])), 'error');
 386        }
 387      }
 388    }
 389  }
 390  
 391  /**
 392   * Menu callback; select certain alias types to delete.
 393   */
 394  function pathauto_admin_delete() {
 395    /* TODO:
 396     1) all - DONE
 397     2) all node aliases - DONE
 398     4) all user aliases - DONE
 399     5) all taxonomy aliases - DONE
 400     6) by node type
 401     7) by taxonomy vocabulary
 402     8) no longer existing aliases (see http://drupal.org/node/128366 )
 403     9) where src like 'pattern' - DON'T DO
 404     10) where dst like 'pattern' - DON'T DO
 405    */
 406  
 407    $form['delete'] = array(
 408      '#type' => 'fieldset',
 409      '#title' => t('Choose aliases to delete'),
 410      '#collapsible' => FALSE,
 411      '#collapsed' => FALSE,
 412    );
 413  
 414    // First we do the "all" case
 415    $total_count = db_result(db_query('SELECT count(1) FROM {url_alias}'));
 416    $form['delete']['all_aliases'] = array(
 417      '#type' => 'checkbox',
 418      '#title' => t('All aliases'),
 419      '#default_value' => FALSE,
 420      '#description' => t('Delete all aliases. Number of aliases which will be deleted: %count.', array('%count' => $total_count)),
 421    );
 422  
 423    // Next, iterate over an array of objects/alias types which can be deleted and provide checkboxes
 424    $objects = module_invoke_all('path_alias_types');
 425    foreach ($objects as $internal_name => $label) {
 426      $count = db_result(db_query("SELECT count(1) FROM {url_alias} WHERE src LIKE '%s%%'", $internal_name));
 427      $form['delete'][$internal_name] = array(
 428        '#type' => 'checkbox',
 429        '#title' => $label, // This label is sent through t() in the hard coded function where it is defined
 430        '#default_value' => FALSE,
 431        '#description' => t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', array('@label' => $label, '%count' => $count)),
 432      );
 433    }
 434  
 435    // Warn them and give a button that shows we mean business
 436    $form['warning'] = array('#value' => '<p>' . t('<strong>Note:</strong> there is no confirmation. Be sure of your action before clicking the "Delete aliases now!" button.<br />You may want to make a backup of the database and/or the url_alias table prior to using this feature.') . '</p>');
 437    $form['buttons']['submit'] = array(
 438      '#type' => 'submit',
 439      '#value' => t('Delete aliases now!'),
 440    );
 441  
 442    return $form;
 443  }
 444  
 445  /**
 446   * Process pathauto_admin_delete form submissions.
 447   */
 448  function pathauto_admin_delete_submit($form, &$form_state) {
 449    foreach ($form_state['values'] as $key => $value) {
 450      if ($value) {
 451        if ($key === 'all_aliases') {
 452          db_query('DELETE FROM {url_alias}');
 453          drupal_set_message(t('All of your path aliases have been deleted.'));
 454        }
 455        $objects = module_invoke_all('path_alias_types');
 456        if (array_key_exists($key, $objects)) {
 457          db_query("DELETE FROM {url_alias} WHERE src LIKE '%s%%'", $key);
 458          drupal_set_message(t('All of your %type path aliases have been deleted.', array('%type' => $objects[$key])));
 459        }
 460      }
 461    }
 462    $form_state['redirect'] = 'admin/build/path/delete_bulk';
 463  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7