use SL::DB::CsvImportProfile;
use SL::DB::CsvImportReport;
use SL::DB::Unit;
+use SL::DB::Helper::Paginated ();
use SL::Helper::Flash;
use SL::SessionFile;
use SL::Controller::CsvImport::Contact;
use SL::System::TaskServer;
use List::MoreUtils qw(none);
+use List::Util qw(min);
use parent qw(SL::Controller::Base);
sub action_report {
my ($self, %params) = @_;
- $self->{report} = SL::DB::Manager::CsvImportReport->find_by(id => $params{report_id} || $::form->{id});
+ my $report_id = $params{report_id} || $::form->{id};
+
+ $self->{report} = SL::DB::Manager::CsvImportReport->find_by(id => $report_id);
+ my $num_rows = SL::DB::Manager::CsvImportReportRow->get_all_count(query => [ csv_import_report_id => $report_id ]);
+ my $num_cols = SL::DB::Manager::CsvImportReportRow->get_all_count(query => [ csv_import_report_id => $report_id, row => 0 ]);
+
+ # manual paginating, yuck
+ my $page = $::form->{page} || 1;
+ my $pages = {};
+ $pages->{per_page} = $::form->{per_page} || 20;
+ $pages->{max} = SL::DB::Helper::Paginated::ceil($num_rows / ($num_cols || 1), $pages->{per_page}) || 1;
+ $pages->{cur} = $page < 1 ? 1
+ : $page > $pages->{max} ? $pages->{max}
+ : $page;
+ $pages->{common} = SL::DB::Helper::Paginated::make_common_pages($pages->{cur}, $pages->{max});
+
+ $self->{display_rows} = [
+ 0,
+ $pages->{per_page} * ($pages->{cur}-1) + 1
+ ..
+ min($pages->{per_page} * $pages->{cur}, $num_rows / ($num_cols || 1) - 1)
+ ];
+
+ my @query = (
+ csv_import_report_id => $report_id,
+ or => [
+ row => 0,
+ and => [
+ row => { gt => $pages->{per_page} * ($pages->{cur}-1) },
+ row => { le => $pages->{per_page} * $pages->{cur} },
+ ]
+ ]
+ );
+
+ my $rows = SL::DB::Manager::CsvImportReportRow->get_all(query => \@query);
+ my $status = SL::DB::Manager::CsvImportReportStatus->get_all(query => \@query);
+
+ $self->{report_rows} = $self->{report}->folded_rows(rows => $rows);
+ $self->{report_status} = $self->{report}->folded_status(status => $status);
+ $self->{pages} = $pages;
+
+ my $base_url = $self->url_for(action => 'report', id => $report_id);
- $self->render('csv_import/report', { no_layout => $params{no_layout} });
+ $self->render('csv_import/report', { no_layout => $params{no_layout} }, base_url => $base_url);
}
__PACKAGE__->meta->initialize;
sub folded_rows {
- my ($self) = @_;
-
- $self->_fold_rows unless $self->{folded_rows};
+ my ($self, %params) = @_;
- return $self->{folded_rows};
-}
+ my $folded_rows = {};
-sub folded_status {
- my ($self) = @_;
-
- $self->_fold_status unless $self->{folded_status};
+ for my $row_obj (@{ $params{rows} || $self->rows }) {
+ $folded_rows->{ $row_obj->row } ||= [];
+ $folded_rows->{ $row_obj->row }[ $row_obj->col ] = $row_obj->value;
+ }
- return $self->{folded_status};
+ $folded_rows;
}
-sub _fold_rows {
- my ($self) = @_;
+sub folded_status {
+ my ($self, %params) = @_;
- $self->{folded_rows} = [];
+ my $folded_status = {};
- for my $row_obj (@{ $self->rows }) {
- $self->{folded_rows}->[ $row_obj->row ] ||= [];
- $self->{folded_rows}->[ $row_obj->row ][ $row_obj->col ] = $row_obj->value;
+ for my $status_obj (@{ $params{status} || $self->status }) {
+ $folded_status->{ $status_obj->row } ||= {};
+ $folded_status->{ $status_obj->row }{information} ||= [];
+ $folded_status->{ $status_obj->row }{errors} ||= [];
+ push @{ $folded_status->{ $status_obj->row }{ $status_obj->type } }, $status_obj->value;
}
-}
-sub _fold_status {
- my ($self) = @_;
-
- $self->{folded_status} = [];
-
- for my $status_obj (@{ $self->status }) {
- $self->{folded_status}->[ $status_obj->row ] ||= {};
- $self->{folded_status}->[ $status_obj->row ]{information} ||= [];
- $self->{folded_status}->[ $status_obj->row ]{errors} ||= [];
- push @{ $self->{folded_status}->[ $status_obj->row ]{ $status_obj->type } }, $status_obj->value;
- }
+ $folded_status;
}
# implementes cascade delete as per documentation
<h3>[%- LxERP.t8('Import result') %]</h3>
<table>
-[%- FOREACH row = SELF.report.folded_rows %]
+[%- FOREACH rownum = SELF.display_rows %]
[%- IF loop.first %]
<tr class="listheading">
- [%- FOREACH value = row %]
+ [%- FOREACH value = SELF.report_rows.${rownum} %]
<th>[% value | html %]</th>
[%- END %]
<th>[%- LxERP.t8('Notes') %]</th>
</tr>
[%- ELSE %]
- [% csv_import_report_errors = SELF.report.folded_status.${loop.index}.errors %]
+ [% csv_import_report_errors = SELF.report_status.${rownum}.errors %]
<tr class="[% IF csv_import_report_errors.size %]redrow[% ELSE %]listrow[% END %][% 1 - loop.count % 2 %]">
- [%- FOREACH value = row %]
+ [%- FOREACH value = SELF.report_rows.${rownum} %]
<td>[%- value | html %]</td>
[%- END %]
<td>
[%- FOREACH error = csv_import_report_errors %][%- error | html %][% UNLESS loop.last %]<br>[%- END %][%- END %]
- [%- FOREACH info = SELF.report.folded_status.${loop.index}.information %][% IF !loop.first || csv_import_report_errors.size %]<br>[%- END %][%- info | html %][%- END %]
+ [%- FOREACH info = SELF.report_status.${rownum}.information %][% IF !loop.first || csv_import_report_errors.size %]<br>[%- END %][%- info | html %][%- END %]
</td>
</tr>
[%- END %]
</table>
+[%- PROCESS 'common/paginate.html' pages=SELF.pages %]
+
<script type='text/javascript'>
$(function(){ $('#action_import').show() });
</script>