marei: headline/colspec keys for SimpleTabular
[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 => [ 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->with_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   }) or do {
85     return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render;
86   };
87
88   my $html = $self->render('requirement_spec_part/show', { output => 0 }, initially_hidden => !!$::form->{keep_open});
89
90   $self->js
91     ->replaceWith('#additional_parts_list_container', $html)
92     ->action_if(!$::form->{keep_open}, 'remove', '#additional_parts_form_container')
93     ->render;
94 }
95
96 #
97 # filters
98 #
99
100 sub check_auth {
101   my ($self, %params) = @_;
102   $::auth->assert('requirement_spec_edit');
103 }
104
105 #
106 # helpers
107 #
108
109 sub init_js { SL::ClientJS->new(controller => $_[0]) }
110
111 sub init_requirement_spec {
112   SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load(
113     with_objects => [ qw(parts parts.part parts.unit) ],
114   );
115 }
116
117 1;