/[drupal]/contributions/modules/pathauto/pathauto.admin.inc
ViewVC logotype

Contents of /contributions/modules/pathauto/pathauto.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (show annotations) (download) (as text)
Sat Sep 13 08:54:06 2008 UTC (14 months, 1 week ago) by freso
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +2 -2 lines
File MIME type: text/x-php
Bug #306702 by Pancho: String bug: "Translation" instead of "Transliteration".
1 <?php
2 // $Id: pathauto.admin.inc,v 1.19 2008/08/26 17:27:08 freso Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the Pathauto module.
7 *
8 * @ingroup pathauto
9 */
10
11 /**
12 * Form builder; Configure the Pathauto system.
13 *
14 * @ingroup forms
15 * @see system_settings_form()
16 */
17 function pathauto_admin_settings() {
18 _pathauto_include();
19
20 // Make sure they have token
21 if (!function_exists('token_get_list')) {
22 drupal_set_message(t('It appears that the <a href="@token_link">Token module</a> is not installed. Please ensure that you have it installed so that Pathauto can work properly. You may need to disable Pathauto and re-enable it to get Token enabled.', array('@token_link' => 'http://drupal.org/project/token')), 'error');
23 return;
24 }
25
26 // Default words to ignore
27 $ignore_words = array(
28 'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from', 'is', 'in',
29 'into', 'like', 'of', 'off', 'on', 'onto', 'per', 'since', 'than', 'the',
30 'this', 'that', 'to', 'up', 'via', 'with',
31 );
32
33 // Generate the form - settings applying to all patterns first
34 $group_weight = -20;
35 $form['general'] = array(
36 '#type' => 'fieldset',
37 '#weight' => $group_weight,
38 '#title' => t('General settings'),
39 '#collapsible' => TRUE,
40 '#collapsed' => TRUE,
41 );
42 $group_weight++;
43
44 $form['general']['pathauto_verbose'] = array(
45 '#type' => 'checkbox',
46 '#title' => t('Verbose'),
47 '#default_value' => variable_get('pathauto_verbose', FALSE),
48 '#description' => t('Display alias changes (except during bulk updates).'),
49 );
50
51 $form['general']['pathauto_separator'] = array(
52 '#type' => 'textfield',
53 '#title' => t('Separator'),
54 '#size' => 1,
55 '#maxlength' => 1,
56 '#default_value' => variable_get('pathauto_separator', '-'),
57 '#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.'),
58 );
59
60 $form['general']['pathauto_case'] = array(
61 '#type' => 'radios',
62 '#title' => t('Character case'),
63 '#default_value' => variable_get('pathauto_case', PATHAUTO_CASE_LOWER),
64 '#options' => array(
65 t('Leave case the same as source token values.'),
66 t('Change to lower case'),
67 ),
68 );
69
70 $form['general']['pathauto_max_length'] = array(
71 '#type' => 'textfield',
72 '#title' => t('Maximum alias length'),
73 '#size' => 3,
74 '#maxlength' => 3,
75 '#default_value' => variable_get('pathauto_max_length', 100),
76 '#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'))),
77 );
78
79 $form['general']['pathauto_max_component_length'] = array(
80 '#type' => 'textfield',
81 '#title' => t('Maximum component length'),
82 '#size' => 3,
83 '#maxlength' => 3,
84 '#default_value' => variable_get('pathauto_max_component_length', 100),
85 '#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'))),
86 );
87
88 $form['general']['pathauto_max_bulk_update'] = array(
89 '#type' => 'textfield',
90 '#title' => t('Maximum number of objects to alias in a bulk update'),
91 '#size' => 4,
92 '#maxlength' => 4,
93 '#default_value' => variable_get('pathauto_max_bulk_update', 50),
94 '#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.'),
95 );
96
97 $actions = array(
98 t('Do nothing. Leave the old alias intact.'),
99 t('Create a new alias. Leave the existing alias functioning.'),
100 t('Create a new alias. Delete the old alias.'),
101 );
102
103 if (function_exists('path_redirect_save')) {
104 $actions[] = t('Create a new alias. Redirect from old alias.');
105 }
106 elseif (variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE) == PATHAUTO_UPDATE_ACTION_REDIRECT) {
107 // the redirect action is selected, but path_redirect is not enabled
108 // let's set the variable back to the default
109 variable_set('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE);
110 }
111
112 $form['general']['pathauto_update_action'] = array(
113 '#type' => 'radios',
114 '#title' => t('Update action'),
115 '#default_value' => variable_get('pathauto_update_action', PATHAUTO_UPDATE_ACTION_DELETE),
116 '#options' => $actions,
117 '#description' => t('What should Pathauto do when updating an existing content item which already has an alias?'),
118 );
119
120 $disable_transliteration = FALSE;
121 $path = drupal_get_path('module', 'pathauto');
122 $transliteration_help = 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.');
123 if (!module_exists('transliteration')) {
124 $disable_transliteration = TRUE;
125 $transliteration_help .= ' <strong>'. t("This option is disabled on your site because the Transliteration module either isn't installed, or isn't installed properly.") .'</strong>';
126 // Make sure we don't try to use Transliteration if it isn't available
127 variable_set('pathauto_transliterate', FALSE);
128 }
129 $form['general']['pathauto_transliterate'] = array(
130 '#type' => 'checkbox',
131 '#title' => t('Transliterate prior to creating alias'),
132 '#default_value' => variable_get('pathauto_transliterate', FALSE),
133 '#description' => $transliteration_help,
134 '#disabled' => $disable_transliteration,
135 );
136 $form['general']['pathauto_reduce_ascii'] = array(
137 '#type' => 'checkbox',
138 '#title' => t('Reduce strings to letters and numbers from ASCII-96'),
139 '#default_value' => variable_get('pathauto_reduce_ascii', FALSE),
140 '#description' => t('Filters the new alias to only letters and numbers found in the ASCII-96 set.'),
141 );
142
143 $wysiwyg_warn = _pathauto_warn_wysiwygs();
144
145 $form['general']['pathauto_ignore_words'] = array(
146 '#type' => 'textarea',
147 '#title' => t('Strings to Remove'),
148 '#default_value' => variable_get('pathauto_ignore_words', implode(',', $ignore_words)),
149 '#description' => t('Words to strip out of the URL alias, separated by commas. Do not use this to remove punctuation.') . $wysiwyg_warn,
150 );
151
152 $form['punctuation'] = array(
153 '#type' => 'fieldset',
154 '#weight' => $group_weight,
155 '#title' => t('Punctuation settings'),
156 '#collapsible' => TRUE,
157 '#collapsed' => TRUE,
158 );
159 $group_weight++;
160
161 $punctuation = pathauto_punctuation_chars();
162 foreach ($punctuation as $name => $details) {
163 $form['punctuation']['pathauto_punctuation_'. $name ] = array(
164 '#type' => 'select',
165 '#title' => $details['name'],
166 '#default_value' => variable_get('pathauto_punctuation_'. $name, PATHAUTO_PUNCTUATION_REMOVE),
167 '#options' => array(
168 PATHAUTO_PUNCTUATION_REMOVE => t('Remove'),
169 PATHAUTO_PUNCTUATION_REPLACE => t('Replace by separator'),
170 PATHAUTO_PUNCTUATION_DO_NOTHING => t('No action (do not replace)'),
171 ),
172 );
173 }
174
175 // Call the hook on all modules - an array of 'settings' objects is returned
176 $all_settings = module_invoke_all('pathauto', 'settings');
177 $modulelist = '';
178 $indexcount = 0;
179 foreach ($all_settings as $settings) {
180 $items = '';
181 $module = $settings->module;
182 $modulelist[] = $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 '#weight' => $group_weight,
193 '#collapsible' => TRUE,
194 '#collapsed' => TRUE,
195 );
196 $group_weight++;
197
198 // Prompt for the default pattern for this module
199 $variable = 'pathauto_'. $module .'_pattern';
200 $description = '';
201 if (!count($_POST)) {
202 $error = _pathauto_check_pattern(variable_get($variable, $patterndefault), $settings->token_type);
203 if ($error) {
204 $form[$module]['#collapsed'] = FALSE;
205 $description = $error;
206 }
207 }
208 $form[$module][$variable] = array(
209 '#type' => 'textfield',
210 '#title' => $patterndescr,
211 '#default_value' => variable_get($variable, $patterndefault),
212 '#description' => $description,
213 '#size' => 65,
214 '#maxlength' => 1280,
215 );
216
217 // If the module supports a set of specialized patterns, set
218 // them up here
219 if (isset($settings->patternitems)) {
220 foreach ($settings->patternitems as $itemname => $itemlabel) {
221 $variable = 'pathauto_'. $module .'_'. $itemname .'_pattern';
222 $description = '';
223 if (!count($_POST)) {
224 $error = _pathauto_check_pattern(variable_get($variable, $patterndefault), $settings->token_type);
225 if ($error) {
226 $form[$module]['#collapsed'] = FALSE;
227 $description = $error;
228 }
229 }
230
231 $form[$module][$variable] = array('#type' => 'textfield',
232 '#title' => $itemlabel,
233 '#default_value' => variable_get($variable, ''),
234 '#description' => $description,
235 '#size' => 65,
236 '#maxlength' => 1280);
237 }
238 }
239
240 // Display the user documentation of placeholders supported by
241 // this module, as a description on the last pattern
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 $form[$module]['token_help'] = array(
249 '#title' => t('Replacement patterns'),
250 '#type' => 'fieldset',
251 '#collapsible' => TRUE,
252 '#collapsed' => TRUE,
253 '#description' => t('Use -raw replacements for text to avoid problems with HTML entities.'),
254 );
255 $form[$module]['token_help']['help'] = array(
256 '#value' => $doc,
257 );
258
259 // If the module supports bulk updates, offer the update action here
260 if ($settings->bulkname) {
261 $variable = 'pathauto_'. $module .'_bulkupdate';
262 if (variable_get($variable, FALSE)) {
263 variable_set($variable, FALSE);
264 $function = $module .'_pathauto_bulkupdate';
265 call_user_func($function);
266 }
267 $form[$module][$variable] = array(
268 '#type' => 'checkbox',
269 '#title' => $settings->bulkname,
270 '#default_value' => FALSE,
271 '#description' => $settings->bulkdescr,
272 );
273 }
274
275 // If the module supports feeds, offer to generate aliases for them
276 if ($supportsfeeds) {
277 $variable = 'pathauto_'. $module .'_applytofeeds';
278 $current = variable_get($variable, $supportsfeeds);
279 // This checks for the old style from 2.0 and earlier. TODO: At some point we can drop that.
280 if (is_numeric($current)) {
281 $current = $supportsfeeds;
282 }
283
284 $form[$module][$variable] = array(
285 '#type' => 'textfield',
286 '#title' => t('Internal feed alias text (leave blank to disable)'),
287 '#size' => 65,
288 '#maxlength' => 1280,
289 '#default_value' => $current,
290 '#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).'),
291 );
292 }
293
294 }
295
296 if (isset($do_index_bulkupdate) && $do_index_bulkupdate) {
297 drupal_set_message(format_plural($indexcount,
298 'Bulk generation of index aliases completed, one alias generated.',
299 'Bulk generation of index aliases completed, @count aliases generated.'));
300 }
301
302 // Keep track of which modules currently support pathauto
303 variable_set('pathauto_modulelist', $modulelist);
304
305 return system_settings_form($form);
306 }
307
308 /**
309 * Helper function for pathauto_admin_settings().
310 *
311 * See if they are using all -raw tokens available
312 * and if all the tokens are valid in the given context.
313 *
314 * @param $pattern
315 * A string containing the pattern to be tested.
316 * @param $type
317 * A flag indicating the class of substitution tokens to use.
318 * @return
319 * A status flag, telling whether there is an error or not.
320 * @see token_get_list()
321 * For more information on the $type parameter.
322 */
323 function _pathauto_check_pattern($pattern, $type) {
324 // Hold items we've warned about so we only warn once per token
325 static $warned;
326 $return = FALSE;
327
328 // Build up a set of all tokens in a format that's easy to search
329 $all_tokens = array();
330 $tokens = token_get_list($type);
331 foreach ($tokens as $actual_tokens) {
332 foreach (array_keys($actual_tokens) as $token) {
333 $all_tokens[$token] = $token;
334 }
335 }
336
337 // Now, search the pattern and evaluate its contents.
338 $matches = array();
339 if (preg_match_all('/\[[a-zA-z_\-]+?\]/i', $pattern, $matches)) {
340 // Loop over each found token.
341 foreach ($matches[0] as $id => $token) {
342 $token = str_replace('[', '', str_replace(']', '', $token));
343 // Check if the token is even valid in this context.
344 if (!array_key_exists($token, $all_tokens)) {
345 drupal_set_message(t('You are using the token [%token] which is not valid within the scope of tokens where you are using it.', array('%token' => $token)), 'error');
346 $return[] = $token;
347 }
348 // Check if there is a raw companion that they should be using.
349 elseif (!preg_match('/\-raw/i', $token)) {
350 $raw_token = $token .'-raw';
351 if (array_key_exists($raw_token, $all_tokens)) {
352 if (!isset($warned) || !array_key_exists($token, $warned)) {
353 drupal_set_message(t('You are using the token [%token] which has a -raw companion available [%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' => $raw_token, '@pathauto-help' => url('admin/help/pathauto'))), 'error');
354 $warned[$token] = $token;
355 }
356 $return[] = $token;
357 }
358 }
359 }
360 }
361
362 if ($return) {
363 $return = t('NOTE: This field contains potentially incorrect patterns. %name %problems.', array(
364 '%name' => format_plural(count($return), 'Problem token:', 'Problem tokens:'),
365 '%problems' => implode(', ', $return),
366 ));
367 }
368
369 return $return;
370 }
371
372 /**
373 * Validate pathauto_admin_settings form submissions.
374 */
375 function pathauto_admin_settings_validate($form, &$form_state) {
376 _pathauto_include();
377
378 // Validate that the separator is not set to be removed per http://drupal.org/node/184119
379 // This isn't really all that bad so warn, but still allow them to save the value.
380 $separator = $form_state['values']['pathauto_separator'];
381 $punctuation = pathauto_punctuation_chars();
382 foreach ($punctuation as $name => $details) {
383 if ($details['value'] == $separator) {
384 $action = $form_state['values']['pathauto_punctuation_'. $name];
385 if ($action == PATHAUTO_PUNCTUATION_REMOVE) {
386 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');
387 }
388 }
389 }
390 }
391
392 /**
393 * Menu callback; select certain alias types to delete.
394 */
395 function pathauto_admin_delete() {
396 /* TODO:
397 1) all - DONE
398 2) all node aliases - DONE
399 4) all user aliases - DONE
400 5) all taxonomy aliases - DONE
401 6) by node type
402 7) by taxonomy vocabulary
403 8) no longer existing aliases (see http://drupal.org/node/128366 )
404 9) where src like 'pattern' - DON'T DO
405 10) where dst like 'pattern' - DON'T DO
406 */
407
408 $form = array();
409 $form['delete'] = array(
410 '#type' => 'fieldset',
411 '#title' => t('Choose aliases to delete'),
412 '#collapsible' => FALSE,
413 '#collapsed' => FALSE,
414 );
415
416 // First we do the "all" case
417 $total_count = db_result(db_query('SELECT count(1) FROM {url_alias}'));
418 $form['delete']['all_aliases'] = array(
419 '#type' => 'checkbox',
420 '#title' => t('All aliases'),
421 '#default_value' => FALSE,
422 '#description' => t('Delete all aliases. Number of aliases which will be deleted: %count.', array('%count' => $total_count)),
423 );
424
425 // Next, iterate over an array of objects/alias types which can be deleted and provide checkboxes
426 $objects = module_invoke_all('path_alias_types');
427 foreach ($objects as $internal_name => $label) {
428 $count = db_result(db_query("SELECT count(1) FROM {url_alias} WHERE src LIKE '%s%%'", $internal_name));
429 $form['delete'][$internal_name] = array(
430 '#type' => 'checkbox',
431 '#title' => $label, // This label is sent through t() in the hard coded function where it is defined
432 '#default_value' => FALSE,
433 '#description' => t('Delete aliases for all @label. Number of aliases which will be deleted: %count.', array('@label' => $label, '%count' => $count)),
434 );
435 }
436
437 // Warn them and give a button that shows we mean business
438 $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>');
439 $form['buttons']['submit'] = array(
440 '#type' => 'submit',
441 '#value' => t('Delete aliases now!'),
442 );
443
444 return $form;
445 }
446
447 /**
448 * Process pathauto_admin_delete form submissions.
449 */
450 function pathauto_admin_delete_submit($form, &$form_state) {
451 foreach ($form_state['values'] as $key => $value) {
452 if ($value) {
453 if ($key === 'all_aliases') {
454 db_query('DELETE FROM {url_alias}');
455 drupal_set_message(t('All of your path aliases have been deleted.'));
456 }
457 $objects = module_invoke_all('path_alias_types');
458 if (array_key_exists($key, $objects)) {
459 db_query("DELETE FROM {url_alias} WHERE src LIKE '%s%%'", $key);
460 drupal_set_message(t('All of your %type path aliases have been deleted.', array('%type' => $objects[$key])));
461 }
462 }
463 }
464 $form_state['redirect'] = 'admin/build/path/delete_bulk';
465 }
466
467 /**
468 * Helper function for pathauto_admin_settings().
469 *
470 * Checks to see if they are using a known WYSIWYG editor and, if so, returns a
471 * a warning string.
472 *
473 * @return
474 * A warning message if appropriate.
475 */
476 function _pathauto_warn_wysiwygs() {
477 $wysiwygs = array(
478 'bueditor', 'fckeditor', 'htmlarea', 'htmlbox', 'tinymce', 'whizzywig',
479 'widgeditor', 'wymeditor', 'wysiwyg', 'xstandard', 'yui_editor',
480 );
481
482 $wysiwyg_problem = FALSE;
483 foreach ($wysiwygs as $key => $wysiwyg) {
484 if (module_exists($wysiwyg)) {
485 $wysiwyg_problem = TRUE;
486 }
487 }
488 if ($wysiwyg_problem) {
489 return '<strong> '. t('You appear to be using a "WYSIWYG" editor which can cause problems if that editor runs on the Pathauto administrative settings page. Please be sure to disable these editors for all text boxes on this page. See the "WYSIWYG Conflicts" section of the README.txt file for more information.') .'</strong>';
490 }
491 }

  ViewVC Help
Powered by ViewVC 1.1.2