Part Controller - callbacks für Artikel speichern und löschen
[kivitendo-erp.git] / SL / Controller / RecordTemplate.pm
1 package SL::Controller::RecordTemplate;
2
3 use strict;
4
5 use base qw(SL::Controller::Base);
6
7 use SL::Helper::Flash qw(flash);
8 use SL::Locale::String qw(t8);
9 use SL::DB::RecordTemplate;
10
11 use Rose::Object::MakeMethods::Generic (
12   'scalar --get_set_init' => [ qw(template_type template templates data) ],
13 );
14
15 __PACKAGE__->run_before('check_auth');
16
17 my %modules = (
18   ar_transaction => {
19     controller    => 'ar.pl',
20     load_action   => 'load_record_template',
21     save_action   => 'save_record_template',
22     form_selector => '#form',
23   },
24
25   ap_transaction => {
26     controller    => 'ap.pl',
27     load_action   => 'load_record_template',
28     save_action   => 'save_record_template',
29     form_selector => '#form',
30   },
31
32   gl_transaction => {
33     controller    => 'gl.pl',
34     load_action   => 'load_record_template',
35     save_action   => 'save_record_template',
36     form_selector => '#form',
37   },
38 );
39
40 #
41 # actions
42 #
43
44 sub action_show_dialog {
45   my ($self) = @_;
46   $self
47     ->js
48     ->dialog->open({
49       html   => $self->dialog_html,
50       id     => 'record_template_dialog',
51       dialog => {
52         title => t8('Record templates'),
53       },
54     })
55     ->focus("#record_template_dialog_new_template_name")
56     ->render;
57 }
58
59 sub action_rename {
60   my ($self) = @_;
61
62   $self->template_type($self->template->template_type);
63   $self->template->update_attributes(template_name => $::form->{template_name});
64
65   $self
66     ->js
67     ->html('#record_template_dialog', $self->dialog_html)
68     ->focus("#record_template_dialog_new_template_name")
69     ->reinit_widgets
70     ->render;
71 }
72
73 sub action_delete {
74   my ($self) = @_;
75
76   $self->template_type($self->template->template_type);
77   $self->template->delete;
78
79   $self
80     ->js
81     ->html('#record_template_dialog', $self->dialog_html)
82     ->focus("#record_template_dialog_new_template_name")
83     ->reinit_widgets
84     ->render;
85 }
86
87 #
88 # helpers
89 #
90
91 sub check_auth {
92   $::auth->assert('ap_transactions | ar_transactions | gl_transactions');
93 }
94
95 sub init_template_type { $::form->{template_type} or die 'need template_type'   }
96 sub init_data          { $modules{ $_[0]->template_type }                       }
97 sub init_template      { SL::DB::RecordTemplate->new(id => $::form->{id})->load }
98
99 sub init_templates {
100   my ($self) = @_;
101
102   return scalar SL::DB::Manager::RecordTemplate->get_all_sorted(
103     where => [ template_type => $self->template_type ],
104   );
105 }
106
107 sub dialog_html {
108   my ($self) = @_;
109
110   return $self->render('record_template/dialog', { layout => 0, output => 0 });
111 }
112
113 1;