[Rt-commit] rt branch, 5.0/custom-date-ranges-config-in-web-ui, updated. rt-4.4.4-727-gdf9552e65

Michel Rodriguez michel at bestpractical.com
Tue Mar 17 07:54:06 EDT 2020


The branch, 5.0/custom-date-ranges-config-in-web-ui has been updated
       via  df9552e65884a3e9834b85d6a294ecadaed38919 (commit)
       via  cf92b7c36bb6b5258b769bb8cb39e3c553ab01c1 (commit)
      from  c7b5c8a6512d86f98f6d5399b8ca598cad035f5e (commit)

Summary of changes:
 lib/RT/Config.pm                        | 51 +++++++++++++++++++++++++++++----
 lib/RT/Record.pm                        |  7 +----
 share/html/Widgets/Form/CustomDateRange | 15 ++++++++--
 3 files changed, 59 insertions(+), 14 deletions(-)

- Log -----------------------------------------------------------------
commit cf92b7c36bb6b5258b769bb8cb39e3c553ab01c1
Author: michel <michel at bestpractical.com>
Date:   Thu Mar 12 19:46:26 2020 +0100

    Added display of other users custom date ranges in user setting page.

diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index f4b173974..761dd6a92 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -2508,7 +2508,7 @@ sub AllCustomDateRanges {
     if ( $include_users ) {
         my $db_config = $self->Get( 'CustomDateRangesUI' );
         _CustomDateRangesSpecOrigin( $db_config, 'database', $cdr_origin );
-        $config = _MergeCustomDateRangesSpecs( $config, $db_config );
+        $config = MergeCustomDateRangesSpecs( $config, $db_config );
     }
     return ( $config, $cdr_origin );
 }
@@ -2530,7 +2530,7 @@ sub _CustomDateRangesSpecOrigin {
 }
 
 # merge all custom date ranges definitions into one
-sub _MergeCustomDateRangesSpecs {
+sub MergeCustomDateRangesSpecs {
     my @specs = @_;
     my $merged = {};
     foreach my $spec ( @specs ) {
diff --git a/share/html/Widgets/Form/CustomDateRange b/share/html/Widgets/Form/CustomDateRange
index 4918e83b4..5b3a7ce18 100644
--- a/share/html/Widgets/Form/CustomDateRange
+++ b/share/html/Widgets/Form/CustomDateRange
@@ -48,8 +48,9 @@
 
 <& /Elements/CustomDateRangesSpecs, Location => "config files", Config => $from_config  &>
 % if ( $UserPreferences ) {
-    <& /Elements/CustomDateRangesSpecs, Location => "database", Config => $from_db &>
-    <& /Widgets/Form/CustomDateRangesInput, Location => "user preferences", Config => $from_user &>
+      <& /Elements/CustomDateRangesSpecs, Location => "database", Config => $from_db &>
+      <& /Elements/CustomDateRangesSpecs, Location => "other users settings", Config => $from_other_users &>
+      <& /Widgets/Form/CustomDateRangesInput, Location => "user preferences", Config => $from_user &>
 % }
 % else {
     <& /Widgets/Form/CustomDateRangesInput, Location => "database", Config => $from_db &>
@@ -62,7 +63,20 @@ Abort(loc("Permission Denied")) unless $session{'CurrentUser'}->HasRight( Object
 my $from_config   = RT->Config->Get('CustomDateRanges');
 my $from_db       = RT->Config->Get('CustomDateRangesUI');
 
-my $from_user     = $UserPreferences ? $session{'CurrentUser'}->UserObj->Preferences("CustomDateRanges" ) : undef;
+my ( $from_other_users, $from_user );
+if( $UserPreferences ) {
+    my $current_user_id = $session{'CurrentUser'}->Id;
+
+    # get the content of all Pref-CustomDateRanges attributes for other users
+    my $attributes = RT::Attributes->new( RT->SystemUser );
+    $attributes->Limit( FIELD => 'Name', VALUE => 'Pref-CustomDateRanges' );
+    $attributes->Limit( FIELD => 'Creator', OPERATOR => '!=', VALUE => $current_user_id );
+    $attributes->OrderBy( FIELD => 'Creator' );
+    my @from_other_users = map { $_->Content } @{$attributes->ItemsArrayRef};
+    $from_other_users = RT::Config::MergeCustomDateRangesSpecs( @from_other_users );
+
+    $from_user = $session{'CurrentUser'}->UserObj->Preferences("CustomDateRanges" );
+}
 
 </%INIT>
 <%ARGS>

commit df9552e65884a3e9834b85d6a294ecadaed38919
Author: michel <michel at bestpractical.com>
Date:   Mon Mar 16 22:11:55 2020 +0100

    Functioning version of Custom Date Ranges in UI.
    
    Emits quite a few warnings.
    If a custom date range is already defined by an other user, doesn't
    display the name of the user.

diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index 761dd6a92..6b4a83f06 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -2499,24 +2499,64 @@ sub EnableExternalAuth {
     return;
 }
 
+# Custom Date Ranges methods
+
+# return a single CDR config, plus a structure that gives the origin for each definition 
+# meaning of the $include_level parameter
+# 0 => return only the config file definitions (CustomDateRanges option)
+#          used when editing the definitions in the DB
+# 1 => return the config file + the global DB definitions (CustomDateRangesUI option)
+#          + definition from other users
+#          used when editing the definitions for a single user
+# 2 => return all definitions
+#          used when actually using the CDR
 sub AllCustomDateRanges {
-    my ( $self, $include_users ) = @_;
+    my ( $self, $include_level ) = @_;
     my $config =  $self->Get( 'CustomDateRanges' );
     my $cdr_origin = {};
     _CustomDateRangesSpecOrigin( $config, 'config files', $cdr_origin );
 
-    if ( $include_users ) {
+    if ( $include_level > 0 ) {
         my $db_config = $self->Get( 'CustomDateRangesUI' );
         _CustomDateRangesSpecOrigin( $db_config, 'database', $cdr_origin );
         $config = MergeCustomDateRangesSpecs( $config, $db_config );
+        my @from_other_users = $self->CustomDateRangesFromOtherUsers;
+        foreach my $cdr ( @from_other_users ) {
+            _CustomDateRangesSpecOrigin( $cdr, 'other user config', $cdr_origin );
+        }
+        $config = MergeCustomDateRangesSpecs( $config, $db_config,  @from_other_users );
+    }
+    if ( $include_level > 1 ) {
+        my $user_cdr = $HTML::Mason::Commands::session{'CurrentUser'}->UserObj->Preferences("CustomDateRanges" );
+        if($user_cdr ) {
+            _CustomDateRangesSpecOrigin( $user_cdr, 'user', $cdr_origin );
+            $config = MergeCustomDateRangesSpecs( $config, $user_cdr );
+        }
     }
+
     return ( $config, $cdr_origin );
 }
 
+sub CustomDateRangesFromOtherUsers {
+
+    my $current_user = $HTML::Mason::Commands::session{'CurrentUser'} ;
+
+    my $attributes = RT::Attributes->new( RT->SystemUser );
+    $attributes->Limit( FIELD => 'Name', VALUE => 'Pref-CustomDateRanges' );
+    $attributes->Limit( FIELD => 'Creator', OPERATOR => '!=', VALUE => $current_user->Id );
+    $attributes->OrderBy( FIELD => 'Creator' );
+    my @from_other_users = map { $_->Content } @{$attributes->ItemsArrayRef};
+
+    return MergeCustomDateRangesSpecs( @from_other_users );
+}
+
 # stores the origin of the custom date ranges specs, for error reporting
 # $spec is a CDR config (from CustomDateRanges, CustomDateRangesUI or a user preferences)
 # $origin is a string that gets recorded
 # $cdr_origin is a hash to which the origin gets added
+# annotating the original $spec would be nicer, but a spec can be a simple string, as in 
+# 'Resolution Time' => 'Resolved - Created', 
+# which doesn't allow for the origin to be recorded
 sub _CustomDateRangesSpecOrigin {
     my( $spec, $origin, $cdr_origin ) = @_;
     return {} if ! CustomDateRangesSpecHasContent( $spec );
@@ -2582,6 +2622,7 @@ sub BuildCustomDateRangesUI {
             my $current_name = $current_names[$id];
             my $spec         = $current->{'RT::Ticket'}{$current_name};
             my $name         = $args->{"$id-name"};
+            next if ! $name;
 
             if ( $config && $config->{'RT::Ticket'}{$name} ) {
                 my $defined_in = $origin->{'RT::Ticket'}->{$name};
@@ -2599,7 +2640,7 @@ sub BuildCustomDateRangesUI {
 
             my $updated;
             for my $field (qw/from from_fallback to to_fallback/) {
-                next if ( $spec->{$field} // '' ) eq $args->{"$id-$field"};
+                next if ( $spec->{$field} // '' ) eq ( $args->{"$id-$field"} // '' );
                 if ((   $args->{"$id-$field"}
                         && RT::Ticket->_ParseCustomDateRangeSpec( $name, join ' - ', 'now', $args->{"$id-$field"} )
                     )
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 6fea220fb..40ec49984 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -2647,15 +2647,10 @@ sub CustomDateRanges {
 
     my %ranges;
 
-    if ( my $config = RT->Config->Get('CustomDateRanges') ) {
+    if ( my( $config, $origin) = RT->Config->AllCustomDateRanges( 2 ) ) {
         %ranges = %{ $config->{$type} } if $config->{$type};
     }
 
-    if ( my $configui =  RT->Config->Get('CustomDateRangesUI') ) {
-        for my $name ( keys %{ $configui->{$type} || {} } ) {
-            $ranges{$name} ||= $configui->{$type}{$name};
-        }
-    }
     return %ranges;
 }
 
diff --git a/share/html/Widgets/Form/CustomDateRange b/share/html/Widgets/Form/CustomDateRange
index 5b3a7ce18..db63c7fb9 100644
--- a/share/html/Widgets/Form/CustomDateRange
+++ b/share/html/Widgets/Form/CustomDateRange
@@ -68,12 +68,7 @@ if( $UserPreferences ) {
     my $current_user_id = $session{'CurrentUser'}->Id;
 
     # get the content of all Pref-CustomDateRanges attributes for other users
-    my $attributes = RT::Attributes->new( RT->SystemUser );
-    $attributes->Limit( FIELD => 'Name', VALUE => 'Pref-CustomDateRanges' );
-    $attributes->Limit( FIELD => 'Creator', OPERATOR => '!=', VALUE => $current_user_id );
-    $attributes->OrderBy( FIELD => 'Creator' );
-    my @from_other_users = map { $_->Content } @{$attributes->ItemsArrayRef};
-    $from_other_users = RT::Config::MergeCustomDateRangesSpecs( @from_other_users );
+    $from_other_users = RT::Config->CustomDateRangesFromOtherUsers( );
 
     $from_user = $session{'CurrentUser'}->UserObj->Preferences("CustomDateRanges" );
 }

-----------------------------------------------------------------------


More information about the rt-commit mailing list