1 package SL::Controller::CustomDataExportDesigner;
6 use parent qw(SL::Controller::Base);
8 use List::UtilsBy qw(sort_by);
10 use SL::DB::CustomDataExportQuery;
11 use SL::Helper::Flash qw(flash_later);
12 use SL::Locale::String qw(t8);
14 use Rose::Object::MakeMethods::Generic
16 'scalar --get_set_init' => [ qw(query queries access_rights) ],
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('setup_javascripts');
29 $self->setup_list_action_bar;
30 $self->render('custom_data_export_designer/list', title => $::locale->text('Design custom data export queries'));
36 my $title = $self->query->id ? t8('Edit custom data export query') : t8('Add custom data export query');
38 $self->setup_edit_action_bar;
39 $self->render('custom_data_export_designer/edit', title => $title);
42 sub action_edit_parameters {
45 my $title = $self->query->id ? t8('Edit custom data export query') : t8('Add custom data export query');
46 my @parameters = $self->gather_query_data;
48 $self->setup_edit_parameters_action_bar;
49 $self->render('custom_data_export_designer/edit_parameters', title => $title, PARAMETERS => \@parameters);
55 my @parameters = $self->gather_query_data;
57 $self->query->parameters(\@parameters);
61 flash_later('info', t8('The custom data export has been saved.'));
63 $self->redirect_to($self->url_for(action => 'list'));
71 flash_later('info', t8('The custom data export has been deleted.'));
73 $self->redirect_to($self->url_for(action => 'list'));
81 $::auth->assert('custom_data_export_designer');
84 sub setup_javascripts {
85 $::request->layout->add_javascripts('kivi.Validator.js', 'kivi.CustomDataExportDesigner.js');
92 sub init_query { $::form->{id} ? SL::DB::CustomDataExportQuery->new(id => $::form->{id})->load : SL::DB::CustomDataExportQuery->new }
93 sub init_queries { scalar SL::DB::Manager::CustomDataExportQuery->get_all_sorted }
95 sub init_access_rights {
96 my @rights = ([ '', t8('Available to all users') ]);
99 foreach my $right ($::auth->all_rights_full) {
100 # name, description, category
103 $category = t8($right->[1]);
104 } elsif ($category) {
105 push @rights, [ $right->[0], sprintf('%s → %s [%s]', $category, t8($right->[1]), $right->[0]) ];
112 sub setup_list_action_bar {
115 for my $bar ($::request->layout->get('actionbar')) {
119 link => $self->url_for(action => 'edit'),
120 accesskey => 'enter',
126 sub setup_edit_action_bar {
129 for my $bar ($::request->layout->get('actionbar')) {
133 submit => [ '#form', { action => 'CustomDataExportDesigner/edit_parameters' } ],
134 checks => [ 'kivi.validate_form' ],
135 accesskey => 'enter',
139 submit => [ '#form', { action => 'CustomDataExportDesigner/delete' } ],
140 confirm => t8('Do you really want to delete this object?'),
141 disabled => !$self->query->id ? t8('This object has not been saved yet.')
146 call => [ 'kivi.history_back' ],
152 sub setup_edit_parameters_action_bar {
155 for my $bar ($::request->layout->get('actionbar')) {
159 submit => [ '#form', { action => 'CustomDataExportDesigner/save' } ],
160 checks => [ 'kivi.validate_form' ],
161 accesskey => 'enter',
165 call => [ 'kivi.history_back' ],
171 sub gather_query_data {
174 $self->query->$_($::form->{query}->{$_}) for qw(name description sql_query access_right);
175 return $self->gather_query_parameters;
178 sub gather_query_parameters {
181 my %used_parameter_names = map { ($_ => 1) } $self->query->used_parameter_names;
182 my @existing_parameters = grep { $used_parameter_names{$_->name} } @{ $self->query->parameters // [] };
183 my %parameters_by_name = map { ($_->name => $_) } @existing_parameters;
184 $parameters_by_name{$_} //= SL::DB::CustomDataExportQueryParameter->new(name => $_, parameter_type => 'text', default_value_type => 'none') for keys %used_parameter_names;
186 foreach my $parameter_data (@{ $::form->{parameters} // [] }) {
187 my $parameter_obj = $parameters_by_name{ $parameter_data->{name} };
188 next unless $parameter_obj;
190 $parameter_obj->$_($parameter_data->{$_}) for qw(parameter_type description default_value_type default_value);
193 return sort_by { lc $_->name } values %parameters_by_name;