Merge branch 'master' of github.com:plainblack/webgui

This commit is contained in:
JT Smith 2009-10-29 14:00:40 -05:00
commit fb864a4620
20 changed files with 578 additions and 90 deletions

View file

@ -0,0 +1,83 @@
package WebGUI::Content::SetLanguage;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2009 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use WebGUI::Session;
use WebGUI::International;
=head1 NAME
Package WebGUI::Content::SetLanguage
=head1 DESCRIPTION
Sets or delete an scratch variable that overrides the profile field language
=head1 SYNOPSIS
use WebGUI::Content::SetLanguage;
WebGUI::Content::SetLanguage::handler();
=head1 SUBROUTINES
These subroutines are available from this package:
handler
=cut
#-------------------------------------------------------------
=head2 handler ( session, op, setLanguage )
sets or delete scratch variable in a session and returns undef
=head3 session
The current WebGUI::Session object.
=head3 op
op should be setLanguage to call the handler
=head3 language
language should be an installed language or delete
=cut
sub handler {
my ($session) = @_;
return undef unless $session->form->get('op') eq 'setLanguage';
my $language = $session->form->get('language');
#check whether a language has been given in the url
if (!$language) {
$session->log->error('There is no language given to this method');
return undef;
}
#make it possible to delete the language scratch variable from the session
if ($language eq 'delete' ) {
$session->scratch->removeLanguageOverride;
return undef;
}
#set a scratch variable language or throw error if language is not installed
else {
return $session->scratch->setLanguageOverride($language);
}
}
1;

View file

@ -0,0 +1,37 @@
package WebGUI::Help::Macro_PickLanguage;
use strict;
our $HELP = {
'template variables' => {
title => 'picklanguage title',
body => '',
fields =>[],
'variables' => [
{
name => "lang_loop",
variables => [
{
name => "language_lang",
},
{
name => "language_langAbbr",
},
{
name => "language_langAbbrLoc",
},
{
name => "language_langEng",
},
],
},
],
related => [
],
},
};
1; ##All perl modules must return true
#vim:ft=perl

View file

@ -284,7 +284,7 @@ Specify a default language. Defaults to user preference or "English".
sub new {
my ($class, $session, $namespace, $language) = @_;
$namespace ||= 'WebGUI';
$language ||= $session->user->profileField('language');
$language ||= $session->scratch->getLanguageOverride() || $session->user->profileField('language');
my $self =
bless {
_session => $session,

View file

@ -0,0 +1,65 @@
package WebGUI::Macro::PickLanguage; # edit this line to match your own macro name
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use strict;
=head1 NAME
Package WebGUI::Macro::PickLanguage
=head1 DESCRIPTION
This macro makes a link for each installed language so when clicked the SetLanguage contetntHandler is called and sets the language in the scratch. The link text is the label from the language.
=head2 process( $session )
The main macro class, Macro.pm, will call this subroutine and pass it
=over 4
=item *
A session variable
=item templateId
This macro takes a templateId to show the links
=back
=cut
#-------------------------------------------------------------------
sub process {
my $session = shift;
my $templateId = shift || "_aE16Rr1-bXBf8SIaLZjCg";
my $template = WebGUI::Asset::Template->new($session, $templateId);
return "Could not instanciate template with id [$templateId]" unless $template;
my $i18n = WebGUI::International->new($session);
my $languages = $i18n->getLanguages();
my $vars = {'lang_loop' => []};
foreach my $language ( keys %$languages ) {
my $langVars = {};
$langVars->{ 'language_url' } = '?op=setLanguage;language=' . $language ;
$langVars->{ 'language_lang' } = $i18n->getLanguage($language , 'label');
$langVars->{ 'language_langAbbr' } = $i18n->getLanguage($language, 'languageAbbreviation');
$langVars->{ 'language_langAbbrLoc' } = $i18n->getLanguage($language, 'locale');
$langVars->{ 'language_langEng' } = $language;
push(@{$vars->{lang_loop}}, $langVars);
}
return $template->process($vars);
}
1;
#vim:ft=perl

View file

@ -15,6 +15,7 @@ package WebGUI::Session::Scratch;
=cut
use strict;
use WebGUI::International;
=head1 NAME
@ -160,6 +161,19 @@ sub get {
return $self->{_data}{$var};
}
#-------------------------------------------------------------------
=head2 getLanguageOverride ()
Retrieves the language of the session scratch
=cut
sub getLanguageOverride {
my $self = shift;
my $languageOverride = $self->session->scratch->get('language');
return $languageOverride;
}
#-------------------------------------------------------------------
@ -180,7 +194,18 @@ sub new {
bless {_session=>$session, _data=>$data}, $class;
}
#-------------------------------------------------------------------
=head2 removeLanguageOverride()
Removes the language scratch variable from the session
=cut
sub removeLanguageOverride {
my $self = shift;
$self->session->scratch->delete('language');
}
#-------------------------------------------------------------------
=head2 session ( )
@ -220,5 +245,30 @@ sub set {
$self->session->db->write("insert into userSessionScratch (sessionId, name, value) values (?,?,?) on duplicate key update value=VALUES(value)", [$self->session->getId, $name, $value]);
}
#----------------------------------------------------------------------
=head2 setLanguageOverride ( language )
Sets a scratch variable language in the session if the language is installed
=head3 language
The language that should be set into the session
=cut
sub setLanguageOverride {
my $self = shift;
my $language = shift;
my $i18n = WebGUI::International->new($self->session);
if($i18n->getLanguages()->{$language}) {
$self->session->scratch->set("language",$language);
return undef;
}
else {
$self->session->log->error("Language $language is not installed in this site");
return undef;
}
}
1;

View file

@ -0,0 +1,48 @@
package WebGUI::i18n::English::Macro_PickLanguage; ##Be sure to change the package name to match the filename
use strict; ##Required for all good Perl::Critic compliant code
our $I18N = { ##hashref of hashes
'picklanguage title' => {
message => q|PickLanguage macro template variables|,
lastUpdated => 1131394070,
context => q|Title of the help object|
},
'lang_loop' => { ##key that will be used to reference this entry. Do not translate this.
message => q|A loop that contains all installed languages|,
lastUpdated => 1131394070, #seconds from the epoch
context => q|A template loop|
},
'language_lang' => {
message => q|The name of the language in that language.|,
lastUpdated => 1131394072,
context => q|A template variable to show the name of the language|
},
'language_langAbbr' => {
message => q|An standard code for the language, for instance "en".|,
lastUpdated => 1131394072,
context => q|A label of the language to use in the template|
},
'language_langAbbrLoc' => {
message => q|An standard abbreviated label for the language, for instance "US".|,
lastUpdated => 1131394072,
context => q|A label of the language to use in the template|
},
'language_langEng' => {
message => q|The English name of the language.|,
lastUpdated => 1131394072,
context => q|A label of the language to use in the template|
},
'language_url' => {
message => q|The url that sets the WebGUI language to the selected language.|,
lastUpdated => 1131394072,
context => q|The url to change languages|
},
};
1;
#vim:ft=perl