259 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			259 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
// $Id$
 | 
						|
 | 
						|
/**
 | 
						|
 * @file
 | 
						|
 * Administrative page callbacks for the path module.
 | 
						|
 */
 | 
						|
 | 
						|
/**
 | 
						|
 * Return a listing of all defined URL aliases.
 | 
						|
 *
 | 
						|
 * When filter key passed, perform a standard search on the given key,
 | 
						|
 * and return the list of matching URL aliases.
 | 
						|
 */
 | 
						|
function path_admin_overview($keys = NULL) {
 | 
						|
  // Add the filter form above the overview table.
 | 
						|
  $build['path_admin_filter_form'] = drupal_get_form('path_admin_filter_form', $keys);
 | 
						|
  // Enable language column if locale is enabled or if we have any alias with language
 | 
						|
  $alias_exists = (bool) db_query_range('SELECT 1 FROM {url_alias} WHERE language <> :language', 0, 1, array(':language' => ''))->fetchField();
 | 
						|
  $multilanguage = (module_exists('locale') || $alias_exists);
 | 
						|
 | 
						|
  $header = array(
 | 
						|
    array('data' => t('Alias'), 'field' => 'alias', 'sort' => 'asc'),
 | 
						|
    array('data' => t('System'), 'field' => 'source'),
 | 
						|
    array('data' => t('Operations'), 'colspan' => '2')
 | 
						|
  );
 | 
						|
  if ($multilanguage) {
 | 
						|
    array_splice($header, 2, 0, array(array('data' => t('Language'), 'field' => 'language')));
 | 
						|
  }
 | 
						|
 | 
						|
  $query = db_select('url_alias')->extend('PagerDefault')->extend('TableSort');
 | 
						|
  if ($keys) {
 | 
						|
    // Replace wildcards with PDO wildcards.
 | 
						|
    $query->condition('alias', '%' . preg_replace('!\*+!', '%', $keys) . '%', 'LIKE');
 | 
						|
  }
 | 
						|
  $result = $query
 | 
						|
    ->fields('url_alias')
 | 
						|
    ->orderByHeader($header)
 | 
						|
    ->limit(50)
 | 
						|
    ->execute();
 | 
						|
 | 
						|
  $rows = array();
 | 
						|
  $destination = drupal_get_destination();
 | 
						|
  foreach ($result as $data) {
 | 
						|
    $row = array(
 | 
						|
      'data' => array(
 | 
						|
        l($data->alias, $data->source),
 | 
						|
        l($data->source, $data->source, array('alias' => TRUE)),
 | 
						|
        l(t('edit'), "admin/config/search/path/edit/$data->pid", array('query' => $destination)),
 | 
						|
        l(t('delete'), "admin/config/search/path/delete/$data->pid", array('query' => $destination)),
 | 
						|
      ),
 | 
						|
    );
 | 
						|
    // If the system path maps to a different URL alias, highlight this table
 | 
						|
    // row to let the user know of old aliases.
 | 
						|
    if ($data->alias != drupal_get_path_alias($data->source, $data->language)) {
 | 
						|
      $row['class'] = array('warning');
 | 
						|
    }
 | 
						|
    if ($multilanguage) {
 | 
						|
      array_splice($row['data'], 2, 0, module_invoke('locale', 'language_name', $data->language));
 | 
						|
    }
 | 
						|
    $rows[] = $row;
 | 
						|
  }
 | 
						|
 | 
						|
  $build['path_table'] = array(
 | 
						|
    '#theme' => 'table',
 | 
						|
    '#header' => $header,
 | 
						|
    '#rows' => $rows,
 | 
						|
    '#empty' => t('No URL aliases available.'),
 | 
						|
  );
 | 
						|
  $build['path_pager'] = array('#theme' => 'pager');
 | 
						|
 | 
						|
  return $build;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; handles pages for creating and editing URL aliases.
 | 
						|
 */
 | 
						|
function path_admin_edit($path = array()) {
 | 
						|
  if ($path) {
 | 
						|
    drupal_set_title($path['alias']);
 | 
						|
    $output = drupal_get_form('path_admin_form', $path);
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $output = drupal_get_form('path_admin_form');
 | 
						|
  }
 | 
						|
 | 
						|
  return $output;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Return a form for editing or creating an individual URL alias.
 | 
						|
 *
 | 
						|
 * @ingroup forms
 | 
						|
 * @see path_admin_form_validate()
 | 
						|
 * @see path_admin_form_submit()
 | 
						|
 */
 | 
						|
function path_admin_form($form, &$form_state, $path = array('source' => '', 'alias' => '', 'language' => '', 'pid' => NULL)) {
 | 
						|
  $form['source'] = array(
 | 
						|
    '#type' => 'textfield',
 | 
						|
    '#title' => t('Existing system path'),
 | 
						|
    '#default_value' => $path['source'],
 | 
						|
    '#maxlength' => 255,
 | 
						|
    '#size' => 45,
 | 
						|
    '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'),
 | 
						|
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
 | 
						|
    '#required' => TRUE,
 | 
						|
  );
 | 
						|
  $form['alias'] = array(
 | 
						|
    '#type' => 'textfield',
 | 
						|
    '#title' => t('Path alias'),
 | 
						|
    '#default_value' => $path['alias'],
 | 
						|
    '#maxlength' => 255,
 | 
						|
    '#size' => 45,
 | 
						|
    '#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.'),
 | 
						|
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
 | 
						|
    '#required' => TRUE,
 | 
						|
  );
 | 
						|
 | 
						|
  // This will be a hidden value unless locale module is enabled.
 | 
						|
  $form['language'] = array(
 | 
						|
    '#type' => 'value',
 | 
						|
    '#value' => $path['language']
 | 
						|
  );
 | 
						|
  if ($path['pid']) {
 | 
						|
    $form['pid'] = array(
 | 
						|
      '#type' => 'hidden',
 | 
						|
      '#value' => $path['pid'],
 | 
						|
    );
 | 
						|
    $form['submit'] = array(
 | 
						|
      '#type' => 'submit',
 | 
						|
      '#value' => t('Update alias'),
 | 
						|
    );
 | 
						|
  }
 | 
						|
  else {
 | 
						|
    $form['submit'] = array(
 | 
						|
      '#type' => 'submit',
 | 
						|
      '#value' => t('Create new alias'),
 | 
						|
    );
 | 
						|
  }
 | 
						|
 | 
						|
  return $form;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/**
 | 
						|
 * Verify that a URL alias is valid
 | 
						|
 */
 | 
						|
function path_admin_form_validate($form, &$form_state) {
 | 
						|
  $source = &$form_state['values']['source'];
 | 
						|
  $source = drupal_get_normal_path($source);
 | 
						|
  $alias = $form_state['values']['alias'];
 | 
						|
  $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
 | 
						|
  // Language is only set if locale module is enabled, otherwise save for all languages.
 | 
						|
  $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
 | 
						|
 | 
						|
  $has_alias = db_query("SELECT COUNT(alias) FROM {url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language", array(
 | 
						|
      ':pid' => $pid,
 | 
						|
      ':alias' => $alias,
 | 
						|
      ':language' => $language,
 | 
						|
    ))
 | 
						|
    ->fetchField();
 | 
						|
 | 
						|
  if ($has_alias) {
 | 
						|
    form_set_error('alias', t('The alias %alias is already in use in this language.', array('%alias' => $alias)));
 | 
						|
  }
 | 
						|
  $item = menu_get_item($source);
 | 
						|
  if (!$item || !$item['access']) {
 | 
						|
    form_set_error('source', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $source)));
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Save a URL alias to the database.
 | 
						|
 */
 | 
						|
function path_admin_form_submit($form, &$form_state) {
 | 
						|
  // Remove unnecessary values.
 | 
						|
  form_state_values_clean($form_state);
 | 
						|
 | 
						|
  path_save($form_state['values']);
 | 
						|
 | 
						|
  drupal_set_message(t('The alias has been saved.'));
 | 
						|
  $form_state['redirect'] = 'admin/config/search/path';
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Menu callback; confirms deleting an URL alias
 | 
						|
 */
 | 
						|
function path_admin_delete_confirm($form, &$form_state, $path) {
 | 
						|
  if (user_access('administer url aliases')) {
 | 
						|
    $form_state['path'] = $path;
 | 
						|
    return confirm_form(
 | 
						|
      $form,
 | 
						|
      t('Are you sure you want to delete path alias %title?',
 | 
						|
      array('%title' => $path['alias'])),
 | 
						|
      'admin/config/search/path'
 | 
						|
    );
 | 
						|
  }
 | 
						|
  return array();
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Execute URL alias deletion
 | 
						|
 */
 | 
						|
function path_admin_delete_confirm_submit($form, &$form_state) {
 | 
						|
  if ($form_state['values']['confirm']) {
 | 
						|
    path_delete($form_state['path']['pid']);
 | 
						|
    $form_state['redirect'] = 'admin/config/search/path';
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
/**
 | 
						|
 * Return a form to filter URL aliases.
 | 
						|
 *
 | 
						|
 * @ingroup forms
 | 
						|
 * @see path_admin_filter_form_submit()
 | 
						|
 */
 | 
						|
function path_admin_filter_form($form, &$form_state, $keys = '') {
 | 
						|
  $form['#attributes'] = array('class' => array('search-form'));
 | 
						|
  $form['basic'] = array('#type' => 'fieldset',
 | 
						|
    '#title' => t('Filter aliases')
 | 
						|
  );
 | 
						|
  $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
 | 
						|
  $form['basic']['inline']['filter'] = array(
 | 
						|
    '#type' => 'textfield',
 | 
						|
    '#title' => '',
 | 
						|
    '#default_value' => $keys,
 | 
						|
    '#maxlength' => 128,
 | 
						|
    '#size' => 25,
 | 
						|
  );
 | 
						|
  $form['basic']['inline']['submit'] = array(
 | 
						|
    '#type' => 'submit',
 | 
						|
    '#value' => t('Filter'),
 | 
						|
    '#submit' => array('path_admin_filter_form_submit_filter'),
 | 
						|
    );
 | 
						|
  if ($keys) {
 | 
						|
    $form['basic']['inline']['reset'] = array(
 | 
						|
      '#type' => 'submit',
 | 
						|
      '#value' => t('Reset'),
 | 
						|
      '#submit' => array('path_admin_filter_form_submit_reset'),
 | 
						|
    );
 | 
						|
  }
 | 
						|
  return $form;
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Process filter form submission when the Filter button is pressed.
 | 
						|
 */
 | 
						|
function path_admin_filter_form_submit_filter($form, &$form_state) {
 | 
						|
  $form_state['redirect'] = 'admin/config/search/path/list/' . trim($form_state['values']['filter']);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Process filter form submission when the Reset button is pressed.
 | 
						|
 */
 | 
						|
function path_admin_filter_form_submit_reset($form, &$form_state) {
 | 
						|
  $form_state['redirect'] = 'admin/config/search/path/list';
 | 
						|
}
 |