kivitendo/SL/Controller/RequirementSpec.pm @ 912e5eff
b2e1809f | Moritz Bunkus | package SL::Controller::RequirementSpec;
|
||
use strict;
|
||||
84fc52bd | Moritz Bunkus | use utf8;
|
||
b2e1809f | Moritz Bunkus | |||
use parent qw(SL::Controller::Base);
|
||||
9cddaf37 | Moritz Bunkus | use File::Spec ();
|
||
use SL::Common ();
|
||||
b2e1809f | Moritz Bunkus | use SL::Controller::Helper::GetModels;
|
||
use SL::Controller::Helper::ReportGenerator;
|
||||
c19b1e03 | Moritz Bunkus | use SL::Controller::Helper::RequirementSpec;
|
||
b2e1809f | Moritz Bunkus | use SL::DB::Customer;
|
||
use SL::DB::Project;
|
||||
8e082571 | Moritz Bunkus | use SL::DB::ProjectStatus;
|
||
use SL::DB::ProjectType;
|
||||
c19b1e03 | Moritz Bunkus | use SL::DB::RequirementSpecComplexity;
|
||
use SL::DB::RequirementSpecRisk;
|
||||
b2e1809f | Moritz Bunkus | use SL::DB::RequirementSpecStatus;
|
||
use SL::DB::RequirementSpecType;
|
||||
use SL::DB::RequirementSpec;
|
||||
7c54e92f | Moritz Bunkus | use SL::Helper::CreatePDF qw();
|
||
b2e1809f | Moritz Bunkus | use SL::Helper::Flash;
|
||
use SL::Locale::String;
|
||||
dc8ffeaa | Moritz Bunkus | use SL::System::Process;
|
||
84fc52bd | Moritz Bunkus | use SL::Template::LaTeX;
|
||
b2e1809f | Moritz Bunkus | |||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
8e082571 | Moritz Bunkus | scalar => [ qw(requirement_spec_item visible_item visible_section) ],
|
||
d41efcfe | Sven Schöling | 'scalar --get_set_init' => [ qw(requirement_spec customers types statuses complexities risks projects project_types project_statuses default_project_type default_project_status copy_source
|
||
071e5546 | Moritz Bunkus | current_text_block_output_position models time_based_units html_template cvar_configs includeable_cvar_configs include_cvars) ],
|
||
b2e1809f | Moritz Bunkus | );
|
||
__PACKAGE__->run_before('setup');
|
||||
0ba48077 | Moritz Bunkus | __PACKAGE__->run_before('set_default_filter_args', only => [ qw(list) ]);
|
||
b2e1809f | Moritz Bunkus | |||
237aee7f | Sven Schöling | my %sort_columns = (
|
||
b2e1809f | Moritz Bunkus | customer => t8('Customer'),
|
||
title => t8('Title'),
|
||||
type => t8('Requirement Spec Type'),
|
||||
status => t8('Requirement Spec Status'),
|
||||
projectnumber => t8('Project Number'),
|
||||
51fec310 | Moritz Bunkus | version => t8('Version'),
|
||
mtime => t8('Last modification'),
|
||||
b2e1809f | Moritz Bunkus | );
|
||
#
|
||||
# actions
|
||||
#
|
||||
c19b1e03 | Moritz Bunkus | |||
b2e1809f | Moritz Bunkus | sub action_list {
|
||
my ($self) = @_;
|
||||
0c23c415 | Moritz Bunkus | $self->_setup_search_action_bar;
|
||
b2e1809f | Moritz Bunkus | $self->prepare_report;
|
||
e7913c4c | Moritz Bunkus | $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
|
||
b2e1809f | Moritz Bunkus | }
|
||
sub action_new {
|
||||
my ($self) = @_;
|
||||
9dffe94b | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new(is_template => $::form->{is_template}));
|
||
405a41ef | Moritz Bunkus | |||
if ($self->copy_source) {
|
||||
0276d36b | Moritz Bunkus | $self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate is_template)
|
||
405a41ef | Moritz Bunkus | }
|
||
6f90b1ba | Moritz Bunkus | $self->_setup_form_action_bar;
|
||
9f687789 | Moritz Bunkus | $self->render('requirement_spec/new', title => $self->requirement_spec->is_template ? t8('Create a new requirement spec template') : t8('Create a new requirement spec'));
|
||
b2e1809f | Moritz Bunkus | }
|
||
e8b07984 | Moritz Bunkus | sub action_ajax_show_basic_settings {
|
||
my ($self) = @_;
|
||||
$self->render('requirement_spec/_show_basic_settings', { layout => 0 });
|
||||
}
|
||||
b48939fe | Moritz Bunkus | sub action_ajax_edit {
|
||
b2e1809f | Moritz Bunkus | my ($self) = @_;
|
||
b48939fe | Moritz Bunkus | |||
e8b07984 | Moritz Bunkus | my $html = $self->render('requirement_spec/_form', { output => 0 }, submit_as => 'ajax');
|
||
$self->js
|
||||
->hide('#basic_settings')
|
||||
->after('#basic_settings', $html)
|
||||
10c5bcd8 | Moritz Bunkus | ->reinit_widgets
|
||
d41efcfe | Sven Schöling | ->render;
|
||
b2e1809f | Moritz Bunkus | }
|
||
8e082571 | Moritz Bunkus | sub action_ajax_edit_project_link {
|
||
my ($self) = @_;
|
||||
my $html = $self->render('requirement_spec/_project_link_form', { output => 0 }, submit_as => 'ajax');
|
||||
$self->js
|
||||
->hide('#basic_settings')
|
||||
->after('#basic_settings', $html)
|
||||
d41efcfe | Sven Schöling | ->render;
|
||
8e082571 | Moritz Bunkus | }
|
||
c19b1e03 | Moritz Bunkus | sub action_ajax_show_time_and_cost_estimate {
|
||
my ($self) = @_;
|
||||
$self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
|
||||
}
|
||||
sub action_ajax_edit_time_and_cost_estimate {
|
||||
my ($self) = @_;
|
||||
my $html = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
|
||||
b2e51e71 | Moritz Bunkus | my $first = ($self->requirement_spec->sections_sorted || [])->[0];
|
||
$first = ($first->children_sorted || [])->[0] if $first;
|
||||
c19b1e03 | Moritz Bunkus | |||
13b0468f | Moritz Bunkus | $self->js
|
||
bdf33d88 | Moritz Bunkus | ->hide('#time_cost_estimate')
|
||
->after('#time_cost_estimate', $html)
|
||||
bca12079 | Moritz Bunkus | ->on('#time_cost_estimate INPUT[type=text]', 'keydown', 'kivi.requirement_spec.time_cost_estimate_input_key_down')
|
||
b2e51e71 | Moritz Bunkus | ->action_if($first && $first->id, 'focus', '#time_and_cost_estimate_form_complexity_id_' . $first->id)
|
||
d41efcfe | Sven Schöling | ->render;
|
||
c19b1e03 | Moritz Bunkus | }
|
||
sub action_ajax_save_time_and_cost_estimate {
|
||||
my ($self) = @_;
|
||||
96670fe8 | Moritz Bunkus | $self->requirement_spec->db->with_transaction(sub {
|
||
c19b1e03 | Moritz Bunkus | # Make Emacs happy
|
||
1;
|
||||
foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
|
||||
SL::DB::RequirementSpecItem
|
||||
->new(id => delete $attributes->{id})
|
||||
->load
|
||||
->update_attributes(%{ $attributes });
|
||||
}
|
||||
1;
|
||||
});
|
||||
4f79e0da | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load);
|
||
b8a90763 | Moritz Bunkus | my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 }, initially_hidden => !!$::form->{keep_open});
|
||
$self->js->replaceWith('#time_cost_estimate', $html);
|
||||
d41efcfe | Sven Schöling | return $self->js->render if $::form->{keep_open};
|
||
b8a90763 | Moritz Bunkus | |||
$self->js->remove('#time_cost_estimate_form_container');
|
||||
c19b1e03 | Moritz Bunkus | |||
if ($self->visible_section) {
|
||||
$html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
|
||||
13b0468f | Moritz Bunkus | $self->js->html('#column-content', $html);
|
||
c19b1e03 | Moritz Bunkus | }
|
||
d41efcfe | Sven Schöling | $self->js->render;
|
||
c19b1e03 | Moritz Bunkus | }
|
||
c1569bc1 | Moritz Bunkus | sub action_show {
|
||
my ($self) = @_;
|
||||
4cab0b74 | Moritz Bunkus | my $title = $self->requirement_spec->is_template ? t8('Show requirement spec template') : t8('Show requirement spec');
|
||
1ae68610 | Moritz Bunkus | my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections_sorted }[0];
|
||
c1569bc1 | Moritz Bunkus | $self->requirement_spec_item($item);
|
||
4cab0b74 | Moritz Bunkus | $self->render('requirement_spec/show', title => $title);
|
||
c1569bc1 | Moritz Bunkus | }
|
||
b2e1809f | Moritz Bunkus | sub action_create {
|
||
my ($self) = @_;
|
||||
b48939fe | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new);
|
||
b2e1809f | Moritz Bunkus | $self->create_or_update;
|
||
}
|
||||
sub action_update {
|
||||
my ($self) = @_;
|
||||
$self->create_or_update;
|
||||
}
|
||||
8e082571 | Moritz Bunkus | sub action_update_project_link {
|
||
my $self = shift;
|
||||
my $action = delete($::form->{project_link_action}) || 'keep';
|
||||
return $self->update_project_link_none_keep_existing($action) if $action =~ m{none|keep|existing};
|
||||
return $self->update_project_link_new($action) if $action eq 'new';
|
||||
return $self->update_project_link_create($action) if $action eq 'create';
|
||||
die "Unknown project link action '$action'";
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | sub action_destroy {
|
||
my ($self) = @_;
|
||||
b48939fe | Moritz Bunkus | if (eval { $self->requirement_spec->delete; 1; }) {
|
||
b2e1809f | Moritz Bunkus | flash_later('info', t8('The requirement spec has been deleted.'));
|
||
} else {
|
||||
flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
|
||||
}
|
||||
$self->redirect_to(action => 'list');
|
||||
}
|
||||
c96c4bb2 | Moritz Bunkus | sub action_revert_to {
|
||
my ($self, %params) = @_;
|
||||
d41efcfe | Sven Schöling | return $self->js->error(t8('Cannot revert a versioned copy.'))->render if $self->requirement_spec->working_copy_id;
|
||
c96c4bb2 | Moritz Bunkus | |||
my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
|
||||
13fbd336 | Moritz Bunkus | $self->requirement_spec->copy_from($versioned_copy);
|
||
my $version = $versioned_copy->versions->[0];
|
||||
$version->update_attributes(working_copy_id => $self->requirement_spec->id);
|
||||
c96c4bb2 | Moritz Bunkus | |||
851362c1 | Moritz Bunkus | flash_later('info', t8('The requirement spec has been reverted to version #1.', $versioned_copy->version->version_number));
|
||
d41efcfe | Sven Schöling | $self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render;
|
||
c96c4bb2 | Moritz Bunkus | }
|
||
84fc52bd | Moritz Bunkus | sub action_create_pdf {
|
||
my ($self, %params) = @_;
|
||||
dc8ffeaa | Moritz Bunkus | my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files};
|
||
my $temp_dir = File::Temp->newdir(
|
||||
"kivitendo-print-XXXXXX",
|
||||
DIR => SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath},
|
||||
CLEANUP => !$keep_temp_files,
|
||||
);
|
||||
4ee26bcc | Moritz Bunkus | my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
|
||
dc8ffeaa | Moritz Bunkus | my @pictures = $self->prepare_pictures_for_printing($temp_dir->dirname);
|
||
my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec, userspath => $temp_dir->dirname);
|
||||
84fc52bd | Moritz Bunkus | |||
9cddaf37 | Moritz Bunkus | unlink @pictures unless ($::lx_office_conf{debug} || {})->{keep_temp_files};
|
||
84fc52bd | Moritz Bunkus | $::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
|
||
my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id);
|
||||
$attachment_name .= ' (v' . $self->requirement_spec->version->version_number . ')' if $self->requirement_spec->version;
|
||||
$attachment_name .= '.pdf';
|
||||
$attachment_name =~ s/[^\wäöüÄÖÜß \-\+\(\)\[\]\{\}\.,]+/_/g;
|
||||
$self->send_file($result{file_name}, type => 'application/pdf', name => $attachment_name);
|
||||
unlink $result{file_name};
|
||||
}
|
||||
7c54e92f | Moritz Bunkus | sub action_create_html {
|
||
my ($self, %params) = @_;
|
||||
my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
|
||||
my @pictures = $self->prepare_pictures_for_printing;
|
||||
my $content = SL::Helper::CreatePDF->create_parsed_file(
|
||||
template => "${base_name}.html",
|
||||
format => 'html',
|
||||
template_type => 'HTML',
|
||||
variables => {
|
||||
SELF => $self,
|
||||
rspec => $self->requirement_spec,
|
||||
});
|
||||
# $content is now a scalar of bytes, but $self->render() expects a
|
||||
# scalar of characters.
|
||||
$content = Encode::decode('utf-8', $content);
|
||||
$self->render(\$content, { layout => 0, process => 0 });
|
||||
}
|
||||
4cab0b74 | Moritz Bunkus | sub action_select_template_to_paste {
|
||
my ($self) = @_;
|
||||
31ead75c | Moritz Bunkus | my @templates = @{ SL::DB::Manager::RequirementSpec->get_all(
|
||
where => [ is_template => 1, SL::DB::Manager::RequirementSpec->not_empty_filter ],
|
||||
sort_by => 'lower(requirement_specs.title)',
|
||||
) };
|
||||
4cab0b74 | Moritz Bunkus | $self->render('requirement_spec/select_template_to_paste', { layout => 0 }, TEMPLATES => \@templates);
|
||
}
|
||||
55e399ab | Moritz Bunkus | sub action_paste_template {
|
||
my ($self, %params) = @_;
|
||||
my $template = SL::DB::RequirementSpec->new(id => $::form->{template_id})->load;
|
||||
my %result = $self->requirement_spec->paste_template($template);
|
||||
d41efcfe | Sven Schöling | return $self->js->error($self->requirement_spec->error)->render if !%result;
|
||
55e399ab | Moritz Bunkus | |||
$self->render_pasted_text_block($_) for sort { $a->position <=> $b->position } @{ $result{text_blocks} };
|
||||
$self->render_pasted_section($_) for sort { $a->position <=> $b->position } @{ $result{sections} };
|
||||
if (@{ $result{sections} } && (($::form->{current_content_type} || 'sections') eq 'sections') && !$::form->{current_content_id}) {
|
||||
$self->render_first_pasted_section_as_list($result{sections}->[0]);
|
||||
}
|
||||
31ead75c | Moritz Bunkus | my $parts_list = $self->render('requirement_spec_part/show', { output => 0 });
|
||
$self->js
|
||||
->replaceWith('#additional_parts_list_container', $parts_list)
|
||||
->show( '#additional_parts_list_container')
|
||||
->remove( '#additional_parts_form_container');
|
||||
d41efcfe | Sven Schöling | $self->invalidate_version->render;
|
||
55e399ab | Moritz Bunkus | }
|
||
e822adb3 | Moritz Bunkus | sub action_renumber_sections {
|
||
my ($self) = @_;
|
||||
my %numbers = map { ($_ => 1) } qw(section function_block);
|
||||
my %formats = map { my $method = "${_}_number_format"; ($_ => $self->requirement_spec->type->$method) } qw(section function_block);
|
||||
my @items = @{ $self->requirement_spec->sections_sorted };
|
||||
$self->requirement_spec->db->with_transaction(sub {
|
||||
while (@items) {
|
||||
my $item = shift @items;
|
||||
my $type = $item->parent_id ? 'function_block' : 'section';
|
||||
$item->update_attributes(fb_number => SL::PrefixedNumber->new(number => $formats{$type} || 0)->set_to($numbers{$type}));
|
||||
$numbers{$type}++;
|
||||
unshift @items, @{ $item->children_sorted };
|
||||
}
|
||||
$self->requirement_spec->invalidate_version unless $self->requirement_spec->is_template;
|
||||
1;
|
||||
});
|
||||
$self->redirect_to(action => 'show', id => $self->requirement_spec->id);
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | #
|
||
# filters
|
||||
#
|
||||
sub setup {
|
||||
my ($self) = @_;
|
||||
8b4ff253 | Moritz Bunkus | $::auth->assert('requirement_spec_edit');
|
||
c1569bc1 | Moritz Bunkus | $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
|
||
10c5bcd8 | Moritz Bunkus | $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery kivi.Part kivi.CustomerVendor
|
||
ckeditor/ckeditor ckeditor/adapters/jquery);
|
||||
c19b1e03 | Moritz Bunkus | $self->init_visible_section;
|
||
b2e1809f | Moritz Bunkus | |||
return 1;
|
||||
}
|
||||
8e082571 | Moritz Bunkus | sub init_complexities { SL::DB::Manager::RequirementSpecComplexity->get_all_sorted }
|
||
sub init_default_project_status { SL::DB::Manager::ProjectStatus->find_by(name => 'planning') }
|
||||
sub init_default_project_type { SL::DB::ProjectType->new(id => 1)->load }
|
||||
sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
|
||||
sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
|
||||
sub init_projects { SL::DB::Manager::Project->get_all_sorted }
|
||||
sub init_risks { SL::DB::Manager::RequirementSpecRisk->get_all_sorted }
|
||||
sub init_statuses { SL::DB::Manager::RequirementSpecStatus->get_all_sorted }
|
||||
fec397bf | Moritz Bunkus | sub init_time_based_units { SL::DB::Manager::Unit->time_based_units }
|
||
8e082571 | Moritz Bunkus | sub init_types { SL::DB::Manager::RequirementSpecType->get_all_sorted }
|
||
93cc0157 | Moritz Bunkus | sub init_cvar_configs { SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => 'RequirementSpecs' ]) }
|
||
8e082571 | Moritz Bunkus | |||
sub init_customers {
|
||||
c19b1e03 | Moritz Bunkus | my ($self) = @_;
|
||
8e082571 | Moritz Bunkus | my @filter = ('!obsolete' => 1);
|
||
@filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
|
||||
c19b1e03 | Moritz Bunkus | |||
8e082571 | Moritz Bunkus | return SL::DB::Manager::Customer->get_all_sorted(where => \@filter);
|
||
c19b1e03 | Moritz Bunkus | }
|
||
sub init_requirement_spec {
|
||||
54daa586 | Moritz Bunkus | my ($self) = @_;
|
||
c19b1e03 | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
|
||
54daa586 | Moritz Bunkus | }
|
||
405a41ef | Moritz Bunkus | sub init_copy_source {
|
||
my ($self) = @_;
|
||||
$self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
|
||||
}
|
||||
55e399ab | Moritz Bunkus | sub init_current_text_block_output_position {
|
||
my ($self) = @_;
|
||||
$self->current_text_block_output_position($::form->{current_content_type} !~ m/^(?:text-blocks|tb)-(front|back)/ ? -1 : $1 eq 'front' ? 0 : 1);
|
||||
}
|
||||
071e5546 | Moritz Bunkus | sub init_includeable_cvar_configs {
|
||
my ($self) = @_;
|
||||
return [ grep { $_->includeable } @{ $self->cvar_configs } ];
|
||||
}
|
||||
sub init_include_cvars {
|
||||
my ($self) = @_;
|
||||
e7088e23 | Moritz Bunkus | return { map { ($_->name => $::form->{"include_cvars_" . $_->name}) } @{ $self->cvar_configs } } if $::form->{_include_cvars_from_form};
|
||
071e5546 | Moritz Bunkus | return { map { ($_->name => ($_->includeable && $_->included_by_default)) } @{ $self->cvar_configs } };
|
||
}
|
||||
b2e1809f | Moritz Bunkus | #
|
||
# helpers
|
||||
#
|
||||
sub create_or_update {
|
||||
bd12dbd6 | Moritz Bunkus | my $self = shift;
|
||
my $is_new = !$self->requirement_spec->id;
|
||||
78969dec | Moritz Bunkus | my $previous_customer_id = $self->requirement_spec->customer_id;
|
||
bd12dbd6 | Moritz Bunkus | my $params = delete($::form->{requirement_spec}) || { };
|
||
my $cvars = delete($::form->{cvars}) || { };
|
||||
b2e1809f | Moritz Bunkus | |||
680c9d5c | Moritz Bunkus | # Forcefully make it clear to Rose which custom_variables exist (or don't), so that the ones added with »add_custom_variables« are visible when calling »custom_variables«.
|
||
if ($is_new) {
|
||||
$params->{custom_variables} = [];
|
||||
} else {
|
||||
$self->requirement_spec->custom_variables;
|
||||
}
|
||||
b48939fe | Moritz Bunkus | $self->requirement_spec->assign_attributes(%{ $params });
|
||
b2e1809f | Moritz Bunkus | |||
0a2fb69e | Moritz Bunkus | foreach my $var (@{ $self->requirement_spec->cvars_by_config }) {
|
||
my $value = $cvars->{ $var->config->name };
|
||||
$value = $::form->parse_amount(\%::myconfig, $value) if $var->config->type eq 'number';
|
||||
$var->value($value);
|
||||
}
|
||||
9f687789 | Moritz Bunkus | my $title = $is_new && $self->requirement_spec->is_template ? t8('Create a new requirement spec template')
|
||
9dffe94b | Moritz Bunkus | : $is_new ? t8('Create a new requirement spec')
|
||
9f687789 | Moritz Bunkus | : $self->requirement_spec->is_template ? t8('Edit requirement spec template')
|
||
9dffe94b | Moritz Bunkus | : t8('Edit requirement spec');
|
||
b48939fe | Moritz Bunkus | my @errors = $self->requirement_spec->validate;
|
||
b2e1809f | Moritz Bunkus | |||
if (@errors) {
|
||||
d41efcfe | Sven Schöling | return $self->js->error(@errors)->render if $::request->is_ajax;
|
||
b48939fe | Moritz Bunkus | |||
b2e1809f | Moritz Bunkus | flash('error', @errors);
|
||
b48939fe | Moritz Bunkus | $self->render('requirement_spec/new', title => $title);
|
||
b2e1809f | Moritz Bunkus | return;
|
||
}
|
||||
405a41ef | Moritz Bunkus | my $db = $self->requirement_spec->db;
|
||
96670fe8 | Moritz Bunkus | if (!$db->with_transaction(sub {
|
||
405a41ef | Moritz Bunkus | if ($self->copy_source) {
|
||
$self->requirement_spec($self->copy_source->create_copy(%{ $params }));
|
||||
} else {
|
||||
0a2fb69e | Moritz Bunkus | $self->requirement_spec->save(cascade => 1);
|
||
78969dec | Moritz Bunkus | |||
# If the current requirement spec has versions and the
|
||||
# customer's been changed, then the customer of all the versions
|
||||
# has to be changed, too.
|
||||
if ( !$is_new
|
||||
&& !$self->requirement_spec->is_template
|
||||
&& ($previous_customer_id != $self->requirement_spec->customer_id)) {
|
||||
SL::DB::Manager::RequirementSpec->update_all(
|
||||
set => { customer_id => $self->requirement_spec->customer_id },
|
||||
where => [ working_copy_id => $self->requirement_spec->id ],
|
||||
);
|
||||
}
|
||||
405a41ef | Moritz Bunkus | }
|
||
96670fe8 | Moritz Bunkus | 1;
|
||
405a41ef | Moritz Bunkus | })) {
|
||
$::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
|
||||
76105d15 | Moritz Bunkus | @errors = ($::locale->text('Saving failed. Error message from the database: #1', $db->error));
|
||
d41efcfe | Sven Schöling | return $self->js->error(@errors)->render if $::request->is_ajax;
|
||
405a41ef | Moritz Bunkus | |||
$self->requirement_spec->id(undef) if $is_new;
|
||||
flash('error', @errors);
|
||||
return $self->render('requirement_spec/new', title => $title);
|
||||
}
|
||||
b48939fe | Moritz Bunkus | |||
9f687789 | Moritz Bunkus | my $info = $self->requirement_spec->is_template ? t8('The requirement spec template has been saved.') : t8('The requirement spec has been saved.');
|
||
9dffe94b | Moritz Bunkus | |||
b48939fe | Moritz Bunkus | if ($::request->is_ajax) {
|
||
e8b07984 | Moritz Bunkus | my $header_html = $self->render('requirement_spec/_header', { output => 0 });
|
||
my $basics_html = $self->render('requirement_spec/_show_basic_settings', { output => 0 });
|
||||
b4fc4933 | Moritz Bunkus | return $self->invalidate_version
|
||
e8b07984 | Moritz Bunkus | ->replaceWith('#requirement-spec-header', $header_html)
|
||
->replaceWith('#basic_settings', $basics_html)
|
||||
->remove('#basic_settings_form')
|
||||
9dffe94b | Moritz Bunkus | ->flash('info', $info)
|
||
d41efcfe | Sven Schöling | ->render;
|
||
b48939fe | Moritz Bunkus | }
|
||
b2e1809f | Moritz Bunkus | |||
9dffe94b | Moritz Bunkus | flash_later('info', $info);
|
||
b48939fe | Moritz Bunkus | $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
|
||
b2e1809f | Moritz Bunkus | }
|
||
sub prepare_report {
|
||||
my ($self) = @_;
|
||||
9dffe94b | Moritz Bunkus | my $is_template = $::form->{is_template};
|
||
b2e1809f | Moritz Bunkus | my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
|
||
237aee7f | Sven Schöling | |||
$self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
|
||||
$self->models->finalize; # for filter laundering
|
||||
my $callback = $self->models->get_callback;
|
||||
b2e1809f | Moritz Bunkus | $self->{report} = $report;
|
||
9dffe94b | Moritz Bunkus | my @columns = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime version);
|
||
my @sortable = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime);
|
||||
b2e1809f | Moritz Bunkus | |||
my %column_defs = (
|
||||
b48939fe | Moritz Bunkus | title => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
|
||
1f1ee792 | Moritz Bunkus | mtime => { sub => sub { ($_[0]->mtime || $_[0]->itime)->to_kivitendo(precision => 'minute') } },
|
||
b2e1809f | Moritz Bunkus | );
|
||
9dffe94b | Moritz Bunkus | if (!$is_template) {
|
||
%column_defs = (
|
||||
%column_defs,
|
||||
6ef53d3f | Sven Schöling | customer => { raw_data => sub { $_[0]->customer->presenter->customer(display => 'table-cell', callback => $callback) },
|
||
9dffe94b | Moritz Bunkus | sub => sub { $_[0]->customer->name } },
|
||
6ef53d3f | Sven Schöling | projectnumber => { raw_data => sub { $_[0]->project ? $_[0]->project->presenter->project(display => 'table-cell', callback => $callback) : '' },
|
||
9dffe94b | Moritz Bunkus | sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
|
||
status => { sub => sub { $_[0]->status->description } },
|
||||
type => { sub => sub { $_[0]->type->description } },
|
||||
13fbd336 | Moritz Bunkus | version => { sub => sub { $_[0]->version ? $_[0]->version->version_number : t8('Working copy without version') } },
|
||
9dffe94b | Moritz Bunkus | );
|
||
}
|
||||
071e5546 | Moritz Bunkus | $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) for keys %column_defs;
|
||
if (!$is_template) {
|
||||
my %cvar_column_defs = map {
|
||||
my $cfg = $_;
|
||||
(('cvar_' . $cfg->name) => {
|
||||
sub => sub { my $var = $_[0]->cvar_by_name($cfg->name); $var ? $var->value_as_text : '' },
|
||||
text => $cfg->description,
|
||||
visible => $self->include_cvars->{ $cfg->name } ? 1 : 0,
|
||||
})
|
||||
} @{ $self->includeable_cvar_configs };
|
||||
push @columns, map { 'cvar_' . $_->name } @{ $self->includeable_cvar_configs };
|
||||
%column_defs = (%column_defs, %cvar_column_defs);
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | |||
e7088e23 | Moritz Bunkus | my @cvar_column_form_names = ('_include_cvars_from_form', map { "include_cvars_" . $_->name } @{ $self->includeable_cvar_configs });
|
||
b2e1809f | Moritz Bunkus | $report->set_options(
|
||
std_column_visibility => 1,
|
||||
controller_class => 'RequirementSpec',
|
||||
output_format => 'HTML',
|
||||
9dffe94b | Moritz Bunkus | raw_top_info_text => $self->render('requirement_spec/report_top', { output => 0 }, is_template => $is_template),
|
||
237aee7f | Sven Schöling | raw_bottom_info_text => $self->render('requirement_spec/report_bottom', { output => 0 }, models => $self->models),
|
||
9f687789 | Moritz Bunkus | title => $is_template ? t8('Requirement Spec Templates') : t8('Requirement Specs'),
|
||
b2e1809f | Moritz Bunkus | allow_pdf_export => 1,
|
||
allow_csv_export => 1,
|
||||
);
|
||||
$report->set_columns(%column_defs);
|
||||
$report->set_column_order(@columns);
|
||||
e7088e23 | Moritz Bunkus | $report->set_export_options(qw(list filter), @cvar_column_form_names);
|
||
b2e1809f | Moritz Bunkus | $report->set_options_from_form;
|
||
237aee7f | Sven Schöling | $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
|
||
b2e1809f | Moritz Bunkus | }
|
||
b4fc4933 | Moritz Bunkus | sub invalidate_version {
|
||
my ($self) = @_;
|
||||
my $rspec = SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load;
|
||||
9dffe94b | Moritz Bunkus | return $self->js if $rspec->is_template;
|
||
b4fc4933 | Moritz Bunkus | $rspec->invalidate_version;
|
||
my $html = $self->render('requirement_spec/_version', { output => 0 }, requirement_spec => $rspec);
|
||||
return $self->js->html('#requirement_spec_version', $html);
|
||||
}
|
||||
55e399ab | Moritz Bunkus | sub render_pasted_text_block {
|
||
my ($self, $text_block, %params) = @_;
|
||||
if ($self->current_text_block_output_position == $text_block->output_position) {
|
||||
my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $text_block);
|
||||
$self->js
|
||||
->appendTo($html, '#text-block-list')
|
||||
->hide('#text-block-list-empty');
|
||||
}
|
||||
6ef53d3f | Sven Schöling | my $node = $text_block->presenter->jstree_data;
|
||
55e399ab | Moritz Bunkus | my $front_back = $text_block->output_position == 0 ? 'front' : 'back';
|
||
$self->js
|
||||
->jstree->create_node('#tree', "#tb-${front_back}", 'last', $node)
|
||||
->jstree->open_node( '#tree', "#tb-${front_back}");
|
||||
}
|
||||
0ba48077 | Moritz Bunkus | sub set_default_filter_args {
|
||
my ($self) = @_;
|
||||
f203bc24 | Moritz Bunkus | if (!$::form->{filter} && !$::form->{is_template}) {
|
||
0ba48077 | Moritz Bunkus | $::form->{filter} = {
|
||
status_id => [ map { $_->{id} } grep { $_->name ne 'done' } @{ $self->statuses } ],
|
||||
};
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
55e399ab | Moritz Bunkus | sub render_pasted_section {
|
||
my ($self, $item, $parent_id) = @_;
|
||||
6ef53d3f | Sven Schöling | my $node = $item->presenter->jstree_data;
|
||
55e399ab | Moritz Bunkus | $self->js
|
||
->jstree->create_node('#tree', $parent_id ? "#fb-${parent_id}" : '#sections', 'last', $node)
|
||||
->jstree->open_node( '#tree', $parent_id ? "#fb-${parent_id}" : '#sections');
|
||||
$self->render_pasted_section($_, $item->id) for @{ $item->children_sorted };
|
||||
}
|
||||
sub render_first_pasted_section_as_list {
|
||||
my ($self, $section, %params) = @_;
|
||||
my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $section);
|
||||
$self->js
|
||||
->html('#column-content', $html)
|
||||
->val( '#current_content_type', $section->item_type)
|
||||
->val( '#current_content_id', $section->id)
|
||||
->jstree->select_node('#tree', '#fb-' . $section->id);
|
||||
}
|
||||
9cddaf37 | Moritz Bunkus | sub prepare_pictures_for_printing {
|
||
dc8ffeaa | Moritz Bunkus | my ($self, $userspath) = @_;
|
||
9cddaf37 | Moritz Bunkus | |||
my @files;
|
||||
dc8ffeaa | Moritz Bunkus | $userspath ||= SL::System::Process::exe_dir() . "/" . $::lx_office_conf{paths}->{userspath};
|
||
my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-';
|
||||
9cddaf37 | Moritz Bunkus | |||
foreach my $picture (map { @{ $_->pictures } } @{ $self->requirement_spec->text_blocks }) {
|
||||
my $output_file_name = $target . $picture->id . '.' . $picture->get_default_file_name_extension;
|
||||
$picture->{print_file_name} = File::Spec->abs2rel($output_file_name, $userspath);
|
||||
my $out = IO::File->new($output_file_name, 'w') || die("Could not create file " . $output_file_name);
|
||||
$out->binmode;
|
||||
$out->print($picture->picture_content);
|
||||
$out->close;
|
||||
push @files, $output_file_name;
|
||||
}
|
||||
return @files;
|
||||
}
|
||||
8e082571 | Moritz Bunkus | sub update_project_link_none_keep_existing {
|
||
my ($self, $action) = @_;
|
||||
$self->requirement_spec->update_attributes(project_id => undef) if $action eq 'none';
|
||||
$self->requirement_spec->update_attributes(project_id => $::form->{new_project_id}) if $action eq 'existing';
|
||||
return $self->invalidate_version
|
||||
->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
|
||||
->remove('#project_link_form')
|
||||
->flash('info', t8('The project link has been updated.'))
|
||||
d41efcfe | Sven Schöling | ->render;
|
||
8e082571 | Moritz Bunkus | }
|
||
sub update_project_link_new {
|
||||
my ($self) = @_;
|
||||
return $self->js
|
||||
->replaceWith('#project_link_form', $self->render('requirement_spec/_new_project_form', { output => 0 }))
|
||||
d41efcfe | Sven Schöling | ->render;
|
||
8e082571 | Moritz Bunkus | }
|
||
sub update_project_link_create {
|
||||
my ($self) = @_;
|
||||
my $params = delete($::form->{project}) || {};
|
||||
my $project = SL::DB::Project->new(
|
||||
%{ $params },
|
||||
valid => 1,
|
||||
active => 1,
|
||||
);
|
||||
my @errors = $project->validate;
|
||||
d41efcfe | Sven Schöling | return $self->js->error(@errors)->render if @errors;
|
||
8e082571 | Moritz Bunkus | |||
my $db = $self->requirement_spec->db;
|
||||
96670fe8 | Moritz Bunkus | if (!$db->with_transaction(sub {
|
||
8e082571 | Moritz Bunkus | $project->save;
|
||
$self->requirement_spec->update_attributes(project_id => $project->id);
|
||||
})) {
|
||||
$::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
|
||||
d41efcfe | Sven Schöling | return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render;
|
||
8e082571 | Moritz Bunkus | }
|
||
return $self->invalidate_version
|
||||
->replaceWith('#basic_settings', $self->render('requirement_spec/_show_basic_settings', { output => 0 }))
|
||||
->remove('#project_link_form')
|
||||
->flash('info', t8('The project has been created.'))
|
||||
->flash('info', t8('The project link has been updated.'))
|
||||
d41efcfe | Sven Schöling | ->render;
|
||
8e082571 | Moritz Bunkus | }
|
||
237aee7f | Sven Schöling | sub init_models {
|
||
my ($self) = @_;
|
||||
SL::Controller::Helper::GetModels->new(
|
||||
controller => $self,
|
||||
sorted => {
|
||||
_default => {
|
||||
by => 'customer',
|
||||
dir => 1,
|
||||
},
|
||||
%sort_columns,
|
||||
},
|
||||
query => [
|
||||
and => [
|
||||
working_copy_id => undef,
|
||||
is_template => $::form->{is_template} ? 1 : 0,
|
||||
],
|
||||
],
|
||||
with_objects => [ 'customer', 'type', 'status', 'project' ],
|
||||
);
|
||||
}
|
||||
7c54e92f | Moritz Bunkus | sub init_html_template {
|
||
my ($self) = @_;
|
||||
my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
|
||||
my $template = SL::Helper::CreatePDF->find_template(name => $base_name, extension => 'html');
|
||||
return !!$template;
|
||||
}
|
||||
6f90b1ba | Moritz Bunkus | sub _setup_form_action_bar {
|
||
my ($self) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
t8('Save'),
|
||||
submit => [ '#basic_settings_form', { action => 'RequirementSpec/' . ($self->requirement_spec->id ? 'update' : 'create') } ],
|
||||
accesskey => 'enter',
|
||||
],
|
||||
link => [
|
||||
t8('Abort'),
|
||||
link => $self->url_for(action => 'list', is_template => $self->requirement_spec->is_template),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
0c23c415 | Moritz Bunkus | sub _setup_search_action_bar {
|
||
my ($self, %params) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
action => [
|
||||
953f63b0 | Moritz Bunkus | t8('Update'),
|
||
0c23c415 | Moritz Bunkus | submit => [ '#search_form', { action => 'RequirementSpec/list' } ],
|
||
accesskey => 'enter',
|
||||
],
|
||||
link => [
|
||||
t8('Add'),
|
||||
link => $self->url_for(action => 'new', is_template => $::form->{is_template}),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | 1;
|