[Rt-commit] rt branch, 4.4/queue-default-article, created. rt-4.4.2-76-g0ee575731

Craig Kaiser craig at bestpractical.com
Thu Feb 22 13:47:28 EST 2018


The branch, 4.4/queue-default-article has been created
        at  0ee5757311abaf56d1f626fe46aacbb145b0b758 (commit)

- Log -----------------------------------------------------------------
commit 30ac41703014fe61f3ed2470ea858bc202ea05ce
Author: craig Kaiser <craig at bestpractical.com>
Date:   Wed Feb 21 14:35:54 2018 -0500

    Add default value for Articles in Queues
    
    Automatically add default article content to tickets on create for queue.

diff --git a/lib/RT/Queue.pm b/lib/RT/Queue.pm
index 4c2f5fb62..aa6131a0b 100644
--- a/lib/RT/Queue.pm
+++ b/lib/RT/Queue.pm
@@ -1185,6 +1185,29 @@ sub SetDefaultValue {
         $new_value = $self->loc( '(no value)' );
     }
 
+    if ( $args{Name} eq 'Article' && $args{Value} ) {
+        my $article = RT::Article->new($self->CurrentUser);
+        my ($ret, $msg);
+        if ( $args{Value} =~ /^\d+$/ ) {
+            ($ret, $msg) = $article->Load( $args{Value} );
+        }
+        else {
+            ($ret, $msg) = $article->LoadByCols( Name => $args{Value} );
+        }
+        return ($ret, $msg ) unless $ret;
+
+        $args{Value} = $article->Id;
+        $new_value = $article->Name;
+    }
+
+    if ( $args{Name} eq 'Article' && $old_value =~ /^\d+$/ ) {
+        my $article = RT::Article->new($self->CurrentUser);
+        my ($ret, $msg) = $article->Load( $old_value );
+        if ($ret) {
+            $old_value = $article->Name;
+        }
+    }
+
     return 1 if $new_value eq $old_value;
 
     my ($ret, $msg) = $self->SetAttribute(
diff --git a/share/html/Admin/Queues/DefaultValues.html b/share/html/Admin/Queues/DefaultValues.html
index aff93cc0b..ccb4fd5a5 100644
--- a/share/html/Admin/Queues/DefaultValues.html
+++ b/share/html/Admin/Queues/DefaultValues.html
@@ -71,6 +71,11 @@
         Grouping => 'Basics',
         InTable => 1,
     &>
+
+    <tr><td class="label"><&|/l&>Article</&>:</td>
+        <td><& /Elements/SelectArticle, QueueObj => $queue &>
+    </td></tr>
+
     </table>
     </&>
 </div>
@@ -149,7 +154,7 @@ if ( $ARGS{Reset} ) {
     }
 }
 elsif ( $ARGS{Update} ) {
-    for my $field ( qw/InitialPriority FinalPriority Starts Due/ ) {
+    for my $field ( qw/InitialPriority FinalPriority Starts Due Article/ ) {
         my ($ret, $msg) = $queue->SetDefaultValue(
             Name => $field,
             Value => $ARGS{$field},
diff --git a/share/html/Articles/Elements/IncludeArticle b/share/html/Articles/Elements/IncludeArticle
index 5307875cf..0f4becbe5 100644
--- a/share/html/Articles/Elements/IncludeArticle
+++ b/share/html/Articles/Elements/IncludeArticle
@@ -48,6 +48,7 @@
 <%INIT>
 
 my $parent_args = $m->caller_args(-1);
+my @articles;
 
 my $name_prefix = '';
 $name_prefix = $ARGS{'Name'} .'-'
@@ -55,18 +56,18 @@ $name_prefix = $ARGS{'Name'} .'-'
     && grep rindex($_, "$ARGS{'Name'}-Articles-", 0) == 0,
         keys %$parent_args;
 
+my $Ticket = $ARGS{Ticket};
+if ( !$Ticket and $parent_args->{id} and $parent_args->{id} ne 'new' ) {
+    $Ticket = RT::Ticket->new($session{'CurrentUser'});
+    $Ticket->Load($parent_args->{id});
+    unless ( $Ticket->id ) {
+        $RT::Logger->error("Couldn't load ticket ". $parent_args->{id} )
+    }
+}
+
 foreach my $arg ( keys %$parent_args ) {
     next if $name_prefix && substr($arg, 0, length($name_prefix)) ne $name_prefix;
 
-    my $Ticket = $ARGS{Ticket};
-    if ( !$Ticket and $parent_args->{id} and $parent_args->{id} ne 'new' ) {
-        $Ticket = RT::Ticket->new($session{'CurrentUser'});
-        $Ticket->Load($parent_args->{id});
-        unless ( $Ticket->id ) {
-            $RT::Logger->error("Couldn't load ticket ". $parent_args->{id} )
-        }
-    }
-
     my $Queue = RT::Queue->new($session{CurrentUser});
     if ($Ticket && $Ticket->Id) {
         $Queue = $Ticket->QueueObj;
@@ -79,7 +80,26 @@ foreach my $arg ( keys %$parent_args ) {
         Queue => $Queue->Id,
     );
     next unless $article && $article->id;
+    push (@articles, $article);
+}
+
+if ( !@articles && ( !$parent_args->{id} || $parent_args->{id} eq 'new' ) && $parent_args->{'Queue'} ) {
+    my $queue_id = $parent_args->{'Queue'};
+    my $QueueObj = RT::Queue->new($session{'CurrentUser'});
+    my ($ret, $msg) = $QueueObj->Load( $queue_id );
+    $RT::Logger->error($msg) unless ($ret);
+
+    if ( $QueueObj->id && $QueueObj->DefaultValue('Article') ) {
+        my $article = RT::Article->new($session{'CurrentUser'});
+        my ($ret, $msg) = $article->Load( $QueueObj->DefaultValue('Article') );
+        $RT::Logger->error($msg) unless ($ret);
+        if ($ret) {
+            push (@articles, $article);
+        }
+    }
+}
 
+foreach my $article (@articles) {
     my $formatted_article = $m->scomp('/Articles/Article/Elements/Preformatted',
         Article => $article, Ticket => $Ticket
     );
diff --git a/share/html/Articles/Elements/IncludeArticle b/share/html/Elements/SelectArticle
similarity index 56%
copy from share/html/Articles/Elements/IncludeArticle
copy to share/html/Elements/SelectArticle
index 5307875cf..77e7e9b0f 100644
--- a/share/html/Articles/Elements/IncludeArticle
+++ b/share/html/Elements/SelectArticle
@@ -45,55 +45,35 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
-<%INIT>
-
-my $parent_args = $m->caller_args(-1);
-
-my $name_prefix = '';
-$name_prefix = $ARGS{'Name'} .'-'
-    if $ARGS{'Name'}
-    && grep rindex($_, "$ARGS{'Name'}-Articles-", 0) == 0,
-        keys %$parent_args;
-
-foreach my $arg ( keys %$parent_args ) {
-    next if $name_prefix && substr($arg, 0, length($name_prefix)) ne $name_prefix;
-
-    my $Ticket = $ARGS{Ticket};
-    if ( !$Ticket and $parent_args->{id} and $parent_args->{id} ne 'new' ) {
-        $Ticket = RT::Ticket->new($session{'CurrentUser'});
-        $Ticket->Load($parent_args->{id});
-        unless ( $Ticket->id ) {
-            $RT::Logger->error("Couldn't load ticket ". $parent_args->{id} )
-        }
-    }
+<select name="<% $Name %>">
+<option value="">-</option>
+% while (my $article = $articles->Next) {
+<option <% ( $article->Name eq $Default) ? qq[ selected="selected"] : '' |n %>
+    value="<%$article->Id%>"
+><% $article->Name %></option>
+% }
+</select>
 
-    my $Queue = RT::Queue->new($session{CurrentUser});
-    if ($Ticket && $Ticket->Id) {
-        $Queue = $Ticket->QueueObj;
-    }
-
-    my $article = RT::Article->new($session{'CurrentUser'});
-    $article->LoadByInclude(
-        Field => substr($arg, length($name_prefix)),
-        Value => $parent_args->{$arg},
-        Queue => $Queue->Id,
-    );
-    next unless $article && $article->id;
+<%INIT>
+my $articles = RT::Articles->new( $session{'CurrentUser'} );
+$articles->LimitHotlistClasses;
+$articles->LimitAppliedClasses( Queue => $QueueObj );
 
-    my $formatted_article = $m->scomp('/Articles/Article/Elements/Preformatted',
-        Article => $article, Ticket => $Ticket
-    );
+my $Default = '';
 
-    $m->callback( Article => $article, Ticket => $Ticket, formatted_article => \$formatted_article );
+my $default_article = RT::Article->new($session{'CurrentUser'});
 
-    if (RT->Config->Get('MessageBoxRichText',  $session{'CurrentUser'})) {
-        $formatted_article =~ s/>/>/g;
-        $formatted_article =~ s/</</g;
-        $formatted_article =~ s/&/&/g;
-        $formatted_article =~ s/\n/\n<br \/>/g;
+if ( my $article_id = $QueueObj->DefaultValue('Article') ) {
+    my ($ret, $msg) = $default_article->Load( $article_id );
+    if ($ret) {
+         $Default = $default_article->Name;
+    } else{
+        RT::Logger->error($msg);
     }
-    $m->print($formatted_article);
-
 }
-return;
 </%INIT>
+
+<%ARGS>
+$QueueObj
+$Name => 'Article'
+</%ARGS>

commit d1d86770f1723816f6d1ff29f20a294846594244
Author: craig Kaiser <craig at bestpractical.com>
Date:   Wed Feb 21 14:43:23 2018 -0500

    Add autocomplete for articles

diff --git a/share/html/Elements/SelectArticle b/share/html/Elements/SelectArticle
index 77e7e9b0f..d0444cb38 100644
--- a/share/html/Elements/SelectArticle
+++ b/share/html/Elements/SelectArticle
@@ -45,6 +45,9 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
+% if ( $autocomplete ) {
+<& "SelectArticleAutocomplete", QueueObj => $QueueObj, Default => $Default, Name => $Name &>
+% } else {
 <select name="<% $Name %>">
 <option value="">-</option>
 % while (my $article = $articles->Next) {
@@ -53,14 +56,20 @@
 ><% $article->Name %></option>
 % }
 </select>
+% }
 
 <%INIT>
 my $articles = RT::Articles->new( $session{'CurrentUser'} );
 $articles->LimitHotlistClasses;
 $articles->LimitAppliedClasses( Queue => $QueueObj );
 
+my $dropdown_limit = 50;
+$m->callback( CallbackName => 'ModifyDropdownLimit', DropdownLimit => \$dropdown_limit );
+
 my $Default = '';
 
+my $autocomplete =  $articles->Count > $dropdown_limit ? 1 : 0;
+
 my $default_article = RT::Article->new($session{'CurrentUser'});
 
 if ( my $article_id = $QueueObj->DefaultValue('Article') ) {
diff --git a/share/html/Elements/SelectArticle b/share/html/Elements/SelectArticleAutocomplete
similarity index 73%
copy from share/html/Elements/SelectArticle
copy to share/html/Elements/SelectArticleAutocomplete
index 77e7e9b0f..18ada32fa 100644
--- a/share/html/Elements/SelectArticle
+++ b/share/html/Elements/SelectArticleAutocomplete
@@ -45,35 +45,10 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
-<select name="<% $Name %>">
-<option value="">-</option>
-% while (my $article = $articles->Next) {
-<option <% ( $article->Name eq $Default) ? qq[ selected="selected"] : '' |n %>
-    value="<%$article->Id%>"
-><% $article->Name %></option>
-% }
-</select>
-
-<%INIT>
-my $articles = RT::Articles->new( $session{'CurrentUser'} );
-$articles->LimitHotlistClasses;
-$articles->LimitAppliedClasses( Queue => $QueueObj );
-
-my $Default = '';
-
-my $default_article = RT::Article->new($session{'CurrentUser'});
-
-if ( my $article_id = $QueueObj->DefaultValue('Article') ) {
-    my ($ret, $msg) = $default_article->Load( $article_id );
-    if ($ret) {
-         $Default = $default_article->Name;
-    } else{
-        RT::Logger->error($msg);
-    }
-}
-</%INIT>
+<input data-autocomplete="Articles" <% $QueueObj && $QueueObj->id ? q{data-autocomplete-queue="} . $QueueObj->id . q{"} : '' |n %> data-autocomplete-return="Name" name="<% $Name %>" value="<% $Default %>">
 
 <%ARGS>
-$QueueObj
+$QueueObj => undef
+$Default => undef
 $Name => 'Article'
 </%ARGS>
diff --git a/share/html/Elements/SelectArticle b/share/html/Helpers/Autocomplete/Articles
similarity index 63%
copy from share/html/Elements/SelectArticle
copy to share/html/Helpers/Autocomplete/Articles
index 77e7e9b0f..21ef68308 100644
--- a/share/html/Elements/SelectArticle
+++ b/share/html/Helpers/Autocomplete/Articles
@@ -45,35 +45,51 @@
 %# those contributions and any derivatives thereof.
 %#
 %# END BPS TAGGED BLOCK }}}
-<select name="<% $Name %>">
-<option value="">-</option>
-% while (my $article = $articles->Next) {
-<option <% ( $article->Name eq $Default) ? qq[ selected="selected"] : '' |n %>
-    value="<%$article->Id%>"
-><% $article->Name %></option>
-% }
-</select>
-
+% $r->content_type('application/json; charset=utf-8');
+<% JSON( \@suggestions ) |n %>
+% $m->abort;
+<%ARGS>
+$term       => undef
+$max        => 10
+$op         => 'STARTSWITH'
+$right      => undef
+$return     => 'Name'
+$queue      => undef
+</%ARGS>
 <%INIT>
-my $articles = RT::Articles->new( $session{'CurrentUser'} );
-$articles->LimitHotlistClasses;
-$articles->LimitAppliedClasses( Queue => $QueueObj );
+# Only allow certain return fields
+$return = 'Name'
+    unless $return =~ /^(?:id|Name)$/;
+
+$m->abort unless defined $return
+             and defined $term
+             and length $term;
+
+# Sanity check the operator
+$op = 'STARTSWITH' unless $op =~ /^(?:LIKE|(?:START|END)SWITH|=|!=)$/i;
 
-my $Default = '';
+$m->callback( CallbackName => 'ModifyMaxResults', max => \$max );
 
-my $default_article = RT::Article->new($session{'CurrentUser'});
+my $articles = RT::Articles->new( $session{CurrentUser} );
+$articles->LimitHotlistClasses;
+if( $queue ) {
+    $articles->LimitAppliedClasses( Queue => $queue );
+}
+
+$articles->RowsPerPage( $max );
+$articles->Limit(
+    FIELD           => 'Name',
+    OPERATOR        => $op,
+    VALUE           => $term,
+    ENTRYAGGREGATOR => 'OR',
+    CASESENSITIVE   => 0,
+);
 
-if ( my $article_id = $QueueObj->DefaultValue('Article') ) {
-    my ($ret, $msg) = $default_article->Load( $article_id );
-    if ($ret) {
-         $Default = $default_article->Name;
-    } else{
-        RT::Logger->error($msg);
-    }
+my @suggestions;
+while (my $a = $articles->Next) {
+    next if $right and not $a->CurrentUserHasRight($right);
+    my $value = $a->$return;
+    push @suggestions, { label => $a->Name, value => $value };
+    $m->callback( CallbackName => "ModifySuggestion", suggestions => @suggestions, label => $a );
 }
 </%INIT>
-
-<%ARGS>
-$QueueObj
-$Name => 'Article'
-</%ARGS>
diff --git a/share/static/js/autocomplete.js b/share/static/js/autocomplete.js
index cd8ab2b0d..c4362edb1 100644
--- a/share/static/js/autocomplete.js
+++ b/share/static/js/autocomplete.js
@@ -5,7 +5,8 @@ window.RT.Autocomplete.Classes = {
     Users: 'user',
     Groups: 'group',
     Tickets: 'tickets',
-    Queues: 'queues'
+    Queues: 'queues',
+    Articles: 'articles'
 };
 
 window.RT.Autocomplete.bind = function(from) {
@@ -84,6 +85,9 @@ window.RT.Autocomplete.bind = function(from) {
             };
         }
 
+        var queue = input.attr("data-autocomplete-queue");
+        if (queue) queryargs.push("queue=" + queue);
+
         var checkRight = input.attr("data-autocomplete-checkright");
         if (checkRight) queryargs.push("right=" + checkRight);
 

commit 0ee5757311abaf56d1f626fe46aacbb145b0b758
Author: craig Kaiser <craig at bestpractical.com>
Date:   Wed Feb 21 14:43:41 2018 -0500

    Test default article content on ticket create

diff --git a/t/web/ticket-create-utf8.t b/t/web/ticket-create-utf8.t
index ebb2d5eab..76a7360b7 100644
--- a/t/web/ticket-create-utf8.t
+++ b/t/web/ticket-create-utf8.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use RT::Test tests => 43;
+use RT::Test tests => 49;
 
 my $ru_test = "\x{442}\x{435}\x{441}\x{442}";
 my $ru_support = "\x{43f}\x{43e}\x{434}\x{434}\x{435}\x{440}\x{436}\x{43a}\x{430}";
@@ -86,3 +86,23 @@ foreach my $test_str ( $ru_test, $l1_test ) {
     }
 }
 
+my $article = RT::Article->new($RT::SystemUser);
+my ( $id, $msg ) = $article->Create(
+    Class   => 'General',
+    Name    => 'My Article',
+    'CustomField-Content' => 'My Article Test Content',
+);
+ok( $id, $msg );
+(my $ret, $msg) = $article->Load(1);
+ok ($ret, $msg);
+
+my $queue = RT::Queue->new(RT->SystemUser);
+$queue->Load('General');
+ok( $queue, 'Loaded General Queue' );
+($ret, $msg) = $queue->SetDefaultValue( Name => 'Article', Value => $article->Id);
+ok( $ret, $msg );
+
+ok $m->login(root => 'password'), "logged in";
+$m->goto_create_ticket('General');
+$m->scraped_id_is('Content', '#1: My Article <br />-------------- <br />Content: <br />------- <br />My Article Test Content <br />');
+

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


More information about the rt-commit mailing list