00001 <?php
00002
00003
00014 function path_admin_overview($keys = NULL) {
00015
00016 $output = drupal_get_form('path_admin_filter_form', $keys);
00017
00018 $count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE language != ''"));
00019 $multilanguage = (module_exists('locale') || $count);
00020
00021 if ($keys) {
00022
00023 $keys = preg_replace('!\*+!', '%', $keys);
00024 $sql = "SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'";
00025 }
00026 else {
00027 $sql = 'SELECT * FROM {url_alias}';
00028 }
00029 $header = array(
00030 array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
00031 array('data' => t('System'), 'field' => 'src'),
00032 array('data' => t('Operations'), 'colspan' => '2')
00033 );
00034 if ($multilanguage) {
00035 $header[3] = $header[2];
00036 $header[2] = array('data' => t('Language'), 'field' => 'language');
00037 }
00038 $sql .= tablesort_sql($header);
00039 $result = pager_query($sql, 50, 0 , NULL, $keys);
00040
00041 $rows = array();
00042 $destination = drupal_get_destination();
00043 while ($data = db_fetch_object($result)) {
00044 $row = array(check_plain($data->dst), check_plain($data->src), l(t('edit'), "admin/build/path/edit/$data->pid", array('query' => $destination)), l(t('delete'), "admin/build/path/delete/$data->pid", array('query' => $destination)));
00045 if ($multilanguage) {
00046 $row[4] = $row[3];
00047 $row[3] = $row[2];
00048 $row[2] = module_invoke('locale', 'language_name', $data->language);
00049 }
00050 $rows[] = $row;
00051 }
00052
00053 if (empty($rows)) {
00054 $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases available.') ;
00055 $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4)));
00056 }
00057
00058 $output .= theme('table', $header, $rows);
00059 $output .= theme('pager', NULL, 50, 0);
00060
00061 return $output;
00062 }
00063
00067 function path_admin_edit($pid = 0) {
00068 if ($pid) {
00069 $alias = path_load($pid);
00070 drupal_set_title(check_plain($alias['dst']));
00071 $output = drupal_get_form('path_admin_form', $alias);
00072 }
00073 else {
00074 $output = drupal_get_form('path_admin_form');
00075 }
00076
00077 return $output;
00078 }
00079
00087 function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' => '', 'language' => '', 'pid' => NULL)) {
00088
00089 $form['#alias'] = $edit;
00090
00091 $form['src'] = array(
00092 '#type' => 'textfield',
00093 '#title' => t('Existing system path'),
00094 '#default_value' => $edit['src'],
00095 '#maxlength' => 64,
00096 '#size' => 45,
00097 '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'),
00098 '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
00099 '#required' => TRUE,
00100 );
00101 $form['dst'] = array(
00102 '#type' => 'textfield',
00103 '#title' => t('Path alias'),
00104 '#default_value' => $edit['dst'],
00105 '#maxlength' => 64,
00106 '#size' => 45,
00107 '#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.'),
00108 '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
00109 '#required' => TRUE,
00110 );
00111
00112 $form['language'] = array(
00113 '#type' => 'value',
00114 '#value' => $edit['language']
00115 );
00116 if ($edit['pid']) {
00117 $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
00118 $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
00119 }
00120 else {
00121 $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
00122 }
00123
00124 return $form;
00125 }
00126
00127
00131 function path_admin_form_validate($form, &$form_state) {
00132 $src = $form_state['values']['src'];
00133 $dst = $form_state['values']['dst'];
00134 $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
00135
00136 $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : '';
00137
00138 if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) {
00139 form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst)));
00140 }
00141 $item = menu_get_item($src);
00142 if (!$item || !$item['access']) {
00143 form_set_error('src', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $src)));
00144 }
00145 }
00146
00150 function path_admin_form_submit($form, &$form_state) {
00151
00152 path_set_alias($form_state['values']['src'], $form_state['values']['dst'], isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0, isset($form_state['values']['language']) ? $form_state['values']['language'] : '');
00153
00154 drupal_set_message(t('The alias has been saved.'));
00155 $form_state['redirect'] = 'admin/build/path';
00156 return;
00157 }
00158
00162 function path_admin_delete_confirm($form_state, $pid) {
00163 $path = path_load($pid);
00164 if (user_access('administer url aliases')) {
00165 $form['pid'] = array('#type' => 'value', '#value' => $pid);
00166 $output = confirm_form($form,
00167 t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])),
00168 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path');
00169 }
00170 return $output;
00171 }
00172
00176 function path_admin_delete_confirm_submit($form, &$form_state) {
00177 if ($form_state['values']['confirm']) {
00178 path_admin_delete($form_state['values']['pid']);
00179 $form_state['redirect'] = 'admin/build/path';
00180 return;
00181 }
00182 }
00183
00184
00191 function path_admin_filter_form(&$form_state, $keys = '') {
00192 $form['#attributes'] = array('class' => 'search-form');
00193 $form['basic'] = array('#type' => 'fieldset',
00194 '#title' => t('Filter aliases')
00195 );
00196 $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
00197 $form['basic']['inline']['filter'] = array(
00198 '#type' => 'textfield',
00199 '#title' => '',
00200 '#default_value' => $keys,
00201 '#maxlength' => 64,
00202 '#size' => 25,
00203 );
00204 $form['basic']['inline']['submit'] = array(
00205 '#type' => 'submit',
00206 '#value' => t('Filter'),
00207 '#submit' => array('path_admin_filter_form_submit_filter'),
00208 );
00209 if ($keys) {
00210 $form['basic']['inline']['reset'] = array(
00211 '#type' => 'submit',
00212 '#value' => t('Reset'),
00213 '#submit' => array('path_admin_filter_form_submit_reset'),
00214 );
00215 }
00216 return $form;
00217 }
00218
00222 function path_admin_filter_form_submit_filter($form, &$form_state) {
00223 $form_state['redirect'] = 'admin/build/path/list/' . trim($form_state['values']['filter']);
00224 }
00225
00229 function path_admin_filter_form_submit_reset($form, &$form_state) {
00230 $form_state['redirect'] = 'admin/build/path/list';
00231 }
00232
00233
00237 function path_admin_filter_get_keys() {
00238
00239 $path = explode('/', $_GET['q'], 5);
00240 return count($path) == 5 ? $path[4] : '';
00241 }