import rt 3.6.10
[freeside.git] / rt / bin / rt-crontool
index cdbc3cb..3171d11 100644 (file)
@@ -1,9 +1,15 @@
 #!/usr/bin/perl
-# BEGIN LICENSE BLOCK
+# BEGIN BPS TAGGED BLOCK {{{
 # 
-# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
+# COPYRIGHT:
+#  
+# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
+#                                          <jesse@bestpractical.com>
 # 
-# (Except where explictly superceded by other copyright notices)
+# (Except where explicitly superseded by other copyright notices)
+# 
+# 
+# LICENSE:
 # 
 # This work is made available to you under the terms of Version 2 of
 # the GNU General Public License. A copy of that license should have
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
 # 
-# Unless otherwise specified, all modifications, corrections or
-# extensions to this work which alter its source code become the
-# property of Best Practical Solutions, LLC when submitted for
-# inclusion in the work.
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 # 
 # 
-# END LICENSE BLOCK
-
+# CONTRIBUTION SUBMISSION POLICY:
+# 
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+# 
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+# 
+# END BPS TAGGED BLOCK }}}
 use strict;
 use Carp;
 
-use lib ("/opt/rt3/lib", "/opt/rt3/local/lib");
+use lib ("/opt/rt3/local/lib", "/opt/rt3/lib");
 
 package RT;
 
@@ -45,9 +68,6 @@ RT::LoadConfig();
 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
 RT::Init();
 
-#Drop setgid permissions
-RT::DropSetGIDPermissions();
-
 #Get the current user all loaded
 my $CurrentUser = GetCurrentUser();
 
@@ -57,18 +77,27 @@ unless ( $CurrentUser->Id ) {
 }
 
 my ( $search, $condition, $action, $search_arg, $condition_arg, $action_arg,
-     $template_id, $help, $verbose );
-GetOptions( "search=s"        => \$search,
-            "search-arg=s"    => \$search_arg,
-            "condition=s"     => \$condition,
-            "condition-arg=s" => \$condition_arg,
-            "action-arg=s"    => \$action_arg,
-            "action=s"        => \$action,
-           "template-id=s"   => \$template_id,
-            "help"            => \$help,
-            "verbose|v"       => \$verbose );
-
-help() if $help;
+     $template_id, $transaction, $transaction_type, $help, $verbose );
+GetOptions( "search=s"           => \$search,
+            "search-arg=s"       => \$search_arg,
+            "condition=s"        => \$condition,
+            "condition-arg=s"    => \$condition_arg,
+            "action-arg=s"       => \$action_arg,
+            "action=s"           => \$action,
+            "template-id=s"      => \$template_id,
+            "transaction=s"      => \$transaction,
+            "transaction-type=s" => \$transaction_type,
+            "help"               => \$help,
+            "verbose|v"          => \$verbose );
+
+help() if $help or not $search or not $action;
+
+$transaction ||= 'first';
+unless ( $transaction =~ /^(first|last)$/i ) {
+    print STDERR loc("--transaction argument could be only 'first' or 'last'");
+    exit 1;
+}
+$transaction = lc($transaction) eq 'first'? 'ASC': 'DESC';
 
 # We _must_ have a search object
 load_module($search);
@@ -78,15 +107,21 @@ load_module($condition) if ($condition);
 # load template if specified
 my $template_obj;
 if ($template_id) {
-    $template_obj = RT::Template->new($RT::Nobody);
-    $template_obj->LoadById($template_id);
+    $template_obj = RT::Template->new($CurrentUser);
+    $template_obj->Load($template_id);
 }
+my $void_scrip = RT::Scrip->new( $CurrentUser );
+my $void_scrip_action = RT::ScripAction->new( $CurrentUser );
 
 #At the appointed time:
 
 #find a bunch of tickets
 my $tickets = RT::Tickets->new($CurrentUser);
-my $search  = $search->new( TicketsObj => $tickets, Argument => $search_arg );
+my $search  = $search->new(
+    TicketsObj  => $tickets,
+    Argument    => $search_arg,
+    CurrentUser => $CurrentUser
+);
 
 $search->Prepare();
 
@@ -95,12 +130,22 @@ my $tickets = $search->TicketsObj;
 
 #for each ticket we've found
 while ( my $ticket = $tickets->Next() ) {
-    print "\n" . $ticket->Id() . ": " if ($verbose);
+    print $ticket->Id() . ": " if ($verbose);
+
+    my $transaction = get_transaction($ticket);
+    print loc("Using transaction #[_1]...", $transaction->id)
+        if $verbose && $transaction;
 
     # perform some more advanced check
     if ($condition) {
-        my $condition_obj = $condition->new( TicketObj => $ticket,
-                                             Argument  => $condition_arg );
+        my $condition_obj = $condition->new(
+            TransactionObj => $transaction,
+            TicketObj      => $ticket,
+            ScripObj       => $void_scrip,
+            TemplateObj    => $template_obj,
+            Argument       => $condition_arg,
+            CurrentUser    => $CurrentUser,
+        );
 
         # if the condition doesn't apply, get out of here
 
@@ -109,9 +154,15 @@ while ( my $ticket = $tickets->Next() ) {
     }
 
     #prepare our action
-    my $action_obj = $action->new( TicketObj => $ticket,
-                                  TemplateObj => $template_obj,
-                                   Argument  => $action_arg );
+    my $action_obj = $action->new(
+        TicketObj      => $ticket,
+        TransactionObj => $transaction,
+        TemplateObj    => $template_obj,
+        Argument       => $action_arg,
+        ScripObj       => $void_scrip,
+        ScripActionObj => $void_scrip_action,
+        CurrentUser    => $CurrentUser,
+    );
 
     #if our preparation, move onto the next ticket
     next unless ( $action_obj->Prepare );
@@ -119,7 +170,27 @@ while ( my $ticket = $tickets->Next() ) {
 
     #commit our action.
     next unless ( $action_obj->Commit );
-    print loc("Action committed.") if ($verbose);
+    print loc("Action committed.\n") if ($verbose);
+}
+
+=head2 get_transaction
+
+Takes ticket and returns its transaction acording to command
+line arguments C<--transaction> and <--transaction-type>.
+
+=cut
+
+sub get_transaction {
+    my $ticket = shift;
+    my $txns = $ticket->Transactions;
+    $txns->OrderByCols(
+        { FIELD => 'Created', ORDER => $transaction },
+        { FIELD => 'id', ORDER => $transaction },
+    );
+    $txns->Limit( FIELD => 'Type', VALUE => $transaction_type )
+        if $transaction_type;
+    $txns->RowsPerPage(1);
+    return $txns->First;
 }
 
 # {{{ load_module 
@@ -181,6 +252,15 @@ sub help {
       . loc( "[_1] - An argument to pass to [_2]", "--action-argument", "--action" )
       . "\n";
     print "    "
+      . loc( "[_1] - Specify id of the template you want to use", "--template-id" )
+      . "\n";
+    print "    "
+      . loc( "[_1] - Specify if you want to use either 'first' or 'last' transaction", "--transaction" )
+      . "\n";
+    print "    "
+      . loc( "[_1] - Specify the type of a transaction you want to use", "--transaction-type" )
+      . "\n";
+    print "    "
       . loc( "[_1] - Output status updates to STDOUT", "--verbose" ) . "\n";
     print "\n";
     print "\n";
@@ -197,19 +277,17 @@ sub help {
       )
       . "\n\n";
 
-    print " bin/rt-cron-tool \\\n";
-    print
-      "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
-    print
-      "  --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
+    print " bin/rt-crontool \\\n";
+    print "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
+    print "  --condition RT::Condition::UntouchedInHours --condition-arg 4 \\\n";
     print "  --action RT::Action::SetPriority --action-arg 99 \\\n";
     print "  --verbose\n";
 
     print "\n";
-    print loc("Escalate tickets");
-    print "rt-crontool \\\n";
-    print " --search RT::Search::ActiveTicketsInQueue  --search-arg thequeuename \\\n";
-    print " --action RT::Action::EscalatePriority \\\n";
+    print loc("Escalate tickets"). "\n";
+    print " bin/rt-crontool \\\n";
+    print "  --search RT::Search::ActiveTicketsInQueue  --search-arg general \\\n";
+    print "  --action RT::Action::EscalatePriority\n";