Aufrufe von 'exit' durch eigene Funktion '::end_of_request()' ersetzt.
[kivitendo-erp.git] / bin / mozilla / fu.pl
1 use POSIX qw(strftime);
2
3 use SL::FU;
4 use SL::ReportGenerator;
5
6 require "bin/mozilla/reportgenerator.pl";
7
8 use strict;
9
10 sub _collect_links {
11   $main::lxdebug->enter_sub();
12
13   my $dest = shift;
14
15   my $form     = $main::form;
16
17   $dest->{LINKS} = [];
18
19   foreach my $i (1 .. $form->{trans_rowcount}) {
20     next if (!$form->{"trans_id_$i"} || !$form->{"trans_type_$i"});
21
22     push @{ $dest->{LINKS} }, { map { +"trans_$_" => $form->{"trans_${_}_$i"} } qw(id type info) };
23   }
24
25   $main::lxdebug->leave_sub();
26 }
27
28 sub add {
29   $main::lxdebug->enter_sub();
30
31   my $form     = $main::form;
32   my %myconfig = %main::myconfig;
33   my $locale   = $main::locale;
34
35   _collect_links($form);
36
37   $form->get_employee($form->get_standard_dbh(\%myconfig));
38   $form->{created_for_user} = $form->{employee_id};
39
40   my $link_details;
41
42   if (0 < scalar @{ $form->{LINKS} }) {
43     $link_details = FU->link_details(%{ $form->{LINKS}->[0] });
44   }
45
46   if ($link_details && $link_details->{title}) {
47     $form->{title} = $locale->text('Add Follow-Up for #1', $link_details->{title});
48   } else {
49     $form->{title} = $locale->text('Add Follow-Up');
50   }
51
52   display_form();
53
54   $main::lxdebug->leave_sub();
55 }
56
57 sub edit {
58   $main::lxdebug->enter_sub();
59
60   my $form     = $main::form;
61   my $locale   = $main::locale;
62
63   my $ref = FU->retrieve('id' => $form->{id});
64
65   if (!$ref) {
66     $form->error($locale->text("Invalid follow-up ID."));
67   }
68
69   map { $form->{$_} = $ref->{$_} } keys %{ $ref };
70
71   if (@{ $form->{LINKS} } && $form->{LINKS}->[0]->{title}) {
72     $form->{title} = $locale->text('Edit Follow-Up for #1', $form->{LINKS}->[0]->{title});
73   } else {
74     $form->{title} = $locale->text('Edit Follow-Up');
75   }
76
77   display_form();
78
79   $main::lxdebug->leave_sub();
80 }
81
82 sub display_form {
83   $main::lxdebug->enter_sub();
84
85   my $form     = $main::form;
86
87   $form->get_lists("employees" => "EMPLOYEES");
88
89   my %params;
90   $params{not_id}     = $form->{id} if ($form->{id});
91   $params{trans_id}   = $form->{LINKS}->[0]->{trans_id} if (@{ $form->{LINKS} });
92   $form->{FOLLOW_UPS} = FU->follow_ups(%params);
93
94   $form->{jsscript}   = 1;
95
96   $form->header();
97   print $form->parse_html_template('fu/add_edit');
98
99   $main::lxdebug->leave_sub();
100 }
101
102 sub save_follow_up {
103   $main::lxdebug->enter_sub();
104
105   my $form     = $main::form;
106   my $locale   = $main::locale;
107
108   $form->isblank('created_for_user', $locale->text('You must chose a user.'));
109   $form->isblank('follow_up_date',   $locale->text('The follow-up date is missing.'));
110   $form->isblank('subject',          $locale->text('The subject is missing.'));
111
112   my %params = (map({ $_ => $form->{$_} } qw(id subject body note_id created_for_user follow_up_date)), 'done' => 0);
113
114   _collect_links(\%params);
115
116   FU->save(%params);
117
118   if ($form->{POPUP_MODE}) {
119     $form->header();
120     print $form->parse_html_template('fu/close_window');
121     ::end_of_request();
122   }
123
124   $form->{SAVED_MESSAGE} = $locale->text('Follow-Up saved.');
125
126   if ($form->{callback}) {
127     $form->redirect();
128   }
129
130   delete @{$form}{qw(id subject body created_for_user follow_up_date)};
131
132   map { $form->{$_} = 1 } qw(due_only all_users not_done);
133
134   report();
135
136   $main::lxdebug->leave_sub();
137 }
138
139 sub finish {
140   $main::lxdebug->enter_sub();
141
142   my $form     = $main::form;
143   my $locale   = $main::locale;
144
145   if ($form->{id}) {
146     my $ref = FU->retrieve('id' => $form->{id});
147
148     if (!$ref) {
149       $form->error($locale->text("Invalid follow-up ID."));
150     }
151
152     FU->finish('id' => $form->{id});
153
154   } else {
155     foreach my $i (1..$form->{rowcount}) {
156       next unless ($form->{"selected_$i"} && $form->{"follow_up_id_$i"});
157
158       FU->finish('id' => $form->{"follow_up_id_$i"});
159     }
160   }
161
162   if ($form->{POPUP_MODE}) {
163     $form->header();
164     print $form->parse_html_template('fu/close_window');
165     ::end_of_request();
166   }
167
168   $form->redirect() if ($form->{callback});
169
170   report();
171
172   $main::lxdebug->leave_sub();
173 }
174
175 sub delete {
176   $main::lxdebug->enter_sub();
177
178   my $form     = $main::form;
179   my $locale   = $main::locale;
180
181   if ($form->{id}) {
182     my $ref = FU->retrieve('id' => $form->{id});
183
184     if (!$ref) {
185       $form->error($locale->text("Invalid follow-up ID."));
186     }
187
188     FU->delete('id' => $form->{id});
189
190   } else {
191     foreach my $i (1..$form->{rowcount}) {
192       next unless ($form->{"selected_$i"} && $form->{"follow_up_id_$i"});
193
194       FU->delete('id' => $form->{"follow_up_id_$i"});
195     }
196   }
197
198   if ($form->{POPUP_MODE}) {
199     $form->header();
200     print $form->parse_html_template('fu/close_window');
201     ::end_of_request();
202   }
203
204   $form->redirect() if ($form->{callback});
205
206   report();
207
208   $main::lxdebug->leave_sub();
209 }
210
211 sub search {
212   $main::lxdebug->enter_sub();
213
214   my $form     = $main::form;
215   my $locale   = $main::locale;
216
217   $form->get_lists("employees" => "EMPLOYEES");
218
219   $form->{jsscript} = 1;
220   $form->{title}    = $locale->text('Follow-Ups');
221
222   $form->header();
223   print $form->parse_html_template('fu/search');
224
225   $main::lxdebug->leave_sub();
226 }
227
228 sub report {
229   $main::lxdebug->enter_sub();
230
231   my $form     = $main::form;
232   my %myconfig = %main::myconfig;
233   my $locale   = $main::locale;
234   my $cgi      = $main::cgi;
235
236   my @report_params = qw(created_for subject body reference follow_up_date_from follow_up_date_to itime_from itime_to due_only all_users done not_done);
237
238   report_generator_set_default_sort('follow_up_date', 1);
239
240   my $follow_ups    = FU->follow_ups(map { $_ => $form->{$_} } @report_params);
241   $form->{rowcount} = scalar @{ $follow_ups };
242
243   $form->{title}    = $locale->text('Follow-Ups');
244
245   my %column_defs = (
246     'selected'              => { 'text' => '', },
247     'follow_up_date'        => { 'text' => $locale->text('Follow-Up Date'), },
248     'created_on'            => { 'text' => $locale->text('Created on'), },
249     'title'                 => { 'text' => $locale->text('Reference'), },
250     'subject'               => { 'text' => $locale->text('Subject'), },
251     'created_by_name'       => { 'text' => $locale->text('Created by'), },
252     'created_for_user_name' => { 'text' => $locale->text('Follow-up for'), },
253     'done'                  => { 'text' => $locale->text('Done'), 'visible' => $form->{done} && $form->{not_done} ? 1 : 0 },
254   );
255
256   my @columns = qw(selected follow_up_date created_on subject title created_by_name created_for_user_name done);
257   my $href    = build_std_url('action=report', grep { $form->{$_} } @report_params);
258
259   foreach my $name (qw(follow_up_date created_on title subject)) {
260     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
261     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
262   }
263
264   my @options;
265
266   if ($form->{created_for}) {
267     $form->get_lists("employees" => "EMPLOYEES");
268
269     foreach my $employee (@{ $form->{EMPLOYEES} }) {
270       if ($employee->{id} == $form->{created_for}) {
271         push @options, $locale->text('Created for') . " : " . ($employee->{name} ? "$employee->{name} ($employee->{login})" : $employee->{login});
272         last;
273       }
274     }
275   }
276
277   push @options, $locale->text('Subject')                  . " : $form->{subject}"   if ($form->{subject});
278   push @options, $locale->text('Body')                     . " : $form->{body}"      if ($form->{body});
279   push @options, $locale->text('Reference')                . " : $form->{reference}" if ($form->{reference});
280   push @options, $locale->text('Done')                                               if ($form->{done});
281   push @options, $locale->text('Not done yet')                                       if ($form->{not_done});
282   push @options, $locale->text('Only due follow-ups')                                if ($form->{due_only});
283   push @options, $locale->text("Other users' follow-ups")                            if ($form->{all_users});
284
285   my @hidden_report_params = map { +{ 'key' => $_, 'value' => $form->{$_} } } @report_params;
286
287   my $report = SL::ReportGenerator->new(\%myconfig, $form, 'std_column_visibility' => 1);
288
289   $report->set_columns(%column_defs);
290   $report->set_column_order(@columns);
291
292   $report->set_export_options('report', @report_params, qw(sort sortdir));
293
294   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
295
296   $report->set_options('raw_top_info_text'    => $form->parse_html_template('fu/report_top',    { 'OPTIONS' => \@options }),
297                        'raw_bottom_info_text' => $form->parse_html_template('fu/report_bottom', { 'HIDDEN'  => \@hidden_report_params }),
298                        'output_format'        => 'HTML',
299                        'title'                => $form->{title},
300                        'attachment_basename'  => $locale->text('follow_up_list') . strftime('_%Y%m%d', localtime time),
301     );
302   $report->set_options_from_form();
303
304   my $idx      = 0;
305   my $callback = build_std_url('action=report', grep { $form->{$_} } @report_params);
306   my $edit_url = build_std_url('action=edit', 'callback=' . E($callback));
307
308   foreach my $fu (@{ $follow_ups }) {
309     $idx++;
310
311     $fu->{done} = $fu->{done} ? $locale->text('Yes') : $locale->text('No');
312
313     my $row = { map { $_ => { 'data' => $fu->{$_} } } keys %{ $fu } };
314
315     $row->{selected} = {
316       'raw_data' =>   $cgi->hidden('-name' => "follow_up_id_${idx}", '-value' => $fu->{id})
317                     . $cgi->checkbox('-name' => "selected_${idx}",   '-value' => 1, '-label' => ''),
318       'valign'   => 'center',
319       'align'    => 'center',
320     };
321
322     if (@{ $fu->{LINKS} }) {
323       my $link = $fu->{LINKS}->[0];
324
325       $row->{title}->{data} = $link->{title};
326       $row->{title}->{link} = $link->{url};
327     }
328
329     $row->{subject}->{link} = $edit_url . '&id=' . Q($fu->{id});
330
331     $report->add_data($row);
332   }
333
334   $report->generate_with_headers();
335
336   $main::lxdebug->leave_sub();
337 }
338
339 sub report_for_todo_list {
340   $main::lxdebug->enter_sub();
341
342   my $form     = $main::form;
343
344   my @report_params = qw(created_for subject body reference follow_up_date_from follow_up_date_to itime_from itime_to due_only all_users done not_done);
345
346   my %params   = (
347     'due_only'          => 1,
348     'not_done'          => 1,
349     'created_for_login' => $form->{login},
350     );
351
352   my $follow_ups = FU->follow_ups(%params);
353   my $content;
354
355   if (@{ $follow_ups }) {
356     my $callback = build_std_url('action');
357     my $edit_url = build_std_url('script=fu.pl', 'action=edit', 'callback=' . E($callback)) . '&id=';
358
359     foreach my $fu (@{ $follow_ups }) {
360       if (@{ $fu->{LINKS} }) {
361         my $link = $fu->{LINKS}->[0];
362
363         $fu->{reference}      = $link->{title};
364         $fu->{reference_link} = $link->{url};
365       }
366     }
367
368     $content = $form->parse_html_template('fu/report_for_todo_list', { 'FOLLOW_UPS' => $follow_ups,
369                                                                        'callback'   => $callback,
370                                                                        'edit_url'   => $edit_url, });
371   }
372
373   $main::lxdebug->leave_sub();
374
375   return $content;
376 }
377
378 sub edit_access_rights {
379   $main::lxdebug->enter_sub();
380
381   my $form     = $main::form;
382   my $locale   = $main::locale;
383
384   my $access = FU->retrieve_access_rights();
385
386   $form->get_lists("employees" => "EMPLOYEES");
387
388   map { $_->{access} = $access->{$_->{id}} } @{ $form->{EMPLOYEES} };
389
390   $form->{title} = $locale->text('Edit Access Rights for Follow-Ups');
391
392   $form->header();
393   print $form->parse_html_template('fu/edit_access_rights');
394
395   $main::lxdebug->leave_sub();
396 }
397
398 sub save_access_rights {
399   $main::lxdebug->enter_sub();
400
401   my $form     = $main::form;
402   my $locale   = $main::locale;
403
404   my %access;
405
406   foreach my $i (1 .. $form->{rowcount}) {
407     my $id = $form->{"employee_id_$i"};
408
409     $access{$id} = 1 if ($id && $form->{"access_$id"});
410   }
411
412   FU->save_access_rights('access' => \%access);
413
414   $form->{SAVED_MESSAGE} = $locale->text('The access rights have been saved.');
415   edit_access_rights();
416
417   $main::lxdebug->leave_sub();
418 }
419
420 sub update {
421   call_sub($main::form->{nextsub});
422 }
423
424 sub continue {
425   call_sub($main::form->{nextsub});
426 }
427
428 sub save {
429   if ($main::form->{save_nextsub}) {
430     call_sub($main::form->{save_nextsub});
431   } else {
432     save_follow_up();
433   }
434 }
435
436 sub dispatcher {
437   $main::lxdebug->enter_sub();
438
439   my $form     = $main::form;
440   my $locale   = $main::locale;
441
442   foreach my $action (qw(finish save delete)) {
443     if ($form->{"action_${action}"}) {
444       call_sub($action);
445       return;
446     }
447   }
448
449   call_sub($form->{default_action}) if ($form->{default_action});
450
451   $form->error($locale->text('No action defined.'));
452 }
453
454 1;