<commit_before><?php

/**
 * TLContent class.
 * 
 * An ORM for describing content and relationships between content
 *
 * Call with an urlid or content id to autopopulate, or construct and then call setContentType for a empty TLContent
 *
 * @package 10Layer
 * @subpackage Core
 */
class TLContent {
	/**
	 * content_id
	 * 
	 * (default value: false)
	 * 
	 * @var int
	 * @access public
	 */
	public $content_id=false;
	/**
	 * urlid
	 * 
	 * (default value: false)
	 * 
	 * @var string
	 * @access public
	 */
	public $urlid=false;
	/**
	 * fields
	 * 
	 * An array of type TLField (default value: array())
	 * 
	 * @var array
	 * @access public
	 */
	public $fields=array();
	/**
	 * content_type
	 * 
	 * (default value: false)
	 * 
	 * @var string
	 * @access public
	 */
	public $content_type=false;
	
	public function __construct($content_type) {
		$this->setContentType($content_type);
	}
	
	/**
	 * __set function.
	 * 
	 * This dynamic setter allows us to set a value for the object
	 *
	 * @access public
	 * @param mixed $name
	 * @param mixed $val
	 * @return void
	 */
	public function __set($name, $val) {
		if (isset($this->$name)) {
			$this->$name=$val;
			return true;
		}
		foreach($this->fields as $key=>$field) {
			if ($name==$key) {
				if (($this->fields[$key]->multiple) && (!is_array($val))) {
					$this->fields[$key]->value[]=$val;
				} else {
					$this->fields[$key]->value=$val;
				}
			}
		}
	}

	
	/**
	 * setContentType function.
	 * 
	 * Lets us specify the content type. It'll take an urlid or an ID.
	 *
	 * @access public
	 * @param mixed $id
	 * @return void
	 */
	public function setContentType($id) {
		$ci=&get_instance();
		$ci->mongo_db->state_save();
		$query=$ci->mongo_db->get_where("content_types", array("_id"=>$id));
		$ci->mongo_db->state_restore();
		if (empty($query)) {
			show_error("Could not find content type $id");
		}		
		$this->content_type=$query[0];
		$this->populateFields();
	}
	
	/**
	 * getContentType function.
	 * 
	 * Returns our content type
	 *
	 * @access public
	 * @param bool $tbl. (default: false)
	 * @return object
	 */
	public function getContentType($tbl=false) {
		return $this->content_type;
	}
	
	/**
	 * populateFields function.
	 * 
	 * This inspects our model and gets our fields
	 *
	 * @access public
	 * @return object
	 */
	public function populateFields() {
		$ci=&get_instance();
		foreach($this->content_type->fields as $field) {
			$this->addField($field);
		}
		$this->getOptions();
		return $this;
	}
	
	/**
	 * addField function.
	 * 
	 * Allows us to add another field with default values.
	 *
	 * @access protected
	 * @param mixed $tbl
	 * @param mixed $field
	 * @return void
	 */
	protected function addField($field) {
		if (empty($field["contenttype"])) {
			$field["contenttype"]=$this->content_type->_id;
		}
		if ($field["contenttype"]!=$this->content_type->_id) {
			$field["external"]=true;
		}
		$this->fields[$field["name"]]=new TLField($field);
		return $this;
	}
	
	/**
	 * setFieldValue function.
	 * 
	 * Sets a specific field's value
	 *
	 * @access public
	 * @param mixed $tbl
	 * @param mixed $name
	 * @param mixed $value
	 * @return void
	 */
	public function setFieldValue($tbl,$name,$value) {
		if (isset($this->{$tbl}->fields[$name])) {
			$this->{$tbl}->fields[$name]->value=$value;
		} else {
			show_error("Couldn't find field $name");
		}
	}
	
	/**
	 * getFields function.
	 * 
	 * Returns all our fields
	 *
	 * @access public
	 * @return void
	 */
	public function getFields() {
		return $this->fields;
	}
	
	/**
	 * getOptions function.
	 * 
	 * Returns all possible options for a specific field
	 *
	 * @access protected
	 * @param mixed $tbl
	 * @return void
	 */
	protected function getOptions() {
		$ci=&get_instance();
		foreach($this->fields as $key=>$field) {
			if (($field->type=="select") && empty($this->fields[$key]->options)) {
				$result=$ci->mongo_db->where(array("content_type"=>$field->contenttype))->sort("title DESC")->get("content");
				if(!empty($result)) {
					foreach($result() as $item) {
						$this->fields[$key]->options[$item->_id]=$item->title;
					}
				}
			}
		}
	}
	<commit_msg>Fix bug with select<commit_after><?php

/**
 * TLContent class.
 * 
 * An ORM for describing content and relationships between content
 *
 * Call with an urlid or content id to autopopulate, or construct and then call setContentType for a empty TLContent
 *
 * @package 10Layer
 * @subpackage Core
 */
class TLContent {
	/**
	 * content_id
	 * 
	 * (default value: false)
	 * 
	 * @var int
	 * @access public
	 */
	public $content_id=false;
	/**
	 * urlid
	 * 
	 * (default value: false)
	 * 
	 * @var string
	 * @access public
	 */
	public $urlid=false;
	/**
	 * fields
	 * 
	 * An array of type TLField (default value: array())
	 * 
	 * @var array
	 * @access public
	 */
	public $fields=array();
	/**
	 * content_type
	 * 
	 * (default value: false)
	 * 
	 * @var string
	 * @access public
	 */
	public $content_type=false;
	
	public function __construct($content_type) {
		$this->setContentType($content_type);
	}
	
	/**
	 * __set function.
	 * 
	 * This dynamic setter allows us to set a value for the object
	 *
	 * @access public
	 * @param mixed $name
	 * @param mixed $val
	 * @return void
	 */
	public function __set($name, $val) {
		if (isset($this->$name)) {
			$this->$name=$val;
			return true;
		}
		foreach($this->fields as $key=>$field) {
			if ($name==$key) {
				if (($this->fields[$key]->multiple) && (!is_array($val))) {
					$this->fields[$key]->value[]=$val;
				} else {
					$this->fields[$key]->value=$val;
				}
			}
		}
	}

	
	/**
	 * setContentType function.
	 * 
	 * Lets us specify the content type. It'll take an urlid or an ID.
	 *
	 * @access public
	 * @param mixed $id
	 * @return void
	 */
	public function setContentType($id) {
		$ci=&get_instance();
		$ci->mongo_db->state_save();
		$query=$ci->mongo_db->get_where("content_types", array("_id"=>$id));
		$ci->mongo_db->state_restore();
		if (empty($query)) {
			show_error("Could not find content type $id");
		}		
		$this->content_type=$query[0];
		$this->populateFields();
	}
	
	/**
	 * getContentType function.
	 * 
	 * Returns our content type
	 *
	 * @access public
	 * @param bool $tbl. (default: false)
	 * @return object
	 */
	public function getContentType($tbl=false) {
		return $this->content_type;
	}
	
	/**
	 * populateFields function.
	 * 
	 * This inspects our model and gets our fields
	 *
	 * @access public
	 * @return object
	 */
	public function populateFields() {
		$ci=&get_instance();
		foreach($this->content_type->fields as $field) {
			$this->addField($field);
		}
		$this->getOptions();
		return $this;
	}
	
	/**
	 * addField function.
	 * 
	 * Allows us to add another field with default values.
	 *
	 * @access protected
	 * @param mixed $tbl
	 * @param mixed $field
	 * @return void
	 */
	protected function addField($field) {
		if (empty($field["contenttype"])) {
			$field["contenttype"]=$this->content_type->_id;
		}
		if ($field["contenttype"]!=$this->content_type->_id) {
			$field["external"]=true;
		}
		$this->fields[$field["name"]]=new TLField($field);
		return $this;
	}
	
	/**
	 * setFieldValue function.
	 * 
	 * Sets a specific field's value
	 *
	 * @access public
	 * @param mixed $tbl
	 * @param mixed $name
	 * @param mixed $value
	 * @return void
	 */
	public function setFieldValue($tbl,$name,$value) {
		if (isset($this->{$tbl}->fields[$name])) {
			$this->{$tbl}->fields[$name]->value=$value;
		} else {
			show_error("Couldn't find field $name");
		}
	}
	
	/**
	 * getFields function.
	 * 
	 * Returns all our fields
	 *
	 * @access public
	 * @return void
	 */
	public function getFields() {
		return $this->fields;
	}
	
	/**
	 * getOptions function.
	 * 
	 * Returns all possible options for a specific field
	 *
	 * @access protected
	 * @param mixed $tbl
	 * @return void
	 */
	protected function getOptions() {
		$ci=&get_instance();
		foreach($this->fields as $key=>$field) {
			if (($field->type=="select") && empty($this->fields[$key]->options)) {
				$result=$ci->mongo_db->where(array("content_type"=>$field->contenttype))->get("content");
				if(!empty($result)) {
					foreach($result() as $item) {
						$this->fields[$key]->options[$item->_id]=$item->title;
					}
				}
			}
		}
	}
	
{'fraction_non_alphanumeric': 0.17177322074788903, 'fraction_numerical': 0.0007237635705669482, 'mean_word_length': 3.12898406374502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 104, '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': '4012843', 'n_tokens_mistral': 3165, 'n_tokens_neox': 2697, 'n_words': 660}