path.admin.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the path module.
  5. */
  6. /**
  7. * Returns a listing of all defined URL aliases.
  8. *
  9. * When filter key passed, perform a standard search on the given key,
  10. * and return the list of matching URL aliases.
  11. */
  12. function path_admin_overview($keys = NULL) {
  13. // Add the filter form above the overview table.
  14. $build['path_admin_filter_form'] = drupal_get_form('path_admin_filter_form', $keys);
  15. // Enable language column if locale is enabled or if we have any alias with language
  16. $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', 0, 1, array(':language' => LANGUAGE_NONE))->fetchField();
  17. $multilanguage = (module_exists('locale') || $alias_exists);
  18. $header = array();
  19. $header[] = array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc');
  20. $header[] = array('data' => t('System'), 'field' => 'source');
  21. if ($multilanguage) {
  22. $header[] = array('data' => t('Language'), 'field' => 'language');
  23. }
  24. $header[] = array('data' => t('Operations'));
  25. $query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
  26. if ($keys) {
  27. // Replace wildcards with PDO wildcards.
  28. $query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
  29. }
  30. $result = $query
  31. ->fields('url_alias')
  32. ->orderByHeader($header)
  33. ->limit(50)
  34. ->execute();
  35. $rows = array();
  36. $destination = drupal_get_destination();
  37. foreach ($result as $data) {
  38. $row = array();
  39. $row['data']['alias'] = l($data->alias, $data->source);
  40. $row['data']['source'] = l($data->source, $data->source, array('alias' => TRUE));
  41. if ($multilanguage) {
  42. $row['data']['language'] = module_invoke('locale', 'language_name', $data->language);
  43. }
  44. $operations = array();
  45. $operations['edit'] = array(
  46. 'title' => t('edit'),
  47. 'href' => "admin/config/search/path/edit/$data->pid",
  48. 'query' => $destination,
  49. );
  50. $operations['delete'] = array(
  51. 'title' => t('delete'),
  52. 'href' => "admin/config/search/path/delete/$data->pid",
  53. 'query' => $destination,
  54. );
  55. $row['data']['operations'] = array(
  56. 'data' => array(
  57. '#theme' => 'links',
  58. '#links' => $operations,
  59. '#attributes' => array('class' => array('links', 'inline', 'nowrap')),
  60. ),
  61. );
  62. // If the system path maps to a different URL alias, highlight this table
  63. // row to let the user know of old aliases.
  64. if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
  65. $row['class'] = array('warning');
  66. }
  67. $rows[] = $row;
  68. }
  69. $build['path_table'] = array(
  70. '#theme' => 'table',
  71. '#header' => $header,
  72. '#rows' => $rows,
  73. '#empty' => t('No URL aliases available. <a href="@link">Add URL alias</a>.', array('@link' => url('admin/config/search/path/add'))),
  74. );
  75. $build['path_pager'] = array('#theme' => 'pager');
  76. return $build;
  77. }
  78. /**
  79. * Page callback: Returns a form creating or editing a path alias.
  80. *
  81. * @param $path
  82. * An array containing the path ID, source, alias, and language code.
  83. *
  84. * @return
  85. * A form for adding or editing a URL alias.
  86. *
  87. * @see path_menu()
  88. */
  89. function path_admin_edit($path = array()) {
  90. if ($path) {
  91. drupal_set_title($path['alias']);
  92. $output = drupal_get_form('path_admin_form', $path);
  93. }
  94. else {
  95. $output = drupal_get_form('path_admin_form');
  96. }
  97. return $output;
  98. }
  99. /**
  100. * Form constructor for the path administration form.
  101. *
  102. * @param $path
  103. * An array containing the path ID, source, alias, and language code.
  104. *
  105. * @ingroup forms
  106. * @see path_admin_form_validate()
  107. * @see path_admin_form_submit()
  108. * @see path_admin_form_delete_submit()
  109. */
  110. function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => LANGUAGE_NONE, 'pid' => NULL)) {
  111. $form['source'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Existing system path'),
  114. '#default_value' => $path['source'],
  115. '#maxlength' => 255,
  116. '#size' => 45,
  117. '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'),
  118. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  119. '#required' => TRUE,
  120. );
  121. $form['alias'] = array(
  122. '#type' => 'textfield',
  123. '#title' => t('Path alias'),
  124. '#default_value' => $path['alias'],
  125. '#maxlength' => 255,
  126. '#size' => 45,
  127. '#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
  128. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  129. '#required' => TRUE,
  130. );
  131. // This will be a hidden value unless locale module is enabled.
  132. $form['language'] = array(
  133. '#type' => 'value',
  134. '#value' => $path['language']
  135. );
  136. $form['actions'] = array('#type' => 'actions');
  137. $form['actions']['submit'] = array(
  138. '#type' => 'submit',
  139. '#value' => t('Save'),
  140. );
  141. if ($path['pid']) {
  142. $form['pid'] = array(
  143. '#type' => 'hidden',
  144. '#value' => $path['pid'],
  145. );
  146. $form['actions']['delete'] = array(
  147. '#type' => 'submit',
  148. '#value' => t('Delete'),
  149. '#submit' => array('path_admin_form_delete_submit'),
  150. );
  151. }
  152. return $form;
  153. }
  154. /**
  155. * Form submission handler for the 'Delete' button on path_admin_form().
  156. *
  157. * @see path_admin_form_validate()
  158. * @see path_admin_form_submit()
  159. */
  160. function path_admin_form_delete_submit($form, &$form_state) {
  161. $destination = array();
  162. if (isset($_GET['destination'])) {
  163. $destination = drupal_get_destination();
  164. unset($_GET['destination']);
  165. }
  166. $form_state['redirect'] = array('admin/config/search/path/delete/' . $form_state['values']['pid'], array('query' => $destination));
  167. }
  168. /**
  169. * Form validation handler for path_admin_form().
  170. *
  171. * @see path_admin_form_submit()
  172. * @see path_admin_form_delete_submit()
  173. */
  174. function path_admin_form_validate($form, &$form_state) {
  175. $source = &$form_state['values']['source'];
  176. $source = drupal_get_normal_path($source);
  177. $alias = $form_state['values']['alias'];
  178. $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
  179. // Language is only set if locale module is enabled, otherwise save for all languages.
  180. $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : LANGUAGE_NONE;
  181. $has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array(
  182. ':pid' => $pid,
  183. ':alias' => $alias,
  184. ':language' => $language,
  185. ))
  186. ->fetchField();
  187. if ($has_alias) {
  188. form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
  189. }
  190. if (!drupal_valid_path($source)) {
  191. form_set_error('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
  192. }
  193. }
  194. /**
  195. * Form submission handler for path_admin_form().
  196. *
  197. * @see path_admin_form_validate()
  198. * @see path_admin_form_delete_submit()
  199. */
  200. function path_admin_form_submit($form, &$form_state) {
  201. // Remove unnecessary values.
  202. form_state_values_clean($form_state);
  203. path_save($form_state['values']);
  204. drupal_set_message(t('The alias has been saved.'));
  205. $form_state['redirect'] = 'admin/config/search/path';
  206. }
  207. /**
  208. * Form constructor for the path deletion form.
  209. *
  210. * @param $path
  211. * The path alias that will be deleted.
  212. *
  213. * @see path_admin_delete_confirm_submit()
  214. */
  215. function path_admin_delete_confirm($form, &$form_state, $path) {
  216. if (user_access('administer url aliases')) {
  217. $form_state['path'] = $path;
  218. return confirm_form(
  219. $form,
  220. t('Are you sure you want to delete path alias %title?',
  221. array('%title' => $path['alias'])),
  222. 'admin/config/search/path'
  223. );
  224. }
  225. return array();
  226. }
  227. /**
  228. * Form submission handler for path_admin_delete_confirm().
  229. */
  230. function path_admin_delete_confirm_submit($form, &$form_state) {
  231. if ($form_state['values']['confirm']) {
  232. path_delete($form_state['path']['pid']);
  233. $form_state['redirect'] = 'admin/config/search/path';
  234. }
  235. }
  236. /**
  237. * Form constructor for the path admin overview filter form.
  238. *
  239. * @ingroup forms
  240. * @see path_admin_filter_form_submit_filter()
  241. * @see path_admin_filter_form_submit_reset()
  242. */
  243. function path_admin_filter_form($form, &$form_state, $keys = '') {
  244. $form['#attributes'] = array('class' => array('search-form'));
  245. $form['basic'] = array('#type' => 'fieldset',
  246. '#title' => t('Filter aliases'),
  247. '#attributes' => array('class' => array('container-inline')),
  248. );
  249. $form['basic']['filter'] = array(
  250. '#type' => 'textfield',
  251. '#title' => 'Path alias',
  252. '#title_display' => 'invisible',
  253. '#default_value' => $keys,
  254. '#maxlength' => 128,
  255. '#size' => 25,
  256. );
  257. $form['basic']['submit'] = array(
  258. '#type' => 'submit',
  259. '#value' => t('Filter'),
  260. '#submit' => array('path_admin_filter_form_submit_filter'),
  261. );
  262. if ($keys) {
  263. $form['basic']['reset'] = array(
  264. '#type' => 'submit',
  265. '#value' => t('Reset'),
  266. '#submit' => array('path_admin_filter_form_submit_reset'),
  267. );
  268. }
  269. return $form;
  270. }
  271. /**
  272. * Form submission handler for the path_admin_filter_form() Filter button.
  273. *
  274. * @see path_admin_filter_form_submit_reset()
  275. */
  276. function path_admin_filter_form_submit_filter($form, &$form_state) {
  277. $form_state['redirect'] = 'admin/config/search/path/list/' . trim($form_state['values']['filter']);
  278. }
  279. /**
  280. * Form submission handler for the path_admin_filter_form() Reset button.
  281. *
  282. * @see path_admin_filter_form_submit_filter()
  283. */
  284. function path_admin_filter_form_submit_reset($form, &$form_state) {
  285. $form_state['redirect'] = 'admin/config/search/path/list';
  286. }