[Rt-commit] rt branch, owner-autocomplete, updated. rt-3.8.8-485-ga9490fb

Thomas Sibley trs at bestpractical.com
Thu Aug 19 17:27:07 EDT 2010


The branch, owner-autocomplete has been updated
       via  a9490fba858e59c0e105bab23620fb3b92199df5 (commit)
      from  30e25947e6a656f55ffb7167b936f2e61ba665f9 (commit)

Summary of changes:
 share/html/Elements/SelectOwnerAutocomplete       |   13 +++
 share/html/Helpers/Autocomplete/{Users => Owners} |   97 +++++++++++++--------
 2 files changed, 75 insertions(+), 35 deletions(-)
 copy share/html/Helpers/Autocomplete/{Users => Owners} (54%)

- Log -----------------------------------------------------------------
commit a9490fba858e59c0e105bab23620fb3b92199df5
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Thu Aug 19 17:19:05 2010 -0400

    Add the autocomplete helper for owners
    
    It doesn't do any caching right now, but it might already be better than
    the dropdown since it's filtering on other columns.  We could make that
    more effective just by waiting for 2 or 3 characters to be typed before
    autocompleting.
    
    Next up, losing those user visible IDs.

diff --git a/share/html/Elements/SelectOwnerAutocomplete b/share/html/Elements/SelectOwnerAutocomplete
index bfbcd1d..d3f4e97 100644
--- a/share/html/Elements/SelectOwnerAutocomplete
+++ b/share/html/Elements/SelectOwnerAutocomplete
@@ -61,8 +61,21 @@ if ( $Default and not $Default =~ /\D/ ) {
     $user->Load($Default);
     $value = $user->$ValueAttribute;
 }
+
+# Map to a string of RT::Ticket-1|RT::Queue-5|...
+my $limit = join '|', map { join '-', ref($_), $_->id } @$Objects;
+
+my $query = $m->comp('/Elements/QueryString',
+    return  => $ValueAttribute,
+    limit   => $limit,
+);
 </%INIT>
 
 <input type="text" name="<%$Name%>" id="<%$Name%>" value="<% $value %>" />
 <script type="text/javascript">
+    jQuery(function() {
+        jQuery("#<% $Name %>").autocomplete({
+            source: "<% RT->Config->Get('WebPath')%>/Helpers/Autocomplete/Owners?<% $query|n %>"
+        });
+    });
 </script>
diff --git a/share/html/Helpers/Autocomplete/Owners b/share/html/Helpers/Autocomplete/Owners
new file mode 100644
index 0000000..af57ad2
--- /dev/null
+++ b/share/html/Helpers/Autocomplete/Owners
@@ -0,0 +1,138 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%# 
+%# COPYRIGHT:
+%# 
+%# This software is Copyright (c) 1996-2010 Best Practical Solutions, LLC
+%#                                          <jesse 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 }}}
+
+<% JSON::to_json( \@suggestions ) |n %>
+% $m->abort;
+<%ARGS>
+$return => ''
+$limit => undef
+$term => undef
+</%ARGS>
+<%INIT>
+require JSON;
+
+# Only allow certain return fields
+$return = 'id'
+    unless $return =~ /^(?:EmailAddress|Name|RealName|id)$/;
+
+$m->abort unless defined $return
+             and defined $term
+             and defined $limit;
+
+my $CurrentUser = $session{'CurrentUser'};
+
+my @fields = @{ RT->Config->Get('UserAutocompleteFields')
+                || [qw(EmailAddress Name RealName)] };
+
+my %user_uniq_hash;
+my $isSU = $session{CurrentUser}
+    ->HasRight( Right => 'SuperUser', Object => $RT::System );
+
+# Turn RT::Ticket-1|RT::Queue-2 into ['RT::Ticket', 1], ['RT::Queue', 2]
+foreach my $spec (map { [split /\-/, $_, 2] } split /\|/, $limit) {
+    my $object = $spec->[0]->new( $session{'CurrentUser'} );
+
+    if ( $spec->[1] ) {
+        $object->Load( $spec->[1] );
+
+        # Warn if we couldn't load an object
+        unless ( $object->id ) {
+            $RT::Logger->warn("Owner autocomplete couldn't load an '$spec->[0]' with id '$spec->[1]'");
+            next;
+        }
+    }
+
+    my $Users = RT::Users->new( $session{CurrentUser} );
+
+    # Limit by our autocomplete term BEFORE we limit to OwnTicket because that
+    # does a funky union hack
+    for (@fields) {
+        $Users->Limit(
+            FIELD           => $_,
+            OPERATOR        => 'LIKE',
+            VALUE           => $term,
+            ENTRYAGGREGATOR => 'OR',
+            SUBCLAUSE       => 'autocomplete',
+        );
+    }
+
+    $Users->WhoHaveRight(
+        Right               => 'OwnTicket',
+        Object              => $object,
+        IncludeSystemRights => 1,
+        IncludeSuperusers   => $isSU
+    );
+
+    while ( my $User = $Users->Next() ) {
+        next if $user_uniq_hash{ $User->Id };
+        $user_uniq_hash{ $User->Id() } = [$User, $m->scomp('/Elements/ShowUser', User => $User)];
+    }
+}
+
+# Make sure we add Nobody if we don't already have it
+my $nobody = qr/^n(?:o(?:b(?:o(?:d(?:y)?)?)?)?)?$/i;
+if ( not $user_uniq_hash{$RT::Nobody->id} and $term =~ $nobody ) {
+    $user_uniq_hash{$RT::Nobody->id} = [
+        $RT::Nobody,
+        $m->scomp('/Elements/ShowUser', User => $RT::Nobody)
+    ];
+}
+
+my @users = sort { lc $a->[1] cmp lc $b->[1] }
+                 values %user_uniq_hash;
+
+my @suggestions;
+for my $tuple ( @users ) {
+    my $formatted = $tuple->[1];
+    $formatted =~ s/\n//g;
+    push @suggestions, {
+        label => $formatted,
+        value => $tuple->[0]->$return
+    };
+}
+</%INIT>

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


More information about the Rt-commit mailing list