From 6e04c4b7052e1402f6916fa8dadf81aa26ccd902 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Tue, 16 Sep 2008 17:51:15 +0000 Subject: [PATCH] rfe: choice of type of redirect on redirect assets --- docs/changelog/7.x.x.txt | 1 + docs/upgrades/upgrade_7.5.21-7.6.0.pl | 14 ++--- lib/WebGUI/Asset/Redirect.pm | 66 ++++++++++++++++------- lib/WebGUI/Session/Http.pm | 14 +++-- lib/WebGUI/i18n/English/Asset_Redirect.pm | 20 +++++++ 5 files changed, 85 insertions(+), 30 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 169bacc5b..e107a5039 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -3,6 +3,7 @@ - rfe: Town Hall: Menu title in search results - rfe: Process Macros in HTTP Proxy's URL - rfe: TimeZone for the User Profiles displays incorrectly after installation + - rfe: choice of type of redirect on redirect assets - rewrite macro parser, improving speed and making parameter parsing more sane - Made the charset metatag the highest thing in the head block. - fixed: AssetProxy allows proxying content in the trash or clipboard diff --git a/docs/upgrades/upgrade_7.5.21-7.6.0.pl b/docs/upgrades/upgrade_7.5.21-7.6.0.pl index 477a5eb13..6674a4c7c 100644 --- a/docs/upgrades/upgrade_7.5.21-7.6.0.pl +++ b/docs/upgrades/upgrade_7.5.21-7.6.0.pl @@ -33,6 +33,7 @@ removeDoNothingOnDelete( $session ); fixIsPublicOnTemplates ( $session ); addSortOrderToFolder( $session ); addEMSBadgeTemplate ( $session ); +redirectChoice ($session); finish($session); # this line required @@ -89,13 +90,12 @@ sub removeDoNothingOnDelete { } #---------------------------------------------------------------------------- -# Describe what our function does -#sub exampleFunction { -# my $session = shift; -# print "\tWe're doing some stuff here that you should know about... " unless $quiet; -# # and here's our code -# print "DONE!\n" unless $quiet; -#} +sub redirectChoice { + my $session = shift; + print "\tGiving a user choice about which type of redirect they'd like to perform... " unless $quiet; + $session->db->write("alter table redirect add column redirectType int not null default 302"); + print "DONE!\n" unless $quiet; +} # -------------- DO NOT EDIT BELOW THIS LINE -------------------------------- diff --git a/lib/WebGUI/Asset/Redirect.pm b/lib/WebGUI/Asset/Redirect.pm index e406bb1b9..7fe91acb8 100644 --- a/lib/WebGUI/Asset/Redirect.pm +++ b/lib/WebGUI/Asset/Redirect.pm @@ -67,27 +67,29 @@ sub definition { tableName=>'redirect', className=>'WebGUI::Asset::Redirect', properties=>{ - redirectUrl=>{ - tab=>"properties", - label=>$i18n->get('redirect url'), - hoverHelp=>$i18n->get('redirect url description'), - fieldType=>'url', - defaultValue=>undef - } - } - }); + redirectUrl=>{ + tab => "properties", + label => $i18n->get('redirect url'), + hoverHelp => $i18n->get('redirect url description'), + fieldType => 'url', + defaultValue => undef + }, + redirectType=>{ + tab => "properties", + label => $i18n->get('Redirect Type'), + hoverHelp => $i18n->get('redirect type description'), + fieldType => 'selectBox', + defaultValue => 302, + options => { + 302 => $i18n->get('302 Moved Temporarily'), + 301 => $i18n->get('301 Moved Permanently'), + } + }, + }, + }); return $class->SUPER::definition($session,$definition); } - -#------------------------------------------------------------------- -sub www_edit { - my $self = shift; - return $self->session->privilege->insufficient() unless $self->canEdit; - return $self->session->privilege->locked() unless $self->canEditIfLocked; - return $self->getAdminConsole->render($self->getEditForm->print, $self->addEditLabel); -} - #------------------------------------------------------------------- =head2 exportHtml_view @@ -108,6 +110,32 @@ sub exportHtml_view { #------------------------------------------------------------------- +=head2 view ( ) + +Display the redirect url when in admin mode. + +=cut + +sub view { + my $self = shift; + if ($self->session->var->get("adminOn")) { + return $self->getToolbar.' '.$self->getTitle.' '.$self->get('redirectUrl'); + } + else { + return ""; + } +} + +#------------------------------------------------------------------- +sub www_edit { + my $self = shift; + return $self->session->privilege->insufficient() unless $self->canEdit; + return $self->session->privilege->locked() unless $self->canEditIfLocked; + return $self->getAdminConsole->render($self->getEditForm->print, $self->addEditLabel); +} + +#------------------------------------------------------------------- + =head2 www_view A web executable method that redirects the user to the specified page, or displays the edit interface when admin mode is enabled. @@ -129,7 +157,7 @@ sub www_view { ',$i18n->get("assetName")); } unless ($url eq $self->get("url")) { - $self->session->http->setRedirect($url); + $self->session->http->setRedirect($url,$self->get('redirectType')); return undef; } return $i18n->get('self_referential'); diff --git a/lib/WebGUI/Session/Http.pm b/lib/WebGUI/Session/Http.pm index 7aa96e81c..4012197ca 100644 --- a/lib/WebGUI/Session/Http.pm +++ b/lib/WebGUI/Session/Http.pm @@ -16,6 +16,7 @@ package WebGUI::Session::Http; use strict; +use WebGUI::Utility; =head1 NAME @@ -231,7 +232,7 @@ Returns a boolean value indicating whether the current page will redirect to som sub isRedirect { my $self = shift; - return ($self->getStatus() eq "302"); + return isIn($self->getStatus(), qw(302 301)); } @@ -279,7 +280,7 @@ sub sendHeader { my %params; if ($self->isRedirect()) { $request->headers_out->set(Location => $self->getRedirectLocation); - $request->status(301); + $request->status($self->getStatus); } else { $request->content_type($self->getMimeType); my $cacheControl = $self->getCacheControl; @@ -494,7 +495,7 @@ sub setNoHeader { #------------------------------------------------------------------- -=head2 setRedirect ( url ) +=head2 setRedirect ( url, [ type ] ) Sets the necessary information in the HTTP header to redirect to another URL. @@ -502,16 +503,21 @@ Sets the necessary information in the HTTP header to redirect to another URL. The URL to redirect to. +=head3 type + +Defaults to 302 (temporary redirect), but you can optionally set 301 (permanent redirect). + =cut sub setRedirect { my $self = shift; my $url = shift; + my $type = shift || 302; my @params = $self->session->form->param; return undef if ($url eq $self->session->url->page() && scalar(@params) < 1); # prevent redirecting to self $self->session->errorHandler->info("Redirecting to $url"); $self->setRedirectLocation($url); - $self->setStatus("302", "Redirect"); + $self->setStatus($type, "Redirect"); $self->session->style->setMeta({"http-equiv"=>"refresh",content=>"0; URL=".$url}); } diff --git a/lib/WebGUI/i18n/English/Asset_Redirect.pm b/lib/WebGUI/i18n/English/Asset_Redirect.pm index ae64a2697..9bd811a2e 100644 --- a/lib/WebGUI/i18n/English/Asset_Redirect.pm +++ b/lib/WebGUI/i18n/English/Asset_Redirect.pm @@ -8,6 +8,26 @@ our $I18N = { lastUpdated => 0, }, + '302 Moved Temporarily' => { + message => q|302 Moved Temporarily|, + lastUpdated => 0, + }, + + '301 Moved Permanently' => { + message => q|301 Moved Permanently|, + lastUpdated => 0, + }, + + 'Redirect Type' => { + message => q|Redirect Type|, + lastUpdated => 0, + }, + + 'redirect type description' => { + message => q|The type of HTTP header that the Redirect asset will send.|, + lastUpdated => 0, + }, + 'go to the redirect url' => { message => q|Go to the redirect URL.|, lastUpdated => 0,