[Rt-commit] r9865 - in rt/branches/3.6-EXPERIMENTAL-ABERDEEN: html/Admin/Elements lib/RT/Extension

audreyt at bestpractical.com audreyt at bestpractical.com
Mon Dec 10 12:28:54 EST 2007


Author: audreyt
Date: Mon Dec 10 12:28:53 2007
New Revision: 9865

Modified:
   rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/Elements/RuleManagerTabs
   rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/Modify.html
   rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/index.html
   rt/branches/3.6-EXPERIMENTAL-ABERDEEN/lib/RT/Extension/RuleManager.pm

Log:
* Basic rule creation and editing.

Modified: rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/Elements/RuleManagerTabs
==============================================================================
--- rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/Elements/RuleManagerTabs	(original)
+++ rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/Elements/RuleManagerTabs	Mon Dec 10 12:28:53 2007
@@ -53,9 +53,9 @@
 <%INIT>
 my $tabs;
 if ($id) {
-    $tabs->{'this'} = {
-        title => eval { $RuleObj->Name },
-        path => "Admin/Users/Modify.html?id=".$id,
+    $tabs->{'R'} = {
+        title     => loc('Rule #[_1]', $id),
+        path      => "Admin/Users/Modify.html?id=".$id,
     };
 }
 if (

Modified: rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/Modify.html
==============================================================================
--- rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/Modify.html	(original)
+++ rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/Modify.html	Mon Dec 10 12:28:53 2007
@@ -46,16 +46,14 @@
 %# 
 %# END BPS TAGGED BLOCK }}}
 <& /Admin/Elements/Header, Title => $title &>
-<& /Admin/Elements/RuleTabs, id => $RuleObj->id, 
+<& /Admin/Elements/RuleManagerTabs, id => $RuleObj ? $RuleObj->Id : 0,
     RuleObj => $RuleObj,
     current_tab => $current_tab, 
     Title => $title &>
 <& /Elements/ListActions, actions => \@results &>
 
-
-
 <form action="<%$RT::WebPath%>/Admin/RuleManager/Modify.html" method="post">
-%if ($Create ) { 
+% if ($Create) {
 <input type="hidden" class="hidden" name="id" value="new" />
 % } else {
 <input type="hidden" class="hidden" name="id" value="<%$RuleObj->Id%>" />
@@ -83,7 +81,8 @@
 if ($Create) {
     $current_tab = 'Admin/RuleManager/Modify.html?Create=1';
     $title = loc("Create a rule");
-} else {
+}
+else {
     if ($id eq 'new') {
         $RuleObj = RT::Extension::RuleManager->create(Name => $Name);
         push @results, loc('Rule created.');
@@ -93,11 +92,13 @@
                 || RT::Extension::RuleManager->named($Name)
                 || Abort("Couldn't load rule '$Name'");
     }
-    $title = loc('Editing Rule [_1]', $RuleObj->Name);
+    $title = loc('Editing Rule #[_1]', $RuleObj->id);
     
     $current_tab = 'Admin/RuleManager/Modify.html?id='.$RuleObj->id;
 }
-if ($RuleObj->Id) {
+
+if ($RuleObj and $RuleObj->Id) {
+    $RuleObj->UpdateRecordObject(\%ARGS);
 }
 </%INIT>
 

Modified: rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/index.html
==============================================================================
--- rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/index.html	(original)
+++ rt/branches/3.6-EXPERIMENTAL-ABERDEEN/html/Admin/RuleManager/index.html	Mon Dec 10 12:28:53 2007
@@ -45,12 +45,11 @@
 %# those contributions and any derivatives thereof.
 %# 
 %# END BPS TAGGED BLOCK }}}
-<& /Admin/Elements/Header, Title => loc('Select a rule') &>
+<& /Admin/Elements/Header, Title => loc('Rule Manager') &>
 <& /Admin/Elements/RuleManagerTabs, current_tab => 'Admin/RuleManager/', 
     current_subtab => 'Admin/RuleManager/',
-    Title => loc('Rule Manager') &>
+    Title => loc('All Rules') &>
 
-<h1><&|/l&>All Rules</&></h1>
 <pre>
 - List rules by order, with move up/down buttons as well as deletion.
 - Clicking on a rule brings Modify.html with the usual controls:

Modified: rt/branches/3.6-EXPERIMENTAL-ABERDEEN/lib/RT/Extension/RuleManager.pm
==============================================================================
--- rt/branches/3.6-EXPERIMENTAL-ABERDEEN/lib/RT/Extension/RuleManager.pm	(original)
+++ rt/branches/3.6-EXPERIMENTAL-ABERDEEN/lib/RT/Extension/RuleManager.pm	Mon Dec 10 12:28:53 2007
@@ -1,15 +1,34 @@
 package RT::Extension::RuleManager;
+use strict;
+use warnings;
+use RT::Template;
 use YAML::Syck '1.00';
+use constant RuleManagerTemplate => 'Rule Manager Template';
+use constant RuleClass => 'RT::Extension::RuleManager::Rule';
 
 sub create {
     my $self = shift;
+    my %args = @_;
+    my $rec = bless {
+        map {
+            $_ => defined $args{$_} ? $args{$_} : ''
+        } RT::Extension::RuleManager::Rule::Fields()
+    } => RuleClass;
+
+    my $rules = $self->rules;
+    $rec->{_root} = $rules;
+    $rec->{_pos} = 0+@$rules;
+    push @$rules, $rec;
+    $self->_save($rules);
+
+    return $rec;
 }
 
 sub load {
     my $self  = shift;
     my $id    = shift;
     my $rules = $self->rules;
-    return undef if $id <= 0 or $id >= @$rules;
+    return undef if $id <= 0 or $id > @$rules;
     return $rules->[$id-1];
 }
 
@@ -17,7 +36,7 @@
     my $self  = shift;
     my $id    = shift;
     my $rules = $self->rules;
-    return undef if $id <= 1 or $id >= @$rules;
+    return undef if $id <= 1 or $id > @$rules;
     @{$rules}[$id-1, $id-2] = @{$rules}[$id-2, $id-1];
     $rules->[$id-1]{_pos} = $id-1;
     $rules->[$id-2]{_pos} = $id-2;
@@ -28,7 +47,7 @@
     my $self = shift;
     my $name = shift;
     foreach my $rule (@{$self->rules}) {
-        return $rule if $rule->name eq $name;
+        return $rule if $rule->Name eq $name;
     }
     return undef;
 }
@@ -39,9 +58,75 @@
     for my $i (0..$#$rules) {
         $rules->[$i]{_pos} = $i;
         $rules->[$i]{_root} = $rules;
-        bless $rules->[$i] => 'RT::Extension::RuleManager::Rule';
+        bless $rules->[$i] => RuleClass;
     }
     return $rules;
 }
 
+sub _load {
+    my $self = shift;
+    return Load($self->_template->Content);
+}
+
+sub _save {
+    my $self = shift;
+    my $rules = shift;
+    my @to_save;
+    foreach my $rule (@$rules) {
+        my %this = %$rule;
+        delete $this{_pos};
+        delete $this{_root};
+        push @to_save, \%this;
+    }
+    return $self->_template->SetContent(Dump(\@to_save));
+}
+
+# Find our own, special RT::Template.  If one does not exist, create it.
+sub _template {
+    my $self = shift;
+    my $template = RT::Template->new($RT::SystemUser);
+    $template->Load(RuleManagerTemplate);
+    if (!$template->Id) {
+        $template->Create(
+            Name        => RuleManagerTemplate,
+            Description => RuleManagerTemplate,
+            Content     => "--- []\n",
+            Queue       => 0,
+        );
+    }
+    return $template;
+}
+
+package RT::Extension::RuleManager::Rule;
+
+use constant Fields => qw( Name Field Pattern Handler Argument );
+
+sub id { $_[0]{_pos}+1 }
+sub Id { $_[0]{_pos}+1 }
+
+sub UpdateRecordObject {
+    my $self = shift;
+    my $args = shift;
+    my $updated;
+    foreach my $field (Fields) {
+        exists $args->{$field} or next;
+        $updated = 1;
+        $self->{$field} = $args->{$field};
+    }
+    RT::Extension::RuleManager->_save($self->{_root}) if $updated;
+}
+
+BEGIN {
+    no strict 'refs';
+    no warnings 'uninitialized';
+    eval join '', map {qq[
+        sub $_ { \$_[0]{'$_'} }
+        sub Set$_ {
+            return if \$_[0]{'$_'} eq \$_[1];
+            \$_[0]{'$_'} = \$_[1];
+            RT::Extension::RuleManager->_save(\$_[0]{_root});
+        }
+    ]} Fields;
+}
+
 1;


More information about the Rt-commit mailing list