<commit_before><?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page_model extends MY_Model {

	protected $table			= 'pages';
	protected $version_table	= 'versions';
	protected $key				= 'page_id';
	protected $soft_deletes		= true;
	protected $date_format		= 'datetime';
	protected $set_created		= true;
	protected $set_modified 	= true;
	
	//--------------------------------------------------------------------
	
	/*
		Updates an existing page and saves a version of the old data.	
	*/
	public function update($id=0, $data=null) 
	{
		// Sanity check
		if (empty($id) || !is_numeric($id) || !is_array($data))
		{
			$this->error = 'Not enough data.';
			return false;
		}
		
		// Grab our existing page so we can save a version of it
		$old_page = $this->find($id);
		
		// Update our version
		$data['revision'] += 1;
		
		// Save the new page data
		if (parent::update($id, $data))
		{
			// Save the version
			$this->save_version($id, $old_page);
			
			return true;
		}
		
		return false;
	}
	
	//--------------------------------------------------------------------
	
	
	//--------------------------------------------------------------------
	// !VERSIONING
	//--------------------------------------------------------------------
	
	/*
		Saves a version of the page data provided.
		
		Parameters:
			$id		- The id of the page to version.
			$data	- An array of data for the parent page. The data needed to save
						the version is pulled from this array. In order to properly
						save the verision, the array MUST include: 
							body		- the unprocessed body of the page.
							rte_type	- a string representing the type of text processor used. 'html', 'markdown', 'textile'
							revision	- the current version of the page. (This will be incremented in this method.)
							
		Return:
			true/false
	*/
	public function save_version($id=0, $data=null) 
	{
		// Sanity check
		if (empty($id) || !is_numeric($id) || !is_array($data))
		{
			$this->error = 'Not enough data.';
			return false;
		}
		
		// Data check
		if (!isset($data['body']) || !isset($data['rte_type']) || !isset($data['revision']))
		{
			$this->error = 'Not all of the page data was provided.';
			return false;
		}
		
		// Save it! 
		$new_data = array(
			'page_id'		=> $id,
			'revision'		=> $data['revision'] -1,
			'body'			=> $data['body'],
			'rte_type'		=> $data['rte_type'],
			'created_on'	=> date('Y-m-d H:i:s', time()),
			'created_by'	=> $this->auth->user_id()	
		);
		
		$i = $this->db->insert($this->version_table, $new_data);
		
		if ($i > 0)
		{
			return true;
		}
		
		$this->error = mysql_error();
		return false;
	}
	
	//--------------------------------------------------------------------
	

}<commit_msg>Fixed bug where version actually wasn't saving.<commit_after><?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page_model extends MY_Model {

	protected $table			= 'pages';
	protected $version_table	= 'versions';
	protected $key				= 'page_id';
	protected $soft_deletes		= true;
	protected $date_format		= 'datetime';
	protected $set_created		= true;
	protected $set_modified 	= true;
	
	//--------------------------------------------------------------------
	
	/*
		Updates an existing page and saves a version of the old data.	
	*/
	public function update($id=0, $data=null) 
	{
		// Sanity check
		if (empty($id) || !is_numeric($id) || !is_array($data))
		{
			$this->error = 'Not enough data.';
			return false;
		}
		
		$return = false;
		
		// Grab our existing page so we can save a version of it
		$old_page = $this->find($id);
		
		// Update our version
		$data['revision'] += 1;
		
		// Save the new page data
		$return = parent::update($id, $data);
		
		// Save the version
		$this->save_version($id, $old_page);
					
		return $return;
	}
	
	//--------------------------------------------------------------------
	
	
	//--------------------------------------------------------------------
	// !VERSIONING
	//--------------------------------------------------------------------
	
	/*
		Saves a version of the page data provided.
		
		Parameters:
			$id		- The id of the page to version.
			$data	- An array of data for the parent page. The data needed to save
						the version is pulled from this array. In order to properly
						save the verision, the array MUST include: 
							body		- the unprocessed body of the page.
							rte_type	- a string representing the type of text processor used. 'html', 'markdown', 'textile'
							revision	- the current version of the page. (This will be incremented in this method.)
							
		Return:
			true/false
	*/
	public function save_version($id=0, $data=null) 
	{	
		// Make sure data is an array
		$data = (array)$data;
	
		// Sanity check
		if (empty($id) || !is_numeric($id))
		{
			$this->error = 'Not enough data.';
			return false;
		}

		// Data check
		if (!isset($data['body']) || !isset($data['rte_type']) || !isset($data['revision']))
		{
			$this->error = 'Not all of the page data was provided.';
			return false;
		}
		
		// Save it! 
		$new_data = array(
			'page_id'		=> $id,
			'revision'		=> $data['revision'] -1,
			'body'			=> $data['body'],
			'rte_type'		=> $data['rte_type'],
			'created_on'	=> date('Y-m-d H:i:s', time()),
			'created_by'	=> $this->auth->user_id()	
		);
		
		$i = $this->db->insert($this->version_table, $new_data);
		
		if ($i > 0)
		{
			return true;
		}
		
		$this->error = mysql_error();
		return false;
	}
	
	//--------------------------------------------------------------------
	

}
{'fraction_non_alphanumeric': 0.2565209570066559, 'fraction_numerical': 0.0017988846914912754, 'mean_word_length': 1.801007556675063, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '7587957', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1691, 'n_words': 507}