<commit_before><?php

/**
 * Collections form.
 *
 * @package    form
 * @subpackage Collections
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class CollectionsForm extends BaseCollectionsForm
{
  public function configure()
  {
    unset($this['path'],
          $this['code_auto_increment'],
          $this['code_last_value'],
          $this['code_prefix'],
          $this['code_prefix_separator'],
          $this['code_suffix'],
          $this['code_suffix_separator'],
          $this['code_part_code_auto_copy'],
          $this['name_indexed']
         );
    $this->widgetSchema['is_public'] = new sfWidgetFormInputCheckbox(array ('default' => 'true'), array('title' => 'checked = public'));          
    $this->validatorSchema['is_public'] = new sfValidatorBoolean() ;
    $this->widgetSchema['code'] = new sfWidgetFormInputText();
    $this->widgetSchema['code']->setAttributes(array('class'=>'small_size'));
    $this->widgetSchema['name'] = new sfWidgetFormInputText();
    $this->widgetSchema['name']->setAttributes(array('class'=>'medium_size'));
    $this->widgetSchema['institution_ref'] = new widgetFormButtonRef(array(
       'model' => 'Institutions',
       'link_url' => 'institution/choose?with_js=1',
       'method' => 'getFamilyName',
       'box_title' => $this->getI18N()->__('Choose Institution'),
     ));
    $this->widgetSchema['institution_ref']->setLabel('Institution');

    $this->widgetSchema['main_manager_ref'] = new widgetFormButtonRef(array(
       'model' => 'Users',
       'link_url' => 'user/choose',
       'method' => 'getFormatedName',
       'box_title' => $this->getI18N()->__('Choose Manager'),
     ));
    $this->widgetSchema['main_manager_ref']->setLabel('Main manager');

    $this->widgetSchema['parent_ref'] = new sfWidgetFormChoice(array(
      'choices' =>  array(),
    ));
    $this->widgetSchema['parent_ref']->setLabel('Parent collection');

    $this->widgetSchema->setHelps(array(
                                       'is_public' => "Uncheck this option if you want your collection to be private. So this collection won't be visible in the public interface neither by simply registered user",
                                       'main_manager_ref' => "Specify the main manager for this collection, you can add other manager on the rights table below", )
                                 );

    $this->validatorSchema['collection_type'] = new sfValidatorChoice(array('choices' => array('mix' => 'mix', 'observation' => 'observation', 'physical' => 'physical'), 'required' => true));

   if(! $this->getObject()->isNew() || isset($this->options['duplicate']))
     $this->widgetSchema['parent_ref']->setOption('choices', Doctrine::getTable('Collections')->getDistinctCollectionByInstitution($this->getObject()->getInstitutionRef()) );
   elseif(isset($this->options['new_with_error']))
     $this->widgetSchema['parent_ref']->setOption('choices', Doctrine::getTable('Collections')->getDistinctCollectionByInstitution($this->options['institution']));   	

     $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(array('callback' => array($this, 'checkSelfAttached')))
     );

    $subForm = new sfForm();
    $this->embedForm('CollectionsRights',$subForm);   
    foreach(Doctrine::getTable('CollectionsRights')->getAllUserRef($this->getObject()->getId()) as $key=>$vals)
    {
      $form = new CollectionsRightsForm($vals);
      $this->embeddedForms['CollectionsRights']->embedForm($key, $form);
    }
    //Re-embedding the container
    $this->embedForm('CollectionsRights', $this->embeddedForms['CollectionsRights']); 
    
    $subForm = new sfForm();
    $this->embedForm('newVal',$subForm);
  }
  
  public function addValue($num,$user_id,$rights)
  {
    $val = new CollectionsRights() ;
    $val->Collections = $this->getObject();      
    $val->setUserRef($user_id) ;
    $val->setDbUserType($rights) ;
    $form = new CollectionsRightsForm($val,array('user_id'=>$user_id));  
    $this->embeddedForms['newVal']->embedForm($num, $form);
    //Re-embedding the container
    $this->embedForm('newVal', $this->embeddedForms['newVal']);
  }

  public function checkSelfAttached($validator, $values)
  {
    if(! empty($values['id']) )
    {
      if($values['parent_ref'] == $values['id'])
      {
	      $error = new sfValidatorError($validator, "A collection can't be attached to itself");
        throw new sfValidatorErrorSchema($validator, array('parent_ref' => $error));
      }
    }
    return $values;
  }
  
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    if(isset($taintedValues['newVal']))
    {
      foreach($taintedValues['newVal'] as $key=>$newVal)
      {
        if (!isset($this['newVal'][$key]))
        {
          $this->addValue($key,$newVal['user_ref'],$newVal['db_user_type']);
        }
      }
    }
    parent::bind($taintedValues, $taintedFiles);
  }

  public function saveEmbeddedForms($con = null, $forms = null)
  {
   if (null === $forms)
   {
	    $value = $this->getValue('CollectionsRights');	    
	    foreach($this->embeddedForms['CollectionsRights']->getEmbeddedForms() as $name => $form)
    	{
    	  if (!isset($value[$name]['user_ref']))
	      {         
	       /* DO BE DONE WITH A TRIGGER
	        if ($form->getObject()->getDbUserType() == Users::REGISTERED_USER ) // so we have to delete widget right for this guy
	          Doctrine::getTable('MyWidgets')->setUserRef($form->getObject()->getUserRef())->doUpdateWidgetRight($form->getObject()->getCollectionRef());
	        */
	        $form->getObject()->delete();
	        unset($this->embeddedForms['CollectionsRights'][$name]);
	      } 
	    }	  
	    $value = $this->getValue('newVal');
	    foreach($this->embeddedForms['newVal']->getEmbeddedForms() as $name => $form)
    	{
    	  if (!isset($value[$name]['user_ref']))<commit_msg>added a test in collectionForm to avoid user to add himself in right when he add a collection<commit_after><?php

/**
 * Collections form.
 *
 * @package    form
 * @subpackage Collections
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class CollectionsForm extends BaseCollectionsForm
{
  public function configure()
  {
    unset($this['path'],
          $this['code_auto_increment'],
          $this['code_last_value'],
          $this['code_prefix'],
          $this['code_prefix_separator'],
          $this['code_suffix'],
          $this['code_suffix_separator'],
          $this['code_part_code_auto_copy'],
          $this['name_indexed']
         );
    $this->widgetSchema['is_public'] = new sfWidgetFormInputCheckbox(array ('default' => 'true'), array('title' => 'checked = public'));          
    $this->validatorSchema['is_public'] = new sfValidatorBoolean() ;
    $this->widgetSchema['code'] = new sfWidgetFormInputText();
    $this->widgetSchema['code']->setAttributes(array('class'=>'small_size'));
    $this->widgetSchema['name'] = new sfWidgetFormInputText();
    $this->widgetSchema['name']->setAttributes(array('class'=>'medium_size'));
    $this->widgetSchema['institution_ref'] = new widgetFormButtonRef(array(
       'model' => 'Institutions',
       'link_url' => 'institution/choose?with_js=1',
       'method' => 'getFamilyName',
       'box_title' => $this->getI18N()->__('Choose Institution'),
     ));
    $this->widgetSchema['institution_ref']->setLabel('Institution');

    $this->widgetSchema['main_manager_ref'] = new widgetFormButtonRef(array(
       'model' => 'Users',
       'link_url' => 'user/choose',
       'method' => 'getFormatedName',
       'box_title' => $this->getI18N()->__('Choose Manager'),
     ));
    $this->widgetSchema['main_manager_ref']->setLabel('Main manager');

    $this->widgetSchema['parent_ref'] = new sfWidgetFormChoice(array(
      'choices' =>  array(),
    ));
    $this->widgetSchema['parent_ref']->setLabel('Parent collection');

    $this->widgetSchema->setHelps(array(
                                       'is_public' => "Uncheck this option if you want your collection to be private. So this collection won't be visible in the public interface neither by simply registered user",
                                       'main_manager_ref' => "Specify the main manager for this collection, you can add other manager on the rights table below", )
                                 );

    $this->validatorSchema['collection_type'] = new sfValidatorChoice(array('choices' => array('mix' => 'mix', 'observation' => 'observation', 'physical' => 'physical'), 'required' => true));

   if(! $this->getObject()->isNew() || isset($this->options['duplicate']))
     $this->widgetSchema['parent_ref']->setOption('choices', Doctrine::getTable('Collections')->getDistinctCollectionByInstitution($this->getObject()->getInstitutionRef()) );
   elseif(isset($this->options['new_with_error']))
     $this->widgetSchema['parent_ref']->setOption('choices', Doctrine::getTable('Collections')->getDistinctCollectionByInstitution($this->options['institution']));   	

     $this->validatorSchema->setPostValidator(
      new sfValidatorCallback(array('callback' => array($this, 'checkSelfAttached')))
     );

    $subForm = new sfForm();
    $this->embedForm('CollectionsRights',$subForm);   
    foreach(Doctrine::getTable('CollectionsRights')->getAllUserRef($this->getObject()->getId()) as $key=>$vals)
    {
      $form = new CollectionsRightsForm($vals);
      $this->embeddedForms['CollectionsRights']->embedForm($key, $form);
    }
    //Re-embedding the container
    $this->embedForm('CollectionsRights', $this->embeddedForms['CollectionsRights']); 
    
    $subForm = new sfForm();
    $this->embedForm('newVal',$subForm);
  }
  
  public function addValue($num,$user_id,$rights)
  {
    $val = new CollectionsRights() ;
    $val->Collections = $this->getObject();      
    $val->setUserRef($user_id) ;
    $val->setDbUserType($rights) ;
    $form = new CollectionsRightsForm($val,array('user_id'=>$user_id));  
    $this->embeddedForms['newVal']->embedForm($num, $form);
    //Re-embedding the container
    $this->embedForm('newVal', $this->embeddedForms['newVal']);
  }

  public function checkSelfAttached($validator, $values)
  {
    if(! empty($values['id']) )
    {
      if($values['parent_ref'] == $values['id'])
      {
	      $error = new sfValidatorError($validator, "A collection can't be attached to itself");
        throw new sfValidatorErrorSchema($validator, array('parent_ref' => $error));
      }
    }
    return $values;
  }
  
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    if(isset($taintedValues['newVal']))
    {
      foreach($taintedValues['newVal'] as $key=>$newVal)
      {
        if (!isset($this['newVal'][$key]))
        {
          $this->addValue($key,$newVal['user_ref'],$newVal['db_user_type']);
        }
      }
    }
    parent::bind($taintedValues, $taintedFiles);
  }

  public function saveEmbeddedForms($con = null, $forms = null)
  {
   if (null === $forms)
   {
	    $value = $this->getValue('CollectionsRights');	    
	    foreach($this->embeddedForms['CollectionsRights']->getEmbeddedForms() as $name => $form)
    	{
    	  if (!isset($value[$name]['user_ref']))
	      {         
	       /* DO BE DONE WITH A TRIGGER
	        if ($form->getObject()->getDbUserType() == Users::REGISTERED_USER ) // so we have to delete widget right for this guy
	          Doctrine::getTable('MyWidgets')->setUserRef($form->getObject()->getUserRef())->doUpdateWidgetRight($form->getObject()->getCollectionRef());
	        */
	        $form->getObject()->delete();
	        unset($this->embeddedForms['CollectionsRights'][$name]);
	      } 
	    }	  
	    $value = $this->getValue('newVal');
	    foreach($this->embeddedForms['newVal']->getEmbeddedForms() as $name => $form)
    	{
    	  if (!isset($value[$name]['user_ref']) || $value[$name]['user_ref'] == sfContext::getInstance()->getUser()->getId())
{'fraction_non_alphanumeric': 0.17578027465667914, 'fraction_numerical': 0.0038285476487723677, 'mean_word_length': 3.6791277258566977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 207, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5926070', 'n_tokens_mistral': 3642, 'n_tokens_neox': 3536, 'n_words': 586}