[Bps-public-commit] rt-extension-tags branch, master, updated. 12a824c318584b64fd1f3788a6f64a945175d8f6

Alex Vandiver alexmv at bestpractical.com
Mon Jul 25 03:04:00 EDT 2016


The branch, master has been updated
       via  12a824c318584b64fd1f3788a6f64a945175d8f6 (commit)
      from  d57f0e0553fc6d90d241ec33e2c1e5e2c11725bb (commit)

Summary of changes:
 etc/initialdata                   | 35 +-------------
 html/Elements/EditCustomFieldTags |  1 +
 html/Elements/ShowCustomFieldTags | 32 +++++++++++++
 lib/RT/Action/AddNewCFValue.pm    | 99 ---------------------------------------
 lib/RT/Extension/Tags.pm          | 68 ++++++++++++++++++---------
 5 files changed, 81 insertions(+), 154 deletions(-)
 create mode 100644 html/Elements/EditCustomFieldTags
 create mode 100644 html/Elements/ShowCustomFieldTags
 delete mode 100644 lib/RT/Action/AddNewCFValue.pm

- Log -----------------------------------------------------------------
commit 12a824c318584b64fd1f3788a6f64a945175d8f6
Author: Alex Vandiver <alex at chmrr.net>
Date:   Sun Jul 24 20:54:09 2016 -0700

    Switch to a Tag CF type

diff --git a/etc/initialdata b/etc/initialdata
index 065292d..99bd3bf 100644
--- a/etc/initialdata
+++ b/etc/initialdata
@@ -1,43 +1,10 @@
-my $status;
-
-if ( RT::Handle::cmp_version($RT::VERSION, "4.4.0") < 0 ){
-    # For pre RT 4.4
-    $status = "(Status = 'new' OR Status = 'open')";
-} else {
-    # RT 4.4 introduced __Active__
-    $status = "Status = '__Active__'";
-}
-
-my $query = "CF.{Tag} = '__CustomField__' AND $status";
-RT::Interface::Web::EscapeURI(\$query);
-
 @CustomFields = (
     {
         Name        => 'Tag',
-        Type        => 'AutocompleteMultiple',
+        Type        => 'TagsMultiple',
         LookupType  => 'RT::Queue-RT::Ticket',
         Disabled    => 0,
         Description => 'Tags for tickets',
-        LinkValueTo => "__WebPath__/Search/Results.html?Query=$query",
         MaxValues   => '0',
     },
 );
-
- at ScripActions = (
-    {
-       Name        => 'Add New CF Value',
-       Description => 'Add new values to the list of available values for this custom field' ,
-       ExecModule  => 'AddNewCFValue',
-       Argument    => 'Tag'
-    },
-);
-
- at Scrips = (
-    {
-       Description    => 'On Custom Field Change Add New Tag Values',
-       ScripCondition => 'On Transaction',
-       ScripAction    => 'Add New CF Value',
-       Template       => 'Blank'
-    },
-);
-
diff --git a/html/Elements/EditCustomFieldTags b/html/Elements/EditCustomFieldTags
new file mode 100644
index 0000000..96b90de
--- /dev/null
+++ b/html/Elements/EditCustomFieldTags
@@ -0,0 +1 @@
+<& /Elements/EditCustomFieldAutocomplete, %ARGS &>
diff --git a/html/Elements/ShowCustomFieldTags b/html/Elements/ShowCustomFieldTags
new file mode 100644
index 0000000..a1da3d7
--- /dev/null
+++ b/html/Elements/ShowCustomFieldTags
@@ -0,0 +1,32 @@
+<%init>
+my $content = $Object->LargeContent || $Object->Content;
+$content = $m->comp('/Elements/ScrubHTML', Content => $content);
+$content =~ s|\n|<br />|g;
+
+my $status;
+if ( RT::Handle::cmp_version($RT::VERSION, "4.4.0") < 0 ){
+    # For pre RT 4.4
+    $status = "(Status = 'new' OR Status = 'open')";
+} else {
+    # RT 4.4 introduced __Active__
+    $status = "Status = '__Active__'";
+}
+
+my $cf = $Object->CustomFieldObj;
+my $cf_id = $cf->id;
+my $query_value = $Object->LargeContent || $Object->Content;
+$query_value =~ s/(['\\])/\\$1/g;
+my $query = "CF.{$cf_id} = '$query_value' AND $status";
+RT::Interface::Web::EscapeURI(\$query);
+
+</%init>
+% if (not $cf->LinkValueTo) {
+<a href="<% RT->Config->Get('WebPath') %>/Search/Results.html?Query=<% $query %>">
+% }
+<%$content|n%>
+% if (not $cf->LinkValueTo) {
+</a>
+% }
+<%ARGS>
+$Object
+</%ARGS>
diff --git a/lib/RT/Action/AddNewCFValue.pm b/lib/RT/Action/AddNewCFValue.pm
deleted file mode 100644
index bbb2fd6..0000000
--- a/lib/RT/Action/AddNewCFValue.pm
+++ /dev/null
@@ -1,99 +0,0 @@
-package RT::Action::AddNewCFValue;
-
-use strict;
-use warnings;
-
-use base qw(RT::Action);
-
-sub CustomField {
-    my $self    = shift;
-    my $cf_name = $self->Argument;
-
-    my $cf = $self->TicketObj->LoadCustomFieldByIdentifier($cf_name);
-    RT::Logger->error("Unable to load custom field $cf_name") unless $cf->Id;
-    return $cf;
-}
-
-sub Prepare {
-    my $self = shift;
-    my $txn  = $self->TransactionObj;
-    my $cf   = $self->CustomField;
-
-    return 1 if $txn->Type eq "Create";
-    return 1 if $txn->Type eq "CustomField" and $txn->Field == $cf->id;
-    return 0;
-}
-
-=head2 DESCRIPTION
-
-If the current value provided for this custom field isn't currently in
-the list of available values, add it.
-
-The custom field name is identified by the Argument set for the scrip
-action when the action is installed.
-
-=cut
-
-sub Commit {
-    my $self = shift;
-    my $txn  = $self->TransactionObj;
-    my $cf   = $self->CustomField;
-
-    my $values_obj = $cf->Values;
-    my @current_values;
-    while ( my $cf_value = $values_obj->Next ) {
-        push @current_values, $cf_value->Name;
-    }
-
-    if ( $txn->Type eq 'Create' ) {
-
-        # Can get multiple new values from create, so check them all
-        my @ticket_values = split /\n/,
-            $self->TicketObj->CustomFieldValuesAsString($cf);
-
-        foreach my $value (@ticket_values) {
-
-            # Already got it
-            next if grep { $value eq $_ } @current_values;
-
-            # Don't have it, so add it
-            $cf->AddValue( Name => $value );
-        }
-        return 1;
-    } else {
-
-        # Defer to core's NewReferenceObject if possible
-        my $cf_value = $txn->can("NewReferenceObject")
-            ? $txn->NewReferenceObject
-            : $self->NewReferenceObject($txn);
-
-        return 0 unless $cf_value and $cf_value->Id;
-
-        foreach my $value (@current_values) {
-
-            # Already have it
-            return 1 if $cf_value->Content eq $value;
-        }
-
-        # It's new, add it
-        $cf->AddValue( Name => $cf_value->Content );
-        return 1;
-    }
-}
-
-# Lifted from RT 4.2's RT::Transaction, for compatibility with RT
-# 4.0.  Can be removed after 4.0 support drops.
-
-sub NewReferenceObject {
-    my $self = shift;
-    my $txn  = shift;
-    my $type = $txn->__Value("ReferenceType");
-    my $id   = $txn->__Value("NewReference");
-    return unless $type and $id;
-
-    my $object = $type->new( $self->CurrentUser );
-    $object->Load($id);
-    return $object;
-}
-
-1;
diff --git a/lib/RT/Extension/Tags.pm b/lib/RT/Extension/Tags.pm
index fc7e847..d5dba3b 100644
--- a/lib/RT/Extension/Tags.pm
+++ b/lib/RT/Extension/Tags.pm
@@ -4,6 +4,48 @@ package RT::Extension::Tags;
 
 our $VERSION = '0.02';
 
+
+require RT::CustomField;
+
+$RT::CustomField::FieldTypes{Tags} = {
+    sort_order     => 85,
+    selection_type => 1,
+    labels         => [
+        'Enter multiple tags',  # loc
+        'Enter one tag',        # loc
+        'Enter up to [_1] tag', # loc
+    ],
+};
+
+
+no warnings 'redefine';
+my $old = \&RT::CustomField::AddValueForObject;
+*RT::CustomField::AddValueForObject = sub {
+    my $self = shift;
+    my %args = (
+        Content => undef,
+        LargeContent => undef,
+        @_
+    );
+
+    my ($ok, $msg) = $old->($self, @_);
+    return ($ok, $msg) unless $ok;
+
+    return ($ok, $msg) unless $self->Type eq "Tags";
+
+
+    my $value = $args{LargeContent} || $args{Content};
+    my $as_super = RT::CustomField->new( RT->SystemUser );
+    $as_super->Load( $self->id );
+    my $values = $as_super->Values;
+    $values->Limit( FIELD => 'Name', VALUE => $value );
+    return ($ok, $msg) if $values->Count;
+
+    $as_super->AddValue( Name => $value );
+    return ($ok, $msg);
+};
+
+
 =head1 NAME
 
 RT-Extension-Tags - Provides simple tagging using custom fields
@@ -11,30 +53,16 @@ RT-Extension-Tags - Provides simple tagging using custom fields
 =head1 DESCRIPTION
 
 This extension allows you to create tags using custom fields on
-tickets. RT has a custom field type that allows you to select
-multiple values with autocomplete from a list of values. This
-extension allows users to add new values (tags) that will then be
-added to the list of available autocomplete values for that custom
-field.
+tickets.  It adds a new custom field type, "Tags", which allows users
+to add new values (tags) that will then be added to the list of
+available autocomplete values for that custom field.
 
 The created tags become links to a search of all active tickets
 with that tag.
 
 =head2 Tag Custom Field
 
-The initdb step installs a Tag custom field by default along with
-the required condition, action, and scrip. The custom field is
-global as installed, but you can limit it to specific queues
-by editing the custom field configuration.
-
-If you want to use this functionality with a different custom field,
-create the custom field, then create a new scrip. You can use
-the provided "On Custom Field Change" condition as is. Then
-create a new action in the RT database using the same values as the
-provided "Add New CF Value" but change the Argument to the name of your
-new custom field. You can add this with a short initial data file,
-directly in the database, or using the helpful extension
-L<RT::Extension::AdminConditionsAndActions>.
+The initdb step installs an example global Tag custom field.
 
 =head1 RT VERSION
 
@@ -54,9 +82,7 @@ May need root permissions
 
 =item C<make initdb>
 
-Only run this the first time you install the module. Creates a global
-Tags custom field and adds the necessary scrip, action, and
-condition.
+This optional step installs an example global C<Tag> custom field.
 
 =item Edit your F</opt/rt4/etc/RT_SiteConfig.pm>
 

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


More information about the Bps-public-commit mailing list