X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/8388c9bf9ba2e2a1fe5b49b4797ed74d1a42a1dc..8e082571fb1ea2d8a46b863a764a56fb26823226:/SL/Controller/RequirementSpec.pm diff --git a/SL/Controller/RequirementSpec.pm b/SL/Controller/RequirementSpec.pm index c7f4b5b1d..2a04cf7fb 100644 --- a/SL/Controller/RequirementSpec.pm +++ b/SL/Controller/RequirementSpec.pm @@ -5,7 +5,10 @@ use utf8; 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; @@ -15,6 +18,8 @@ use SL::Controller::Helper::ReportGenerator; 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; @@ -26,12 +31,12 @@ use SL::Template::LaTeX; use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(requirement_spec_item customers types statuses 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__->make_filtered( MODEL => 'RequirementSpec', @@ -107,6 +112,17 @@ sub action_ajax_edit { ->render($self); } +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) + ->render($self); +} + sub action_ajax_show_time_and_cost_estimate { my ($self) = @_; @@ -117,11 +133,14 @@ 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 ->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); } @@ -143,9 +162,12 @@ sub action_ajax_save_time_and_cost_estimate { $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 }); - $self->js->replaceWith('#time_cost_estimate', $html) - ->remove('#time_cost_estimate_form_container'); + 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); @@ -177,6 +199,17 @@ sub action_update { $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) = @_; @@ -208,8 +241,11 @@ sub action_create_pdf { my ($self, %params) = @_; 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}; my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id); @@ -255,25 +291,30 @@ sub setup { $::auth->assert('sales_quotation_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 { @@ -286,27 +327,11 @@ sub init_copy_source { $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 # @@ -342,7 +367,7 @@ sub create_or_update { } })) { $::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; @@ -469,4 +494,77 @@ sub render_first_pasted_section_as_list { ->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;