Projet

Général

Profil

Paste
Télécharger (16,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / pathauto / pathauto.admin.inc @ 384fc62a

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
 */
16
function pathauto_patterns_form($form, $form_state) {
17
  // Call the hook on all modules - an array of 'settings' objects is returned
18
  $all_settings = module_invoke_all('pathauto', 'settings');
19
  foreach ($all_settings as $settings) {
20
    $module = $settings->module;
21
    $patterndescr = $settings->patterndescr;
22
    $patterndefault = $settings->patterndefault;
23
    $groupheader = $settings->groupheader;
24

    
25
    $form[$module] = array(
26
      '#type' => 'fieldset',
27
      '#title' => $groupheader,
28
      '#collapsible' => TRUE,
29
      '#collapsed' => FALSE,
30
    );
31

    
32
    // Prompt for the default pattern for this module
33
    $variable = 'pathauto_' . $module . '_pattern';
34
    $form[$module][$variable] = array(
35
      '#type' => 'textfield',
36
      '#title' => $patterndescr,
37
      '#default_value' => variable_get($variable, $patterndefault),
38
      '#size' => 65,
39
      '#maxlength' => 1280,
40
      '#element_validate' => array('token_element_validate'),
41
      '#after_build' => array('token_element_validate'),
42
      '#token_types' => array($settings->token_type),
43
      '#min_tokens' => 1,
44
      '#parents' => array($variable),
45
    );
46

    
47
    // If the module supports a set of specialized patterns, set
48
    // them up here
49
    if (isset($settings->patternitems)) {
50
      foreach ($settings->patternitems as $itemname => $itemlabel) {
51
        $variable = 'pathauto_' . $module . '_' . $itemname . '_pattern';
52
        $form[$module][$variable] = array(
53
          '#type' => 'textfield',
54
          '#title' => $itemlabel,
55
          '#default_value' => variable_get($variable, ''),
56
          '#size' => 65,
57
          '#maxlength' => 1280,
58
          '#element_validate' => array('token_element_validate'),
59
          '#after_build' => array('token_element_validate'),
60
          '#token_types' => array($settings->token_type),
61
          '#min_tokens' => 1,
62
          '#parents' => array($variable),
63
        );
64
      }
65
    }
66

    
67
    // Display the user documentation of placeholders supported by
68
    // this module, as a description on the last pattern
69
    $form[$module]['token_help'] = array(
70
      '#title' => t('Replacement patterns'),
71
      '#type' => 'fieldset',
72
      '#collapsible' => TRUE,
73
      '#collapsed' => TRUE,
74
    );
75
    $form[$module]['token_help']['help'] = array(
76
      '#theme' => 'token_tree',
77
      '#token_types' => array($settings->token_type),
78
    );
79
  }
80

    
81
  return system_settings_form($form);
82
}
83

    
84
/**
85
 * Form builder; Configure the Pathauto settings.
86
 *
87
 * @ingroup forms
88
 * @see system_settings_form()
89
 */
90
function pathauto_settings_form($form) {
91
  module_load_include('inc', 'pathauto');
92

    
93
  $form['pathauto_verbose'] = array(
94
    '#type' => 'checkbox',
95
    '#title' => t('Verbose'),
96
    '#default_value' => variable_get('pathauto_verbose', FALSE),
97
    '#description' => t('Display alias changes (except during bulk updates).'),
98
  );
99

    
100
  $form['pathauto_separator'] = array(
101
    '#type' => 'textfield',
102
    '#title' => t('Separator'),
103
    '#size' => 1,
104
    '#maxlength' => 1,
105
    '#default_value' => variable_get('pathauto_separator', '-'),
106
    '#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.'),
107
  );
108

    
109
  $form['pathauto_case'] = array(
110
    '#type' => 'radios',
111
    '#title' => t('Character case'),
112
    '#default_value' => variable_get('pathauto_case', PATHAUTO_CASE_LOWER),
113
    '#options' => array(
114
      PATHAUTO_CASE_LEAVE_ASIS => t('Leave case the same as source token values.'),
115
      PATHAUTO_CASE_LOWER => t('Change to lower case'),
116
    ),
117
  );
118

    
119
  $max_length = _pathauto_get_schema_alias_maxlength();
120

    
121
  $form['pathauto_max_length'] = array(
122
    '#type' => 'textfield',
123
    '#title' => t('Maximum alias length'),
124
    '#size' => 3,
125
    '#maxlength' => 3,
126
    '#default_value' => variable_get('pathauto_max_length', 100),
127
    '#min_value' => 1,
128
    '#max_value' => $max_length,
129
    '#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)),
130
    '#element_validate' => array('_pathauto_validate_numeric_element'),
131
  );
132
  $form['pathauto_max_component_length'] = array(
133
    '#type' => 'textfield',
134
    '#title' => t('Maximum component length'),
135
    '#size' => 3,
136
    '#maxlength' => 3,
137
    '#default_value' => variable_get('pathauto_max_component_length', 100),
138
    '#min_value' => 1,
139
    '#max_value' => $max_length,
140
    '#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)),
141
    '#element_validate' => array('_pathauto_validate_numeric_element'),
142
  );
143

    
144

    
145
  $description = t('What should Pathauto do when updating an existing content item which already has an alias?');
146
  if (module_exists('redirect')) {
147
    $description .= ' ' . t('The <a href="!url">Redirect module settings</a> affect whether a redirect is created when an alias is deleted.', array('!url' => url('admin/config/search/redirect')));
148
  }
149
  else {
150
    $description .= ' ' . t('Considering installing the <a href="!url">Redirect module</a> to get redirects when your aliases change.', array('!url' => 'http://drupal.org/project/redirect'));
151
  }
152
  $form['pathauto_update_action'] = array(
153
    '#type' => 'radios',
154
    '#title' => t('Update action'),
155
    '#default_value' => variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE),
156
    '#options' => array(
157
      PATHAUTO_UPDATE_ACTION_NO_NEW => t('Do nothing. Leave the old alias intact.'),
158
      PATHAUTO_UPDATE_ACTION_LEAVE => t('Create a new alias. Leave the existing alias functioning.'),
159
      PATHAUTO_UPDATE_ACTION_DELETE => t('Create a new alias. Delete the old alias.'),
160
    ),
161
    '#description' => $description,
162
  );
163

    
164
  $form['pathauto_transliterate'] = array(
165
    '#type' => 'checkbox',
166
    '#title' => t('Transliterate prior to creating alias'),
167
    '#default_value' => variable_get('pathauto_transliterate', FALSE) && module_exists('transliteration'),
168
    '#description' => t('When a pattern includes certain characters (such as those with accents) should Pathauto attempt to transliterate them into the ASCII-96 alphabet? Transliteration is handled by the Transliteration module.'),
169
    '#access' => module_exists('transliteration'),
170
  );
171

    
172
  $form['pathauto_reduce_ascii'] = array(
173
    '#type' => 'checkbox',
174
    '#title' => t('Reduce strings to letters and numbers'),
175
    '#default_value' => variable_get('pathauto_reduce_ascii', FALSE),
176
    '#description' => t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
177
  );
178

    
179
  $form['pathauto_ignore_words'] = array(
180
    '#type' => 'textarea',
181
    '#title' => t('Strings to Remove'),
182
    '#default_value' => variable_get('pathauto_ignore_words', PATHAUTO_IGNORE_WORDS),
183
    '#description' => t('Words to strip out of the URL alias, separated by commas. Do not use this to remove punctuation.'),
184
    '#wysiwyg' => FALSE,
185
  );
186

    
187
  $form['punctuation'] = array(
188
    '#type' => 'fieldset',
189
    '#title' => t('Punctuation'),
190
    '#collapsible' => TRUE,
191
    '#collapsed' => TRUE,
192
  );
193

    
194
  $punctuation = pathauto_punctuation_chars();
195
  foreach ($punctuation as $name => $details) {
196
    $details['default'] = PATHAUTO_PUNCTUATION_REMOVE;
197
    if ($details['value'] == variable_get('pathauto_separator', '-')) {
198
      $details['default'] = PATHAUTO_PUNCTUATION_REPLACE;
199
    }
200
    $form['punctuation']['pathauto_punctuation_' . $name] = array(
201
      '#type' => 'select',
202
      '#title' => $details['name'] . ' (<code>' . check_plain($details['value']) . '</code>)',
203
      '#default_value' => variable_get('pathauto_punctuation_' . $name, $details['default']),
204
      '#options' => array(
205
        PATHAUTO_PUNCTUATION_REMOVE => t('Remove'),
206
        PATHAUTO_PUNCTUATION_REPLACE => t('Replace by separator'),
207
        PATHAUTO_PUNCTUATION_DO_NOTHING => t('No action (do not replace)'),
208
      ),
209
    );
210
  }
211

    
212
  return system_settings_form($form);
213
}
214

    
215
/**
216
 * Validate a form element that should have an numeric value.
217
 */
218
function _pathauto_validate_numeric_element($element, &$form_state) {
219
  $value = $element['#value'];
220

    
221
  if (!is_numeric($value)) {
222
    form_error($element, t('The field %name is not a valid number.', array('%name' => $element['#title'])));
223
  }
224
  elseif (isset($element['#max_value']) && $value > $element['#max_value']) {
225
    form_error($element, t('The field %name cannot be greater than @max.', array('%name' => $element['#title'], '@max' => $element['#max_value'])));
226
  }
227
  elseif (isset($element['#min_value']) && $value < $element['#min_value']) {
228
    form_error($element, t('The field %name cannot be less than @min.', array('%name' => $element['#title'], '@min' => $element['#min_value'])));
229
  }
230
}
231

    
232
/**
233
 * Validate pathauto_settings_form form submissions.
234
 */
235
function pathauto_settings_form_validate($form, &$form_state) {
236
  module_load_include('inc', 'pathauto');
237

    
238
  // Perform a basic check for HTML characters in the strings to remove field.
239
  if (strip_tags($form_state['values']['pathauto_ignore_words']) != $form_state['values']['pathauto_ignore_words']) {
240
    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.'));
241
  }
242

    
243
  // Validate that the separator is not set to be removed per http://drupal.org/node/184119
244
  // This isn't really all that bad so warn, but still allow them to save the value.
245
  $separator = $form_state['values']['pathauto_separator'];
246
  $punctuation = pathauto_punctuation_chars();
247
  foreach ($punctuation as $name => $details) {
248
    if ($details['value'] == $separator) {
249
      $action = $form_state['values']['pathauto_punctuation_' . $name];
250
      if ($action == PATHAUTO_PUNCTUATION_REMOVE) {
251
        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 term:path token. You should probably set the action for @name to be "replace by separator".', array('@name' => $details['name'])), 'error');
252
      }
253
    }
254
  }
255
}
256

    
257
/**
258
 * Form contructor for path alias bulk update form.
259
 *
260
 * @see pathauto_bulk_update_form_submit()
261
 * @ingroup forms
262
 */
263
function pathauto_bulk_update_form() {
264
  $form['#update_callbacks'] = array();
265

    
266
  $form['update'] = array(
267
    '#type' => 'checkboxes',
268
    '#title' => t('Select the types of un-aliased paths for which to generate URL aliases'),
269
    '#options' => array(),
270
    '#default_value' => array(),
271
  );
272

    
273
  $pathauto_settings = module_invoke_all('pathauto', 'settings');
274
  foreach ($pathauto_settings as $settings) {
275
    if (!empty($settings->batch_update_callback)) {
276
      $form['#update_callbacks'][$settings->batch_update_callback] = $settings;
277
      $form['update']['#options'][$settings->batch_update_callback] = $settings->groupheader;
278
    }
279
  }
280

    
281
  $form['actions']['#type'] = 'actions';
282
  $form['actions']['submit'] = array(
283
    '#type' => 'submit',
284
    '#value' => t('Update'),
285
  );
286

    
287
  return $form;
288
}
289

    
290
/**
291
 * Form submit handler for path alias bulk update form.
292
 *
293
 * @see pathauto_batch_update_form()
294
 * @see pathauto_bulk_update_batch_finished()
295
 */
296
function pathauto_bulk_update_form_submit($form, &$form_state) {
297
  $batch = array(
298
    'title' => t('Bulk updating URL aliases'),
299
    'operations' => array(
300
      array('pathauto_bulk_update_batch_start', array()),
301
    ),
302
    'finished' => 'pathauto_bulk_update_batch_finished',
303
    'file' => drupal_get_path('module', 'pathauto') . '/pathauto.admin.inc',
304
  );
305

    
306
  foreach ($form_state['values']['update'] as $callback) {
307
    if (!empty($callback)) {
308
      $settings = $form['#update_callbacks'][$callback];
309
      if (!empty($settings->batch_file)) {
310
        $batch['operations'][] = array('pathauto_bulk_update_batch_process', array($callback, $settings));
311
      }
312
      else {
313
        $batch['operations'][] = array($callback, array());
314
      }
315
    }
316
  }
317

    
318
  batch_set($batch);
319
}
320

    
321
/**
322
 * Batch callback; count the current number of URL aliases for comparison later.
323
 */
324
function pathauto_bulk_update_batch_start(&$context) {
325
  $context['results']['count_before'] = db_select('url_alias')->countQuery()->execute()->fetchField();
326
}
327

    
328
/**
329
 * Common batch processing callback for all operations.
330
 *
331
 * Required to load our include the proper batch file.
332
 */
333
function pathauto_bulk_update_batch_process($callback, $settings, &$context) {
334
  if (!empty($settings->batch_file)) {
335
    require_once DRUPAL_ROOT . '/' . $settings->batch_file;
336
  }
337
  return $callback($context);
338
}
339

    
340
/**
341
 * Batch finished callback.
342
 */
343
function pathauto_bulk_update_batch_finished($success, $results, $operations) {
344
  if ($success) {
345
    // Count the current number of URL aliases after the batch is completed
346
    // and compare to the count before the batch started.
347
    $results['count_after'] = db_select('url_alias')->countQuery()->execute()->fetchField();
348
    $results['count_changed'] = max($results['count_after'] - $results['count_before'], 0);
349
    if ($results['count_changed']) {
350
      drupal_set_message(format_plural($results['count_changed'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
351
    }
352
    else {
353
      drupal_set_message(t('No new URL aliases to generate.'));
354
    }
355
  }
356
  else {
357
    $error_operation = reset($operations);
358
    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))));
359
  }
360
}
361

    
362
/**
363
 * Menu callback; select certain alias types to delete.
364
 */
365
function pathauto_admin_delete() {
366
  /* TODO:
367
   1) all - DONE
368
   2) all node aliases - DONE
369
   4) all user aliases - DONE
370
   5) all taxonomy aliases - DONE
371
   6) by node type
372
   7) by taxonomy vocabulary
373
   8) no longer existing aliases (see http://drupal.org/node/128366 )
374
   9) where src like 'pattern' - DON'T DO
375
   10) where dst like 'pattern' - DON'T DO
376
  */
377

    
378
  $form['delete'] = array(
379
    '#type' => 'fieldset',
380
    '#title' => t('Choose aliases to delete'),
381
    '#collapsible' => FALSE,
382
    '#collapsed' => FALSE,
383
  );
384

    
385
  // First we do the "all" case
386
  $total_count = db_query('SELECT count(1) FROM {url_alias}')->fetchField();
387
  $form['delete']['all_aliases'] = array(
388
    '#type' => 'checkbox',
389
    '#title' => t('All aliases'),
390
    '#default_value' => FALSE,
391
    '#description' => t('Delete all aliases. Number of aliases which will be deleted: %count.', array('%count' => $total_count)),
392
  );
393

    
394
  // Next, iterate over an array of objects/alias types which can be deleted and provide checkboxes
395
  $objects = module_invoke_all('path_alias_types');
396
  foreach ($objects as $internal_name => $label) {
397
    $count = db_query("SELECT count(1) FROM {url_alias} WHERE source LIKE :src", array(':src' => "$internal_name%"))->fetchField();
398
    $form['delete'][$internal_name] = array(
399
      '#type' => 'checkbox',
400
      '#title' => $label, // This label is sent through t() in the hard coded function where it is defined
401
      '#default_value' => FALSE,
402
      '#description' => t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', array('@label' => $label, '%count' => $count)),
403
    );
404
  }
405

    
406
  // Warn them and give a button that shows we mean business
407
  $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>');
408
  $form['buttons']['submit'] = array(
409
    '#type' => 'submit',
410
    '#value' => t('Delete aliases now!'),
411
  );
412

    
413
  return $form;
414
}
415

    
416
/**
417
 * Process pathauto_admin_delete form submissions.
418
 */
419
function pathauto_admin_delete_submit($form, &$form_state) {
420
  foreach ($form_state['values'] as $key => $value) {
421
    if ($value) {
422
      if ($key === 'all_aliases') {
423
        db_delete('url_alias')
424
          ->execute();
425
        drupal_set_message(t('All of your path aliases have been deleted.'));
426
      }
427
      $objects = module_invoke_all('path_alias_types');
428
      if (array_key_exists($key, $objects)) {
429
        db_delete('url_alias')
430
          ->condition('source', db_like($key) . '%', 'LIKE')
431
          ->execute();
432
        drupal_set_message(t('All of your %type path aliases have been deleted.', array('%type' => $objects[$key])));
433
      }
434
    }
435
  }
436
  $form_state['redirect'] = 'admin/config/search/path/delete_bulk';
437
}