Pflichtenheft Zeit-/Kostenschätzung: nach Speichern Form entfernen, nicht nur verstecken
[kivitendo-erp.git] / SL / Controller / RequirementSpec.pm
index 75d8034..3acb6b9 100644 (file)
@@ -26,7 +26,7 @@ use SL::Template::LaTeX;
 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) ],
+  'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects copy_source js current_text_block_output_position) ],
 );
 
 __PACKAGE__->run_before('setup');
@@ -86,26 +86,27 @@ sub action_new {
   $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_show_time_and_cost_estimate {
   my ($self) = @_;
 
-  my $html   = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
-
-  $self->js
-   ->replaceWith('#time_cost_estimate', $html)
-   ->render($self);
+  $self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
 }
 
 sub action_ajax_edit_time_and_cost_estimate {
@@ -114,7 +115,9 @@ sub action_ajax_edit_time_and_cost_estimate {
   my $html   = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
 
   $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')
    ->render($self);
 }
 
@@ -134,8 +137,11 @@ sub action_ajax_save_time_and_cost_estimate {
     1;
   });
 
+  $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);
+  $self->js->replaceWith('#time_cost_estimate', $html)
+           ->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);
@@ -148,10 +154,11 @@ sub action_ajax_save_time_and_cost_estimate {
 sub action_show {
   my ($self) = @_;
 
-  my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
+  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];
   $self->requirement_spec_item($item);
 
-  $self->render('requirement_spec/show', title => t8('Show requirement spec'));
+  $self->render('requirement_spec/show', title => $title);
 }
 
 sub action_create {
@@ -210,6 +217,31 @@ sub action_create_pdf {
   unlink $result{file_name};
 }
 
+sub action_select_template_to_paste {
+  my ($self) = @_;
+
+  my @templates = grep { @{ $_->sections } || @{ $_->text_blocks } } @{ SL::DB::Manager::RequirementSpec->get_all(where => [ is_template => 1 ], sort_by => 'lower(title)') };
+  $self->render('requirement_spec/select_template_to_paste', { layout => 0 }, TEMPLATES => \@templates);
+}
+
+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);
+
+  return $self->js->error($self->requirement_spec->error)->render($self) if !%result;
+
+  $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]);
+  }
+
+  $self->invalidate_version->render($self);
+}
+
 #
 # filters
 #
@@ -255,6 +287,11 @@ sub init_js {
   $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) = @_;
 
@@ -312,9 +349,12 @@ sub create_or_update {
   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);
   }
@@ -406,4 +446,43 @@ sub invalidate_version {
   return $self->js->html('#requirement_spec_version', $html);
 }
 
+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');
+  }
+
+  my $node       = $self->presenter->requirement_spec_text_block_jstree_data($text_block);
+  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}");
+}
+
+sub render_pasted_section {
+  my ($self, $item, $parent_id) = @_;
+
+  my $node = $self->presenter->requirement_spec_item_jstree_data($item);
+  $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);
+}
+
 1;