<commit_before><?php

/**
 * Element
 * 
 * @property integer $pageID
 * @property integer $weight
 * @property integer $fixedID
 * @property integer $elementType
 * @property string $title
 * @property string $format
 * @property string $instructions
 * @property string $defaultValue
 * @property boolean $required
 * @property double $min
 * @property double $max
 * @property Page $Page
 * @property ElementType $ElementType
 * @property Doctrine_Collection $ListItems
 * @property Doctrine_Collection $ElementAnswer
 * @package    jazzee
 * @subpackage orm
 * @author     <NAME> <<EMAIL>>
 */
class Element extends Doctrine_Record{
  /**
   * @see BaseElement::setTableDefinition()
   */
  public function setTableDefinition(){
    $this->setTableName('element');
    $this->hasColumn('pageID', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('weight', 'integer', null, array(
      'type' => 'integer',
      'default' => 0,
     ));
    $this->hasColumn('fixedID', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('elementType', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('title', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('format', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('instructions', 'string', 3000, array(
      'type' => 'string',
      'length' => '3000',
     ));
    $this->hasColumn('defaultValue', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('required', 'boolean', null, array(
      'type' => 'boolean',
     ));
    $this->hasColumn('min', 'double', null, array(
      'type' => 'double',
     ));
    $this->hasColumn('max', 'double', null, array(
      'type' => 'double',
     ));


    $this->index('UniqueFixedID', array(
      'fields' => array(
        0 => 'fixedID',
        1 => 'pageID',
      ),
      'type' => 'unique',
     ));
  }

  /**
   * (non-PHPdoc)
   * @see BaseElement::setUp()
   */
  public function setUp(){
    parent::setUp();
    $this->hasOne('Page', array(
      'local' => 'pageID',
      'foreign' => 'id',
      'onDelete' => 'CASCADE',
      'onUpdate' => 'CASCADE')
    );

    $this->hasOne('ElementType', array(
      'local' => 'elementType',
      'foreign' => 'id')
    );

    $this->hasMany('ElementListItem as ListItems', array(
      'local' => 'id',
      'foreign' => 'elementID')
    );

    $this->hasMany('ElementAnswer', array(
      'local' => 'id',
      'foreign' => 'elementID')
    );
  }
  
  /**
   * Find ListItems by weight
   * @returns Doctrine_Collection
   */
  public function findListItemsByWeight(){
    $q = Doctrine_Query::create()
      ->select('*')
      ->from('ElementListItem')
      ->where('elementID = ?', $this->id)
      ->orderBy('weight asc');
    return $q->execute();
  }
  
 /**
  * Get ListItem
  * @param integer $itemId
  * @return ListItem
  */
  public function getItemById($itemId){
    $key = array_search($itemId, $this->ListItems->getPrimaryKeys());
    if($key !== false){ //use === becuase 0 is returned often
      return $this->ListItems->get($key);
    }
    return false;
  }
  
 /**
  * Get Item by value
  * @param string $value
  * @return ListItem
  */
  public function getItemByValue($value){
    $q = Doctrine_Query::create()
      ->select('*')
      ->from('ElementListItem')
      ->where('elementID = ?', $this->id)
      ->andWhere('value=?', $value);
    return $q->execute();
  }
  
  /**
   * After we save the Element make sure all of its Items
   * At some point doctrine is unable to follow the relationships deep enough<commit_msg>Fix Element:getItemByValue to return the ElementItem and not a collection.<commit_after><?php

/**
 * Element
 * 
 * @property integer $pageID
 * @property integer $weight
 * @property integer $fixedID
 * @property integer $elementType
 * @property string $title
 * @property string $format
 * @property string $instructions
 * @property string $defaultValue
 * @property boolean $required
 * @property double $min
 * @property double $max
 * @property Page $Page
 * @property ElementType $ElementType
 * @property Doctrine_Collection $ListItems
 * @property Doctrine_Collection $ElementAnswer
 * @package    jazzee
 * @subpackage orm
 * @author     <NAME> <<EMAIL>>
 */
class Element extends Doctrine_Record{
  /**
   * @see BaseElement::setTableDefinition()
   */
  public function setTableDefinition(){
    $this->setTableName('element');
    $this->hasColumn('pageID', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('weight', 'integer', null, array(
      'type' => 'integer',
      'default' => 0,
     ));
    $this->hasColumn('fixedID', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('elementType', 'integer', null, array(
      'type' => 'integer',
     ));
    $this->hasColumn('title', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('format', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('instructions', 'string', 3000, array(
      'type' => 'string',
      'length' => '3000',
     ));
    $this->hasColumn('defaultValue', 'string', 255, array(
      'type' => 'string',
      'length' => '255',
     ));
    $this->hasColumn('required', 'boolean', null, array(
      'type' => 'boolean',
     ));
    $this->hasColumn('min', 'double', null, array(
      'type' => 'double',
     ));
    $this->hasColumn('max', 'double', null, array(
      'type' => 'double',
     ));


    $this->index('UniqueFixedID', array(
      'fields' => array(
        0 => 'fixedID',
        1 => 'pageID',
      ),
      'type' => 'unique',
     ));
  }

  /**
   * (non-PHPdoc)
   * @see BaseElement::setUp()
   */
  public function setUp(){
    parent::setUp();
    $this->hasOne('Page', array(
      'local' => 'pageID',
      'foreign' => 'id',
      'onDelete' => 'CASCADE',
      'onUpdate' => 'CASCADE')
    );

    $this->hasOne('ElementType', array(
      'local' => 'elementType',
      'foreign' => 'id')
    );

    $this->hasMany('ElementListItem as ListItems', array(
      'local' => 'id',
      'foreign' => 'elementID')
    );

    $this->hasMany('ElementAnswer', array(
      'local' => 'id',
      'foreign' => 'elementID')
    );
  }
  
  /**
   * Find ListItems by weight
   * @returns Doctrine_Collection
   */
  public function findListItemsByWeight(){
    $q = Doctrine_Query::create()
      ->select('*')
      ->from('ElementListItem')
      ->where('elementID = ?', $this->id)
      ->orderBy('weight asc');
    return $q->execute();
  }
  
 /**
  * Get ListItem
  * @param integer $itemId
  * @return ListItem
  */
  public function getItemById($itemId){
    $key = array_search($itemId, $this->ListItems->getPrimaryKeys());
    if($key !== false){ //use === becuase 0 is returned often
      return $this->ListItems->get($key);
    }
    return false;
  }
  
 /**
  * Get Item by value
  * @param string $value
  * @return ListItem
  */
  public function getItemByValue($value){
    $q = Doctrine_Query::create()
      ->select('*')
      ->from('ElementListItem')
      ->where('elementID = ?', $this->id)
      ->andWhere('value=?', $value);
    $result = $q->execute();
    if($result->count()) return $result->getFirst();
    return false;
  }
  
  /**
   * After we save the Element make sure all of its Items
   * At some point doctrine is unable to follow the relationships deep enough
{'fraction_non_alphanumeric': 0.18920684698810925, 'fraction_numerical': 0.007840062720501764, 'mean_word_length': 2.852038248616004, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 137, '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': '6791373', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2417, 'n_words': 565}