1 package SL::Controller::RequirementSpecTextBlock;
 
   5 use parent qw(SL::Controller::Base);
 
   8 use Params::Validate ();
 
  13 use SL::Controller::Helper::RequirementSpec;
 
  14 use SL::DB::RequirementSpec;
 
  15 use SL::DB::RequirementSpecPicture;
 
  16 use SL::DB::RequirementSpecPredefinedText;
 
  17 use SL::DB::RequirementSpecTextBlock;
 
  18 use SL::Helper::Flash;
 
  19 use SL::Locale::String;
 
  21 use constant SORTABLE_PICTURE_LIST => 'kivi.requirement_spec.make_text_block_picture_lists_sortable';
 
  23 use Rose::Object::MakeMethods::Generic
 
  25   scalar                  => [ qw(text_block) ],
 
  26   'scalar --get_set_init' => [ qw(predefined_texts js picture) ],
 
  29 __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)]);
 
  35 sub action_ajax_list {
 
  39   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
 
  42   if ($::form->{clicked_type} =~ m/^text-blocks-(front|back)/) {
 
  43     $new_where = $1 eq 'front' ? 0 : 1;
 
  46     $new_where = $self->output_position_from_id($::form->{clicked_id});
 
  49   # $::lxdebug->message(0, "cur $current_where new $new_where");
 
  51   $self->show_list(output_position => $new_where, id => $::form->{clicked_id}, set_type => 1) if ($new_where != ($current_where // -1));
 
  54     ->run(SORTABLE_PICTURE_LIST())
 
  61   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
 
  62   my $new_where     = $self->output_position_from_id($::form->{id})                                                  // $::form->{output_position};
 
  64   $self->show_list(output_position => $new_where) if $new_where != $current_where;
 
  66   $self->add_new_text_block_form(output_position => $new_where, insert_after_id => $::form->{id}, requirement_spec_id => $::form->{requirement_spec_id});
 
  68   $self->invalidate_version->render($self);
 
  71 sub action_ajax_edit {
 
  74   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
 
  76   if ($self->text_block->output_position != $current_where) {
 
  77     $self->show_list(output_position => $self->text_block->output_position, id => $self->text_block->id, requirement_spec_id => $self->text_block->requirement_spec_id);
 
  80   my $html = $self->render('requirement_spec_text_block/_form', { output => 0 });
 
  83      ->hide('#text-block-' . $self->text_block->id)
 
  84      ->remove('#edit_text_block_' . $self->text_block->id . '_form')
 
  85      ->insertAfter($html, '#text-block-' . $self->text_block->id)
 
  86      ->jstree->select_node('#tree', '#tb-' . $self->text_block->id)
 
  87      ->focus('#edit_text_block_' . $self->text_block->id . '_title')
 
  91 sub action_ajax_create {
 
  92   my ($self, %params) = @_;
 
  94   my $attributes   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
 
  95   my $insert_after = delete $attributes->{insert_after};
 
  97   my @errors = $self->text_block(SL::DB::RequirementSpecTextBlock->new(%{ $attributes }))->validate;
 
  98   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
 
 100   $self->text_block->save;
 
 101   $self->text_block->add_to_list(position => 'after', reference => $insert_after) if $insert_after;
 
 103   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
 104   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
 
 106   $self->invalidate_version
 
 107     ->hide('#text-block-list-empty')
 
 108     ->replaceWith('#' . $::form->{form_prefix} . '_form', $html)
 
 109     ->run(SORTABLE_PICTURE_LIST())
 
 110     ->jstree->create_node('#tree', $insert_after ? ('#tb-' . $insert_after, 'after') : ('#tb-' . ($attributes->{output_position} == 0 ? 'front' : 'back'), 'last'), $node)
 
 111     ->jstree->select_node('#tree', '#tb-' . $self->text_block->id);
 
 112   $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)
 
 117 sub action_ajax_update {
 
 118   my ($self, %params) = @_;
 
 120   my $prefix     = $::form->{form_prefix} || 'text_block';
 
 121   my $attributes = $::form->{$prefix}     || {};
 
 123   foreach (qw(requirement_spec_id output_position position)) {
 
 124     delete $attributes->{$_} if !defined $attributes->{$_};
 
 127   my @errors = $self->text_block->assign_attributes(%{ $attributes })->validate;
 
 128   return SL::ClientJS->new->error(@errors)->render($self) if @errors;
 
 130   $self->text_block->save;
 
 132   my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
 134   $self->invalidate_version
 
 135     ->remove('#' . $prefix . '_form')
 
 136     ->replaceWith('#text-block-' . $self->text_block->id, $html)
 
 137     ->run(SORTABLE_PICTURE_LIST())
 
 138     ->jstree->rename_node('#tree', '#tb-' . $self->text_block->id, $self->text_block->title)
 
 139     ->prop('#tb-' . $self->text_block->id . ' a', 'title', $self->text_block->content_excerpt)
 
 140     ->addClass('#tb-' . $self->text_block->id . ' a', 'tooltip')
 
 145 sub action_ajax_delete {
 
 148   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
 
 149   if ($self->text_block->output_position == $current_where) {
 
 151        ->remove('#edit_text_block_' . $self->text_block->id . '_form')
 
 152        ->remove('#text-block-' . $self->text_block->id);
 
 154     $self->js->show('#text-block-list-empty') if 1 == scalar @{ $self->text_block->get_full_list };
 
 157   $self->text_block->delete;
 
 159   $self->invalidate_version
 
 160      ->jstree->delete_node('#tree', '#tb-' . $self->text_block->id)
 
 164 sub action_ajax_flag {
 
 167   $self->text_block->update_attributes(is_flagged => !$self->text_block->is_flagged);
 
 169   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
 
 172    ->action_if($current_where == $self->text_block->output_position, 'toggleClass', '#text-block-' . $self->text_block->id, 'flagged')
 
 173    ->toggleClass('#tb-' . $self->text_block->id, 'flagged')
 
 177 sub action_dragged_and_dropped {
 
 180   my $position           = $::form->{position} =~ m/^ (?: before | after | last ) $/x ? $::form->{position}                                                      : die "Unknown 'position' parameter";
 
 181   my $dropped_text_block = $position           =~ m/^ (?: before | after ) $/x        ? SL::DB::RequirementSpecTextBlock->new(id => $::form->{dropped_id})->load : undef;
 
 183   my $dropped_type       = $position ne 'last' ? undef : $::form->{dropped_type} =~ m/^ text-blocks- (?:front|back) $/x ? $::form->{dropped_type} : die "Unknown 'dropped_type' parameter";
 
 184   my $old_where          = $self->text_block->output_position;
 
 186   $self->text_block->db->do_transaction(sub {
 
 188     $self->text_block->remove_from_list;
 
 189     $self->text_block->output_position($position =~ m/before|after/ ? $dropped_text_block->output_position : $::form->{dropped_type} eq 'text-blocks-front' ? 0 : 1);
 
 190     $self->text_block->add_to_list(position => $position, reference => $dropped_text_block ? $dropped_text_block->id : undef);
 
 193   # $::lxdebug->dump(0, "form", $::form);
 
 195   $self->invalidate_version
 
 196     ->jstree->open_node('#tree', '#tb-' . (!$self->text_block->output_position ? 'front' : 'back'));
 
 198   return $self->js->render($self) if $::form->{current_content_type} !~ m/^text-block/;
 
 200   my $current_where = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type}) // -1;
 
 201   my $new_where     = $self->text_block->output_position;
 
 202   my $id            = $self->text_block->id;
 
 204   # $::lxdebug->message(0, "old $old_where current $current_where new $new_where current_CID " . $::form->{current_content_id} . ' selfid ' . $self->text_block->id);
 
 205   if (($old_where != $new_where) && ($::form->{current_content_id} == $self->text_block->id)) {
 
 206     # The currently selected text block is dragged to the opposite
 
 207     # text block location. Re-render the whole content column.
 
 208     $self->show_list(output_position => $new_where, id => $id);
 
 211     if ($old_where == $current_where) {
 
 212       $self->js->remove('#text-block-' . $self->text_block->id);
 
 214       if (0 == scalar(@{ SL::DB::Manager::RequirementSpecTextBlock->get_all(where => [ requirement_spec_id => $self->text_block->requirement_spec_id, output_position => $current_where ]) })) {
 
 215         $self->js->show('#text-block-list-empty');
 
 219     if ($new_where == $current_where) {
 
 220       $self->js->hide('#text-block-list-empty');
 
 222       my $html             = "" . $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
 224       my $prior_text_block = $self->text_block->get_previous_in_list;
 
 226       if ($prior_text_block) {
 
 227         $self->js->insertAfter($html, '#text-block-' . $prior_text_block->id);
 
 229         $self->js->prependTo($html, '#text-block-list');
 
 235     ->run(SORTABLE_PICTURE_LIST())
 
 239 sub action_ajax_copy {
 
 240   my ($self, %params) = @_;
 
 242   SL::Clipboard->new->copy($self->text_block);
 
 243   SL::ClientJS->new->render($self);
 
 246 sub action_ajax_paste {
 
 247   my ($self, %params) = @_;
 
 249   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpec(?:TextBlock|Picture)$/);
 
 251     return SL::ClientJS->new
 
 252       ->error(t8("The clipboard does not contain anything that can be pasted here."))
 
 256   if (ref($copied) =~ m/Picture$/) {
 
 257     $self->load_requirement_spec_text_block;
 
 258     return $self->paste_picture($copied);
 
 261   my $current_output_position = $self->output_position_from_id($::form->{current_content_id}, $::form->{current_content_type});
 
 262   my $new_output_position     = $::form->{id} ? $self->output_position_from_id($::form->{id}) : $::form->{output_position};
 
 263   my $front_back              = 0 == $new_output_position ? 'front' : 'back';
 
 265   $self->text_block($copied->to_object);
 
 266   $self->text_block->update_attributes(requirement_spec_id => $::form->{requirement_spec_id}, output_position => $new_output_position);
 
 267   $self->text_block->add_to_list(position => 'after', reference => $::form->{id}) if $::form->{id};
 
 269   if ($current_output_position == $new_output_position) {
 
 270     my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $self->text_block);
 
 271     $self->js->action($::form->{id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($::form->{id} || 'list'));
 
 274   my $node = $self->presenter->requirement_spec_text_block_jstree_data($self->text_block);
 
 275   $self->invalidate_version
 
 276     ->run(SORTABLE_PICTURE_LIST())
 
 277     ->jstree->create_node('#tree', $::form->{id} ? ('#tb-' . $::form->{id}, 'after') : ("#tb-${front_back}", 'last'), $node)
 
 282 # actions for pictures
 
 285 sub action_ajax_add_picture {
 
 288   $self->picture(SL::DB::RequirementSpecPicture->new);
 
 289   $self->render('requirement_spec_text_block/_picture_form', { layout => 0 });
 
 292 sub action_ajax_edit_picture {
 
 295   $self->text_block($self->picture->text_block);
 
 296   $self->render('requirement_spec_text_block/_picture_form', { layout => 0 });
 
 299 sub action_ajax_create_picture {
 
 300   my ($self, %params)              = @_;
 
 302   my $attributes                   = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
 
 303   $attributes->{picture_file_name} = ((($::form->{ATTACHMENTS} || {})->{ $::form->{form_prefix} } || {})->{picture_content} || {})->{filename};
 
 304   my @errors                       = $self->picture(SL::DB::RequirementSpecPicture->new(%{ $attributes }))->validate;
 
 306   return $self->js->error(@errors)->render($self) if @errors;
 
 308   $self->picture->save;
 
 310   $self->text_block($self->picture->text_block);
 
 311   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
 
 313   $self->invalidate_version
 
 314     ->dialog->close('#jqueryui_popup_dialog')
 
 315     ->append('#text-block-' . $self->text_block->id . '-pictures', $html)
 
 316     ->show('#text-block-' . $self->text_block->id . '-pictures')
 
 320 sub action_ajax_update_picture {
 
 323   my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing attributes";
 
 325   if (!$attributes->{picture_content}) {
 
 326     delete $attributes->{picture_content};
 
 328     $attributes->{picture_file_name} = ((($::form->{ATTACHMENTS} || {})->{ $::form->{form_prefix} } || {})->{picture_content} || {})->{filename};
 
 331   $self->picture->assign_attributes(%{ $attributes });
 
 332   my @errors = $self->picture->validate;
 
 334   return $self->js->error(@errors)->render($self) if @errors;
 
 336   $self->picture->save;
 
 338   $self->text_block($self->picture->text_block);
 
 339   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
 
 341   $self->invalidate_version
 
 342     ->dialog->close('#jqueryui_popup_dialog')
 
 343     ->replaceWith('#text-block-picture-' . $self->picture->id, $html)
 
 344     ->show('#text-block-' . $self->text_block->id . '-pictures')
 
 348 sub action_ajax_delete_picture {
 
 351   $self->picture->delete;
 
 352   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $self->picture->text_block_id)->load);
 
 354   $self->invalidate_version
 
 355     ->remove('#text-block-picture-' . $self->picture->id)
 
 356     ->action_if(!@{ $self->text_block->pictures }, 'hide', '#text-block-' . $self->text_block->id . '-pictures')
 
 360 sub action_ajax_download_picture {
 
 363   $self->send_file(\$self->picture->{picture_content}, type => $self->picture->picture_content_type, name => $self->picture->picture_file_name);
 
 366 sub action_ajax_copy_picture {
 
 367   my ($self, %params) = @_;
 
 369   SL::Clipboard->new->copy($self->picture);
 
 370   SL::ClientJS->new->render($self);
 
 373 sub action_ajax_paste_picture {
 
 374   my ($self, %params) = @_;
 
 376   my $copied = SL::Clipboard->new->get_entry(qr/^RequirementSpecPicture$/);
 
 378     return SL::ClientJS->new
 
 379       ->error(t8("The clipboard does not contain anything that can be pasted here."))
 
 383   $self->text_block($self->picture->text_block);   # Save text block via the picture the user clicked on
 
 385   $self->paste_picture($copied);
 
 388 sub action_reorder_pictures {
 
 391   SL::DB::RequirementSpecPicture->reorder_list(@{ $::form->{picture_id} || [] });
 
 393   $self->render(\'', { type => 'json' });
 
 400 sub load_requirement_spec_text_block {
 
 402   $self->text_block(SL::DB::RequirementSpecTextBlock->new(id => $::form->{id})->load || die "No such requirement spec text block");
 
 409 sub output_position_from_id {
 
 410   my ($self, $id, $type, %params) = @_;
 
 413     return $1 eq 'front' ? 0 : 1 if $type =~ m/-(front|back)$/;
 
 414     return undef                 if $type !~ m/text-block/;
 
 417   my $text_block = $id ? SL::DB::Manager::RequirementSpecTextBlock->find_by(id => $id) : undef;
 
 419   return $text_block ? $text_block->output_position : undef;
 
 422 sub init_predefined_texts {
 
 423   return SL::DB::Manager::RequirementSpecPredefinedText->get_all_sorted;
 
 427   return SL::DB::RequirementSpecPicture->new(id => $::form->{picture_id} || $::form->{id})->load;
 
 432   $self->js(SL::ClientJS->new);
 
 435 sub invalidate_version {
 
 438   my $html   = $self->render('requirement_spec/_version', { output => 0 },
 
 439                              requirement_spec => SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id} || $self->text_block->requirement_spec_id)->load);
 
 440   return $self->js->html('#requirement_spec_version', $html);
 
 443 sub add_new_text_block_form {
 
 444   my ($self, %params) = @_;
 
 446   croak "Missing parameter output_position"     unless defined($params{output_position}) && ($params{output_position} ne '');
 
 447   croak "Missing parameter requirement_spec_id" unless $params{requirement_spec_id};
 
 449   $self->text_block(SL::DB::RequirementSpecTextBlock->new(
 
 450     requirement_spec_id => $params{requirement_spec_id},
 
 451     output_position     => $params{output_position},
 
 454   my $id_base = join('_', 'new_text_block', Time::HiRes::gettimeofday(), int rand 1000000000000);
 
 455   my $html    = $self->render('requirement_spec_text_block/_form', { output => 0 }, id_base => $id_base, insert_after => $params{insert_after_id});
 
 458      ->action($params{insert_after_id} ? 'insertAfter' : 'appendTo', $html, '#text-block-' . ($params{insert_after_id} || 'list'))
 
 459      ->focus('#' . $id_base . '_title');
 
 464   my %params = Params::Validate::validate(@_, { output_position => 1, id => 0, requirement_spec_id => 0, set_type => 0, });
 
 466   $params{requirement_spec_id} ||= $::form->{requirement_spec_id};
 
 467   croak "Unknown requirement_spec_id" if !$params{requirement_spec_id};
 
 469   my $text_blocks = SL::DB::Manager::RequirementSpecTextBlock->get_all_sorted(where => [ output_position => $params{output_position}, requirement_spec_id => $params{requirement_spec_id} ]);
 
 470   my $html        = $self->render('requirement_spec_text_block/ajax_list', { output => 0 }, TEXT_BLOCKS => $text_blocks, output_position => $params{output_position});
 
 472   $self->js->html('#column-content', $html);
 
 474   $self->js->val('#current_content_type', 'text-blocks-' . (0 == $params{output_position} ? 'front' : 'back')) if $params{id} || $params{set_type};
 
 475   $self->js->val('#current_content_id',   $params{id})                                                         if $params{id};
 
 477   return $self->set_function_blocks_tab_menu_class(class => 'text-block-context-menu');
 
 481   my ($self, $copied) = @_;
 
 483   if (!$self->text_block->db->do_transaction(sub {
 
 485     $self->picture($copied->to_object)->save;        # Create new picture from copied data and save
 
 486     $self->text_block->add_pictures($self->picture); # Add new picture to text block
 
 487     $self->text_block->save;
 
 489     $::lxdebug->message(LXDebug::WARN(), "Error: " . $self->text_block->db->error);
 
 490     return $self->js->error($::locale->text('Saving failed. Error message from the database: #1', $self->text_block->db->error))->render($self);
 
 493   my $html = $self->render('requirement_spec_text_block/_text_block_picture', { output => 0 }, picture => $self->picture);
 
 495   $self->invalidate_version
 
 496     ->append('#text-block-' . $self->text_block->id . '-pictures', $html)
 
 497     ->show('#text-block-' . $self->text_block->id . '-pictures')