| 1 | package App::SD::CLI::Command::Ticket::Review; | ||
| 2 | use Any::Moose; | ||
| 3 | extends 'App::SD::CLI::Command::Ticket::Search'; | ||
| 4 | #with 'App::SD::CLI::Command'; | ||
| 5 | |||
| 6 | override usage_msg => sub { | ||
| 7 | my $self = shift; | ||
| 8 | my $script = $self->cli->get_script_name; | ||
| 9 | |||
| 10 | my @primary_commands = @{ $self->context->primary_commands }; | ||
| 11 | |||
| 12 | # if primary commands was only length 1, the type was not specified | ||
| 13 | # and we should indicate that a type is expected | ||
| 14 | push @primary_commands, '<record-type>' if @primary_commands <= 1; | ||
| 15 | |||
| 16 | my $type_and_subcmd = join( q{ }, @primary_commands ); | ||
| 17 | |||
| 18 | return <<"END_USAGE"; | ||
| 19 | usage: ${script}${type_and_subcmd} | ||
| 20 | ${script}${type_and_subcmd} -- summary=~foo status!~new|open | ||
| 21 | END_USAGE | ||
| 22 | }; | ||
| 23 | |||
| 24 | before run => sub { | ||
| 25 | Prophet::CLI->end_pager(); | ||
| 26 | }; | ||
| 27 | |||
| 28 | our %ACTIONS = (); | ||
| 29 | |||
| 30 | our %INFO = ( | ||
| 31 | |||
| 32 | ); | ||
| 33 | |||
| 34 | after out_record => sub { | ||
| 35 | my $self = shift; | ||
| 36 | my $record = shift; | ||
| 37 | |||
| 38 | $self->out_widget( $record ); | ||
| 39 | ASK_AGAIN: | ||
| 40 | print "Update> "; | ||
| 41 | |||
| 42 | my $do = <STDIN>; | ||
| 43 | chomp $do; | ||
| 44 | $do =~ s/^\s+//; | ||
| 45 | $do =~ s/\s+$//; | ||
| 46 | return unless length $do; | ||
| 47 | |||
| 48 | my @list = split /\+/, $do; | ||
| 49 | |||
| 50 | my $ask_again = 0; | ||
| 51 | foreach my $do ( @list ) { | ||
| 52 | my $action = $ACTIONS{ $do }; | ||
| 53 | unless ( $action ) { | ||
| 54 | print "No action bound to '$do', try again...\n"; | ||
| 55 | $ask_again = 1; next; | ||
| 56 | } | ||
| 57 | next unless $action->{'action'}; | ||
| 58 | |||
| 59 | my $name = 'action_'. $action->{'action'}; | ||
| 60 | unless ( $self->can($name) ) { | ||
| 61 | print "Not implemented, patches are welcome\n"; | ||
| 62 | $ask_again = 1; next; | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->$name( $record, %$action ); | ||
| 66 | print "Done $do\n"; | ||
| 67 | } | ||
| 68 | goto ASK_AGAIN if $ask_again; | ||
| 69 | }; | ||
| 70 | |||
| 71 | sub out_widget { | ||
| 72 | my $self = shift; | ||
| 73 | my $record = shift; | ||
| 74 | |||
| 75 | $self->prepare_actions($record) unless keys %ACTIONS; | ||
| 76 | |||
| 77 | print "show [b] basics or [d] details\n"; | ||
| 78 | foreach my $property ( @{ $INFO{'properties'} } ) { | ||
| 79 | my $prop_shortcut = $INFO{'shortcuts'}{$property}; | ||
| 80 | print "$property:\n"; | ||
| 81 | print "\t"; | ||
| 82 | my $current = $record->prop($property); | ||
| 83 | my $not_first = 0; | ||
| 84 | foreach my $value ( @{ $INFO{'values'}{$property} } ) { | ||
| 85 | print ", " if $not_first++; | ||
| 86 | print "[". $prop_shortcut . $INFO{vshortcuts}{$property}{$value} ."] $value"; | ||
| 87 | print "*" if $value eq $current; | ||
| 88 | } | ||
| 89 | print "\n"; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub action_property { | ||
| 94 | my $self = shift; | ||
| 95 | my $record = shift; | ||
| 96 | my %args = ( name => undef, value => undef, @_ ); | ||
| 97 | $record->set_prop( name => $args{'name'}, value => $args{'value'} ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub prepare_actions { | ||
| 101 | my $self = shift; | ||
| 102 | my $record = shift; | ||
| 103 | |||
| 104 | %ACTIONS = ( | ||
| 105 | b => { action => 'show', value => 'basics' }, | ||
| 106 | d => { action => 'show', value => 'details' }, | ||
| 107 | ); | ||
| 108 | |||
| 109 | my @reserved = keys %ACTIONS; | ||
| 110 | my $app_handle = $record->app_handle; | ||
| 111 | my @props = @{ $app_handle->setting( label => 'common_ticket_props' )->get }; | ||
| 112 | |||
| 113 | foreach my $property ( @props ) { | ||
| 114 | my $plural_form = $self->plural_noun( $property ); | ||
| 115 | # XXX: dirty hack | ||
| 116 | next unless $app_handle->database_settings->{$plural_form}; | ||
| 117 | |||
| 118 | my @values = @{ $app_handle->setting( label => $plural_form )->get }; | ||
| 119 | next unless @values; | ||
| 120 | |||
| 121 | $INFO{'values'}{$property} = \@values; | ||
| 122 | |||
| 123 | my $shortcut = $INFO{'shortcuts'}{$property} | ||
| 124 | = $self->shortcut( $property, @reserved ); | ||
| 125 | push @reserved, $shortcut; | ||
| 126 | $ACTIONS{ $shortcut } = {}; | ||
| 127 | } | ||
| 128 | |||
| 129 | @props = grep $INFO{'values'}{ $_ }, @props; | ||
| 130 | $INFO{'properties'} = \@props; | ||
| 131 | |||
| 132 | foreach my $property ( @props ) { | ||
| 133 | my @reserved = (); | ||
| 134 | foreach my $value ( @{ $INFO{'values'}{$property} } ) { | ||
| 135 | my $shortcut = $self->shortcut( $value, @reserved ); | ||
| 136 | push @reserved, $shortcut; | ||
| 137 | |||
| 138 | $ACTIONS{ $INFO{'shortcuts'}{$property} . $shortcut } = { | ||
| 139 | action => 'property', name => $property, value => $value, | ||
| 140 | }; | ||
| 141 | $INFO{'vshortcuts'}{$property}{$value} = $shortcut; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub plural_noun { | ||
| 147 | my $self = shift; | ||
| 148 | my $noun = shift; | ||
| 149 | |||
| 150 | # simple plural form generation, full info on | ||
| 151 | # http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html | ||
| 152 | |||
| 153 | return $noun.'es' if $noun =~ /[cs]h$/; | ||
| 154 | return $noun.'es' if $noun =~ /ss$/; | ||
| 155 | return $noun if $noun =~ s/([aeo]l|[^d]ea|ar)f$/$1ves/; | ||
| 156 | return $noun if $noun =~ s/([nlw]i)fe$/$1ves/; | ||
| 157 | return $noun.'s' if $noun =~ /[aeiou]y$/; | ||
| 158 | return $noun if $noun =~ s/y$/ies/; | ||
| 159 | return $noun.'s' if $noun =~ /[aeiou]o$/; | ||
| 160 | return $noun.'es' if $noun =~ /o$/; | ||
| 161 | return $noun.'es' if $noun =~ /s$/; | ||
| 162 | return $noun.'s'; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub shortcut { | ||
| 166 | my $self = shift; | ||
| 167 | my $word = shift; | ||
| 168 | my @reserved = @_; | ||
| 169 | |||
| 170 | for (my $i = 0; $i < length $word; $i++ ) { | ||
| 171 | my $char = substr $word, $i, 1; | ||
| 172 | return wantarray? ($char, $i) : $char | ||
| 173 | unless grep $_ eq $char, @reserved; | ||
| 174 | } | ||
| 175 | for (my $i = 1; $i <= length $word; $i++ ) { | ||
| 176 | my $prefix = substr $word, 0, $i; | ||
| 177 | return wantarray? ($prefix, $i) : $prefix | ||
| 178 | unless grep $_ eq $prefix, @reserved; | ||
| 179 | } | ||
| 180 | return $word, 0; | ||
| 181 | } | ||
| 182 | |||
| 183 | __PACKAGE__->meta->make_immutable; | ||
| 184 | no Any::Moose; | ||
| 185 | |||
| 186 | 1; |