<commit_before><?php

/**
 * Individuals actions.
 *
 * @package    darwin
 * @subpackage individuals
 * @author     DB team <<EMAIL>>
 */
class individualsActions extends DarwinActions
{
  protected $widgetCategory = 'individuals_widget';
  protected $table = 'specimen_individuals';

  protected function getSpecimenIndividualsForm(sfWebRequest $request)
  {
    if(! $request->hasParameter('id'))
    {
      $this->spec_individual = new SpecimenIndividuals(); 
      $duplic = $request->getParameter('duplicate_id','0') ;          
      if ($duplic) // then it's a duplicate individual
      {
        $this->spec_individual = $this->getRecordIfDuplicate($duplic,$this->spec_individual);    
        // set all necessary widgets to visible 
        if($request->hasParameter('all_duplicate'))        
          Doctrine::getTable('SpecimenIndividuals')->getRequiredWidget($this->spec_individual, $this->getUser()->getId(), 'individuals_widget',1);
      }    
    }
    else
    {
      $this->spec_individual = Doctrine::getTable('SpecimenIndividuals')->findExcept($request->getParameter('id'));
      $this->forward404Unless($this->spec_individual);
    }
    if($this->spec_individual->isNew())
    {
      $this->specimen = Doctrine::getTable('Specimens')->findExcept($request->getParameter('spec_id'));

      $this->forward404Unless($this->specimen);

      $this->spec_individual->Specimens = $this->specimen;

    }
    else
    {
      $this->specimen = Doctrine::getTable('Specimens')->findExcept($this->spec_individual->getSpecimenRef());
    }

    $individual = new SpecimenIndividualsForm($this->spec_individual);

    return $individual;
  }

  public function executeEdit(sfWebRequest $request)
  {
    if($this->getUser()->isA(Users::REGISTERED_USER)) $this->forwardToSecureAction();
    if($request->hasParameter('id') && !$this->getUser()->isA(Users::ADMIN))
    {    
      $spec = Doctrine::getTable('SpecimenSearch')->findOneByIndividualRef($request->getParameter('id'));
      if(in_array($spec->getCollectionRef(),Doctrine::getTable('Specimens')->testNoRightsCollections('individual_ref',
                                                                                                      $request->getParameter('id'), 
                                                                                                      $this->getUser()->getId())))    
        $this->redirect("individuals/view?id=".$request->getParameter('id')) ;  
    }
    $this->individual = $this->getSpecimenIndividualsForm($request);  

    if($this->individual->getObject()->isNew())
    {
      $duplic = $request->getParameter('duplicate_id') ;
      if($duplic)
      {
        // reembed duplicated comment
        $Comments = Doctrine::getTable('Comments')->findForTable('specimen_individuals',$duplic) ;
        foreach ($Comments as $key=>$val)
        {
          $comment = new Comments() ;
          $comment = $this->getRecordIfDuplicate($val->getId(),$comment); 
          $this->individual->addComments($key, $comment) ;          
        }
        // reembed duplicated external url
        $ExtLinks = Doctrine::getTable('ExtLinks')->findForTable('specimen_individuals',$duplic) ;
        foreach ($ExtLinks as $key=>$val)
        {
          $links = new ExtLinks() ;
          $links = $this->getRecordIfDuplicate($val->getId(),$links); 
          $this->individual->addExtLinks($key, $links) ;          
        }        
        //reembed identification
        $Identifications = Doctrine::getTable('Identifications')->getIdentificationsRelated('specimen_individuals',$duplic) ;
        foreach ($Identifications as $key=>$val)
        {
          $identification = new Identifications() ;
          $identification = $this->getRecordIfDuplicate($val->getId(),$identification); 
          $this->individual->addIdentifications($key, $val->getOrderBy(), $identification);
          $Identifier = Doctrine::getTable('CataloguePeople')->getPeopleRelated('identifications', 'identifier', $val->getId()) ;     
          foreach ($Identifier as $key2=>$val2)
          {
            $ident = $this->individual->getEmbeddedForm('newIdentification')->getEmbeddedForm($key);
            $ident->addIdentifiers($key2,$val2->getPeopleRef(),0);        
            $this->individual->reembedNewIdentification($ident, $key);    
          }
        }
      }
    }
    if($request->isMethod('post'))
    {
      $this->individual->bind( $request->getParameter('specimen_individuals'), $request->getFiles('specimen_individuals') );
      if( $this->individual->isValid())
      {
        try
        {
          $this->individual->save();
          $this->redirect('individuals/edit?id='.$this->individual->getObject()->getSpecimenRef());
        }
        catch(Doctrine_Exception $ne)
        {
          $e = new DarwinPgErrorParser($ne);
          $extd_message = '';
          if(preg_match('/unique constraint "unq_specimen_individuals"/i',$ne->getMessage()))
          {
            $dup_spec = Doctrine::getTable('SpecimenIndividuals')->findDuplicate($this->individual->getObject());
            if(!$dup_spec)
            {
                $this->logMessage('Duplicate Individual not found: '. json_encode($this->individual->getObject()->toArray()), 'err');
            }
            else
            {
              $extd_message = '<br /><a href="'.$this->getController()->genUrl( 'individuals/edit?id='.$dup_spec->getId() ).'">'.
                $this->getI18N()->__('Go the the original record')
                .'</a>';
            }
          }
          $error = new sfValidatorError(new savedValidator(),$e->getMessage().$extd_message);
          $this->individual->getErrorSchema()->addError($error, 'Darwin2 :');
        }
      }
    }
    $this->loadWidgets();
  }

  public function executeOverview(sfWebRequest $request)
  {  
    $this->specimen = Doctrine::getTable('Specimens')->findExcept($request->getParameter('spec_id',0));
    $this->forward404Unless($this->specimen, sprintf('Specimen does not exist (%s).', $request->getParameter('spec_id',0)));<commit_msg>next time, try to modify correctly stupid ex military guy<commit_after><?php

/**
 * Individuals actions.
 *
 * @package    darwin
 * @subpackage individuals
 * @author     DB team <<EMAIL>>
 */
class individualsActions extends DarwinActions
{
  protected $widgetCategory = 'individuals_widget';
  protected $table = 'specimen_individuals';

  protected function getSpecimenIndividualsForm(sfWebRequest $request)
  {
    if(! $request->hasParameter('id'))
    {
      $this->spec_individual = new SpecimenIndividuals(); 
      $duplic = $request->getParameter('duplicate_id','0') ;          
      if ($duplic) // then it's a duplicate individual
      {
        $this->spec_individual = $this->getRecordIfDuplicate($duplic,$this->spec_individual);    
        // set all necessary widgets to visible 
        if($request->hasParameter('all_duplicate'))        
          Doctrine::getTable('SpecimenIndividuals')->getRequiredWidget($this->spec_individual, $this->getUser()->getId(), 'individuals_widget',1);
      }    
    }
    else
    {
      $this->spec_individual = Doctrine::getTable('SpecimenIndividuals')->findExcept($request->getParameter('id'));
      $this->forward404Unless($this->spec_individual);
    }
    if($this->spec_individual->isNew())
    {
      $this->specimen = Doctrine::getTable('Specimens')->findExcept($request->getParameter('spec_id'));

      $this->forward404Unless($this->specimen);

      $this->spec_individual->Specimens = $this->specimen;

    }
    else
    {
      $this->specimen = Doctrine::getTable('Specimens')->findExcept($this->spec_individual->getSpecimenRef());
    }

    $individual = new SpecimenIndividualsForm($this->spec_individual);

    return $individual;
  }

  public function executeEdit(sfWebRequest $request)
  {
    if($this->getUser()->isA(Users::REGISTERED_USER)) $this->forwardToSecureAction();
    if($request->hasParameter('id') && !$this->getUser()->isA(Users::ADMIN))
    {    
      $spec = Doctrine::getTable('SpecimenSearch')->findOneByIndividualRef($request->getParameter('id'));
      if(in_array($spec->getCollectionRef(),Doctrine::getTable('Specimens')->testNoRightsCollections('individual_ref',
                                                                                                      $request->getParameter('id'), 
                                                                                                      $this->getUser()->getId())))    
        $this->redirect("individuals/view?id=".$request->getParameter('id')) ;  
    }
    $this->individual = $this->getSpecimenIndividualsForm($request);  

    if($this->individual->getObject()->isNew())
    {
      $duplic = $request->getParameter('duplicate_id') ;
      if($duplic)
      {
        // reembed duplicated comment
        $Comments = Doctrine::getTable('Comments')->findForTable('specimen_individuals',$duplic) ;
        foreach ($Comments as $key=>$val)
        {
          $comment = new Comments() ;
          $comment = $this->getRecordIfDuplicate($val->getId(),$comment); 
          $this->individual->addComments($key, $comment) ;          
        }
        // reembed duplicated external url
        $ExtLinks = Doctrine::getTable('ExtLinks')->findForTable('specimen_individuals',$duplic) ;
        foreach ($ExtLinks as $key=>$val)
        {
          $links = new ExtLinks() ;
          $links = $this->getRecordIfDuplicate($val->getId(),$links); 
          $this->individual->addExtLinks($key, $links) ;          
        }        
        //reembed identification
        $Identifications = Doctrine::getTable('Identifications')->getIdentificationsRelated('specimen_individuals',$duplic) ;
        foreach ($Identifications as $key=>$val)
        {
          $identification = new Identifications() ;
          $identification = $this->getRecordIfDuplicate($val->getId(),$identification); 
          $this->individual->addIdentifications($key, $val->getOrderBy(), $identification);
          $Identifier = Doctrine::getTable('CataloguePeople')->getPeopleRelated('identifications', 'identifier', $val->getId()) ;     
          foreach ($Identifier as $key2=>$val2)
          {
            $ident = $this->individual->getEmbeddedForm('newIdentification')->getEmbeddedForm($key);
            $ident->addIdentifiers($key2,$val2->getPeopleRef(),0);        
            $this->individual->reembedNewIdentification($ident, $key);    
          }
        }
      }
    }
    if($request->isMethod('post'))
    {
      $this->individual->bind( $request->getParameter('specimen_individuals'), $request->getFiles('specimen_individuals') );
      if( $this->individual->isValid())
      {
        try
        {
          $this->individual->save();
          $this->redirect('individuals/edit?id='.$this->individual->getObject()->getId());
        }
        catch(Doctrine_Exception $ne)
        {
          $e = new DarwinPgErrorParser($ne);
          $extd_message = '';
          if(preg_match('/unique constraint "unq_specimen_individuals"/i',$ne->getMessage()))
          {
            $dup_spec = Doctrine::getTable('SpecimenIndividuals')->findDuplicate($this->individual->getObject());
            if(!$dup_spec)
            {
                $this->logMessage('Duplicate Individual not found: '. json_encode($this->individual->getObject()->toArray()), 'err');
            }
            else
            {
              $extd_message = '<br /><a href="'.$this->getController()->genUrl( 'individuals/edit?id='.$dup_spec->getId() ).'">'.
                $this->getI18N()->__('Go the the original record')
                .'</a>';
            }
          }
          $error = new sfValidatorError(new savedValidator(),$e->getMessage().$extd_message);
          $this->individual->getErrorSchema()->addError($error, 'Darwin2 :');
        }
      }
    }
    $this->loadWidgets();
  }

  public function executeOverview(sfWebRequest $request)
  {  
    $this->specimen = Doctrine::getTable('Specimens')->findExcept($request->getParameter('spec_id',0));
    $this->forward404Unless($this->specimen, sprintf('Specimen does not exist (%s).', $request->getParameter('spec_id',0)));
{'fraction_non_alphanumeric': 0.1620805095125337, 'fraction_numerical': 0.0034294112843961787, 'mean_word_length': 2.7137659187386296, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 249, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3312544', 'n_tokens_mistral': 3556, 'n_tokens_neox': 3332, 'n_words': 428}