source: sipes/modules_contrib/pathauto/pathauto.admin.inc @ 49072ea

stableversion-3.0
Last change on this file since 49072ea was 49072ea, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo el modulo

  • Propiedad mode establecida a 100755
File size: 18.8 KB
Línea 
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 URL alias patterns.
12 *
13 * @ingroup forms
14 * @see system_settings_form()
15 */
16function pathauto_patterns_form($form_state) {
17  module_load_include('inc', 'pathauto', 'pathauto.pathauto');
18
19  // Call the hook on all modules - an array of 'settings' objects is returned
20  $all_settings = module_invoke_all('pathauto', 'settings');
21  foreach ($all_settings as $settings) {
22    $module = $settings->module;
23    $patterndescr = $settings->patterndescr;
24    $patterndefault = $settings->patterndefault;
25    $groupheader = $settings->groupheader;
26
27    $form[$module] = array(
28      '#type' => 'fieldset',
29      '#title' => $groupheader,
30      '#collapsible' => TRUE,
31      '#collapsed' => FALSE,
32    );
33
34    // Prompt for the default pattern for this module
35    $variable = 'pathauto_' . $module . '_pattern';
36    $form[$module][$variable] = array(
37      '#type' => 'textfield',
38      '#title' => $patterndescr,
39      '#default_value' => variable_get($variable, $patterndefault),
40      '#size' => 65,
41      '#maxlength' => 1280,
42      '#element_validate' => array('_pathauto_validate_pattern_element'),
43      '#after_build' => array('_pathauto_validate_pattern_element'),
44      '#token_types' => array($settings->token_type),
45      '#min_tokens' => 1,
46      '#parents' => array($variable),
47    );
48
49    // If the module supports a set of specialized patterns, set
50    // them up here
51    if (isset($settings->patternitems)) {
52      foreach ($settings->patternitems as $itemname => $itemlabel) {
53        $variable = 'pathauto_' . $module . '_' . $itemname . '_pattern';
54        $form[$module][$variable] = array(
55          '#type' => 'textfield',
56          '#title' => $itemlabel,
57          '#default_value' => variable_get($variable, ''),
58          '#size' => 65,
59          '#maxlength' => 1280,
60          '#element_validate' => array('_pathauto_validate_pattern_element'),
61          '#after_build' => array('_pathauto_validate_pattern_element'),
62          '#token_types' => array($settings->token_type),
63          '#min_tokens' => 1,
64          '#parents' => array($variable),
65        );
66      }
67    }
68
69    // Display the user documentation of placeholders supported by
70    // this module, as a description on the last pattern
71    $form[$module]['token_help'] = array(
72      '#title' => t('Replacement patterns'),
73      '#type' => 'fieldset',
74      '#collapsible' => TRUE,
75      '#collapsed' => TRUE,
76      '#description' => t('Use -raw replacements for text to avoid problems with HTML entities.'),
77    );
78    $form[$module]['token_help']['help'] = array(
79      '#type' => 'markup',
80      '#value' => theme('token_tree', array($settings->token_type)),
81    );
82  }
83
84  return system_settings_form($form);
85}
86
87/**
88 * Form builder; Configure the Pathauto settings.
89 *
90 * @ingroup forms
91 * @see system_settings_form()
92 */
93function pathauto_settings_form() {
94  module_load_include('inc', 'pathauto');
95
96  $form['pathauto_verbose'] = array(
97    '#type' => 'checkbox',
98    '#title' => t('Verbose'),
99    '#default_value' => variable_get('pathauto_verbose', FALSE),
100    '#description' => t('Display alias changes (except during bulk updates).'),
101  );
102
103  $form['pathauto_separator'] = array(
104    '#type' => 'textfield',
105    '#title' => t('Separator'),
106    '#size' => 1,
107    '#maxlength' => 1,
108    '#default_value' => variable_get('pathauto_separator', '-'),
109    '#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.'),
110  );
111
112  $form['pathauto_case'] = array(
113    '#type' => 'radios',
114    '#title' => t('Character case'),
115    '#default_value' => variable_get('pathauto_case', PATHAUTO_CASE_LOWER),
116    '#options' => array(
117      PATHAUTO_CASE_LEAVE_ASIS => t('Leave case the same as source token values.'),
118      PATHAUTO_CASE_LOWER => t('Change to lower case'),
119    ),
120  );
121
122  $max_length = _pathauto_get_schema_alias_maxlength();
123
124  $form['pathauto_max_length'] = array(
125    '#type' => 'textfield',
126    '#title' => t('Maximum alias length'),
127    '#size' => 3,
128    '#maxlength' => 3,
129    '#default_value' => variable_get('pathauto_max_length', 100),
130    '#min_value' => 1,
131    '#max_value' => $max_length,
132    '#description' => t('Maximum length of aliases to generate. 100 is the recommended length. @max is the maximum possible length. See <a href="@pathauto-help">Pathauto help</a> for details.', array('@pathauto-help' => url('admin/help/pathauto'), '@max' => $max_length)),
133    '#element_validate' => array('_pathauto_validate_numeric_element'),
134  );
135  $form['pathauto_max_component_length'] = array(
136    '#type' => 'textfield',
137    '#title' => t('Maximum component length'),
138    '#size' => 3,
139    '#maxlength' => 3,
140    '#default_value' => variable_get('pathauto_max_component_length', 100),
141    '#min_value' => 1,
142    '#max_value' => $max_length,
143    '#description' => t('Maximum text length of any component in the alias (e.g., [title]). 100 is the recommended length. @max is the maximum possible length. See <a href="@pathauto-help">Pathauto help</a> for details.', array('@pathauto-help' => url('admin/help/pathauto'), '@max' => $max_length)),
144    '#element_validate' => array('_pathauto_validate_numeric_element'),
145  );
146
147  $form['pathauto_update_action'] = array(
148    '#type' => 'radios',
149    '#title' => t('Update action'),
150    '#default_value' => variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE),
151    '#options' => array(
152      PATHAUTO_UPDATE_ACTION_NO_NEW => t('Do nothing. Leave the old alias intact.'),
153      PATHAUTO_UPDATE_ACTION_LEAVE => t('Create a new alias. Leave the existing alias functioning.'),
154      PATHAUTO_UPDATE_ACTION_DELETE => t('Create a new alias. Delete the old alias.'),
155      PATHAUTO_UPDATE_ACTION_REDIRECT => t('Create a new alias. Redirect from old alias.'),
156    ),
157    '#description' => t('What should Pathauto do when updating an existing content item which already has an alias?'),
158  );
159  if (!module_exists('path_redirect')) {
160    // Remove the redirection option if Path redirect is not enabled.
161    unset($form['pathauto_update_action']['#options'][PATHAUTO_UPDATE_ACTION_REDIRECT]);
162    if (variable_get('pathauto_update_action', NULL) == PATHAUTO_UPDATE_ACTION_REDIRECT) {
163      // Fix the current update action variable as well.
164      variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
165    }
166  }
167
168  $form['pathauto_transliterate'] = array(
169    '#type' => 'checkbox',
170    '#title' => t('Transliterate prior to creating alias'),
171    '#default_value' => variable_get('pathauto_transliterate', FALSE),
172    '#description' => t('When a pattern includes certain characters (such as those with accents) should Pathauto attempt to transliterate them into the US-ASCII alphabet? Transliteration is handled by the Transliteration module.'),
173  );
174  if (!module_exists('transliteration')) {
175    // Remove the redirection option if Transliteration is not enabled.
176    $form['pathauto_transliterate']['#access'] = FALSE;
177    variable_set('pathauto_transliterate', FALSE);
178  }
179
180  $form['pathauto_reduce_ascii'] = array(
181    '#type' => 'checkbox',
182    '#title' => t('Reduce strings to letters and numbers'),
183    '#default_value' => variable_get('pathauto_reduce_ascii', FALSE),
184    '#description' => t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
185  );
186
187  $form['pathauto_ignore_words'] = array(
188    '#type' => 'textarea',
189    '#title' => t('Strings to Remove'),
190    '#default_value' => variable_get('pathauto_ignore_words', PATHAUTO_IGNORE_WORDS),
191    '#description' => t('Words to strip out of the URL alias, separated by commas. Do not use this to remove punctuation.'),
192    '#wysiwyg' => FALSE,
193  );
194
195  $form['punctuation'] = array(
196    '#type' => 'fieldset',
197    '#title' => t('Punctuation'),
198    '#collapsible' => TRUE,
199    '#collapsed' => TRUE,
200  );
201
202  $punctuation = pathauto_punctuation_chars();
203  foreach ($punctuation as $name => $details) {
204    $details['default'] = PATHAUTO_PUNCTUATION_REMOVE;
205    if ($details['value'] == variable_get('pathauto_separator', '-')) {
206      $details['default'] = PATHAUTO_PUNCTUATION_REPLACE;
207    }
208    $form['punctuation']['pathauto_punctuation_' . $name] = array(
209      '#type' => 'select',
210      '#title' => $details['name'] . ' (<code>' . check_plain($details['value']) . '</code>)',
211      '#default_value' => variable_get('pathauto_punctuation_' . $name, $details['default']),
212      '#options' => array(
213        PATHAUTO_PUNCTUATION_REMOVE => t('Remove'),
214        PATHAUTO_PUNCTUATION_REPLACE => t('Replace by separator'),
215        PATHAUTO_PUNCTUATION_DO_NOTHING => t('No action (do not replace)'),
216      ),
217    );
218  }
219
220  return system_settings_form($form);
221}
222
223/**
224 * Element validation callback for URL alias patterns.
225 *
226 * This function performs the following validations:
227 * - Checks if the pattern has at least one token.
228 * - Checks if any tokens with raw companions are being used and recommends
229 *   use of the raw tokens.
230 */
231function _pathauto_validate_pattern_element(&$element, &$form_state) {
232  // Get the current value of the element (since this can be used during both
233  // form display and validation).
234  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
235
236  // Empty patterns need no further validation.
237  if (!drupal_strlen($value)) {
238    return $element;
239  }
240
241  // Check to see if the required token functions are available.
242  if (!function_exists('token_scan') || !function_exists('token_element_validate')) {
243    drupal_set_message(t('Please make sure you are using the latest version of the Token module.'), 'error', FALSE);
244    return $element;
245  }
246
247  // Run token validation.
248  $element = token_element_validate($element, $form_state);
249
250  // Find any non-raw tokens that do have a raw companion token and warn.
251  if (!form_get_errors($element)) {
252    // Skip if there are already errors on this field.
253    module_load_include('inc', 'pathauto');
254    $not_raw_tokens = array();
255    $raw_tokens = _pathauto_get_raw_tokens();
256    foreach (token_scan($value) as $token) {
257      if (substr($token, -4) === '-raw') {
258        // Skip raw tokens.
259        continue;
260      }
261      elseif (in_array($token . '-raw', $raw_tokens)) {
262        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);
263      }
264    }
265  }
266
267  return $element;
268}
269
270/**
271 * Validate a form element that should have an numeric value.
272 */
273function _pathauto_validate_numeric_element($element, &$form_state) {
274  $value = $element['#value'];
275
276  if (!is_numeric($value)) {
277    form_error($element, t('The field %name is not a valid number.', array('%name' => $element['#title'])));
278  }
279  elseif (isset($element['#max_value']) && $value > $element['#max_value']) {
280    form_error($element, t('The field %name cannot be greater than @max.', array('%name' => $element['#title'], '@max' => $element['#max_value'])));
281  }
282  elseif (isset($element['#min_value']) && $value < $element['#min_value']) {
283    form_error($element, t('The field %name cannot be less than @min.', array('%name' => $element['#title'], '@min' => $element['#min_value'])));
284  }
285}
286
287/**
288 * Validate pathauto_settings_form form submissions.
289 */
290function pathauto_settings_form_validate($form, &$form_state) {
291  module_load_include('inc', 'pathauto');
292
293  // Perform a basic check for HTML characters in the strings to remove field.
294  if (strip_tags($form_state['values']['pathauto_ignore_words']) != $form_state['values']['pathauto_ignore_words']) {
295    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.'));
296  }
297
298  // Validate that the separator is not set to be removed per http://drupal.org/node/184119
299  // This isn't really all that bad so warn, but still allow them to save the value.
300  $separator = $form_state['values']['pathauto_separator'];
301  $punctuation = pathauto_punctuation_chars();
302  foreach ($punctuation as $name => $details) {
303    if ($details['value'] == $separator) {
304      $action = $form_state['values']['pathauto_punctuation_' . $name];
305      if ($action == PATHAUTO_PUNCTUATION_REMOVE) {
306        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');
307      }
308    }
309  }
310}
311
312/**
313 * Form contructor for path alias bulk update form.
314 *
315 * @see pathauto_bulk_update_form_submit()
316 * @ingroup forms
317 */
318function pathauto_bulk_update_form() {
319  $form['#update_callbacks'] = array();
320
321  $form['update'] = array(
322    '#type' => 'checkboxes',
323    '#title' => t('Select the types of un-aliased paths for which to generate URL aliases'),
324    '#options' => array(),
325    '#default_value' => array(),
326  );
327
328  $pathauto_settings = module_invoke_all('pathauto', 'settings');
329  foreach ($pathauto_settings as $settings) {
330    if (!empty($settings->batch_update_callback)) {
331      $form['#update_callbacks'][$settings->batch_update_callback] = $settings;
332      $form['update']['#options'][$settings->batch_update_callback] = $settings->groupheader;
333    }
334  }
335
336  $form['actions']['#weight'] = 100;
337  $form['actions']['submit'] = array(
338    '#type' => 'submit',
339    '#value' => t('Update'),
340  );
341
342  return $form;
343}
344
345/**
346 * Form submit handler for path alias bulk update form.
347 *
348 * @see pathauto_batch_update_form()
349 * @see pathauto_bulk_update_batch_finished()
350 */
351function pathauto_bulk_update_form_submit($form, &$form_state) {
352  $batch = array(
353    'title' => t('Bulk updating URL aliases'),
354    'operations' => array(
355      array('pathauto_bulk_update_batch_start', array()),
356    ),
357    'finished' => 'pathauto_bulk_update_batch_finished',
358    'file' => drupal_get_path('module', 'pathauto') . '/pathauto.admin.inc',
359  );
360
361  foreach ($form_state['values']['update'] as $callback) {
362    if (!empty($callback)) {
363      $settings = $form['#update_callbacks'][$callback];
364      if (!empty($settings->batch_file)) {
365        $batch['operations'][] = array('pathauto_bulk_update_batch_process', array($callback, $settings));
366      }
367      else {
368        $batch['operations'][] = array($callback, array());
369      }
370    }
371  }
372
373  batch_set($batch);
374}
375
376/**
377 * Batch callback; count the current number of URL aliases for comparison later.
378 */
379function pathauto_bulk_update_batch_start(&$context) {
380  $context['results']['count_before'] = db_result(db_query("SELECT COUNT(*) FROM {url_alias}"));
381}
382
383/**
384 * Common batch processing callback for all operations.
385 *
386 * Required to load our include the proper batch file.
387 */
388function pathauto_bulk_update_batch_process($callback, $settings, &$context) {
389  if (!empty($settings->batch_file)) {
390    require_once './' . $settings->batch_file;
391  }
392  return $callback($context);
393}
394
395/**
396 * Batch finished callback.
397 */
398function pathauto_bulk_update_batch_finished($success, $results, $operations) {
399  if ($success) {
400    // Count the current number of URL aliases after the batch is completed
401    // and compare to the count before the batch started.
402    $results['count_after'] = db_result(db_query("SELECT COUNT(*) FROM {url_alias}"));
403    $results['count_changed'] = max($results['count_after'] - $results['count_before'], 0);
404    if ($results['count_changed']) {
405      drupal_set_message(format_plural($results['count_changed'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
406    }
407    else {
408      drupal_set_message(t('No new URL aliases to generate.'));
409    }
410  }
411  else {
412    $error_operation = reset($operations);
413    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
414  }
415}
416
417/**
418 * Menu callback; select certain alias types to delete.
419 */
420function pathauto_admin_delete() {
421  /* TODO:
422   1) all - DONE
423   2) all node aliases - DONE
424   4) all user aliases - DONE
425   5) all taxonomy aliases - DONE
426   6) by node type
427   7) by taxonomy vocabulary
428   8) no longer existing aliases (see http://drupal.org/node/128366 )
429   9) where src like 'pattern' - DON'T DO
430   10) where dst like 'pattern' - DON'T DO
431  */
432
433  $form['delete'] = array(
434    '#type' => 'fieldset',
435    '#title' => t('Choose aliases to delete'),
436    '#collapsible' => FALSE,
437    '#collapsed' => FALSE,
438  );
439
440  // First we do the "all" case
441  $total_count = db_result(db_query('SELECT count(1) FROM {url_alias}'));
442  $form['delete']['all_aliases'] = array(
443    '#type' => 'checkbox',
444    '#title' => t('All aliases'),
445    '#default_value' => FALSE,
446    '#description' => t('Delete all aliases. Number of aliases which will be deleted: %count.', array('%count' => $total_count)),
447  );
448
449  // Next, iterate over an array of objects/alias types which can be deleted and provide checkboxes
450  $objects = module_invoke_all('path_alias_types');
451  foreach ($objects as $internal_name => $label) {
452    $count = db_result(db_query("SELECT count(1) FROM {url_alias} WHERE src LIKE '%s%%'", $internal_name));
453    $form['delete'][$internal_name] = array(
454      '#type' => 'checkbox',
455      '#title' => $label, // This label is sent through t() in the hard coded function where it is defined
456      '#default_value' => FALSE,
457      '#description' => t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', array('@label' => $label, '%count' => $count)),
458    );
459  }
460
461  // Warn them and give a button that shows we mean business
462  $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>');
463  $form['buttons']['submit'] = array(
464    '#type' => 'submit',
465    '#value' => t('Delete aliases now!'),
466  );
467
468  return $form;
469}
470
471/**
472 * Process pathauto_admin_delete form submissions.
473 */
474function pathauto_admin_delete_submit($form, &$form_state) {
475  foreach ($form_state['values'] as $key => $value) {
476    if ($value) {
477      if ($key === 'all_aliases') {
478        db_query('DELETE FROM {url_alias}');
479        drupal_set_message(t('All of your path aliases have been deleted.'));
480      }
481      $objects = module_invoke_all('path_alias_types');
482      if (array_key_exists($key, $objects)) {
483        db_query("DELETE FROM {url_alias} WHERE src LIKE '%s%%'", $key);
484        drupal_set_message(t('All of your %type path aliases have been deleted.', array('%type' => $objects[$key])));
485      }
486    }
487  }
488  $form_state['redirect'] = 'admin/build/path/delete_bulk';
489}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.