Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Controller / RecordTemplate.pm
diff --git a/SL/Controller/RecordTemplate.pm b/SL/Controller/RecordTemplate.pm
new file mode 100644 (file)
index 0000000..9ce8e15
--- /dev/null
@@ -0,0 +1,128 @@
+package SL::Controller::RecordTemplate;
+
+use strict;
+
+use base qw(SL::Controller::Base);
+
+use SL::Helper::Flash qw(flash);
+use SL::Locale::String qw(t8);
+use SL::DB::RecordTemplate;
+
+use Rose::Object::MakeMethods::Generic (
+  'scalar --get_set_init' => [ qw(template_type template templates data) ],
+);
+
+__PACKAGE__->run_before('check_auth');
+
+my %modules = (
+  ar_transaction => {
+    controller    => 'ar.pl',
+    load_action   => 'load_record_template',
+    save_action   => 'save_record_template',
+    form_selector => '#form',
+  },
+
+  ap_transaction => {
+    controller    => 'ap.pl',
+    load_action   => 'load_record_template',
+    save_action   => 'save_record_template',
+    form_selector => '#form',
+  },
+
+  gl_transaction => {
+    controller    => 'gl.pl',
+    load_action   => 'load_record_template',
+    save_action   => 'save_record_template',
+    form_selector => '#form',
+  },
+);
+
+#
+# actions
+#
+
+sub action_show_dialog {
+  my ($self) = @_;
+  $self
+    ->js
+    ->dialog->open({
+      html   => $self->dialog_html,
+      id     => 'record_template_dialog',
+      dialog => {
+        title => t8('Record templates'),
+      },
+    })
+    ->focus("#template_filter")
+    ->render;
+}
+
+sub action_rename {
+  my ($self) = @_;
+
+  $self->template_type($self->template->template_type);
+  $self->template->update_attributes(template_name => $::form->{template_name});
+
+  $self
+    ->js
+    ->html('#record_template_dialog', $self->dialog_html)
+    ->focus("#record_template_dialog_new_template_name")
+    ->reinit_widgets
+    ->render;
+}
+
+sub action_delete {
+  my ($self) = @_;
+
+  $self->template_type($self->template->template_type);
+  $self->template->delete;
+
+  $self
+    ->js
+    ->html('#record_template_dialog', $self->dialog_html)
+    ->focus("#record_template_dialog_new_template_name")
+    ->reinit_widgets
+    ->render;
+}
+
+sub action_filter_templates {
+  my ($self) = @_;
+
+  $self->{template_filter} = $::form->{template_filter};
+
+  $self
+    ->js
+    ->html('#record_template_dialog', $self->dialog_html)
+    ->focus("#record_template_dialog_new_template_name")
+    ->reinit_widgets
+    ->focus("#template_filter")
+    ->render();
+}
+
+#
+# helpers
+#
+
+sub check_auth {
+  $::auth->assert('ap_transactions | ar_transactions | gl_transactions');
+}
+
+sub init_template_type { $::form->{template_type} or die 'need template_type'   }
+sub init_data          { $modules{ $_[0]->template_type }                       }
+sub init_template      { SL::DB::RecordTemplate->new(id => $::form->{id})->load }
+
+sub init_templates {
+  my ($self) = @_;
+  return scalar SL::DB::Manager::RecordTemplate->get_all_sorted(
+    where => [ template_type => $self->template_type,
+              (template_name => { ilike => '%' . $::form->{template_filter} . '%' })x!! ($::form->{template_filter})
+             ],
+  );
+}
+
+sub dialog_html {
+  my ($self) = @_;
+
+  return $self->render('record_template/dialog', { layout => 0, output => 0 });
+}
+
+1;