2007-07-22 20:21:02 +00:00
< ? php
// $Id$
/**
* @ file
2007-07-30 18:23:25 +00:00
* Administrative page callbacks for the path module .
2007-07-22 20:21:02 +00:00
*/
/**
* Return a listing of all defined URL aliases .
2009-10-20 01:24:34 +00:00
*
2007-07-22 20:21:02 +00:00
* 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.
2009-07-29 06:39:35 +00:00
$build [ 'path_admin_filter_form' ] = drupal_get_form ( 'path_admin_filter_form' , $keys );
2007-07-22 20:21:02 +00:00
// Enable language column if locale is enabled or if we have any alias with language
2009-12-02 19:26:23 +00:00
$alias_exists = ( bool ) db_query_range ( 'SELECT 1 FROM {url_alias} WHERE language <> :language' , 0 , 1 , array ( ':language' => LANGUAGE_NONE )) -> fetchField ();
2009-05-16 15:23:16 +00:00
$multilanguage = ( module_exists ( 'locale' ) || $alias_exists );
2007-07-22 20:21:02 +00:00
$header = array (
2009-10-15 17:53:34 +00:00
array ( 'data' => t ( 'Alias' ), 'field' => 'alias' , 'sort' => 'asc' ),
array ( 'data' => t ( 'System' ), 'field' => 'source' ),
2007-07-22 20:21:02 +00:00
array ( 'data' => t ( 'Operations' ), 'colspan' => '2' )
);
if ( $multilanguage ) {
2008-10-09 20:20:49 +00:00
array_splice ( $header , 2 , 0 , array ( array ( 'data' => t ( 'Language' ), 'field' => 'language' )));
2007-07-22 20:21:02 +00:00
}
2009-05-10 16:50:19 +00:00
$query = db_select ( 'url_alias' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
if ( $keys ) {
// Replace wildcards with PDO wildcards.
2009-10-15 17:53:34 +00:00
$query -> condition ( 'alias' , '%' . preg_replace ( '!\*+!' , '%' , $keys ) . '%' , 'LIKE' );
2009-05-10 16:50:19 +00:00
}
$result = $query
-> fields ( 'url_alias' )
2009-05-22 11:33:18 +00:00
-> orderByHeader ( $header )
2009-05-10 16:50:19 +00:00
-> limit ( 50 )
-> execute ();
2007-07-22 20:21:02 +00:00
$rows = array ();
$destination = drupal_get_destination ();
2009-05-10 16:50:19 +00:00
foreach ( $result as $data ) {
2008-12-05 12:50:28 +00:00
$row = array (
'data' => array (
2009-10-15 17:53:34 +00:00
l ( $data -> alias , $data -> source ),
l ( $data -> source , $data -> source , array ( 'alias' => TRUE )),
2009-08-28 14:40:12 +00:00
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 )),
2008-12-05 12:50:28 +00:00
),
);
2009-08-22 14:34:23 +00:00
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
2009-10-15 17:53:34 +00:00
if ( $data -> alias != drupal_get_path_alias ( $data -> source , $data -> language )) {
2009-08-22 14:34:23 +00:00
$row [ 'class' ] = array ( 'warning' );
}
2007-07-22 20:21:02 +00:00
if ( $multilanguage ) {
2008-12-05 12:50:28 +00:00
array_splice ( $row [ 'data' ], 2 , 0 , module_invoke ( 'locale' , 'language_name' , $data -> language ));
2007-07-22 20:21:02 +00:00
}
$rows [] = $row ;
}
2009-07-29 06:39:35 +00:00
$build [ 'path_table' ] = array (
2009-10-10 20:46:17 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-12-02 14:56:32 +00:00
'#rows' => $rows ,
'#empty' => t ( 'No URL aliases available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'path_pager' ] = array ( '#theme' => 'pager' );
2007-07-22 20:21:02 +00:00
2009-07-29 06:39:35 +00:00
return $build ;
2007-07-22 20:21:02 +00:00
}
/**
* Menu callback ; handles pages for creating and editing URL aliases .
*/
2009-10-15 17:53:34 +00:00
function path_admin_edit ( $path = array ()) {
if ( $path ) {
drupal_set_title ( $path [ 'alias' ]);
$output = drupal_get_form ( 'path_admin_form' , $path );
2007-07-22 20:21:02 +00:00
}
else {
2007-08-12 16:34:56 +00:00
$output = drupal_get_form ( 'path_admin_form' );
2007-07-22 20:21:02 +00:00
}
return $output ;
}
/**
* Return a form for editing or creating an individual URL alias .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see path_admin_form_validate ()
* @ see path_admin_form_submit ()
2007-07-22 20:21:02 +00:00
*/
2009-12-02 19:26:23 +00:00
function path_admin_form ( $form , & $form_state , $path = array ( 'source' => '' , 'alias' => '' , 'language' => LANGUAGE_NONE , 'pid' => NULL )) {
2009-10-15 17:53:34 +00:00
$form [ 'source' ] = array (
2007-07-22 20:21:02 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Existing system path' ),
2009-10-15 17:53:34 +00:00
'#default_value' => $path [ 'source' ],
2009-06-11 04:59:26 +00:00
'#maxlength' => 255 ,
2007-07-22 20:21:02 +00:00
'#size' => 45 ,
2009-10-24 05:13:44 +00:00
'#description' => t ( 'Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.' ),
2007-11-10 12:08:22 +00:00
'#field_prefix' => url ( NULL , array ( 'absolute' => TRUE )) . ( variable_get ( 'clean_url' , 0 ) ? '' : '?q=' ),
'#required' => TRUE ,
2007-07-22 20:21:02 +00:00
);
2009-10-15 17:53:34 +00:00
$form [ 'alias' ] = array (
2007-07-22 20:21:02 +00:00
'#type' => 'textfield' ,
'#title' => t ( 'Path alias' ),
2009-10-15 17:53:34 +00:00
'#default_value' => $path [ 'alias' ],
2009-06-11 04:59:26 +00:00
'#maxlength' => 255 ,
2007-07-22 20:21:02 +00:00
'#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.' ),
2007-11-10 12:08:22 +00:00
'#field_prefix' => url ( NULL , array ( 'absolute' => TRUE )) . ( variable_get ( 'clean_url' , 0 ) ? '' : '?q=' ),
'#required' => TRUE ,
2007-07-22 20:21:02 +00:00
);
2009-10-15 17:53:34 +00:00
// This will be a hidden value unless locale module is enabled.
2007-07-22 20:21:02 +00:00
$form [ 'language' ] = array (
'#type' => 'value' ,
2009-10-15 17:53:34 +00:00
'#value' => $path [ 'language' ]
2007-07-22 20:21:02 +00:00
);
2009-10-15 17:53:34 +00:00
if ( $path [ 'pid' ]) {
$form [ 'pid' ] = array (
'#type' => 'hidden' ,
'#value' => $path [ 'pid' ],
);
$form [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Update alias' ),
);
2007-07-22 20:21:02 +00:00
}
else {
2009-10-15 17:53:34 +00:00
$form [ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Create new alias' ),
);
2007-07-22 20:21:02 +00:00
}
return $form ;
}
/**
2009-10-15 17:53:34 +00:00
* Verify that a URL alias is valid
2007-07-22 20:21:02 +00:00
*/
function path_admin_form_validate ( $form , & $form_state ) {
2009-10-24 05:13:44 +00:00
$source = & $form_state [ 'values' ][ 'source' ];
$source = drupal_get_normal_path ( $source );
2009-10-15 17:53:34 +00:00
$alias = $form_state [ 'values' ][ 'alias' ];
2007-07-22 20:21:02 +00:00
$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.
2009-12-02 19:26:23 +00:00
$language = isset ( $form_state [ 'values' ][ 'language' ]) ? $form_state [ 'values' ][ 'language' ] : LANGUAGE_NONE ;
2007-07-22 20:21:02 +00:00
2009-10-15 17:53:34 +00:00
$has_alias = db_query ( " SELECT COUNT(alias) FROM { url_alias} WHERE pid <> :pid AND alias = :alias AND language = :language " , array (
2009-10-20 01:24:34 +00:00
':pid' => $pid ,
':alias' => $alias ,
':language' => $language ,
))
-> fetchField ();
2009-05-10 16:50:19 +00:00
if ( $has_alias ) {
2009-10-15 17:53:34 +00:00
form_set_error ( 'alias' , t ( 'The alias %alias is already in use in this language.' , array ( '%alias' => $alias )));
2007-07-22 20:21:02 +00:00
}
2009-12-17 13:10:19 +00:00
if ( ! drupal_valid_path ( $source )) {
2009-10-15 17:53:34 +00:00
form_set_error ( 'source' , t ( " The path '@link_path' is either invalid or you do not have access to it. " , array ( '@link_path' => $source )));
2007-11-10 12:08:22 +00:00
}
2007-07-22 20:21:02 +00:00
}
/**
2009-10-15 17:53:34 +00:00
* Save a URL alias to the database .
2007-07-22 20:21:02 +00:00
*/
function path_admin_form_submit ( $form , & $form_state ) {
2009-10-20 01:24:34 +00:00
// Remove unnecessary values.
form_state_values_clean ( $form_state );
path_save ( $form_state [ 'values' ]);
2007-07-22 20:21:02 +00:00
drupal_set_message ( t ( 'The alias has been saved.' ));
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path' ;
2007-07-22 20:21:02 +00:00
}
/**
* Menu callback ; confirms deleting an URL alias
2007-12-28 12:02:52 +00:00
*/
2009-10-15 17:53:34 +00:00
function path_admin_delete_confirm ( $form , & $form_state , $path ) {
2007-07-22 20:21:02 +00:00
if ( user_access ( 'administer url aliases' )) {
2009-10-15 17:53:34 +00:00
$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'
);
2007-07-22 20:21:02 +00:00
}
2009-10-15 17:53:34 +00:00
return array ();
2007-07-22 20:21:02 +00:00
}
/**
* Execute URL alias deletion
2007-12-28 12:02:52 +00:00
*/
2007-07-22 20:21:02 +00:00
function path_admin_delete_confirm_submit ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'confirm' ]) {
2009-10-15 17:53:34 +00:00
path_delete ( $form_state [ 'path' ][ 'pid' ]);
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path' ;
2007-07-22 20:21:02 +00:00
}
}
/**
* Return a form to filter URL aliases .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see path_admin_filter_form_submit ()
2007-07-22 20:21:02 +00:00
*/
2009-09-18 00:12:48 +00:00
function path_admin_filter_form ( $form , & $form_state , $keys = '' ) {
2009-08-22 14:34:23 +00:00
$form [ '#attributes' ] = array ( 'class' => array ( 'search-form' ));
2007-07-22 20:21:02 +00:00
$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 ,
2008-11-22 10:49:01 +00:00
'#maxlength' => 128 ,
2007-07-22 20:21:02 +00:00
'#size' => 25 ,
);
2007-11-10 12:08:22 +00:00
$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' ),
);
}
2007-07-22 20:21:02 +00:00
return $form ;
}
/**
2007-11-10 12:08:22 +00:00
* Process filter form submission when the Filter button is pressed .
2007-07-22 20:21:02 +00:00
*/
2007-11-10 12:08:22 +00:00
function path_admin_filter_form_submit_filter ( $form , & $form_state ) {
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path/list/' . trim ( $form_state [ 'values' ][ 'filter' ]);
2007-07-22 20:21:02 +00:00
}
2007-11-10 12:08:22 +00:00
/**
* Process filter form submission when the Reset button is pressed .
*/
function path_admin_filter_form_submit_reset ( $form , & $form_state ) {
2009-08-28 14:40:12 +00:00
$form_state [ 'redirect' ] = 'admin/config/search/path/list' ;
2007-11-10 12:08:22 +00:00
}