Pflichtenhefte: benutzerdefinierte Variablen anzeigen und bearbeiten
[kivitendo-erp.git] / SL / Controller / RequirementSpecPart.pm
1 package SL::Controller::RequirementSpecPart;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Carp;
8 use List::MoreUtils qw(any);
9
10 use SL::ClientJS;
11 use SL::DB::Customer;
12 use SL::DB::Project;
13 use SL::DB::RequirementSpec;
14 use SL::DB::RequirementSpecPart;
15 use SL::Helper::Flash;
16 use SL::Locale::String;
17
18 use Rose::Object::MakeMethods::Generic
19 (
20   'scalar --get_set_init' => [ qw(requirement_spec js) ],
21 );
22
23 __PACKAGE__->run_before('check_auth');
24
25 #
26 # actions
27 #
28
29 sub action_show {
30   my ($self, %params) = @_;
31
32   $self->render('requirement_spec_part/show', { layout => 0 });
33 }
34
35 sub action_ajax_edit {
36   my ($self, %params) = @_;
37
38   my $html = $self->render('requirement_spec_part/_edit', { output => 0 });
39
40   $self->js
41    ->hide('#additional_parts_list_container')
42    ->after('#additional_parts_list_container', $html)
43    ->on('#edit_additional_parts_form INPUT[type=text]', 'keydown', 'kivi.requirement_spec.additional_parts_input_key_down')
44    ->focus('#additional_parts_add_part_id_name')
45    ->run('kivi.requirement_spec.prepare_edit_additional_parts_form')
46    ->reinit_widgets
47    ->render;
48 }
49
50 sub action_ajax_add {
51   my ($self)  = @_;
52
53   my $part      = SL::DB::Part->new(id => $::form->{part_id})->load(with_objects => [ qw(unit_obj) ]);
54   my $rs_part   = SL::DB::RequirementSpecPart->new(
55     part        => $part,
56     qty         => 1,
57     unit        => $part->unit_obj,
58     description => $part->description,
59   );
60   my $row       = $self->render('requirement_spec_part/_part', { output => 0 }, part => $rs_part);
61
62   $self->js
63    ->val(  '#additional_parts_add_part_id',      '')
64    ->val(  '#additional_parts_add_part_id_name', '')
65    ->focus('#additional_parts_add_part_id_name')
66    ->append('#edit_additional_parts_list tbody', $row)
67    ->hide('#edit_additional_parts_list_empty')
68    ->show('#edit_additional_parts_list')
69    ->render;
70 }
71
72 sub action_ajax_save {
73   my ($self) = @_;
74
75   my $db = $self->requirement_spec->db;
76   $db->do_transaction(sub {
77     # Make Emacs happy
78     1;
79     my $parts    = $::form->{additional_parts} || [];
80     my $position = 1;
81     $_->{position} = $position++ for @{ $parts };
82
83     $self->requirement_spec->update_attributes(parts => $parts)->load;
84
85     1;
86   }) or do {
87     return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render;
88   };
89
90   my $html = $self->render('requirement_spec_part/show', { output => 0 }, initially_hidden => !!$::form->{keep_open});
91
92   $self->js
93     ->replaceWith('#additional_parts_list_container', $html)
94     ->action_if(!$::form->{keep_open}, 'remove', '#additional_parts_form_container')
95     ->render;
96 }
97
98 #
99 # filters
100 #
101
102 sub check_auth {
103   my ($self, %params) = @_;
104   $::auth->assert('requirement_spec_edit');
105 }
106
107 #
108 # helpers
109 #
110
111 sub init_js { SL::ClientJS->new(controller => $_[0]) }
112
113 sub init_requirement_spec {
114   SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load(
115     with_objects => [ qw(parts parts.part parts.unit) ],
116   );
117 }
118
119 1;