1 package SL::Controller::RequirementSpecPart;
5 use parent qw(SL::Controller::Base);
8 use List::MoreUtils qw(any);
13 use SL::DB::RequirementSpec;
14 use SL::DB::RequirementSpecPart;
15 use SL::Helper::Flash;
16 use SL::Locale::String;
18 use Rose::Object::MakeMethods::Generic
20 'scalar --get_set_init' => [ qw(requirement_spec js) ],
23 __PACKAGE__->run_before('check_auth');
30 my ($self, %params) = @_;
32 $self->render('requirement_spec_part/show', { layout => 0 });
35 sub action_ajax_edit {
36 my ($self, %params) = @_;
38 my $html = $self->render('requirement_spec_part/_edit', { output => 0 });
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')
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(
57 unit => $part->unit_obj,
58 description => $part->description,
60 my $row = $self->render('requirement_spec_part/_part', { output => 0 }, part => $rs_part);
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')
72 sub action_ajax_save {
75 my $db = $self->requirement_spec->db;
76 $db->do_transaction(sub {
79 my $parts = $::form->{additional_parts} || [];
81 $_->{position} = $position++ for @{ $parts };
83 $self->requirement_spec->update_attributes(parts => $parts)->load;
87 return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render;
90 my $html = $self->render('requirement_spec_part/show', { output => 0 }, initially_hidden => !!$::form->{keep_open});
93 ->replaceWith('#additional_parts_list_container', $html)
94 ->action_if(!$::form->{keep_open}, 'remove', '#additional_parts_form_container')
103 my ($self, %params) = @_;
104 $::auth->assert('requirement_spec_edit');
111 sub init_js { SL::ClientJS->new(controller => $_[0]) }
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) ],