<commit_before>package EnsEMBL::Web::Component::Interface;

### Module to create generic forms for Document::Interface and its associated modules

use EnsEMBL::Web::Component;
use EnsEMBL::Web::Form;

our @ISA = qw( EnsEMBL::Web::Component);
use strict;
use warnings;
no warnings "uninitialized";

sub add_form {
  ### Builds an empty HTML form for a new record
  my($panel, $object) = @_;
  my $form = _data_form($panel, $object, 'add');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'prev_action', 'value' => 'Save Record');
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'save');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'Preview');

  return $form ;
}

sub select_to_edit_form {
  ### Builds a form consisting of a dropdown widget that lists all records
  ### Sends the user to a second form where the record can be edited
  my($panel, $object) = @_;

  my $form = _record_select($panel, $object, 'select');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'edit');
  $form->add_element( 'type' => 'Submit', 'value' => 'View Record');
  return $form ;
}

sub edit_form {
  ### Builds an HTML form populated with a database record
  my($panel, $object) = @_;

  my $primary_key = $panel->interface->data->get_primary_key;
  my $id = $object->param($primary_key);
  
  my $form = _data_form($panel, $object, 'edit');
  $form->add_element(
          'type'  => 'Hidden',
          'name'  => $primary_key,
          'value' => $id,  
        );

  ## Show creation/modification details?
  if ($panel->interface->show_history) {
    my $history = $panel->interface->history_fields($id);
    foreach my $field (@$history) {
      $form->add_element(%$field);
    }
  }

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'save');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'Preview');
  return $form ;
}

sub select_to_delete_form {
  ### Builds a form consisting of a dropdown widget that lists all records
  ### Sends the user to a page where the record can be previewed before deletion
  my($panel, $object) = @_;
  
  my $form = _record_select($panel, $object, 'select');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'delete');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'View Record');
  return $form ;
}

sub script_name {
  my ($panel, $object) = @_;
  if ($panel->interface->script_name) {
    return $panel->interface->script_name;
  }
  return $object->script;
}

sub preview_form {
  ### Displays a record or form input as non-editable text, 
  ### and also passes the data as hidden form elements
  my($panel, $object) = @_;
  
  ## Create form  
  my $script = script_name($panel, $object);
  my $form = EnsEMBL::Web::Form->new('preview', "/common/$script", 'post');

  ## get data and assemble form
  my $primary_key = $panel->interface->data->get_primary_key;
  my $id = $object->param($primary_key);
  my $db_action = $object->param('db_action');

  if ($db_action eq 'delete') {
    $panel->interface->data->populate($id);
  }
  else {
    $panel->interface->cgi_populate($object, $id);
  }

  ## add form elements
  $form->add_element(
            'type'  => 'Hidden',
            'name'  => $primary_key,
            'value' => $id,  
        );
  my $preview_fields = $panel->interface->preview_fields($id);
  my $element;
  foreach $element (@$preview_fields) {
    $form->add_element(%$element);
  }
  my $pass_fields = $panel->interface->pass_fields($id);
  foreach $element (@$pass_fields) {
    $form->add_element(%$element);
  }

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => $db_action);
  $form->add_element( 'type' => 'Submit', 'value' => ucfirst($db_action));
  return $form ;
}

sub _data_form {
  ### Function to build a record editing form
  my($panel, $object, $name) = @_;
  warn "PANEL: " . $panel; 
  my $script = script_name($panel, $object);
  my $form = EnsEMBL::Web::Form->new($name, "/common/$script", 'post');

  ## form widgets
  my $key = $panel->interface->data->get_primary_key;
  my $id = $object->param($key);
  if ($id) {
    $panel->interface->data->populate($id);
  }
  my @widgets = @{$panel->interface->edit_fields};

  foreach my $element (@widgets) {
    $form->add_element(%$element);
  }
  return $form;
}
<commit_msg>Changed default button labels to 'Next' and 'OK' as more generic<commit_after>package EnsEMBL::Web::Component::Interface;

### Module to create generic forms for Document::Interface and its associated modules

use EnsEMBL::Web::Component;
use EnsEMBL::Web::Form;

our @ISA = qw( EnsEMBL::Web::Component);
use strict;
use warnings;
no warnings "uninitialized";

sub add_form {
  ### Builds an empty HTML form for a new record
  my($panel, $object) = @_;
  my $form = _data_form($panel, $object, 'add');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'prev_action', 'value' => 'Save Record');
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'save');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'Next');

  return $form ;
}

sub select_to_edit_form {
  ### Builds a form consisting of a dropdown widget that lists all records
  ### Sends the user to a second form where the record can be edited
  my($panel, $object) = @_;

  my $form = _record_select($panel, $object, 'select');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'edit');
  $form->add_element( 'type' => 'Submit', 'value' => 'Next');
  return $form ;
}

sub edit_form {
  ### Builds an HTML form populated with a database record
  my($panel, $object) = @_;

  my $primary_key = $panel->interface->data->get_primary_key;
  my $id = $object->param($primary_key);
  
  my $form = _data_form($panel, $object, 'edit');
  $form->add_element(
          'type'  => 'Hidden',
          'name'  => $primary_key,
          'value' => $id,  
        );

  ## Show creation/modification details?
  if ($panel->interface->show_history) {
    my $history = $panel->interface->history_fields($id);
    foreach my $field (@$history) {
      $form->add_element(%$field);
    }
  }

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'save');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'Preview');
  return $form ;
}

sub select_to_delete_form {
  ### Builds a form consisting of a dropdown widget that lists all records
  ### Sends the user to a page where the record can be previewed before deletion
  my($panel, $object) = @_;
  
  my $form = _record_select($panel, $object, 'select');

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'db_action', 'value' => 'delete');
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => 'preview');
  $form->add_element( 'type' => 'Submit', 'value' => 'View Record');
  return $form ;
}

sub script_name {
  my ($panel, $object) = @_;
  if ($panel->interface->script_name) {
    return $panel->interface->script_name;
  }
  return $object->script;
}

sub preview_form {
  ### Displays a record or form input as non-editable text, 
  ### and also passes the data as hidden form elements
  my($panel, $object) = @_;
  
  ## Create form  
  my $script = script_name($panel, $object);
  my $form = EnsEMBL::Web::Form->new('preview', "/common/$script", 'post');

  ## get data and assemble form
  my $primary_key = $panel->interface->data->get_primary_key;
  my $id = $object->param($primary_key);
  my $db_action = $object->param('db_action');

  if ($db_action eq 'delete') {
    $panel->interface->data->populate($id);
  }
  else {
    $panel->interface->cgi_populate($object, $id);
  }

  ## add form elements
  $form->add_element(
            'type'  => 'Hidden',
            'name'  => $primary_key,
            'value' => $id,  
        );
  my $preview_fields = $panel->interface->preview_fields($id);
  my $element;
  foreach $element (@$preview_fields) {
    $form->add_element(%$element);
  }
  my $pass_fields = $panel->interface->pass_fields($id);
  foreach $element (@$pass_fields) {
    $form->add_element(%$element);
  }

  ## navigation elements
  $form->add_element( 'type' => 'Hidden', 'name' => 'dataview', 'value' => $db_action);
  $form->add_element( 'type' => 'Submit', 'value' => 'OK');
  return $form ;
}

sub _data_form {
  ### Function to build a record editing form
  my($panel, $object, $name) = @_;
  warn "PANEL: " . $panel; 
  my $script = script_name($panel, $object);
  my $form = EnsEMBL::Web::Form->new($name, "/common/$script", 'post');

  ## form widgets
  my $key = $panel->interface->data->get_primary_key;
  my $id = $object->param($key);
  if ($id) {
    $panel->interface->data->populate($id);
  }
  my @widgets = @{$panel->interface->edit_fields};

  foreach my $element (@widgets) {
    $form->add_element(%$element);
  }
  return $form;
}

{'fraction_non_alphanumeric': 0.19148264984227129, 'fraction_numerical': 0.0, 'mean_word_length': 3.9639874739039667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 205, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6804221', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3185, 'n_words': 823}