[Rt-commit] rt branch 5.0/add-boolean-select-render-type created. rt-5.0.3-126-g8d669565e5

BPS Git Server git at git.bestpractical.com
Mon Oct 3 13:10:23 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/add-boolean-select-render-type has been created
        at  8d669565e50c69f4d8fd6cc088f83f276ba40345 (commit)

- Log -----------------------------------------------------------------
commit 8d669565e50c69f4d8fd6cc088f83f276ba40345
Author: Brian Conry <bconry at bestpractical.com>
Date:   Mon Jul 11 14:49:53 2022 -0500

    Preserve error messages on value delete
    
    The previous behavior was for RT::CustomField->DeleteValue and
    RT::CustomField->DeleteValueForObject to always substitute their own
    generic error message when the delete failed, which prevents a
    more-specific message from being displayed to the user.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index c8e33747cb..f4b5057c5a 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -715,7 +715,7 @@ sub DeleteValue {
 
     my ($ok, $msg) = $val_to_del->Delete;
     unless ( $ok ) {
-        return (0, $self->loc("Custom field value could not be deleted"));
+        return (0, $msg || $self->loc("Custom field value could not be deleted"));
     }
     return ($ok, $self->loc("Custom field value deleted"));
 }
@@ -2101,7 +2101,7 @@ sub DeleteValueForObject {
 
     my ($ok, $msg) = $oldval->Delete();
     unless ($ok) {
-        return(0, $self->loc("Custom field value could not be deleted"));
+        return(0, $msg || $self->loc("Custom field value could not be deleted"));
     }
     return($oldval->Id, $self->loc("Custom field value deleted"));
 }

commit 257c76598f659005a1882ab5f26ab2480bef169e
Author: Brian Conry <bconry at bestpractical.com>
Date:   Mon Jul 11 16:17:56 2022 -0500

    Add the Checkbox RenderType for SelectSingle CFs
    
    This change adds a new RenderType, "Checkbox", Select CFs that accept
    only a single value.
    
    It is recommended, but not required, that only one value be configured
    for a Checkbox CF.

diff --git a/docs/initialdata.pod b/docs/initialdata.pod
index d2343349b0..e8cad22e0b 100644
--- a/docs/initialdata.pod
+++ b/docs/initialdata.pod
@@ -217,10 +217,9 @@ common C<LookupType>.
 =item C<RenderType>
 
 Only valid when C<Type> is "Select".  Controls how the CF is displayed when
-editing it.  Valid values are: C<Select box>, C<List>, and C<Dropdown>.
+editing it.  Valid values are: C<Select box>, C<List>, C<Dropdown>, and C<Checkbox>.
 
-C<List> is either a list of radio buttons or a list of checkboxes depending on
-C<MaxValues>.
+See the documentation for L<RT::CustomField/SetRenderType> for more details.
 
 =item C<MaxValues>
 
diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index 3f78fc216b..c8e33747cb 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -87,6 +87,7 @@ our %FieldTypes = (
             single => [ 'Dropdown',                # loc
                         'Select box',              # loc
                         'List',                    # loc
+                        'Checkbox',                # loc
                       ]
         },
 
@@ -1348,6 +1349,15 @@ sub SetRenderType {
                                 $self->FriendlyType));
     }
 
+    if ( $type eq 'Checkbox' ) {
+        if ( $self->Values->Count < 2 ) {
+            return (0, $self->loc("Must have at least two Values for Render Type Checkbox"));
+        }
+        elsif ( $self->BasedOn ) {
+            return (0, $self->loc("We can't currently render as a Checkbox when basing categories on another custom field.  Please use another render type."));
+        }
+    }
+
     return $self->_Set( Field => 'RenderType', Value => $type, @_ );
 }
 
@@ -2257,6 +2267,9 @@ sub SetBasedOn {
     if ( $self->RenderType =~ /List/ ) {
         return (0, $self->loc("We can't currently render as a List when basing categories on another custom field.  Please use another render type."));
     }
+    elsif ( $self->RenderType eq 'Checkbox' ) {
+        return (0, $self->loc("We can't currently render as a Checkbox when basing categories on another custom field.  Please use another render type."));
+    }
 
     return $self->_Set( Field => 'BasedOn', Value => $value, @_ )
 }
@@ -2506,6 +2519,23 @@ Set RenderType to VALUE.
 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
 (In the database, RenderType will be stored as a varchar(64).)
 
+Only valid when C<Type> is "Select".  Controls how the CF is displayed when
+editing it.  Valid values are: C<Select box>, C<List>, C<Dropdown>, and C<Checkbox>.
+
+C<List> is either a list of radio buttons when C<MaxValues> is one and a list of
+checkboxes when C<MaxValues> is greater than one.
+
+C<Checkbox> is only available on when C<MaxValues> is one and the field is not
+based on any other custom field.  The CF will render as a checkbox.
+
+A Checkbox CF must always have at least two values.  The first value will be
+used for the C<Unchecked> state and the second value will be used for the
+C<Checked> state.
+
+When updating an object with a C<Checkbox> CF, if the CF is unset it will be
+treated as C<Unchecked> and and will be silently updated to the designated
+C<Unchecked> value.  If the CF has any other value will be treated as
+C<Checked> and will be noisily updated to the designated C<Checked> value.
 
 =cut
 
diff --git a/lib/RT/CustomFieldValue.pm b/lib/RT/CustomFieldValue.pm
index 2c991f2b1e..2f932e600f 100644
--- a/lib/RT/CustomFieldValue.pm
+++ b/lib/RT/CustomFieldValue.pm
@@ -102,6 +102,13 @@ sub ValidateName {
 
 sub Delete {
     my $self = shift;
+
+    if ($self->CustomFieldObj->RenderType eq 'Checkbox') {
+        if ($self->CustomFieldObj->Values->Count < 3) {
+            return ( 0, $self->loc('A Checkbox must always have at least two values' ));
+        }
+    }
+
     my ( $ret, $msg ) = $self->SUPER::Delete;
     $self->CustomFieldObj->CleanupDefaultValues;
     return ( $ret, $msg );
diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 6e827ffe28..3e4a135ac0 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -3453,6 +3453,28 @@ sub _ProcessObjectCustomFieldUpdates {
                     Value => $value,
                 );
 
+                # The specification we've implemented for the checkbox render type
+                # is that an unset custom field is logically considered the same as
+                # having the explicit "unchecked" value (the first value), and that
+                # it is preferred to store the explicit "unchecked" value rather than
+                # leaving the field unset.
+                # Therefore this condition here, where the field didn't have a value
+                # and now is being set to the "unchecked" value, does not represent
+                # a logical change in the state of the field and should not show in
+                # the transaction history.
+                if (
+                    $cf_type eq 'Select' and
+                    $cf->RenderType eq 'Checkbox' and
+                    not defined $cf_values->First and
+                    lc $value eq lc $cf_values->First->Name
+                ) {
+                    my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
+                        Field             => $cf,
+                        Value             => $value,
+                        RecordTransaction => 0,
+                    );
+                }
+
                 my ( $val, $msg ) = $args{'Object'}->AddCustomFieldValue(
                     Field => $cf,
                     Value => $value
diff --git a/share/html/Admin/Elements/EditCustomFieldValues b/share/html/Admin/Elements/EditCustomFieldValues
index 915b471b80..045ee30593 100644
--- a/share/html/Admin/Elements/EditCustomFieldValues
+++ b/share/html/Admin/Elements/EditCustomFieldValues
@@ -50,9 +50,13 @@
 %    return;
 % }
 
-% # we need to allow for an extra col-2 if not combobox and categories are enabled
-% # if so, make the description cols -2 smaller to allow for categories
-% my $description_col_size = ( $CustomField->Type ne 'Combobox' && $Categories ? 4 : 6 );
+% # we need to allow for an extra col-2 for:
+% # a) Any CF with categories
+% #    The extra space is used to set the category for each value
+% # b) Any CF with the Checkbox render type
+% #    The extra space is used for the "Unchecked"/"Checked" descriptive text
+% # Note that the Combobox CF Type and Checkbox RenderType both preclude having categories,
+% my $description_col_size = ( ( $CustomField->Type ne 'Combobox' && $Categories ) || $renderType eq "Checkbox" ? 4 : 6 );
 
 <div class="form-row">
   <div class="label col-auto">
@@ -75,8 +79,14 @@
       <&|/l&>Category</&>
     </div>
 % }
+% elsif ( $renderType eq 'Checkbox' ) {
+    <div class="label categoryheader col-2 text-left">
+      <&|/l&>Checkbox Type</&>
+    </div>
+% }
 </div>
 
+% my $firstValue = 1;
 % while ( my $value = $values->Next ) {
 % my $paramtag = "CustomField-". $CustomField->Id ."-Value-". $value->Id;
 <div class="form-row">
@@ -106,6 +116,12 @@
 %   }
     </select>
   </div>
+% }
+% if ( $renderType eq 'Checkbox' ) {
+  <div class="col-2">
+    <% $firstValue ? loc('Unchecked') : loc('Checked') %>
+%   $firstValue = 0;
+  </div>
 % }
   <div class="col-1">
     <input type="button" class="delete_custom_field_value button btn btn-primary" data-cfv-id="<% $value->id %>" value="<&|/l&>Delete</&>" onclick="delete_custom_field_value(<% $value->id %>)" />
@@ -139,6 +155,9 @@ my $Categories;
 if ($BasedOnObj and $BasedOnObj->Id) {
     $Categories = $BasedOnObj->Values;
 }
+
+my $renderType = $CustomField->RenderType;
+
 </%init>
 <%args>
 $CustomField => undef
diff --git a/share/html/Elements/EditCustomFieldSelect b/share/html/Elements/EditCustomFieldSelect
index acc8e9e5f3..1493b24ecd 100644
--- a/share/html/Elements/EditCustomFieldSelect
+++ b/share/html/Elements/EditCustomFieldSelect
@@ -78,6 +78,13 @@
 %   }
 </div>
 </fieldset>
+% } elsif ( $RenderType eq 'Checkbox' ) {
+%   my $CFVs = CachedCustomFieldValues($CustomField);
+%   my $FalseValue = lc $CFVs->First->Name;
+%   my $TrueValue  = lc $CFVs->Next->Name;
+%   my $isChecked = (scalar keys %default) && (not exists $default{$FalseValue});
+  <input type="checkbox" class="custom-control" <% $isChecked ? "checked" : "" %> onclick="document.getElementById('<% $name |n%>').value = this.checked ? '<% $TrueValue %>' : '<% $FalseValue %>'" />
+  <input type="hidden" id="<% $name %>" name="<% $name %>" class="custom-control" value="<% $isChecked ? $TrueValue : $FalseValue %>" />
 % } else {
 % if (@category) {
 %# this hidden select is to supply a full list of values,
diff --git a/share/html/Elements/ShowCustomFieldSelect b/share/html/Elements/ShowCustomFieldSelect
new file mode 100644
index 0000000000..e04aa6afb7
--- /dev/null
+++ b/share/html/Elements/ShowCustomFieldSelect
@@ -0,0 +1,60 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2022 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 }}}
+<%INIT>
+my $cf = $Object->CustomFieldObj;
+my $content = $Object->Content;
+</%INIT>
+% if ( $cf->RenderType eq 'Checkbox' )  {
+    <input type="checkbox" disabled="disabled" class="custom-control" <% ( defined $content and lc $content ne lc CachedCustomFieldValues($cf)->First->Name ) ? "checked" : "" %> />
+% }
+% else {
+    <% $content |h %>
+% }
+<%ARGS>
+$Object => undef
+</%ARGS>

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list