<commit_before>
use strict;

use CXGN::DB::Connection;
use CXGN::Page;
use CXGN::Page::FormattingHelpers qw | page_title_html |;
use CXGN::People::Login;
use CXGN::People::Person;
use CXGN::Contact;
use SGN::Config;

my $page = CXGN::Page->new("solpeople create account", "Koni");
my $dbh = CXGN::DB::Connection->new();

my $conf = SGN::Config->load();

if ($conf->{is_mirror}) { 
    $page->message_page("This site is a mirror site and does not support adding users. Please go to the main site to create an account.");
}


my ($first_name, $last_name, $username, $password, $confirm_password, $email_address)
  = $page->get_arguments(qw(first_name last_name username password confirm_password email_address));

if ($username) {
  #
  # check password properties...
  #
  my @fail = ();
  if (length($username) < 7) {
    push @fail, "Username is too short. Username must be 7 or more characters";
  } else {
    # does user already exist?
    #
      my $existing_login = CXGN::People::Login -> get_login($dbh, $username); 
      
      if ($existing_login->get_username()) { 
	  push @fail, "Username \"$username\" is already in use. Please pick a different username.";
      }
      
  }
  if (length($password) < 7) {
    push @fail, "Password is too short. Password must be 7 or more characters";
  }
  if ("$password" ne "$confirm_password") {
    push @fail, "Password and confirm password do not match.";
  }
  if ($password eq $username) {
    push @fail, "Password must not be the same as your username.";
  }
  if ($email_address !~ m/[^\@]+\@[^\@]+/) {
    push @fail, "Email address is invalid.";
  }
  unless($first_name) {
      push @fail,"You must enter a first name or initial.";
  }
  unless($last_name) {
      push @fail,"You must enter a last name.";
  }  

  if (@fail) {

      #$page->message_page("Account could not be created ".(join "  ", @fail));

      &show_fail_reasons(\@fail, $page);
      exit();
      
  }


  my $confirm_code = $page->tempname();
  my $new_user = CXGN::People::Login->new($dbh);
  $new_user -> set_username($username);
  $new_user -> set_password($password);
  $new_user -> set_pending_email($email_address);
  $new_user -> set_confirm_code($confirm_code);
  $new_user -> set_disabled('unconfirmed account');
  $new_user -> store();

  #this is being added because the person object still uses two different objects, despite the fact that we've merged the tables
  my $person_id=$new_user->get_sp_person_id();
  my $new_person=CXGN::People::Person->new($dbh,$person_id);
  $new_person->set_first_name($first_name);
  $new_person->set_last_name($last_name);
  $new_person->store();
  
  my $host=$page->{request}->hostname();
  my $subject="[SGN] Email Address Confirmation Request";
  my $body=<<END_HEREDOC;
<commit_msg>removing requests, use CGI to find the server hostname<commit_after>
use strict;

use CXGN::DB::Connection;
use CXGN::Page;
use CXGN::Page::FormattingHelpers qw | page_title_html |;
use CXGN::People::Login;
use CXGN::People::Person;
use CXGN::Contact;
use SGN::Config;

my $page = CXGN::Page->new("solpeople create account", "Koni");
my $dbh = CXGN::DB::Connection->new();

my $conf = SGN::Config->load();

if ($conf->{is_mirror}) { 
    $page->message_page("This site is a mirror site and does not support adding users. Please go to the main site to create an account.");
}


my ($first_name, $last_name, $username, $password, $confirm_password, $email_address)
  = $page->get_arguments(qw(first_name last_name username password confirm_password email_address));

if ($username) {
  #
  # check password properties...
  #
  my @fail = ();
  if (length($username) < 7) {
    push @fail, "Username is too short. Username must be 7 or more characters";
  } else {
    # does user already exist?
    #
      my $existing_login = CXGN::People::Login -> get_login($dbh, $username); 
      
      if ($existing_login->get_username()) { 
	  push @fail, "Username \"$username\" is already in use. Please pick a different username.";
      }
      
  }
  if (length($password) < 7) {
    push @fail, "Password is too short. Password must be 7 or <PASSWORD>";
  }
  if ("$password" ne "$confirm_password") {
    push @fail, "Password and confirm password do not match.";
  }
  if ($password eq $username) {
    push @fail, "Password must not be the same as your username.";
  }
  if ($email_address !~ m/[^\@]+\@[^\@]+/) {
    push @fail, "Email address is invalid.";
  }
  unless($first_name) {
      push @fail,"You must enter a first name or initial.";
  }
  unless($last_name) {
      push @fail,"You must enter a last name.";
  }  

  if (@fail) {

      #$page->message_page("Account could not be created ".(join "  ", @fail));

      &show_fail_reasons(\@fail, $page);
      exit();
      
  }


  my $confirm_code = $page->tempname();
  my $new_user = CXGN::People::Login->new($dbh);
  $new_user -> set_username($username);
  $new_user -> set_password($password);
  $new_user -> set_pending_email($email_address);
  $new_user -> set_confirm_code($confirm_code);
  $new_user -> set_disabled('unconfirmed account');
  $new_user -> store();

  #this is being added because the person object still uses two different objects, despite the fact that we've merged the tables
  my $person_id=$new_user->get_sp_person_id();
  my $new_person=CXGN::People::Person->new($dbh,$person_id);
  $new_person->set_first_name($first_name);
  $new_person->set_last_name($last_name);
  $new_person->store();
  
  my $host = CGI->new->server_name;
  my $subject="[SGN] Email Address Confirmation Request";
  my $body=<<END_HEREDOC;

{'fraction_non_alphanumeric': 0.1554719684757299, 'fraction_numerical': 0.0014329213684399068, 'mean_word_length': 3.959147424511545, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 52, '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': '6020158', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1825, 'n_words': 520}