[Rt-commit] rt branch, 5.0/group-default-values, created. rt-5.0.0-188-g593c3adf01
Craig Kaiser
craig at bestpractical.com
Mon Jan 4 14:48:47 EST 2021
The branch, 5.0/group-default-values has been created
at 593c3adf0140b4164f5f9691daf601a594295b65 (commit)
- Log -----------------------------------------------------------------
commit 5b078d5d524b1585e839e2157f0a12d621cc6c6b
Author: craig kaiser <craig at bestpractical.com>
Date: Mon Jan 4 14:47:44 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..83828d79b6 100644
--- a/lib/RT/Group.pm
+++ b/lib/RT/Group.pm
@@ -1722,6 +1722,91 @@ 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,
+ @_
+ );
+ unless ( $args{'Field'} ) {
+ RT::Logger->error( "Custom field object required to set group default value" );
+ return ( 0, $self->loc( 'An internal RT error has occurred. Your administrator can find more details in RT\'s log files.' ) );
+ }
+ 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 };
+ }
+
+ if ( defined $old_value && length $old_value ) {
+ $old_value = join ', ', @$old_value if ref $old_value eq 'ARRAY';
+ }
+ else {
+ $old_value = $self->loc('(no value)');
+ }
+
+ $new_value = $args{'Value'};
+ if ( defined $new_value && length $new_value ) {
+ $new_value = join ', ', @$new_value if ref $new_value eq 'ARRAY';
+ }
+ else {
+ $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 {
commit 593c3adf0140b4164f5f9691daf601a594295b65
Author: craig kaiser <craig at bestpractical.com>
Date: Mon Jan 4 14:48:00 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..50292fa5f9 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'),
@@ -1315,6 +1316,10 @@ sub _BuildAdminMenu {
$section->child( select => title => loc('Select'), path => "/Admin/$type/" );
$section->child( create => title => loc('Create'), path => "/Admin/$type/Modify.html?Create=1" );
+
+ if ( $type eq 'Groups' ) {
+ $section->child( 'default-values' => title => loc('Default Values'), path => "/Admin/Groups/DefaultValues.html" );
+ }
}
if ( $request_path =~ m{^/Admin/Queues} ) {
diff --git a/share/html/Admin/Groups/DefaultValues.html b/share/html/Admin/Groups/DefaultValues.html
new file mode 100644
index 0000000000..664f07f44c
--- /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 Group 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 = $GroupObject->GroupCustomFields->LimitToDefaultValuesSupportedTypes;
+ while ( my $cf = $cfs->Next ) {
+ my ($ret, $msg) = $GroupObject->SetDefaultValue(
+ Field => $cf,
+ Value => undef,
+ );
+ }
+ push @results, "Group 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