diff -urp path_redirect/path_redirect.admin.inc path_redirect/path_redirect.admin.inc
--- path_redirect/path_redirect.admin.inc 2009-06-27 17:24:27.000000000 -0500
+++ path_redirect/path_redirect.admin.inc 2009-08-25 13:22:17.000000000 -0500
@@ -10,48 +10,109 @@
* Render a list of redirects for the main admin page.
*/
function path_redirect_admin() {
+ if ($_POST['operation'] == 'delete' and !empty($_POST['redirects'])) {
+ return drupal_get_form('path_redirect_multiple_delete_confirm');
+ } else {
+ return drupal_get_form('path_redirect_admin_form');
+ }
+}
+
+/**
+ * Build a form out of the list of redirects.
+ */
+function path_redirect_admin_form(&$form_state) {
$multilanguage = (module_exists('locale') || db_result(db_query("SELECT rid FROM {path_redirect} WHERE language <> ''")));
$languages = language_list();
-
+ $limit = 50;
+ $redirects = array();
+ $destination = drupal_get_destination();
+
+ $form['options'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Update options'),
+ '#prefix' => '
',
+ '#suffix' => '
',
+ );
+ $form['options']['operation'] = array(
+ '#type' => 'select',
+ '#options' => array(
+ 'delete' => 'Delete',
+ ),
+ '#default_value' => 'delete',
+ );
+ $form['options']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Update'),
+ );
+
$header = array(
+ array('data' => ''),
array('data' => t('From'), 'field' => 'path', 'sort' => 'asc'),
array('data' => t('To'), 'field' => 'redirect'),
array('data' => t('Type'), 'field' => 'type'),
- array('data' => t('Operations'), 'colspan' => '2'),
+ array('data' => t('Operations'), 'colspan' => 2),
);
if ($multilanguage) {
- array_splice($header, 3, 0, array(array('data' => t('Language'), 'field' => 'language')));
+ array_splice($header, 4, 0, array(array('data' => t('Language'), 'field' => 'language')));
}
- $rows = array();
- $limit = 50;
-
- $redirects = pager_query('SELECT rid, path, redirect, query, fragment, language, type FROM {path_redirect}'. tablesort_sql($header), $limit);
- while ($r = db_fetch_object($redirects)) {
+ $form['#header'] = $header;
+
+ $result = pager_query('SELECT rid, path, redirect, query, fragment, language, type FROM {path_redirect}'. tablesort_sql($header), $limit);
+ while ($r = db_fetch_object($result)) {
+ $redirects[$r->rid] = '';
+ // @todo: Revise the following messy, confusing line.
+ $form['from'][$r->rid] = array('#value' => l($r->path, preg_replace('/[\?\&].*/', '', $r->path), array('query' => strstr($r->path, '?') ? preg_replace('/.*\?/', '', $r->path) : NULL, 'language' => $r->language ? $languages[$r->language] : NULL)));
+ // @todo: Fix sorting on the redirect field
$redirect = url($r->redirect, array('query' => $r->query, 'fragment' => $r->fragment, 'absolute' => TRUE, 'alias' => TRUE));
- $row = array(
- 'data' => array(
- // @todo: Revise the following messy, confusing line.
- l($r->path, preg_replace('/[\?\&].*/', '', $r->path), array('query' => strstr($r->path, '?') ? preg_replace('/.*\?/', '', $r->path) : NULL, 'language' => $r->language ? $languages[$r->language] : NULL)),
- // @todo: Fix sorting on the redirect field
- l($redirect, $redirect, array('external' => TRUE)),
- $r->type,
- l(t('Edit'), 'admin/build/path-redirect/edit/'. $r->rid, array('query' => drupal_get_destination())),
- l(t('Delete'), 'admin/build/path-redirect/delete/'. $r->rid, array('query' => drupal_get_destination())),
- ),
- );
+ $form['redirect'][$r->rid] = array('#value' => l($redirect, $redirect, array('external' => TRUE)));
+ $form['type'][$r->rid] = array('#value' => $r->type);
if ($multilanguage) {
- array_splice($row['data'], 3, 0, module_invoke('locale', 'language_name', $r->language));
+ $form['language'][$r->rid] = array('#value' => module_invoke('locale', 'language_name', $r->language));
}
- $rows[] = $row;
+ $form['edit'][$r->rid] = array('#value' => l(t('Edit'), 'admin/build/path-redirect/edit/'. $r->rid, array('query' => $destination)));
+ $form['delete'][$r->rid] = array('#value' => l(t('Delete'), 'admin/build/path-redirect/delete/'. $r->rid, array('query' => $destination)));
}
+
+ $form['redirects'] = array('#type' => 'checkboxes', '#options' => $redirects);
+ $form['pager'] = array('#value' => theme('pager', NULL, $limit));
+ $form['#theme'] = 'path_redirect_admin_form';
+ return $form;
+}
- if (empty($rows)) {
- $rows[] = array(array('data' => t('No redirects available.'), 'colspan' => $multilanguage ? 5 : 4));
+/**
+ * Theme main admin page.
+ */
+function theme_path_redirect_admin_form($form) {
+ $has_redirects = isset($form['from']) && is_array($form['from']);
+ $output = drupal_render($form['options']);
+ if ($has_redirects) {
+ foreach (element_children($form['from']) as $key) {
+ $row = array();
+ $row[] = drupal_render($form['redirects'][$key]);
+ $row[] = drupal_render($form['from'][$key]);
+ $row[] = drupal_render($form['redirect'][$key]);
+ $row[] = drupal_render($form['type'][$key]);
+ if (isset($form['language'])) {
+ $row[] = drupal_render($form['language'][$key]);
+ }
+ $row[] = drupal_render($form['edit'][$key]);
+ $row[] = drupal_render($form['delete'][$key]);
+ $rows[] = $row;
+ }
+ } else {
+ $rows[] = array(array('data' => t('No redirects available.'), 'colspan' => isset($form['language']) ? 6 : 5));
}
-
- $output = theme('table', $header, $rows, array('class' => 'path-redirects'));
- $output .= theme('pager', NULL, $limit);
-
+
+ if ($has_redirects) {
+ $form['#header'][0] = theme('table_select_header_cell');
+ }
+ $output .= theme('table', $form['#header'], $rows, array('class' => 'path-redirects'));
+ unset($form['#header']);
+
+ if ($form['pager']['#value']) {
+ $output .= drupal_render($form['pager']);
+ }
+ $output .= drupal_render($form);
return $output;
}
@@ -242,6 +303,33 @@ function path_redirect_delete_confirm_su
}
}
+function path_redirect_multiple_delete_confirm(&$form_state) {
+ $form['redirects'] = array('#prefix' => '', '#tree' => true);
+ foreach (array_filter($form_state['post']['redirects']) as $rid => $value) {
+ $redirect = path_redirect_load($rid); // db_fetch_array(db_query('SELECT path, redirect FROM {path_redirect} WHERE rid = %d', $rid));
+ $form['redirects'][$rid] = array(
+ '#type' => 'hidden',
+ '#value' => $rid,
+ '#prefix' => '',
+ '#suffix' => check_plain($redirect->path) .' -> '. check_plain($redirect->redirect) ."\n",
+ );
+ }
+ $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
+ return confirm_form($form,
+ t('Are you sure you want to delete these items?'),
+ 'admin/build/path-redirect', t('This action cannot be undone.'),
+ t('Delete all'), t('Cancel'));
+}
+
+function path_redirect_multiple_delete_confirm_submit($form, &$form_state) {
+ if ($form_state['values']['confirm']) {
+ foreach ($form_state['values']['redirects'] as $rid => $value) {
+ path_redirect_delete($rid);
+ }
+ drupal_set_message(t('The redirects have been deleted.'));
+ }
+}
+
/**
* Form builder; administrative settings for the module.
*
diff -urp path_redirect/path_redirect.module path_redirect/path_redirect.module
--- path_redirect/path_redirect.module 2009-06-27 16:57:20.000000000 -0500
+++ path_redirect/path_redirect.module 2009-08-25 13:21:04.000000000 -0500
@@ -125,6 +125,18 @@ function path_redirect_menu() {
}
/**
+ * Implementation of hook_theme().
+ */
+function path_redirect_theme() {
+ return array(
+ 'path_redirect_admin_form' => array(
+ 'arguments' => array('form' => NULL),
+ 'file' => 'path_redirect.admin.inc',
+ ),
+ );
+}
+
+/**
* Implementation of hook_init().
*/
function path_redirect_init() {
@@ -312,12 +324,11 @@ function path_redirect_delete($where = a
}
foreach ($where as $key => $value) {
- if (is_string($value)) {
+ if (is_string($key)) {
$args[] = $value;
$where[$key] = $key .' = '. (is_numeric($value) ? '%d' : "'%s'");
}
}
-
if ($where && $args) {
return db_query("DELETE FROM {path_redirect} WHERE ". implode(' AND ', $where), $args);
}