| 1 | package Jifty::Action::Record::Bulk; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Jifty::Action::Record::Bulk - Perform multiple record actions | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | package MyApp::Action::BulkUpdateFoo; | ||
| 16 | use base qw/ Jifty::Action::Record::Bulk /; | ||
| 17 | |||
| 18 | __PACKAGE__->add_action('MyApp::Action::DeleteFoo' => { trigger => 'delete', final => 1 }); | ||
| 19 | __PACKAGE__->add_action('MyApp::Action::UpdateFoo'); | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use base qw/Jifty::Action::Record Class::Data::Inheritable/; | ||
| 24 | |||
| 25 | __PACKAGE__->mk_classdata( actions => [] ); | ||
| 26 | __PACKAGE__->mk_classdata( record_class => undef ); | ||
| 27 | |||
| 28 | use constant ids_name => 'ids'; | ||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | =head2 add_action CLASS [, OPTIONS] | ||
| 33 | |||
| 34 | Merges the given action class into this one. Will C<die> if the | ||
| 35 | L<Jifty::Action::Record/action_class> of the given C<CLASS> doesn't | ||
| 36 | match previously added classes. | ||
| 37 | |||
| 38 | OPTIONS should be a hash reference of additional options. The | ||
| 39 | existing options are: | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item trigger | ||
| 44 | |||
| 45 | Only run if this argument is provided | ||
| 46 | |||
| 47 | =item final | ||
| 48 | |||
| 49 | If this action runs, run B<only> this action. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub add_action { | ||
| 56 | my ($class, $name, $param) = @_; | ||
| 57 | push @{$class->actions}, [ $name, $param ]; | ||
| 58 | Jifty::Util->require($name); | ||
| 59 | if ($class->record_class) { | ||
| 60 | die "$class is not a action of @{[ $class->record_class ]}" | ||
| 61 | unless $class->record_class eq $name->record_class; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | $class->record_class( $name->record_class ); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 arguments | ||
| 69 | |||
| 70 | Merges together arguments from all of the actions added with | ||
| 71 | L</add_action>. The record IDs to act on are stored (comma-separated) | ||
| 72 | in an argument named C<ids>, by default. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub arguments { | ||
| 77 | my $self = shift; | ||
| 78 | my $arguments = { $self->ids_name => { render_as => 'text', sort_order => -999 } }; | ||
| 79 | |||
| 80 | # composite of the arguments from all actions, and remove the pk | ||
| 81 | require Jifty::Param::Schema; | ||
| 82 | |||
| 83 | for (@{$self->actions}) { | ||
| 84 | my ($action_class, $param) = @$_; | ||
| 85 | $arguments = Jifty::Param::Schema::merge_params( $arguments, $action_class->can('arguments')->($self) ); | ||
| 86 | delete $arguments->{id}; | ||
| 87 | } | ||
| 88 | |||
| 89 | if ( $self->can('PARAMS') ) { | ||
| 90 | $arguments = Jifty::Param::Schema::merge_params( | ||
| 91 | $arguments, ($self->PARAMS || {}) | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | return $arguments; | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 perform_action CLASS, IDS | ||
| 98 | |||
| 99 | Performs the given action C<CLASS> on the given record C<ID>s, which | ||
| 100 | should be an array reference. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub perform_action { | ||
| 105 | my ($self, $action_class, $ids) = @_; | ||
| 106 | $self->result->content('detailed_messages', {}) | ||
| 107 | unless $self->result->content('detailed_messages'); | ||
| 108 | |||
| 109 | for (@$ids) { | ||
| 110 | my $record = $self->record_class->new; | ||
| 111 | $record->load($_); | ||
| 112 | |||
| 113 | my $action = $action_class->new( | ||
| 114 | moniker => join('-', $self->moniker, $action_class, $_), | ||
| 115 | record => $record, | ||
| 116 | arguments => $self->argument_values ); | ||
| 117 | |||
| 118 | $action->take_action; | ||
| 119 | $self->result->content('detailed_messages')->{ $action->moniker } = $action->result->message; | ||
| 120 | } | ||
| 121 | # allow bulk action to define if they allow individual action to fail | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 take_action | ||
| 125 | |||
| 126 | Completes the actions on all of the IDs given. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub take_action { | ||
| 131 | my $self = shift; | ||
| 132 | my $ids = $self->argument_value('ids'); | ||
| 133 | # ids can be '0', and we don't want to keep '0' | ||
| 134 | $ids = [ grep { $_ ne 0 } split /,/,$ids] if !ref($ids); | ||
| 135 | for (@{$self->actions}) { | ||
| 136 | my ($action_class, $param) = @$_; | ||
| 137 | if (my $trigger = $param->{trigger}) { | ||
| 138 | if ($self->argument_value($trigger)) { | ||
| 139 | $self->perform_action($action_class, $ids); | ||
| 140 | last if $param->{final}; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $self->perform_action($action_class, $ids); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 report_success | ||
| 150 | |||
| 151 | Reports C<Bulk update successful>. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub report_success { | ||
| 156 | my $self = shift; | ||
| 157 | $self->result->message(_("Bulk update successful")); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 |