Index: path_redirect.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/path_redirect/path_redirect.module,v retrieving revision 1.3.2.30 diff -u -p -r1.3.2.30 path_redirect.module --- path_redirect.module 8 Mar 2008 02:09:53 -0000 1.3.2.30 +++ path_redirect.module 12 Apr 2008 22:21:22 -0000 @@ -419,6 +419,14 @@ function path_redirect_delete_confirm_su } function path_redirect_settings() { + $form['path_redirect_nodeapi_enabled'] = array( + '#type' => 'radios', + '#title' => t('Enable on edit pages'), + '#default_value' => variable_get('path_redirect_nodeapi_enabled', 0), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('Enable management of URL redirects directly on content editing pages.'), + ); + $form['path_redirect_redirect_warning'] = array( '#type' => 'radios', '#title' => t('Warn on redirect'), @@ -438,6 +446,117 @@ function path_redirect_settings() { return system_settings_form($form); } +function path_redirect_form_alter($form_id, &$form) { + if (variable_get('path_redirect_nodeapi_enabled', 0) && isset($form['#id']) && $form['#id'] == 'node-form' && user_access('administer redirects')) { + $form['redirects'] = array( + '#type' => 'fieldset', + '#access' => user_access('administer redirects'), + '#title' => t('URL redirects'), + '#collapsible' => TRUE, + '#collapsed' => !db_num_rows(path_redirect_node_redirects($form['#node']->nid)), + '#prefix' => '
', + '#suffix' => '
', + '#weight' => 31, + ); + $form['redirects']['list'] = _path_redirect_node_form_list($form['#node']); + $form['redirects']['path_redirect_add'] = array( + '#type' => 'textfield', + '#title' => t('Add a redirect from'), + '#description' => t('Path from which to redirect to this node. Do not include the leading slash.'), + '#maxlength' => 255, + '#size' => 42, + ); + } +} + +function path_redirect_node_redirects($nid) { + return db_query(" + SELECT rid, path, redirect, type + FROM {path_redirect} pr LEFT JOIN {url_alias} ua ON pr.redirect = ua.dst + WHERE pr.redirect = 'node/%d' OR ua.src = 'node/%d' + ORDER BY pr.path", $nid, $nid); +} + +function _path_redirect_node_form_list($node) { + $form['#theme'] = 'path_redirect_node_form_list'; + if(!empty($node->nid)) { + $result = path_redirect_node_redirects($node->nid); + if ($result) { + $destination = drupal_get_destination(); + $types = path_redirect_status_codes(); + while ($redirect = db_fetch_object($result)) { + $form['redirects'][$redirect->rid]['path'] = array( + '#value' => $redirect->path, + ); + $form['redirects'][$redirect->rid]['redirect'] = array( + '#value' => $redirect->redirect, + ); + $form['redirects'][$redirect->rid]['type'] = array( + '#value' => ''. $redirect->type .'', + ); + $form['redirects'][$redirect->rid]['test'] = array( + '#value' => l(t('test'), $redirect->path), + ); + $form['redirects'][$redirect->rid]['edit'] = array( + '#value' => l(t('edit'), 'admin/build/path-redirect/edit/'. $redirect->rid, array(), $destination), + ); + $form['redirects'][$redirect->rid]['delete'] = array( + '#value' => l(t('delete'), 'admin/build/path-redirect/delete/'. $redirect->rid, array(), $destination), + ); + } + } + } + return $form; +} + +function theme_path_redirect_node_form_list(&$form) { + if (count(element_children($form['redirects']))) { + $header = array( + t('From'), + t('To'), + t('Type'), + array('data' => t('Operations'), 'colspan' => 3), + ); + foreach (element_children($form['redirects']) as $key) { + $rows[] = array( + drupal_render($form['redirects'][$key]['path']), + drupal_render($form['redirects'][$key]['redirect']), + drupal_render($form['redirects'][$key]['type']), + drupal_render($form['redirects'][$key]['test']), + drupal_render($form['redirects'][$key]['edit']), + drupal_render($form['redirects'][$key]['delete']), + ); + } + return theme('table', $header, $rows); + } +} + +function path_redirect_nodeapi(&$node, $op, $arg) { + if (user_access('administer redirects')) { + switch ($op) { + case 'validate': + if (db_result(db_query("SELECT COUNT(rid) FROM {path_redirect} WHERE path = '%s' AND redirect != '%s' AND redirect != '%s'", $node->path_redirect_add, "node/$node->nid", $node->path))) { + form_set_error('path_redirect_add', t('The path is already redirected elsewhere.')); + } + break; + case 'load': + break; + case 'update': + case 'insert': + if ($node->path_redirect_add) { + path_redirect_save(array( + 'path' => $node->path_redirect_add, + 'redirect' => $node->path ? $node->path : 'node/'. $node->nid, + 'type' => 301, + )); + } + break; + case 'delete': + break; + } + } +} + /** * This is a copy of drupal_goto() redesigned for use during the bootstrap */