use parent qw(SL::Controller::Base);
+use File::Spec ();
+
use SL::ClientJS;
+use SL::Common ();
use SL::Controller::Helper::GetModels;
+use SL::Controller::Helper::Filtered;
use SL::Controller::Helper::Paginated;
use SL::Controller::Helper::Sorted;
use SL::Controller::Helper::ParseFilter;
use SL::Controller::Helper::RequirementSpec;
use SL::DB::Customer;
use SL::DB::Project;
+use SL::DB::ProjectStatus;
+use SL::DB::ProjectType;
use SL::DB::RequirementSpecComplexity;
use SL::DB::RequirementSpecRisk;
use SL::DB::RequirementSpecStatus;
use Rose::Object::MakeMethods::Generic
(
- scalar => [ qw(requirement_spec_item customers types statuses db_args flat_filter visible_item visible_section) ],
- 'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects copy_source js current_text_block_output_position) ],
+ scalar => [ qw(requirement_spec_item visible_item visible_section) ],
+ '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 js
+ current_text_block_output_position) ],
);
__PACKAGE__->run_before('setup');
-__PACKAGE__->run_before('load_select_options', only => [ qw(new ajax_edit create update list) ]);
-
-__PACKAGE__->get_models_url_params('flat_filter');
+__PACKAGE__->make_filtered(
+ MODEL => 'RequirementSpec',
+ LAUNDER_TO => 'filter'
+);
__PACKAGE__->make_paginated(
- MODEL => 'RequirementSpec',
- PAGINATE_ARGS => 'db_args',
- ONLY => [ qw(list) ],
+ MODEL => 'RequirementSpec',
+ ONLY => [ qw(list) ],
);
-
__PACKAGE__->make_sorted(
MODEL => 'RequirementSpec',
ONLY => [ qw(list) ],
sub action_list {
my ($self) = @_;
- $self->setup_db_args_from_filter;
- $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
+ my $requirement_specs = $self->get_models(
+ query => [
+ and => [
+ working_copy_id => undef,
+ is_template => $::form->{is_template} ? 1 : 0,
+ ]],
+ with_objects => [ 'customer', 'type', 'status', 'project' ],
+ );
$self->prepare_report;
-
- my $requirement_specs = $self->get_models(%{ $self->db_args });
-
$self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
}
$self->render('requirement_spec/new', title => $self->requirement_spec->is_template ? t8('Create a new requirement spec template') : t8('Create a new requirement spec'));
}
-sub action_ajax_edit {
+sub action_ajax_show_basic_settings {
my ($self) = @_;
- $self->render('requirement_spec/_form', { layout => 0 }, submit_as => 'ajax');
+ $self->render('requirement_spec/_show_basic_settings', { layout => 0 });
}
-sub action_ajax_show_time_and_cost_estimate {
+sub action_ajax_edit {
my ($self) = @_;
- $self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
+ my $html = $self->render('requirement_spec/_form', { output => 0 }, submit_as => 'ajax');
+
+ $self->js
+ ->hide('#basic_settings')
+ ->after('#basic_settings', $html)
+ ->render($self);
}
-sub action_ajax_cancel_time_and_cost_estimate {
+sub action_ajax_edit_project_link {
my ($self) = @_;
- my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
+ my $html = $self->render('requirement_spec/_project_link_form', { output => 0 }, submit_as => 'ajax');
$self->js
- ->replaceWith('#time_cost_estimate', $html)
- ->render($self);
+ ->hide('#basic_settings')
+ ->after('#basic_settings', $html)
+ ->render($self);
+}
+
+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 });
+ my $first = ($self->requirement_spec->sections_sorted || [])->[0];
+ $first = ($first->children_sorted || [])->[0] if $first;
$self->js
- ->replaceWith('#time_cost_estimate', $html)
+ ->hide('#time_cost_estimate')
+ ->after('#time_cost_estimate', $html)
->on('#time_cost_estimate INPUT[type=text]', 'keydown', 'kivi.requirement_spec.time_cost_estimate_input_key_down')
+ ->action_if($first && $first->id, 'focus', '#time_and_cost_estimate_form_complexity_id_' . $first->id)
->render($self);
}
$self->requirement_spec(SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load);
- my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
+ 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);
+ return $self->js->render($self) if $::form->{keep_open};
+
+ $self->js->remove('#time_cost_estimate_form_container');
+
if ($self->visible_section) {
$html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
$self->js->html('#column-content', $html);
my ($self) = @_;
my $title = $self->requirement_spec->is_template ? t8('Show requirement spec template') : t8('Show requirement spec');
- my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
+ my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections_sorted }[0];
$self->requirement_spec_item($item);
$self->render('requirement_spec/show', title => $title);
$self->create_or_update;
}
+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'";
+}
+
sub action_destroy {
my ($self) = @_;
my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
- $self->requirement_spec->copy_from(
- $versioned_copy,
- version_id => $versioned_copy->version_id,
- );
+ $self->requirement_spec->copy_from($versioned_copy);
+ my $version = $versioned_copy->versions->[0];
+ $version->update_attributes(working_copy_id => $self->requirement_spec->id);
flash_later('info', t8('The requirement spec has been reverted to version #1.', $self->requirement_spec->version->version_number));
$self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render($self);
sub action_create_pdf {
my ($self, %params) = @_;
- my %result = SL::Template::LaTeX->parse_and_create_pdf('requirement_spec.tex', SELF => $self, rspec => $self->requirement_spec);
+ my $base_name = $self->requirement_spec->type->template_file_name || 'requirement_spec';
+ my @pictures = $self->prepare_pictures_for_printing;
+ my %result = SL::Template::LaTeX->parse_and_create_pdf("${base_name}.tex", SELF => $self, rspec => $self->requirement_spec);
+
+ unlink @pictures unless ($::lx_office_conf{debug} || {})->{keep_temp_files};
$::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
sub setup {
my ($self) = @_;
- $::auth->assert('sales_quotation_edit');
+ $::auth->assert('requirement_spec_edit');
$::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
- $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
+ $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu jquery/jquery.hotkeys requirement_spec ckeditor/ckeditor ckeditor/adapters/jquery);
$self->init_visible_section;
return 1;
}
-sub init_complexities {
+sub init_js { SL::ClientJS->new }
+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 }
+sub init_types { SL::DB::Manager::RequirementSpecType->get_all_sorted }
+
+sub init_customers {
my ($self) = @_;
- return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
-}
-sub init_risks {
- my ($self) = @_;
- return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
-}
+ my @filter = ('!obsolete' => 1);
+ @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
-sub init_projects {
- my ($self) = @_;
- $self->projects(SL::DB::Manager::Project->get_all_sorted);
+ return SL::DB::Manager::Customer->get_all_sorted(where => \@filter);
}
sub init_requirement_spec {
$self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
}
-sub init_js {
- my ($self) = @_;
- $self->js(SL::ClientJS->new);
-}
-
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);
}
-sub load_select_options {
- my ($self) = @_;
-
- my @filter = ('!obsolete' => 1);
- @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
-
- $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
- $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
- $self->types( SL::DB::Manager::RequirementSpecType->get_all_sorted);
-}
-
#
# helpers
#
}
})) {
$::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
- @errors = ($::locale->text('Saving failed. Error message from the database: #1'), $db->error);
+ @errors = ($::locale->text('Saving failed. Error message from the database: #1', $db->error));
return $self->js->error(@errors)->render($self) if $::request->is_ajax;
$self->requirement_spec->id(undef) if $is_new;
my $info = $self->requirement_spec->is_template ? t8('The requirement spec template has been saved.') : t8('The requirement spec has been saved.');
if ($::request->is_ajax) {
- my $html = $self->render('requirement_spec/_header', { output => 0 });
+ my $header_html = $self->render('requirement_spec/_header', { output => 0 });
+ my $basics_html = $self->render('requirement_spec/_show_basic_settings', { output => 0 });
return $self->invalidate_version
- ->replaceWith('#requirement-spec-header', $html)
+ ->replaceWith('#requirement-spec-header', $header_html)
+ ->replaceWith('#basic_settings', $basics_html)
+ ->remove('#basic_settings_form')
->flash('info', $info)
->render($self);
}
$self->redirect_to(action => 'show', id => $self->requirement_spec->id);
}
-sub setup_db_args_from_filter {
- my ($self) = @_;
-
- $self->{filter} = {};
- my %args = parse_filter(
- $::form->{filter},
- with_objects => [ 'customer', 'type', 'status', 'project' ],
- launder_to => $self->{filter},
- );
-
- $args{where} = [
- and => [
- @{ $args{where} || [] },
- working_copy_id => undef,
- is_template => $::form->{is_template} ? 1 : 0,
- ]];
-
- $self->db_args(\%args);
-}
-
sub prepare_report {
my ($self) = @_;
sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
status => { sub => sub { $_[0]->status->description } },
type => { sub => sub { $_[0]->type->description } },
- version => { sub => sub { $_[0]->version_id ? $_[0]->version->version_number : t8('Working copy without version') } },
+ version => { sub => sub { $_[0]->version ? $_[0]->version->version_number : t8('Working copy without version') } },
);
}
->jstree->select_node('#tree', '#fb-' . $section->id);
}
+sub prepare_pictures_for_printing {
+ my ($self) = @_;
+
+ my @files;
+ my $userspath = File::Spec->rel2abs($::lx_office_conf{paths}->{userspath});
+ my $target = "${userspath}/kivitendo-print-requirement-spec-picture-" . Common::unique_id() . '-';
+
+ 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;
+}
+
+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.'))
+ ->render($self);
+}
+
+sub update_project_link_new {
+ my ($self) = @_;
+
+ return $self->js
+ ->replaceWith('#project_link_form', $self->render('requirement_spec/_new_project_form', { output => 0 }))
+ ->render($self);
+}
+
+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;
+
+ return $self->js->error(@errors)->render($self) if @errors;
+
+ my $db = $self->requirement_spec->db;
+ if (!$db->do_transaction(sub {
+ $project->save;
+ $self->requirement_spec->update_attributes(project_id => $project->id);
+
+ })) {
+ $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
+ return $self->js->error(t8('Saving failed. Error message from the database: #1', $db->error))->render($self);
+ }
+
+ 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.'))
+ ->render($self);
+}
+
1;