| 1 | package App::SD::CLI::Command::Ticket::Search; | ||
| 2 | use Any::Moose; | ||
| 3 | extends 'Prophet::CLI::Command::Search'; | ||
| 4 | with 'App::SD::CLI::Command'; | ||
| 5 | |||
| 6 | sub ARG_TRANSLATIONS { shift->SUPER::ARG_TRANSLATIONS(), s => 'sort', g => 'group' }; | ||
| 7 | |||
| 8 | override usage_msg => sub { | ||
| 9 | my $self = shift; | ||
| 10 | my $script = $self->cli->get_script_name; | ||
| 11 | |||
| 12 | my @primary_commands = @{ $self->context->primary_commands }; | ||
| 13 | |||
| 14 | # if primary commands was only length 1, the type was not specified | ||
| 15 | # and we should indicate that a type is expected | ||
| 16 | push @primary_commands, '<record-type>' if @primary_commands <= 1; | ||
| 17 | |||
| 18 | my $type_and_subcmd = join( q{ }, @primary_commands ); | ||
| 19 | |||
| 20 | return <<"END_USAGE"; | ||
| 21 | usage: ${script}${type_and_subcmd} | ||
| 22 | ${script}${type_and_subcmd} -- summary=~foo status!~new|open | ||
| 23 | END_USAGE | ||
| 24 | }; | ||
| 25 | |||
| 26 | # frob the sort routine before running prophet's search command | ||
| 27 | override run => sub { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | $self->print_usage if $self->has_arg('h'); | ||
| 31 | |||
| 32 | if ( (!$self->has_arg('sort') || !$self->arg('sort')) | ||
| 33 | && $self->app_handle->config->get( key => 'ticket.default-sort') ) | ||
| 34 | { | ||
| 35 | $self->set_arg( | ||
| 36 | 'sort' => $self->app_handle->config->get( | ||
| 37 | key => 'ticket.default-sort' | ||
| 38 | ) | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | if ( (!$self->has_arg('group') || !$self->arg('group')) | ||
| 43 | && $self->app_handle->config->get( key => 'ticket.default-group') ) | ||
| 44 | { | ||
| 45 | $self->set_arg( 'group' => | ||
| 46 | $self->app_handle->config->get( key => 'ticket.default-group') ); | ||
| 47 | } | ||
| 48 | |||
| 49 | # sort output by given prop if user specifies --sort | ||
| 50 | if ( $self->has_arg('sort') && $self->arg('sort') | ||
| 51 | && ( $self->arg('sort') ne 'none' ) ) { | ||
| 52 | |||
| 53 | my $sort_prop = $self->arg('sort'); | ||
| 54 | |||
| 55 | my $sort_undef_last = $self->app_handle->config->get( | ||
| 56 | key => $self->type . ".$sort_prop.sort-undef-last" ); | ||
| 57 | |||
| 58 | $self->sort_routine( | ||
| 59 | sub { | ||
| 60 | my $records = shift; | ||
| 61 | return $self->sort_by_prop( $sort_prop, $records, | ||
| 62 | $sort_undef_last ); | ||
| 63 | } | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | if ( $self->has_arg('group') && $self->arg('group') && ( | ||
| 68 | $self->arg('group') ne 'none' ) ) { | ||
| 69 | $self->group_routine( | ||
| 70 | sub { | ||
| 71 | my $records = shift; | ||
| 72 | my $groups = | ||
| 73 | $self->group_by_prop( $self->arg('group') => $records ); | ||
| 74 | if ( $self->arg('group') eq 'milestone' ) { | ||
| 75 | my $order = | ||
| 76 | $self->app_handle->setting( label => 'milestones' ) | ||
| 77 | ->get(); | ||
| 78 | my %group_hash = | ||
| 79 | map { $_->{'label'} => $_->{'records'} } @$groups; | ||
| 80 | my $sorted_groups = [ | ||
| 81 | map { | ||
| 82 | |||
| 83 | { | ||
| 84 | label => $_, | ||
| 85 | records => ( delete $group_hash{$_} || [] ) | ||
| 86 | } | ||
| 87 | |||
| 88 | } @$order | ||
| 89 | ]; | ||
| 90 | return [ | ||
| 91 | @$sorted_groups, | ||
| 92 | ( | ||
| 93 | map { { label => $_, records => $group_hash{$_} } } | ||
| 94 | keys %group_hash | ||
| 95 | ) | ||
| 96 | ]; | ||
| 97 | } | ||
| 98 | return $groups; | ||
| 99 | } | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | $self->SUPER::run(@_); | ||
| 103 | }; | ||
| 104 | |||
| 105 | # implicit status != closed | ||
| 106 | override default_match => sub { | ||
| 107 | my $self = shift; | ||
| 108 | my $ticket = shift; | ||
| 109 | |||
| 110 | return 1 if $ticket->has_active_status(); | ||
| 111 | return 0; | ||
| 112 | }; | ||
| 113 | |||
| 114 | __PACKAGE__->meta->make_immutable; | ||
| 115 | no Any::Moose; | ||
| 116 | |||
| 117 | 1; | ||
| 118 |