Projet

Général

Profil

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

root / drupal7 / sites / all / modules / pathauto / pathauto.admin.inc @ ba09eb79

1 85ad3d82 Assos Assos
<?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 790c5a62 Assos Assos
    // Show the token help relevant to this pattern type.
68 85ad3d82 Assos Assos
    $form[$module]['token_help'] = array(
69
      '#theme' => 'token_tree',
70
      '#token_types' => array($settings->token_type),
71 790c5a62 Assos Assos
      '#dialog' => TRUE,
72 85ad3d82 Assos Assos
    );
73
  }
74
75
  return system_settings_form($form);
76
}
77
78
/**
79
 * Form builder; Configure the Pathauto settings.
80
 *
81
 * @ingroup forms
82
 * @see system_settings_form()
83
 */
84
function pathauto_settings_form($form) {
85
  module_load_include('inc', 'pathauto');
86
87
  $form['pathauto_verbose'] = array(
88
    '#type' => 'checkbox',
89
    '#title' => t('Verbose'),
90
    '#default_value' => variable_get('pathauto_verbose', FALSE),
91
    '#description' => t('Display alias changes (except during bulk updates).'),
92
  );
93
94
  $form['pathauto_separator'] = array(
95
    '#type' => 'textfield',
96
    '#title' => t('Separator'),
97
    '#size' => 1,
98
    '#maxlength' => 1,
99
    '#default_value' => variable_get('pathauto_separator', '-'),
100
    '#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.'),
101
  );
102
103
  $form['pathauto_case'] = array(
104
    '#type' => 'radios',
105
    '#title' => t('Character case'),
106
    '#default_value' => variable_get('pathauto_case', PATHAUTO_CASE_LOWER),
107
    '#options' => array(
108
      PATHAUTO_CASE_LEAVE_ASIS => t('Leave case the same as source token values.'),
109
      PATHAUTO_CASE_LOWER => t('Change to lower case'),
110
    ),
111
  );
112
113
  $max_length = _pathauto_get_schema_alias_maxlength();
114
115
  $form['pathauto_max_length'] = array(
116
    '#type' => 'textfield',
117
    '#title' => t('Maximum alias length'),
118
    '#size' => 3,
119
    '#maxlength' => 3,
120
    '#default_value' => variable_get('pathauto_max_length', 100),
121
    '#min_value' => 1,
122
    '#max_value' => $max_length,
123
    '#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)),
124
    '#element_validate' => array('_pathauto_validate_numeric_element'),
125
  );
126
  $form['pathauto_max_component_length'] = array(
127
    '#type' => 'textfield',
128
    '#title' => t('Maximum component length'),
129
    '#size' => 3,
130
    '#maxlength' => 3,
131
    '#default_value' => variable_get('pathauto_max_component_length', 100),
132
    '#min_value' => 1,
133
    '#max_value' => $max_length,
134
    '#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)),
135
    '#element_validate' => array('_pathauto_validate_numeric_element'),
136
  );
137
138
139
  $description = t('What should Pathauto do when updating an existing content item which already has an alias?');
140
  if (module_exists('redirect')) {
141 790c5a62 Assos Assos
    $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/settings')));
142 85ad3d82 Assos Assos
  }
143
  else {
144
    $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'));
145
  }
146
  $form['pathauto_update_action'] = array(
147
    '#type' => 'radios',
148
    '#title' => t('Update action'),
149
    '#default_value' => variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE),
150
    '#options' => array(
151
      PATHAUTO_UPDATE_ACTION_NO_NEW => t('Do nothing. Leave the old alias intact.'),
152
      PATHAUTO_UPDATE_ACTION_LEAVE => t('Create a new alias. Leave the existing alias functioning.'),
153
      PATHAUTO_UPDATE_ACTION_DELETE => t('Create a new alias. Delete the old alias.'),
154
    ),
155
    '#description' => $description,
156
  );
157
158
  $form['pathauto_transliterate'] = array(
159
    '#type' => 'checkbox',
160
    '#title' => t('Transliterate prior to creating alias'),
161
    '#default_value' => variable_get('pathauto_transliterate', FALSE) && module_exists('transliteration'),
162 790c5a62 Assos Assos
    '#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.'),
163 85ad3d82 Assos Assos
    '#access' => module_exists('transliteration'),
164
  );
165
166
  $form['pathauto_reduce_ascii'] = array(
167
    '#type' => 'checkbox',
168
    '#title' => t('Reduce strings to letters and numbers'),
169
    '#default_value' => variable_get('pathauto_reduce_ascii', FALSE),
170
    '#description' => t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
171
  );
172
173
  $form['pathauto_ignore_words'] = array(
174
    '#type' => 'textarea',
175
    '#title' => t('Strings to Remove'),
176
    '#default_value' => variable_get('pathauto_ignore_words', PATHAUTO_IGNORE_WORDS),
177
    '#description' => t('Words to strip out of the URL alias, separated by commas. Do not use this to remove punctuation.'),
178
    '#wysiwyg' => FALSE,
179
  );
180
181
  $form['punctuation'] = array(
182
    '#type' => 'fieldset',
183
    '#title' => t('Punctuation'),
184
    '#collapsible' => TRUE,
185
    '#collapsed' => TRUE,
186
  );
187
188
  $punctuation = pathauto_punctuation_chars();
189
  foreach ($punctuation as $name => $details) {
190
    $details['default'] = PATHAUTO_PUNCTUATION_REMOVE;
191
    if ($details['value'] == variable_get('pathauto_separator', '-')) {
192
      $details['default'] = PATHAUTO_PUNCTUATION_REPLACE;
193
    }
194
    $form['punctuation']['pathauto_punctuation_' . $name] = array(
195
      '#type' => 'select',
196
      '#title' => $details['name'] . ' (<code>' . check_plain($details['value']) . '</code>)',
197
      '#default_value' => variable_get('pathauto_punctuation_' . $name, $details['default']),
198
      '#options' => array(
199
        PATHAUTO_PUNCTUATION_REMOVE => t('Remove'),
200
        PATHAUTO_PUNCTUATION_REPLACE => t('Replace by separator'),
201
        PATHAUTO_PUNCTUATION_DO_NOTHING => t('No action (do not replace)'),
202
      ),
203
    );
204
  }
205
206
  return system_settings_form($form);
207
}
208
209
/**
210
 * Validate a form element that should have an numeric value.
211
 */
212
function _pathauto_validate_numeric_element($element, &$form_state) {
213
  $value = $element['#value'];
214
215
  if (!is_numeric($value)) {
216
    form_error($element, t('The field %name is not a valid number.', array('%name' => $element['#title'])));
217
  }
218
  elseif (isset($element['#max_value']) && $value > $element['#max_value']) {
219
    form_error($element, t('The field %name cannot be greater than @max.', array('%name' => $element['#title'], '@max' => $element['#max_value'])));
220
  }
221
  elseif (isset($element['#min_value']) && $value < $element['#min_value']) {
222
    form_error($element, t('The field %name cannot be less than @min.', array('%name' => $element['#title'], '@min' => $element['#min_value'])));
223
  }
224
}
225
226
/**
227
 * Validate pathauto_settings_form form submissions.
228
 */
229
function pathauto_settings_form_validate($form, &$form_state) {
230
  module_load_include('inc', 'pathauto');
231
232
  // Perform a basic check for HTML characters in the strings to remove field.
233
  if (strip_tags($form_state['values']['pathauto_ignore_words']) != $form_state['values']['pathauto_ignore_words']) {
234
    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.'));
235
  }
236
237
  // Validate that the separator is not set to be removed per http://drupal.org/node/184119
238
  // This isn't really all that bad so warn, but still allow them to save the value.
239
  $separator = $form_state['values']['pathauto_separator'];
240
  $punctuation = pathauto_punctuation_chars();
241
  foreach ($punctuation as $name => $details) {
242
    if ($details['value'] == $separator) {
243
      $action = $form_state['values']['pathauto_punctuation_' . $name];
244
      if ($action == PATHAUTO_PUNCTUATION_REMOVE) {
245
        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');
246
      }
247
    }
248
  }
249
}
250
251
/**
252
 * Form contructor for path alias bulk update form.
253
 *
254
 * @see pathauto_bulk_update_form_submit()
255
 * @ingroup forms
256
 */
257
function pathauto_bulk_update_form() {
258
  $form['#update_callbacks'] = array();
259
260
  $form['update'] = array(
261
    '#type' => 'checkboxes',
262
    '#title' => t('Select the types of un-aliased paths for which to generate URL aliases'),
263
    '#options' => array(),
264
    '#default_value' => array(),
265
  );
266
267
  $pathauto_settings = module_invoke_all('pathauto', 'settings');
268
  foreach ($pathauto_settings as $settings) {
269
    if (!empty($settings->batch_update_callback)) {
270
      $form['#update_callbacks'][$settings->batch_update_callback] = $settings;
271
      $form['update']['#options'][$settings->batch_update_callback] = $settings->groupheader;
272
    }
273
  }
274
275
  $form['actions']['#type'] = 'actions';
276
  $form['actions']['submit'] = array(
277
    '#type' => 'submit',
278
    '#value' => t('Update'),
279
  );
280
281
  return $form;
282
}
283
284
/**
285
 * Form submit handler for path alias bulk update form.
286
 *
287
 * @see pathauto_batch_update_form()
288
 * @see pathauto_bulk_update_batch_finished()
289
 */
290
function pathauto_bulk_update_form_submit($form, &$form_state) {
291
  $batch = array(
292
    'title' => t('Bulk updating URL aliases'),
293
    'operations' => array(
294
      array('pathauto_bulk_update_batch_start', array()),
295
    ),
296
    'finished' => 'pathauto_bulk_update_batch_finished',
297
    'file' => drupal_get_path('module', 'pathauto') . '/pathauto.admin.inc',
298
  );
299
300
  foreach ($form_state['values']['update'] as $callback) {
301
    if (!empty($callback)) {
302
      $settings = $form['#update_callbacks'][$callback];
303
      if (!empty($settings->batch_file)) {
304
        $batch['operations'][] = array('pathauto_bulk_update_batch_process', array($callback, $settings));
305
      }
306
      else {
307
        $batch['operations'][] = array($callback, array());
308
      }
309
    }
310
  }
311
312
  batch_set($batch);
313
}
314
315
/**
316
 * Batch callback; count the current number of URL aliases for comparison later.
317
 */
318
function pathauto_bulk_update_batch_start(&$context) {
319
  $context['results']['count_before'] = db_select('url_alias')->countQuery()->execute()->fetchField();
320
}
321
322
/**
323
 * Common batch processing callback for all operations.
324
 *
325
 * Required to load our include the proper batch file.
326
 */
327
function pathauto_bulk_update_batch_process($callback, $settings, &$context) {
328
  if (!empty($settings->batch_file)) {
329
    require_once DRUPAL_ROOT . '/' . $settings->batch_file;
330
  }
331
  return $callback($context);
332
}
333
334
/**
335
 * Batch finished callback.
336
 */
337
function pathauto_bulk_update_batch_finished($success, $results, $operations) {
338
  if ($success) {
339
    // Count the current number of URL aliases after the batch is completed
340
    // and compare to the count before the batch started.
341
    $results['count_after'] = db_select('url_alias')->countQuery()->execute()->fetchField();
342
    $results['count_changed'] = max($results['count_after'] - $results['count_before'], 0);
343
    if ($results['count_changed']) {
344
      drupal_set_message(format_plural($results['count_changed'], 'Generated 1 URL alias.', 'Generated @count URL aliases.'));
345
    }
346
    else {
347
      drupal_set_message(t('No new URL aliases to generate.'));
348
    }
349
  }
350
  else {
351
    $error_operation = reset($operations);
352
    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))));
353
  }
354
}
355
356
/**
357
 * Menu callback; select certain alias types to delete.
358
 */
359
function pathauto_admin_delete() {
360
  /* TODO:
361
   1) all - DONE
362
   2) all node aliases - DONE
363
   4) all user aliases - DONE
364
   5) all taxonomy aliases - DONE
365
   6) by node type
366
   7) by taxonomy vocabulary
367
   8) no longer existing aliases (see http://drupal.org/node/128366 )
368
   9) where src like 'pattern' - DON'T DO
369
   10) where dst like 'pattern' - DON'T DO
370
  */
371
372
  $form['delete'] = array(
373
    '#type' => 'fieldset',
374
    '#title' => t('Choose aliases to delete'),
375
    '#collapsible' => FALSE,
376
    '#collapsed' => FALSE,
377
  );
378
379
  // First we do the "all" case
380
  $total_count = db_query('SELECT count(1) FROM {url_alias}')->fetchField();
381
  $form['delete']['all_aliases'] = array(
382
    '#type' => 'checkbox',
383
    '#title' => t('All aliases'),
384
    '#default_value' => FALSE,
385
    '#description' => t('Delete all aliases. Number of aliases which will be deleted: %count.', array('%count' => $total_count)),
386
  );
387
388
  // Next, iterate over an array of objects/alias types which can be deleted and provide checkboxes
389
  $objects = module_invoke_all('path_alias_types');
390
  foreach ($objects as $internal_name => $label) {
391
    $count = db_query("SELECT count(1) FROM {url_alias} WHERE source LIKE :src", array(':src' => "$internal_name%"))->fetchField();
392
    $form['delete'][$internal_name] = array(
393
      '#type' => 'checkbox',
394
      '#title' => $label, // This label is sent through t() in the hard coded function where it is defined
395
      '#default_value' => FALSE,
396
      '#description' => t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', array('@label' => $label, '%count' => $count)),
397
    );
398
  }
399
400
  // Warn them and give a button that shows we mean business
401
  $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>');
402
  $form['buttons']['submit'] = array(
403
    '#type' => 'submit',
404
    '#value' => t('Delete aliases now!'),
405
  );
406
407
  return $form;
408
}
409
410
/**
411
 * Process pathauto_admin_delete form submissions.
412
 */
413
function pathauto_admin_delete_submit($form, &$form_state) {
414
  foreach ($form_state['values'] as $key => $value) {
415
    if ($value) {
416
      if ($key === 'all_aliases') {
417
        db_delete('url_alias')
418
          ->execute();
419
        drupal_set_message(t('All of your path aliases have been deleted.'));
420
      }
421
      $objects = module_invoke_all('path_alias_types');
422
      if (array_key_exists($key, $objects)) {
423
        db_delete('url_alias')
424
          ->condition('source', db_like($key) . '%', 'LIKE')
425
          ->execute();
426
        drupal_set_message(t('All of your %type path aliases have been deleted.', array('%type' => $objects[$key])));
427
      }
428
    }
429
  }
430
  $form_state['redirect'] = 'admin/config/search/path/delete_bulk';
431
}