FW: choosing template on the fly

Actually I already found a way to switch templates on the fl, so forget
about my question

Dimitry

Senoner Samuel wrote:

RTFM

Just to clarify: I think Samuel means the RTFM package, not that
the original poster needs to RTFM. (But I could be wrong :slight_smile:
http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-users

RT Developer and Administrator training is coming to LA, DC and Frankfurt
this spring and summer.
http://bestpractical.com/services/training.html

Sign up early, as class space is limited.

Dimitry Faynerman wrote:

Actually I already found a way to switch templates on the fl, so forget
about my question
Please, share short soluton summary with other.
Best regards. Ruslan.

Please, share short soluton summary with other.
Best regards. Ruslan.

Ok, so what I needed was to use different template depending on the uesrā€™s
language. So I modified lib/RT/Action/SendEmali.pm slightly.
First it checks the language from userā€™s profile. If, for example, itā€™s
Japanese, then it replaces current template with the template which name has
"(ja)" at the end. So, instead of, for example, ā€œAutoreplyā€ template, it
will try to find template ā€œAutoreply (ja)ā€.

Hereā€™s the code:

sub Prepare {
my $self = shift;

# This actually populates the MIME::Entity fields in the Template Object

unless ( $self->TemplateObj ) {
    $RT::Logger->warning("No template object handed to $self\n");
}

# my changes start here ===============================================
# Use japanese template (if exists) if user's language is japanese

my $UserObj = RT::User->new( $RT::SystemUser );
$UserObj->Load($self->TicketObj->Requestors->UserMembersObj->First->Id);

if (($self->TemplateObj) &&
    ((!$UserObj->Lang) || ($UserObj->Lang ne 'en'))) {
   my $rememberEnglishId = $self->TemplateObj->Id;
   $self->TemplateObj->Load(
        $self->TemplateObj->Name . " (" . $UserObj->Lang . ")");
   if (! $self->TemplateObj->Id) {
      $self->TemplateObj->Load($rememberEnglishId);
   }
}
# my changes end here ===============================================

unless ( $self->TransactionObj ) {
$RT::Logger->warning(ā€œNo transaction object handed to $self\nā€);

}

ā€¦
ā€¦

}

-Dimitry

I like your idea and solution, but I refactored it a little into _Local
overlay file which doesnā€™t touch Prepare sub. It use TemplateObj sub
cause it simple and right places to do such override.

Iā€™ve done only use_ok test. Could you test it? Then I or you can fill it
in wiki page.

Comment about strange code below.

			Best regards. Ruslan.

Dimitry Faynerman wrote:
[snip]

Hereā€™s the code:

sub Prepare {
my $self = shift;

# This actually populates the MIME::Entity fields in the Template Object

unless ( $self->TemplateObj ) {
    $RT::Logger->warning("No template object handed to $self\n");
}

# my changes start here ===============================================
# Use japanese template (if exists) if user's language is japanese

my $UserObj = RT::User->new( $RT::SystemUser );
$UserObj->Load($self->TicketObj->Requestors->UserMembersObj->First->Id);

if (($self->TemplateObj) &&
    ((!$UserObj->Lang) || ($UserObj->Lang ne 'en'))) {

This ā€˜if conditionā€™ will be true if $UserObj->Lang is not defined or
empty string. Refactored code has slightly different condtion.

   my $rememberEnglishId = $self->TemplateObj->Id;
   $self->TemplateObj->Load(
        $self->TemplateObj->Name . " (" . $UserObj->Lang . ")");
   if (! $self->TemplateObj->Id) {
      $self->TemplateObj->Load($rememberEnglishId);
   }
}
# my changes end here ===============================================

[snip]

SendEmail_Local.pm (505 Bytes)

Yep, youā€™re correct about the strange code
Iā€™ll retest it againFrom: Ruslan U. Zakirov [mailto:cubic@acronis.ru]
Sent: Tuesday, May 11, 2004 8:14 AM
To: Dimitry Faynerman
Cc: rt-users@lists.fsck.com
Subject: Re: FW: [rt-users] choosing template on the fly

I like your idea and solution, but I refactored it a little into _Local
overlay file which doesnā€™t touch Prepare sub. It use TemplateObj sub
cause it simple and right places to do such override.

Iā€™ve done only use_ok test. Could you test it? Then I or you can fill it
in wiki page.

Comment about strange code below.

			Best regards. Ruslan.

Dimitry Faynerman wrote:
[snip]

Hereā€™s the code:

sub Prepare {
my $self = shift;

# This actually populates the MIME::Entity fields in the Template

Object

unless ( $self->TemplateObj ) {
    $RT::Logger->warning("No template object handed to $self\n");
}

# my changes start here
# Use japanese template (if exists) if user's language is japanese

my $UserObj = RT::User->new( $RT::SystemUser );

$UserObj->Load($self->TicketObj->Requestors->UserMembersObj->First->Id);

if (($self->TemplateObj) &&
    ((!$UserObj->Lang) || ($UserObj->Lang ne 'en'))) {

This ā€˜if conditionā€™ will be true if $UserObj->Lang is not defined or
empty string. Refactored code has slightly different condtion.

   my $rememberEnglishId = $self->TemplateObj->Id;
   $self->TemplateObj->Load(
        $self->TemplateObj->Name . " (" . $UserObj->Lang . ")");
   if (! $self->TemplateObj->Id) {
      $self->TemplateObj->Load($rememberEnglishId);
   }
}
# my changes end here ===============================================

[snip]

-Dimitry

Sorry I couldnā€™t really understand how these local things work.
So, if Iā€™m correct, I need to put SendEmail_Local.pm somewhere (local/lib -
??? directory) so that the parts of the code inside SendEmail_Local.pm
overrides corresponding code from the ā€œoriginalā€ lib directory.

But what about the name of the subroutine TemplateObj??? How does this work?
Does RT try to override subroutine with the same name??

Thanks,

DimitryFrom: Ruslan U. Zakirov [mailto:cubic@acronis.ru]
Sent: Tuesday, May 11, 2004 8:14 AM
To: Dimitry Faynerman
Cc: rt-users@lists.fsck.com
Subject: Re: FW: [rt-users] choosing template on the fly

I like your idea and solution, but I refactored it a little into _Local
overlay file which doesnā€™t touch Prepare sub. It use TemplateObj sub
cause it simple and right places to do such override.

Iā€™ve done only use_ok test. Could you test it? Then I or you can fill it
in wiki page.

Comment about strange code below.

			Best regards. Ruslan.

Dimitry Faynerman wrote:
[snip]

Hereā€™s the code:

sub Prepare {
my $self = shift;

# This actually populates the MIME::Entity fields in the Template

Object

unless ( $self->TemplateObj ) {
    $RT::Logger->warning("No template object handed to $self\n");
}

# my changes start here
# Use japanese template (if exists) if user's language is japanese

my $UserObj = RT::User->new( $RT::SystemUser );

$UserObj->Load($self->TicketObj->Requestors->UserMembersObj->First->Id);

if (($self->TemplateObj) &&
    ((!$UserObj->Lang) || ($UserObj->Lang ne 'en'))) {

This ā€˜if conditionā€™ will be true if $UserObj->Lang is not defined or
empty string. Refactored code has slightly different condtion.

   my $rememberEnglishId = $self->TemplateObj->Id;
   $self->TemplateObj->Load(
        $self->TemplateObj->Name . " (" . $UserObj->Lang . ")");
   if (! $self->TemplateObj->Id) {
      $self->TemplateObj->Load($rememberEnglishId);
   }
}
# my changes end here ===============================================

[snip]

-Dimitry

Dimitry Faynerman wrote:

Sorry I couldnā€™t really understand how these local things work.
So, if Iā€™m correct, I need to put SendEmail_Local.pm somewhere (local/lib -
??? directory) so that the parts of the code inside SendEmail_Local.pm
overrides corresponding code from the ā€œoriginalā€ lib directory.
Read this to understand overlays:
Request Tracker Wiki

You should place it in
/path/to/rt/lib/RT/Actions/
/path/to/rt/local/lib/RT/Actions/

But what about the name of the subroutine TemplateObj??? How does this work?
TemplateObj inherited from RT/Action/Generic.pm
Itā€™s simple ā€œgetterā€.

Does RT try to override subroutine with the same name??
Yes, override.
Look into last lines in RT/Action/SendEmail.pm, RT does next things:
1) disable warning about override.
2) require module
3) produces error when it takes place only if overlay exists.

Ruslan.

Ruslan U. Zakirov wrote:

Dimitry Faynerman wrote:

Sorry I couldnā€™t really understand how these local things work.
So, if Iā€™m correct, I need to put SendEmail_Local.pm somewhere
(local/lib -
??? directory) so that the parts of the code inside SendEmail_Local.pm
overrides corresponding code from the ā€œoriginalā€ lib directory.

Read this to understand overlays:
Request Tracker Wiki

You should place it in
/path/to/rt/lib/RT/Actions/
/path/to/rt/local/lib/RT/Actions/

Oops, not into both dirs, use only one. local is preferred

But what about the name of the subroutine TemplateObj??? How does this
work?
TemplateObj inherited from RT/Action/Generic.pm
Itā€™s simple ā€œgetterā€.

Oh I seeā€¦ so TemplateObj was inherited from Generic package by default and
now you override it within SendEmail package.
Cool! Thanks, it worked!

Dimitry