<commit_before><?php

/**
 * Data model for the text content item
 *
 * @author <NAME> <<EMAIL>>
 * @copyright G3D Development Limited
 * @license https://github.com/Dlayer/dlayer/blob/master/LICENSE
 */
class Dlayer_Model_Page_Content_Items_Text extends Zend_Db_Table_Abstract
{
	/**
	 * Add a new text content item
	 *
	 * @param integer $site_id
	 * @param integer $page_id
	 * @param integer $content_id
	 * @param array $params The params data array from the tool
	 * @return boolean
	 */
	public function add($site_id, $page_id, $content_id, array $params)
	{
		$result = false;

		$data_id = $this->existingDataId($site_id, $params['content']);

		if($data_id === FALSE)
		{
			$data_id = $this->addData($site_id, $params['name'], $params['content']);
		}

		if($data_id !== FALSE)
		{
			$sql = "INSERT INTO user_site_page_content_item_text 
                    (site_id, page_id, content_id, data_id) 
                    VALUES 
                    (:site_id, :page_id, :content_id, :data_id)";
			$stmt = $this->_db->prepare($sql);
			$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
			$stmt->bindValue(':page_id', $page_id, PDO::PARAM_INT);
			$stmt->bindValue(':content_id', $content_id, PDO::PARAM_INT);
			$stmt->bindValue(':data_id', $data_id, PDO::PARAM_INT);
			$result = $stmt->execute();
		}

		return $result;
	}

	/**
	 * Check to see if the content exists in the data tables, if so we re-=use the data from a previous content item
	 *
	 * @param integer $site_id
	 * @param string $content
	 * @return integer|FALSE Id of the existing data array or FALSE if a new content item
	 */
	private function existingDataId($site_id, $content)
	{
		$sql = "SELECT id 
                FROM user_site_content_text  
                WHERE site_id = :site_id  
				AND UPPER(content) = :content 
				LIMIT 1";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':content', strtoupper($content), PDO::PARAM_STMT);
		$stmt->execute();

		$result = $stmt->fetch();

		if($result !== FALSE)
		{
			return intval($result['id']);
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Add the new content item data into the content table for text items
	 *
	 * @param integer $site_id
	 * @param string $name
	 * @param string $content
	 * @return integer|FALSE The id for the new data or FALSE upon failure
	 */
	private function addData($site_id, $name, $content)
	{
		$sql = "INSERT INTO user_site_content_text 
				(site_id, `name`, content) 
				VALUES 
				(:site_id, :name, :content)";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':name', $name, PDO::PARAM_STR);
		$stmt->bindValue(':content', $content, PDO::PARAM_STR);
		$result = $stmt->execute();

		if($result === TRUE)
		{
			return intval($this->_db->lastInsertId('user_site_content_text'));
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Fetch the existing data for the content item
	 *
	 * @param integer $site_id
	 * @param integer $id
	 * @return array|FALSE The data array for the content item or FALSE upon failure
	 */
	public function existingData($site_id, $id)
	{
		$sql = "SELECT usct.`name`, usct.content
				FROM user_site_page_content_item_text uspcit 
				JOIN user_site_content_text usct 
					ON uspcit.data_id = usct.id
					AND usct.site_id = :site_id 
				WHERE uspcit.site_id = :site_id
				AND uspcit.content_id = :content_id";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':content_id', $id, PDO::PARAM_INT);
		$stmt->execute();

		return $stmt->fetch();
	}

	/**
	 * Edit the existing data
	 *
	 * @param integer $site_id
	 * @param integer $page_id
	 * @param integer $content_id
	 * @param array $params The params data array from the tool
	 * @return boolean
	 */
	public function edit($site_id, $page_id, $content_id, array $params)
	{
		var_dump($params); // loop for instances value


		$data_id = $this->existingDataId($site_id, $params['content']);

		// Add new data if id can't be using posted data, when instances are added, convert this to a check to see if the data has changed and then an instances check (see if new data equals old data)s
		if($data_id === FALSE)
		{
			$data_id = $this->addData($site_id, $params['name'], $params['content']);
		}<commit_msg>Instances in model<commit_after><?php

/**
 * Data model for the text content item
 *
 * @author <NAME> <<EMAIL>>
 * @copyright G3D Development Limited
 * @license https://github.com/Dlayer/dlayer/blob/master/LICENSE
 */
class Dlayer_Model_Page_Content_Items_Text extends Zend_Db_Table_Abstract
{
	/**
	 * Add a new text content item
	 *
	 * @param integer $site_id
	 * @param integer $page_id
	 * @param integer $content_id
	 * @param array $params The params data array from the tool
	 * @return boolean
	 */
	public function add($site_id, $page_id, $content_id, array $params)
	{
		$result = false;

		$data_id = $this->existingDataId($site_id, $params['content']);

		if($data_id === FALSE)
		{
			$data_id = $this->addData($site_id, $params['name'], $params['content']);
		}

		if($data_id !== FALSE)
		{
			$sql = "INSERT INTO user_site_page_content_item_text 
                    (site_id, page_id, content_id, data_id) 
                    VALUES 
                    (:site_id, :page_id, :content_id, :data_id)";
			$stmt = $this->_db->prepare($sql);
			$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
			$stmt->bindValue(':page_id', $page_id, PDO::PARAM_INT);
			$stmt->bindValue(':content_id', $content_id, PDO::PARAM_INT);
			$stmt->bindValue(':data_id', $data_id, PDO::PARAM_INT);
			$result = $stmt->execute();
		}

		return $result;
	}

	/**
	 * Check to see if the content exists in the data tables, if so we re-=use the data from a previous content item
	 *
	 * @param integer $site_id
	 * @param string $content
	 * @return integer|FALSE Id of the existing data array or FALSE if a new content item
	 */
	private function existingDataId($site_id, $content)
	{
		$sql = "SELECT id 
                FROM user_site_content_text  
                WHERE site_id = :site_id  
				AND UPPER(content) = :content 
				LIMIT 1";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':content', strtoupper($content), PDO::PARAM_STMT);
		$stmt->execute();

		$result = $stmt->fetch();

		if($result !== FALSE)
		{
			return intval($result['id']);
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Add the new content item data into the content table for text items
	 *
	 * @param integer $site_id
	 * @param string $name
	 * @param string $content
	 * @return integer|FALSE The id for the new data or FALSE upon failure
	 */
	private function addData($site_id, $name, $content)
	{
		$sql = "INSERT INTO user_site_content_text 
				(site_id, `name`, content) 
				VALUES 
				(:site_id, :name, :content)";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':name', $name, PDO::PARAM_STR);
		$stmt->bindValue(':content', $content, PDO::PARAM_STR);
		$result = $stmt->execute();

		if($result === TRUE)
		{
			return intval($this->_db->lastInsertId('user_site_content_text'));
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * Fetch the existing data for the content item
	 *
	 * @param integer $site_id
	 * @param integer $id
	 * @return array|FALSE The data array for the content item or FALSE upon failure
	 */
	public function existingData($site_id, $id)
	{
		$sql = "SELECT usct.`name`, usct.content
				FROM user_site_page_content_item_text uspcit 
				JOIN user_site_content_text usct 
					ON uspcit.data_id = usct.id
					AND usct.site_id = :site_id 
				WHERE uspcit.site_id = :site_id
				AND uspcit.content_id = :content_id";
		$stmt = $this->_db->prepare($sql);
		$stmt->bindValue(':site_id', $site_id, PDO::PARAM_INT);
		$stmt->bindValue(':content_id', $id, PDO::PARAM_INT);
		$stmt->execute();

		return $stmt->fetch();
	}

	/**
	 * Edit the existing data
	 *
	 * @param integer $site_id
	 * @param integer $page_id
	 * @param integer $content_id
	 * @param array $params The params data array from the tool
	 * @return boolean
	 */
	public function edit($site_id, $page_id, $content_id, array $params)
	{
		if(array_key_exists('instances', $params) === TRUE)
		{
			if($params['instances'] === 1)
			{
				// Update all items

				// Check to see if there is a data id for the new content, excluding current
					// Yes - Use that id and delete the old id after updating all content items
					// No - update the content for the selected data id

						// Will delete old
			}
			else
			{
				// Update just this item

				// Check to see if there is a data id for the new content, excluding current
					// Yes - Use that id - existing id
					// No - Add new data - new data id

						// Cant delete old, used
			}
		}
		else
		{
			// Existing data id has only one value in text table by virtue of no instances index.

			// Check to see if there is a data id for new content data, excluding current
				// Yes - Use new data id, delete old data
				// No - update the text for old data id

						// Will delete old
		}
{'fraction_non_alphanumeric': 0.14060134112048453, 'fraction_numerical': 0.0005407743889249405, 'mean_word_length': 3.3352086263478666, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '364141', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3077, 'n_words': 877}