<commit_before><?php
/**
 * G_Model_Chapter
 * class description
 *
 * @author       <NAME> | grrr.nl
 * @package      Garp
 * @subpackage   Model
 */
class G_Model_Chapter extends Model_Base_Chapter {
	/**
 	 * Content nodes. Saved here from beforeInsert til afterInsert
 	 * @var Array
 	 */
	protected $_contentNodeList = array();


	/**
 	 * BeforeInsert event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function beforeInsert(&$args) {
		$data = &$args[1];
		$this->_beforeSave($data);
	}


	/**
 	 * BeforeUpdate event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function beforeUpdate(&$args) {
		$data = &$args[1];
		$this->_beforeSave($data);
	}


	/**
 	 * AfterInsert event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function afterInsert(&$args) {
		$pk = $args[2];
		$this->_afterSave($pk);
	}


	/**
 	 * AfterUpdate event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function afterUpdate(&$args) {
		$model = $args[0];
		$where = $args[3];
		$primaryKey = $model->extractPrimaryKey($where);
		$id = $primaryKey['id'];
		$this->_afterSave($id);
	}


	/**
 	 * Generic beforeSave handler, called by beforeInsert and beforeUpdate
 	 * @param Array $data
 	 * @return Void
 	 */
	protected function _beforeSave(&$data) {
		// Make sure chapters are not saved.
		if (!empty($data['content'])) {
			// Save chapters until after the Article is inserted, because we need the primary key.
			if (is_string($data['content'])) {
				$data['content'] = Zend_Json::decode($data['content']);
			}
			$this->_contentNodeList = $data['content'];
		}
		unset($data['content']);
	}


	/**
 	 * Generic afterSave handler, called from afterInsert and afterUpdate
 	 * @param Int $id The id of the involved Article
 	 * @return Void
 	 */
	protected function _afterSave($id) {
		if (!empty($this->_contentNodeList)) {
			$this->relateContentNodes($this->_contentNodeList, $id);
			// Reset queue.
			$this->_contentNodeList = array();
		}
	}


	/**
	 * Relate ContentNodes to a chapter.
	 * @param Array $contentNodeList
	 * @param Int $chapterId
	 * @return Void
 	 */
	public function relateContentNodes($contentNodeList, $chapterId) {
		foreach ($contentNodeList as $contentNode) {
			$contentNode = $this->_getValidContentNodeData($contentNode);
			
			// Save ContentNode
			$contentNode['chapter_id'] = $chapterId;
			$contentNodeId = $this->_insertContentNode($contentNode);

			// @todo Move everything below here to G_Model_ContentNode::afterInsert()

			// Determine content type
			$contentTypeModelName = 'Model_'.$contentNode['model'];
			$contentTypeModel = new $contentTypeModelName();

			// Check for existing id
			$data = $contentNode['data'];
			if (empty($data['id'])) {
				// If no id is present, create a new subtype record
				$contentTypeId = $contentTypeModel->insert($data);
			} else {
				// Update the chapter subtype's content
				$contentTypeModel->update($data, 'id = '.$contentTypeModel->getAdapter()->quote($data['id']));<commit_msg>Reverse order of ContentNodes before saving<commit_after><?php
/**
 * G_Model_Chapter
 * class description
 *
 * @author       <NAME> | grrr.nl
 * @package      Garp
 * @subpackage   Model
 */
class G_Model_Chapter extends Model_Base_Chapter {
	/**
 	 * Content nodes. Saved here from beforeInsert til afterInsert
 	 * @var Array
 	 */
	protected $_contentNodeList = array();


	/**
 	 * BeforeInsert event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function beforeInsert(&$args) {
		$data = &$args[1];
		$this->_beforeSave($data);
	}


	/**
 	 * BeforeUpdate event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function beforeUpdate(&$args) {
		$data = &$args[1];
		$this->_beforeSave($data);
	}


	/**
 	 * AfterInsert event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function afterInsert(&$args) {
		$pk = $args[2];
		$this->_afterSave($pk);
	}


	/**
 	 * AfterUpdate event listener.
 	 * @param Array $args Event listener parameters
 	 * @return Void
 	 */
	public function afterUpdate(&$args) {
		$model = $args[0];
		$where = $args[3];
		$primaryKey = $model->extractPrimaryKey($where);
		$id = $primaryKey['id'];
		$this->_afterSave($id);
	}


	/**
 	 * Generic beforeSave handler, called by beforeInsert and beforeUpdate
 	 * @param Array $data
 	 * @return Void
 	 */
	protected function _beforeSave(&$data) {
		// Make sure chapters are not saved.
		if (!empty($data['content'])) {
			// Save chapters until after the Article is inserted, because we need the primary key.
			if (is_string($data['content'])) {
				$data['content'] = Zend_Json::decode($data['content']);
			}
			$this->_contentNodeList = $data['content'];
		}
		unset($data['content']);
	}


	/**
 	 * Generic afterSave handler, called from afterInsert and afterUpdate
 	 * @param Int $id The id of the involved Article
 	 * @return Void
 	 */
	protected function _afterSave($id) {
		if (!empty($this->_contentNodeList)) {
			$this->relateContentNodes($this->_contentNodeList, $id);
			// Reset queue.
			$this->_contentNodeList = array();
		}
	}


	/**
	 * Relate ContentNodes to a chapter.
	 * @param Array $contentNodeList
	 * @param Int $chapterId
	 * @return Void
 	 */
	public function relateContentNodes($contentNodeList, $chapterId) {
		// Reverse node list because the Weighable behavior sorts different from the way
		// the CMS sends us the nodes.
		$contentNodeList = array_reverse($contentNodeList);
		foreach ($contentNodeList as $contentNode) {
			$contentNode = $this->_getValidContentNodeData($contentNode);
			
			// Save ContentNode
			$contentNode['chapter_id'] = $chapterId;
			$contentNodeId = $this->_insertContentNode($contentNode);

			// @todo Move everything below here to G_Model_ContentNode::afterInsert()

			// Determine content type
			$contentTypeModelName = 'Model_'.$contentNode['model'];
			$contentTypeModel = new $contentTypeModelName();

			// Check for existing id
			$data = $contentNode['data'];
			if (empty($data['id'])) {
				// If no id is present, create a new subtype record
				$contentTypeId = $contentTypeModel->insert($data);
			} else {
				// Update the chapter subtype's content
				$contentTypeModel->update($data, 'id = '.$contentTypeModel->getAdapter()->quote($data['id']));
{'fraction_non_alphanumeric': 0.1430986781432524, 'fraction_numerical': 0.001537042729787888, 'mean_word_length': 3.6346153846153846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '513041', 'n_tokens_mistral': 2230, 'n_tokens_neox': 1939, 'n_words': 524}