pathauto.admin.inc 17 KB

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