Sprache auf ungültig setzen im Admin-Bereich
[kivitendo-erp.git] / SL / Controller / RequirementSpecTextBlock.pm
index f52cbc8..ca34ede 100644 (file)
@@ -8,7 +8,6 @@ use Carp;
 use Params::Validate ();
 use Time::HiRes ();
 
-use SL::ClientJS;
 use SL::Clipboard;
 use SL::Controller::Helper::RequirementSpec;
 use SL::DB::RequirementSpec;
@@ -18,12 +17,15 @@ use SL::DB::RequirementSpecTextBlock;
 use SL::Helper::Flash;
 use SL::Locale::String;
 
+use constant SORTABLE_PICTURE_LIST => 'kivi.requirement_spec.make_text_block_picture_lists_sortable';
+
 use Rose::Object::MakeMethods::Generic
 (
-  scalar                  => [ qw(text_block picture) ],
-  'scalar --get_set_init' => [ qw(predefined_texts js) ],
+  scalar                  => [ qw(text_block) ],
+  'scalar --get_set_init' => [ qw(predefined_texts picture) ],
 );
 
+__PACKAGE__->run_before('check_auth');
 __PACKAGE__->run_before('load_requirement_spec_text_block', only => [qw(ajax_edit ajax_update ajax_delete ajax_flag dragged_and_dropped ajax_copy ajax_add_picture)]);
 
 #
@@ -48,7 +50,9 @@ sub action_ajax_list {
 
   $self->show_list(output_position => $new_where, id => $::form->{clicked_id}, set_type => 1) if ($new_where != ($current_where // -1));
 
-  $self->render($self->js);
+  $self->js
+    ->run(SORTABLE_PICTURE_LIST())
+    ->render($self);
 }
 
 sub action_ajax_add {
@@ -61,7 +65,7 @@ sub action_ajax_add {
 
   $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id}, requirement_spec_id => $::form->{requirement_spec_id});
 
-  $self->invalidate_version->render($self);
+  $self->invalidate_version->render;
 }
 
 sub action_ajax_edit {
@@ -81,6 +85,7 @@ sub action_ajax_edit {
      ->insertAfter($html, '#text-block-' . $self->text_block->id)
      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
+     ->reinit_widgets
      ->render($self);
 }
 
@@ -97,14 +102,16 @@ sub action_ajax_create {
   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
 
   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
-  my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
+  my $node = $self->text_block->presenter->jstree_data;
 
   $self->invalidate_version
     ->hide('#text-block-list-empty')
     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
+    ->run(SORTABLE_PICTURE_LIST())
     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id);
   $self->add_new_text_block_form(output_position => $self->text_block->output_position, insert_after_id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id)
+    ->reinit_widgets
     ->render($self);
 }
 
@@ -128,7 +135,11 @@ sub action_ajax_update {
   $self->invalidate_version
     ->remove('#' . $prefix . '_form')
     ->replaceWith('#text-block-' . $self->text_block->id, $html)
+    ->run(SORTABLE_PICTURE_LIST())
     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
+    ->prop('#tb-' . $self->text_block->id . ' a', 'title', $self->text_block->content_excerpt)
+    ->addClass('#tb-' . $self->text_block->id . ' a', 'tooltip')
+    ->reinit_widgets
     ->render($self);
 }
 
@@ -173,7 +184,7 @@ sub action_dragged_and_dropped {
   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
   my $old_where          = $self->text_block->output_position;
 
-  $self->text_block->db->do_transaction(sub {
+  $self->text_block->db->with_transaction(sub {
     1;
     $self->text_block->remove_from_list;
     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
@@ -221,7 +232,9 @@ sub action_dragged_and_dropped {
     }
   }
 
-  $self->js->render($self);
+  $self->js
+    ->run(SORTABLE_PICTURE_LIST())
+    ->render($self);
 }
 
 sub action_ajax_copy {
@@ -234,13 +247,18 @@ sub action_ajax_copy {
 sub action_ajax_paste {
   my ($self, %params) = @_;
 
-  my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecTextBlock$/);
+  my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpec(?:TextBlock|Picture)$/);
   if (!$copied) {
     return SL::ClientJS->new
       ->error(t8("The clipboard does not contain anything that can be pasted here."))
       ->render($self);
   }
 
+  if (ref($copied) =~ m/Picture$/) {
+    $self->load_requirement_spec_text_block;
+    return $self->paste_picture($copied);
+  }
+
   my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
   my $new_output_position     = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
   my $front_back              = 0 == $new_output_position ? 'front' : 'back';
@@ -254,8 +272,9 @@ sub action_ajax_paste {
     $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
   }
 
-  my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
+  my $node = $self->text_block->presenter->jstree_data;
   $self->invalidate_version
+    ->run(SORTABLE_PICTURE_LIST())
     ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
     ->render($self);
 }
@@ -274,7 +293,6 @@ sub action_ajax_add_picture {
 sub action_ajax_edit_picture {
   my ($self) = @_;
 
-  $self->picture(SL::DB::RequirementSpecPicture->new(id => $::form->{picture_id})->load);
   $self->text_block($self->picture->text_block);
   $self->render('requirement_spec_text_block/_picture_form', { layout => 0 });
 }
@@ -304,7 +322,6 @@ sub action_ajax_update_picture {
   my ($self)     = @_;
 
   my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
-  $self->picture(SL::DB::RequirementSpecPicture->new(id => $::form->{id})->load);
 
   if (!$attributes->{picture_content}) {
     delete $attributes->{picture_content};
@@ -332,7 +349,6 @@ sub action_ajax_update_picture {
 sub action_ajax_delete_picture {
   my ($self) = @_;
 
-  $self->picture(SL::DB::RequirementSpecPicture->new(id => $::form->{id})->load);
   $self->picture->delete;
   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $self->picture->text_block_id)->load);
 
@@ -345,14 +361,48 @@ sub action_ajax_delete_picture {
 sub action_ajax_download_picture {
   my ($self) = @_;
 
-  $self->picture(SL::DB::RequirementSpecPicture->new(id => $::form->{id})->load);
   $self->send_file(\$self->picture->{picture_content}, type => $self->picture->picture_content_type, name => $self->picture->picture_file_name);
 }
 
+sub action_ajax_copy_picture {
+  my ($self, %params) = @_;
+
+  SL::Clipboard->new->copy($self->picture);
+  SL::ClientJS->new->render($self);
+}
+
+sub action_ajax_paste_picture {
+  my ($self, %params) = @_;
+
+  my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecPicture$/);
+  if (!$copied) {
+    return SL::ClientJS->new
+      ->error(t8("The clipboard does not contain anything that can be pasted here."))
+      ->render($self);
+  }
+
+  $self->text_block($self->picture->text_block);   # Save text block via the picture the user clicked on
+
+  $self->paste_picture($copied);
+}
+
+sub action_reorder_pictures {
+  my ($self) = @_;
+
+  SL::DB::RequirementSpecPicture->reorder_list(@{ $::form->{picture_id} || [] });
+
+  $self->render(\'', { type => 'json' });
+}
+
 #
 # filters
 #
 
+sub check_auth {
+  my ($self) = @_;
+  $::auth->assert('requirement_spec_edit');
+}
+
 sub load_requirement_spec_text_block {
   my ($self) = @_;
   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
@@ -376,12 +426,11 @@ sub output_position_from_id {
 }
 
 sub init_predefined_texts {
-  return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
+  return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted(where => [ useable_for_text_blocks => 1 ]);
 }
 
-sub init_js {
-  my ($self) = @_;
-  $self->js(SL::ClientJS->new);
+sub init_picture {
+  return SL::DB::RequirementSpecPicture->new(id => $::form->{picture_id} || $::form->{id})->load;
 }
 
 sub invalidate_version {
@@ -408,6 +457,7 @@ sub add_new_text_block_form {
 
   $self->js
      ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
+     ->reinit_widgets
      ->focus('#' . $id_base . '_title');
 }
 
@@ -429,4 +479,25 @@ sub show_list {
   return $self->set_function_blocks_tab_menu_class(class => 'text-block-context-menu');
 }
 
+sub paste_picture {
+  my ($self, $copied) = @_;
+
+  if (!$self->text_block->db->with_transaction(sub {
+    1;
+    $self->picture($copied->to_object)->save;        # Create new picture from copied data and save
+    $self->text_block->add_pictures($self->picture); # Add new picture to text block
+    $self->text_block->save;
+  })) {
+    $::lxdebug->message(LXDebug::WARN(), "Error: " . $self->text_block->db->error);
+    return $self->js->error($::locale->text('Saving failed. Error message from the database: #1', $self->text_block->db->error))->render;
+  }
+
+  my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
+
+  $self->invalidate_version
+    ->append('#text-block-' . $self->text_block->id . '-pictures', $html)
+    ->show('#text-block-' . $self->text_block->id . '-pictures')
+    ->render;
+}
+
 1;