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