draft strict v2
[kivitendo-erp.git] / bin / mozilla / drafts.pl
1 #======================================================================
2 # LX-Office ERP
3 #
4 #======================================================================
5 #
6 # Saving and loading drafts
7 #
8 #======================================================================
9
10 use YAML;
11
12 use SL::Drafts;
13
14 require "bin/mozilla/common.pl";
15
16 use strict;
17
18 sub save_draft {
19   $main::lxdebug->enter_sub();
20
21   my $form     = $main::form;
22   my %myconfig = %main::myconfig;
23   my $locale   = $main::locale;
24
25   if (!$form->{draft_id} && !$form->{draft_description}) {
26     restore_form($form->{SAVED_FORM}, 1) if ($form->{SAVED_FORM});
27     delete $form->{SAVED_FORM};
28
29     $form->{SAVED_FORM}   = save_form(qw(stylesheet login password));
30     $form->{remove_draft} = 1;
31
32     $form->header();
33     print($form->parse_html_template("drafts/save_new"));
34
35     return $main::lxdebug->leave_sub();
36   }
37
38   my ($draft_id, $draft_description) = ($form->{draft_id}, $form->{draft_description});
39
40   restore_form($form->{SAVED_FORM}, 1);
41   delete $form->{SAVED_FORM};
42
43   Drafts->save(\%myconfig, $form, $draft_id, $draft_description);
44
45   $form->{saved_message} = $locale->text("Draft saved.");
46
47   update();
48
49   $main::lxdebug->leave_sub();
50 }
51
52 sub remove_draft {
53   $main::lxdebug->enter_sub();
54
55   my $form     = $main::form;
56   my %myconfig = %main::myconfig;
57
58   Drafts->remove(\%myconfig, $form, $form->{draft_id}) if ($form->{draft_id});
59
60   delete @{$form}{qw(draft_id draft_description)};
61
62   $main::lxdebug->leave_sub();
63 }
64
65 sub load_draft_maybe {
66   $main::lxdebug->enter_sub();
67
68   my $form     = $main::form;
69   my %myconfig = %main::myconfig;
70
71   $main::lxdebug->leave_sub() and return 0 if ($form->{DONT_LOAD_DRAFT});
72
73   my ($draft_nextsub) = @_;
74
75   my @drafts = Drafts->list(\%myconfig, $form);
76
77   $main::lxdebug->leave_sub() and return 0 unless (@drafts);
78
79   $draft_nextsub = "add" unless ($draft_nextsub);
80
81   delete $form->{action};
82   my $saved_form = save_form(qw(stylesheet login password));
83
84   $form->header();
85   print($form->parse_html_template("drafts/load",
86                                    { "DRAFTS"        => \@drafts,
87                                      "SAVED_FORM"    => $saved_form,
88                                      "draft_nextsub" => $draft_nextsub }));
89
90   $main::lxdebug->leave_sub();
91
92   return 1;
93 }
94
95 sub dont_load_draft {
96   $main::lxdebug->enter_sub();
97
98   my $form     = $main::form;
99
100   my $draft_nextsub = $form->{draft_nextsub} || "add";
101
102   restore_form($form->{SAVED_FORM}, 1);
103   delete $form->{SAVED_FORM};
104
105   $form->{DONT_LOAD_DRAFT} = 1;
106
107   call_sub($draft_nextsub);
108
109   $main::lxdebug->leave_sub();
110 }
111
112 sub load_draft {
113   $main::lxdebug->enter_sub();
114
115   my $form     = $main::form;
116   my %myconfig = %main::myconfig;
117
118   my ($old_form, $id, $description) = Drafts->load(\%myconfig, $form, $form->{id});
119
120   if ($old_form) {
121     $old_form = YAML::Load($old_form);
122
123     my %dont_save_vars      = map { $_ => 1 } @Drafts::dont_save;
124     my @restore_vars        = grep { !$dont_save_vars{$_} } keys %{ $old_form };
125
126     @{$form}{@restore_vars} = @{$old_form}{@restore_vars};
127
128     $form->{draft_id}              = $id;
129     $form->{draft_description}     = $description;
130     $form->{remove_draft}          = 'checked';
131   }
132
133   update();
134
135   $main::lxdebug->leave_sub();
136 }
137
138 sub delete_drafts {
139   $main::lxdebug->enter_sub();
140
141   my $form     = $main::form;
142   my %myconfig = %main::myconfig;
143
144   my @ids;
145   foreach (keys %{$form}) {
146     push @ids, $1 if (/^checked_(.*)/ && $form->{$_});
147   }
148   Drafts->remove(\%myconfig, $form, @ids) if (@ids);
149
150   restore_form($form->{SAVED_FORM}, 1);
151   delete $form->{SAVED_FORM};
152
153   add();
154
155   $main::lxdebug->leave_sub();
156 }
157
158 sub draft_action_dispatcher {
159   $main::lxdebug->enter_sub();
160
161   my $form     = $main::form;
162   my $locale   = $main::locale;
163
164   if ($form->{draft_action} eq $locale->text("Skip")) {
165     dont_load_draft();
166
167   } elsif ($form->{draft_action} eq $locale->text("Delete drafts")) {
168     delete_drafts();
169   }
170
171   $main::lxdebug->leave_sub();
172 }
173
174 1;