pathauto.admin.inc 16 KB

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