path.admin.inc 9.8 KB

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