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