popups ohne layout rendern
[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(no_layout => $::form->{POPUP_MODE});
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      = $::request->{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   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
304
305   my $idx      = 0;
306   my $callback = build_std_url('action=report', grep { $form->{$_} } @report_params);
307   my $edit_url = build_std_url('action=edit', 'callback=' . E($callback));
308
309   foreach my $fu (@{ $follow_ups }) {
310     $idx++;
311
312     $fu->{done} = $fu->{done} ? $locale->text('Yes') : $locale->text('No');
313
314     my $row = { map { $_ => { 'data' => $fu->{$_} } } keys %{ $fu } };
315
316     $row->{selected} = {
317       'raw_data' =>   $cgi->hidden('-name' => "follow_up_id_${idx}", '-value' => $fu->{id})
318                     . $cgi->checkbox('-name' => "selected_${idx}",   '-value' => 1, '-label' => ''),
319       'valign'   => 'center',
320       'align'    => 'center',
321     };
322
323     if (@{ $fu->{LINKS} }) {
324       my $link = $fu->{LINKS}->[0];
325
326       $row->{title}->{data} = $link->{title};
327       $row->{title}->{link} = $link->{url};
328     }
329
330     $row->{subject}->{link} = $edit_url . '&id=' . Q($fu->{id});
331
332     $report->add_data($row);
333   }
334
335   $report->generate_with_headers();
336
337   $main::lxdebug->leave_sub();
338 }
339
340 sub report_for_todo_list {
341   $main::lxdebug->enter_sub();
342
343   my $form     = $main::form;
344
345   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);
346
347   my %params   = (
348     'due_only'          => 1,
349     'not_done'          => 1,
350     'created_for_login' => $form->{login},
351     );
352
353   my $follow_ups = FU->follow_ups(%params);
354   my $content;
355
356   if (@{ $follow_ups }) {
357     my $callback = build_std_url('action');
358     my $edit_url = build_std_url('script=fu.pl', 'action=edit', 'callback=' . E($callback)) . '&id=';
359
360     foreach my $fu (@{ $follow_ups }) {
361       if (@{ $fu->{LINKS} }) {
362         my $link = $fu->{LINKS}->[0];
363
364         $fu->{reference}      = $link->{title};
365         $fu->{reference_link} = $link->{url};
366       }
367     }
368
369     $content = $form->parse_html_template('fu/report_for_todo_list', { 'FOLLOW_UPS' => $follow_ups,
370                                                                        'callback'   => $callback,
371                                                                        'edit_url'   => $edit_url, });
372   }
373
374   $main::lxdebug->leave_sub();
375
376   return $content;
377 }
378
379 sub edit_access_rights {
380   $main::lxdebug->enter_sub();
381
382   my $form     = $main::form;
383   my $locale   = $main::locale;
384
385   my $access = FU->retrieve_access_rights();
386
387   $form->{EMPLOYEES} = SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
388
389   map { $_->{access} = $access->{$_->{id}} } @{ $form->{EMPLOYEES} };
390
391   $form->{title} = $locale->text('Edit Access Rights for Follow-Ups');
392
393   $form->header();
394   print $form->parse_html_template('fu/edit_access_rights');
395
396   $main::lxdebug->leave_sub();
397 }
398
399 sub save_access_rights {
400   $main::lxdebug->enter_sub();
401
402   my $form     = $main::form;
403   my $locale   = $main::locale;
404
405   my %access;
406
407   foreach my $i (1 .. $form->{rowcount}) {
408     my $id = $form->{"employee_id_$i"};
409
410     $access{$id} = 1 if ($id && $form->{"access_$id"});
411   }
412
413   FU->save_access_rights('access' => \%access);
414
415   $form->{SAVED_MESSAGE} = $locale->text('The access rights have been saved.');
416   edit_access_rights();
417
418   $main::lxdebug->leave_sub();
419 }
420
421 sub update {
422   call_sub($main::form->{nextsub});
423 }
424
425 sub continue {
426   call_sub($main::form->{nextsub});
427 }
428
429 sub save {
430   if ($main::form->{save_nextsub}) {
431     call_sub($main::form->{save_nextsub});
432   } else {
433     save_follow_up();
434   }
435 }
436
437 sub dispatcher {
438   $main::lxdebug->enter_sub();
439
440   my $form     = $main::form;
441   my $locale   = $main::locale;
442
443   foreach my $action (qw(finish save delete)) {
444     if ($form->{"action_${action}"}) {
445       call_sub($action);
446       return;
447     }
448   }
449
450   call_sub($form->{default_action}) if ($form->{default_action});
451
452   $form->error($locale->text('No action defined.'));
453 }
454
455 1;