[Rt-commit] rt branch, 5.0/group-default-values, created. rt-5.0.0-189-gc860b02804

Craig Kaiser craig at bestpractical.com
Mon Jan 4 14:19:45 EST 2021


The branch, 5.0/group-default-values has been created
        at  c860b028045b690aab67414ff35687f04a1176ac (commit)

- Log -----------------------------------------------------------------
commit 73c0b4fa7d6dded7094897f2e1a4b73b35895871
Author: craig kaiser <craig at bestpractical.com>
Date:   Mon Jan 4 13:43:01 2021 -0500

    Add page for setting groups default values

diff --git a/lib/RT/Interface/Web/MenuBuilder.pm b/lib/RT/Interface/Web/MenuBuilder.pm
index c93e0d92c0..20f65e70fd 100644
--- a/lib/RT/Interface/Web/MenuBuilder.pm
+++ b/lib/RT/Interface/Web/MenuBuilder.pm
@@ -1041,6 +1041,7 @@ sub _BuildAdminMenu {
     );
     $groups->child( select => title => loc('Select'), path => "/Admin/Groups/" );
     $groups->child( create => title => loc('Create'), path => "/Admin/Groups/Modify.html?Create=1" );
+    $groups->child( 'default-values' => title => loc('Default Values'), path => "/Admin/Groups/DefaultValues.html" );
 
     my $queues = $admin->child( queues =>
         title       => loc('Queues'),
@@ -1415,9 +1416,10 @@ sub _BuildAdminMenu {
                               path        => "/Admin/Groups/ModifyLinks.html?id=" . $obj->id,
                               description => loc("Group links"),
                 );
-                $page->child( 'group-rights' => title => loc('Group Rights'), path => "/Admin/Groups/GroupRights.html?id=" . $obj->id );
-                $page->child( 'user-rights'  => title => loc('User Rights'),  path => "/Admin/Groups/UserRights.html?id=" . $obj->id );
-                $page->child( history        => title => loc('History'),      path => "/Admin/Groups/History.html?id=" . $obj->id );
+                $page->child( 'group-rights'   => title => loc('Group Rights'),   path => "/Admin/Groups/GroupRights.html?id=" . $obj->id );
+                $page->child( 'user-rights'    => title => loc('User Rights'),    path => "/Admin/Groups/UserRights.html?id=" . $obj->id );
+                $page->child( history          => title => loc('History'),        path => "/Admin/Groups/History.html?id=" . $obj->id );
+
                 $page->child( 'summary'   =>
                               title       => loc("Group Summary"),
                               path        => "/Group/Summary.html?id=" . $obj->id,

commit c860b028045b690aab67414ff35687f04a1176ac
Author: craig kaiser <craig at bestpractical.com>
Date:   Mon Jan 4 14:19:00 2021 -0500

    Add default values for groups

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index ae1ac0bd19..5390b2c19c 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -2175,7 +2175,7 @@ sub BasedOnObj {
 sub SupportDefaultValues {
     my $self = shift;
     return 0 unless $self->id;
-    return 0 unless $self->LookupType =~ /RT::(?:Ticket|Transaction|Asset)$/;
+    return 0 unless $self->LookupType =~ /RT::(?:Ticket|Transaction|Asset|Group)$/;
     return $self->Type !~ /^(?:Image|Binary)$/;
 }
 
@@ -2185,7 +2185,15 @@ sub DefaultValues {
         Object => RT->System,
         @_,
     );
-    my $attr = $args{Object}->FirstAttribute('CustomFieldDefaultValues');
+
+    my $attr;
+    if ( ref $args{'Object'} eq 'RT::Group' ) {
+        $attr = $self->FirstAttribute('GroupsDefaultValue');
+    }
+    else {
+        $attr = $args{Object}->FirstAttribute('CustomFieldDefaultValues');
+    }
+
     my $values;
     $values = $attr->Content->{$self->id} if $attr && $attr->Content;
     return $values if defined $values;
diff --git a/lib/RT/Group.pm b/lib/RT/Group.pm
index ec00829a31..4c7b22bb26 100644
--- a/lib/RT/Group.pm
+++ b/lib/RT/Group.pm
@@ -1722,6 +1722,81 @@ sub PostInflate {
     );
 }
 
+sub DefaultValue {
+    my $self  = shift;
+    my $field = shift;
+
+    my $attr = $field->FirstAttribute( 'GroupsDefaultValue' );
+
+    return undef unless $attr && $attr->Content;
+    return $attr->Content->{$field};
+}
+
+sub SetDefaultValue {
+    my $self = shift;
+    my %args = (
+        Value => undef,
+        Field => undef,
+        @_
+    );
+    my $attr = $args{'Field'}->FirstAttribute( 'GroupsDefaultValue' );
+
+    my ($old_value, $old_content, $new_value);
+    if ( $attr && $attr->Content ) {
+        $old_content = $attr->Content;
+        $old_value   = $old_content->{ $args{'Field'}->id };
+    }
+
+    unless ( defined $old_value && length $old_value ) {
+        $old_value = $self->loc('(no value)');
+    }
+
+    $new_value = $args{Value};
+    unless ( defined $new_value && length $new_value ) {
+        $new_value = $self->loc( '(no value)' );
+    }
+
+    return 1 if $new_value eq $old_value;
+
+    my ($ret, $msg);
+    if ( $attr) {
+        ($ret, $msg) = $args{'Field'}->SetAttribute(
+            Name       => 'GroupsDefaultValue',
+            Content    => { %{ $old_content || {} }, $args{'Field'}->Id => $args{Value} },
+            Object     => $args{'Field'},
+       );
+    }
+    else {
+      $attr = RT::Attribute->new( $self->CurrentUser );
+      ($ret, $msg) = $attr->Create(
+          Name       => 'GroupsDefaultValue',
+          Content    => { %{ $old_content || {} }, $args{'Field'}->Id => $args{Value} },
+          Object     => $args{'Field'},
+          ObjectType => 'RT::Group'
+      );
+    }
+
+    if ( $ret ) {
+        return ( $ret, $self->loc( 'Default value of [_1] changed from [_2] to [_3]', $args{'Field'}->Name, $old_value, $new_value ) );
+    }
+    else {
+        return ( $ret, $self->loc( "Can't change default value of [_1] from [_2] to [_3]: [_4]", $args{'Field'}->Name, $old_value, $new_value, $msg ) );
+    }
+}
+
+sub GroupCustomFields {
+    my $self = shift;
+
+    my $cfs = RT::CustomFields->new( $self->CurrentUser );
+    if ( $self->CurrentUserHasRight('SeeGroup') ) {
+        $cfs->SetContextObject( $self );
+        $cfs->LimitToGlobalOrObjectId( $self->Id );
+        $cfs->LimitToLookupType( 'RT::Group' );
+        $cfs->ApplySortOrder;
+    }
+    return ($cfs);
+}
+
 # If this group represents the members of a custom role, then return
 # the RT::CustomRole object. Otherwise, return undef
 sub _CustomRoleObj {
diff --git a/share/html/Admin/Groups/DefaultValues.html b/share/html/Admin/Groups/DefaultValues.html
new file mode 100644
index 0000000000..feb3707f38
--- /dev/null
+++ b/share/html/Admin/Groups/DefaultValues.html
@@ -0,0 +1,142 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2020 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (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
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# 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.
+%#
+%#
+%# 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 }}}
+<& /Admin/Elements/Header, Title => $title &>
+<& /Elements/Tabs &>
+<& /Elements/ListActions, actions => \@results &>
+
+<form method="post" action="DefaultValues.html" name="ModifyDefaultValues" id="ModifyDefaultValues" class="mx-auto max-width-lg">
+
+% if ( RT->Config->ObjectHasCustomFieldGrouping( Object => $GroupObject, Grouping => 'Dates' ) ) {
+  <&|/Widgets/TitleBox, title => loc("Dates"), content_class => 'mx-auto width-sm' &>
+    <& /Elements/EditCustomFields,
+        Object       => $GroupObject,
+        CustomFields => RT::Group->GroupCustomFields->LimitToDefaultValuesSupportedTypes,
+        Grouping     => 'Dates',
+    &>
+  </&>
+% }
+
+% if ( RT->Config->ObjectHasCustomFieldGrouping( Object => $GroupObject, Grouping => 'People' ) ) {
+  <&|/Widgets/TitleBox, title => loc("People"), content_class => 'mx-auto width-sm' &>
+    <& /Elements/EditCustomFields,
+        Object       => $GroupObject,
+        CustomFields => RT::Group->GroupCustomFields->LimitToDefaultValuesSupportedTypes,
+        Grouping     => 'People',
+    &>
+  </&>
+% }
+
+% if ( RT->Config->ObjectHasCustomFieldGrouping( Object => $GroupObject, Grouping => 'Links' ) ) {
+  <&|/Widgets/TitleBox, title => loc("Links"), content_class => 'mx-auto width-sm' &>
+    <& /Elements/EditCustomFields,
+        Object       => $GroupObject,
+        CustomFields => RT::Group->GroupCustomFields->LimitToDefaultValuesSupportedTypes,
+        Grouping     => 'Links',
+    &>
+  </&>
+% }
+
+<& /Elements/EditCustomFieldCustomGroupings, CustomFieldGenerator => sub { $GroupObject->GroupCustomFields->LimitToDefaultValuesSupportedTypes }, Object => $GroupObject, Groupings => \@groupings &>
+
+<div class="form-row">
+  <div class="col-12">
+    <& /Elements/Submit, Name => 'Update', Label => loc('Save Changes') &>
+  </div>
+</div>
+<div class="form-row">
+  <div class="col-12">
+    <& /Elements/Submit, Name => 'Reset', Label => loc('Reset Custom Field Values to Default') &>
+  </div>
+</div>
+</form>
+
+<%INIT>
+my $GroupObject = RT::Group->new( $session{'CurrentUser'} );
+
+my $title = loc( 'Default Values for groups' );
+my @groupings = ( RT::CustomField->CustomGroupings( 'RT::Group' ), '' );
+
+$m->callback( CallbackName => 'Init', ARGSRef => \%ARGS, Title => \$title, Groupings => \@groupings );
+
+my @results;
+if ( $ARGS{Reset} ) {
+    my $cfs = RT::Group->GroupCustomFields->LimitToDefaultValuesSupportedTypes;
+    while ( my $cf = $cfs->Next ) {
+      my ($ret, $msg) = $cf->SetDefaultValues(
+            Object => $GroupObject,
+            Values => undef,
+        );
+    }
+    push @results, "Custom Field default values are reset";
+}
+elsif ( $ARGS{Update} ) {
+    my $cfs = _ParseObjectCustomFieldArgs( \%ARGS )->{'RT::Group'}{0};
+    for my $cf_id ( keys %$cfs ) {
+        # In the case of inconsistent CFV submission,
+        # we'll get the 1st grouping in the hash, alphabetically
+        my ($ret, $grouping_name) = _ValidateConsistentCustomFieldValues( $cf_id, $cfs->{$cf_id} );
+
+        my $grouping = $cfs->{$cf_id}{$grouping_name};
+        my $value = $grouping->{Value} // $grouping->{Values};
+
+        my $cf = RT::CustomField->new( $session{CurrentUser} );
+        $cf->Load( $cf_id );
+        if ( $cf->id && $cf->SupportDefaultValues ) {
+            my ($ret, $msg) = $GroupObject->SetDefaultValue (
+                Field  => $cf,
+                Value  => $value,
+            );
+            push @results, $msg;
+        }
+    }
+}
+
+MaybeRedirectForResults(
+    Actions   => \@results,
+);
+</%INIT>

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


More information about the rt-commit mailing list