<commit_before><?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Bonfire
 *
 * An open source project to allow developers get a jumpstart their development of CodeIgniter applications
 *
 * @package   Bonfire
 * @author    Bonfire Dev Team
 * @copyright Copyright (c) 2011 - 2012, Bonfire Dev Team
 * @license   http://cibonfire.com/docs/guides/license.html
 * @link      http://cibonfire.com
 * @since     Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Bonfire Base Model
 *
 * The Base model implements standard CRUD functions that can be
 * used and overriden by module models. This helps to maintain
 * a standard interface to program to, and makes module creation
 * faster.
 *
 * @package    Bonfire
 * @subpackage MY_Model
 * @category   Models
 * @author     <NAME>
 * @link       http://cibonfire.com/docs/guides/models.html
 *
 */
class BF_Model extends CI_Model
{

	/**
	 * Stores custom errors that can be used in UI error reporting.
	 *
	 * @var string
	 */
	public $error 		= '';

	/**
	 * The name of the db table this model primarily uses.
	 *
	 * @var string
	 * @access protected
	 */
	protected $table 	= '';

	/**
	 * The primary key of the table. Used as the 'id' throughout.
	 *
	 * @var string
	 * @access protected
	 */
	protected $key		= 'id';

	/**
	 * Field name to use to the created time column in the DB table.
	 *
	 * @var string
	 * @access protected
	 */
	protected $created_field = 'created_on';

	/**
	 * Field name to use to the modified time column in the DB table.
	 *
	 * @var string
	 * @access protected
	 */
	protected $modified_field = 'modified_on';

	/**
	 * Whether or not to auto-fill a 'created_on' field on inserts.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $set_created	= TRUE;

	/**
	 * Whether or not to auto-fill a 'modified_on' field on updates.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $set_modified = TRUE;

	/*
		Var: $log_user
		If TRUE, will log user id for 'created_by', 'modified_by' and 'deleted_by'.

		Access:
			Protected
	*/
	protected $log_user = FALSE;

	/*
		Var: $created_by_field
		Field name to use to the created by column in the DB table.

		Access:
			Protected
	*/
	protected $created_by_field = 'created_by';

	/*
		Var: $modified_by_field
		Field name to use to the modified by column in the DB table.

		Access:
			Protected
	*/
	protected $modified_by_field = 'modified_by';

	/*
		Var: $deleted_by_field
		Field name to use for the deleted by column in the DB table.

		Access:
			Protected
	*/
	protected $deleted_by_field = 'deleted_by';

	/**
	 * The type of date/time field used for created_on and modified_on fields.
	 * Valid types are: 'int', 'datetime', 'date'
	 *
	 * @var string
	 * @access protected
	 */
	protected $date_format = 'int';

	/**
	 * If FALSE, the delete() method will perform a TRUE delete of that row.
	 * If TRUE, a 'deleted' field will be set to 1.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $soft_deletes = FALSE;

	/**
	 * Stores any selects here for use by the find* functions.
	 *
	 * @var string
	 * @access protected
	 */
	protected $selects = '';

	/*
	Var: $escape
	If FALSE, the select() method will not try to protect your field or table names with backticks.
	This is useful if you need a compound select statement.

	Access:
		Protected
	*/
	protected $escape = TRUE;


	/**
	 * DB Connection details (string or array)
	 *
	 * @var mixed
	 */
	protected $db_con = '';

	//---------------------------------------------------------------

	/**
	 * Setup the DB connection if it doesn't exist
	 *
	 */
	public function __construct()
	{
		parent::__construct();

		// if there are specific DB connection settings used in a model
		// load the database using those settings.
		if (!empty($this->db_con)) {

			$this->db = $this->load->database($this->db_con, TRUE);
		}

		// If we're loading the model, then we probably need the
		// database, so make sure it's loaded.
		if (!isset($this->db))
		{
			$this->load->database();
		}

	}//end __construct()

	//---------------------------------------------------------------

	/**
	 * Searches for a single row in the database.
	 *
	 * @param string $id The primary key of the record to search for.
	 *
	 * @return mixed An object representing the db row, or FALSE.
	 */
	public function find($id='')
	{
		if ($this->_function_check($id) === FALSE)
		{
			return FALSE;
		}

		$this->set_selects();

		$query = $this->db->get_where($this->table, array($this->table.'.'. $this->key => $id));

		if ($query->num_rows())
		{
			return $query->row();
		}

		return FALSE;

	}//end find()

	//---------------------------------------------------------------

	/**
	 * Returns all records in the table.
	 *
	 * By default, there is no 'where' clause, but you can filter
	 * the results that are returned by using either CodeIgniter's
	 * Active Record functions before calling this function, or
	 * through method chaining with the where() method of this class.
	 *
	 * @return mixed An array of objects representing the results, or FALSE on failure or empty set.
	 */
	public function find_all()
	{
		if ($this->_function_check() === FALSE)
		{
			return FALSE;
		}

		$this->set_selects();

		$this->db->from($this->table);

		$query = $this->db->get();

		if (!empty($query) && $query->num_rows() > 0)
		{
			return $query->result();
		}

		$this->error = $this->lang->line('bf_model_bad_select');
		$this->logit('['. get_class($this) .': '. __METHOD__ .'] '. $this->lang->line('bf_model_bad_select'));
		return FALSE;

	}//end find_all()

	//---------------------------------------------------------------

	/**
	 * A convenience method that combines a where() and find_all() call into a single call.
	 *
	 * @param mixed  $field The table field to search in.
	 * @param mixed  $value The value that field should be.
	 * @param string $type  The type of where clause to create. Either 'and' or 'or'.
	 *
	 * @return bool|mixed An array of objects representing the results, or FALSE on failure or empty set.
	 */
	public function find_all_by($field=NULL, $value=NULL, $type='and')
	{
		if (empty($field)) return FALSE;

		// Setup our field/value check
		if (is_array($field))
		{
			foreach ($field as $key => $value)
			{
				if ($type == 'or')
				{
					$this->db->or_where($key, $value);
				}
				else
				{
					$this->db->where($key, $value);
				}
			}
		}
		else
		{
			$this->db->where($field, $value);
		}

		$this->set_selects();

		return $this->find_all();

	}//end find_all_by()

	//--------------------------------------------------------------------

	/**
	 * Returns the first result that matches the field/values passed.
	 *
	 * @param string $field Either a string or an array of fields to match against. If an array is passed it, the $value parameter is ignored since the array is expected to have key/value pairs in it.
	 * @param string $value The value to match on the $field. Only used when $field is a string.
	 * @param string $type  The type of where clause to create. Either 'and' or 'or'.
	 *
	 * @return bool|mixed An object representing the first result returned.
	 */
	public function find_by($field='', $value='', $type='and')
	{
		if (empty($field) || (!is_array($field) && empty($value)))
		{
			$this->error = $this->lang->line('bf_model_find_error');
			$this->logit('['. get_class($this) .': '. __METHOD__ .'] '. $this->lang->line('bf_model_find_error'));
			return FALSE;
		}

		if (is_array($field))
		{
			foreach ($field as $key => $value)
			{
				if ($type == 'or')
				{
					$this->db->or_where($key, $value);
				}
				else
				{
					$this->db->where($key, $value);
				}
			}
		}
		else
		{
			$this->db->where($field, $value);
		}

		$this->set_selects();

		$query = $this->db->get($this->table);

		if ($query && $query->num_rows() > 0)
		{
			return $query->row();
		}

		return FALSE;

	}//end find_by()

	//---------------------------------------------------------------

	/**
	 * Inserts a row of data into the database.
	 *
	 * @param array $data an array of key/value pairs to insert.
	 *
	 * @return bool|mixed Either the $id of the row inserted, or FALSE on failure.
	 */
	public function insert($data=NULL)
	{
		if ($this->_function_check(FALSE, $data) === FALSE)
		{
			return FALSE;
		}

		// Add the created field
		if ($this->set_created === TRUE && !array_key_exists($this->created_field, $data))
		{
			$data[$this->created_field] = $this->set_date();
		}

		if ($this->set_created === TRUE && $this->log_user === TRUE && !array_key_exists($this->created_by_field, $data))
		{
			$data[$this->created_by_field] = $this->auth->user_id();
		}

		// Insert it
		$status = $this->db->insert($this->table, $data);

		if ($status != FALSE)
		{
			return $this->db->insert_id();
		}
		else
		{
			$this->error = mysql_error();
			return FALSE;
		}

	}//end insert()

	//---------------------------------------------------------------
	
	/**
	 * Inserts a batch of data into the database.
	 *
	 * @param array $data an array of key/value pairs to insert.
	 *
	 * @return bool|mixed Either the $id of the row inserted, or FALSE on failure.
	 */
	public function insert_batch($data=NULL)
	{
		if ($this->_function_check(FALSE, $data) === FALSE)
		{
			return FALSE;
		}
		
		$set = array();

		// Add the created field
		if ($this->set_created === TRUE )
		{
			$set[$this->created_field] = $this->set_date();
		} 

		if ($this->set_created === TRUE && $this->log_user === TRUE)
		{
			$set[$this->created_by_field] = $this->auth->user_id();
		}

		if(!empty($set))
		{
			foreach($data as $key => $record)
			{
				$data[$key] = array_merge($set,$data[$key]);
			}
		}


		// Insert it
		$status = $this->db->insert_batch($this->table, $data);

		if ($status != FALSE)
		{
			return TRUE;
		}
		else
		{
			$this->error = mysql_error();
			return FALSE;
		}

	}//end insert_batch()

	//---------------------------------------------------------------

	/**
	 * Updates an existing row in the database.
	 *<commit_msg>checking for false and returning<commit_after><?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Bonfire
 *
 * An open source project to allow developers get a jumpstart their development of CodeIgniter applications
 *
 * @package   Bonfire
 * @author    Bonfire Dev Team
 * @copyright Copyright (c) 2011 - 2012, Bonfire Dev Team
 * @license   http://cibonfire.com/docs/guides/license.html
 * @link      http://cibonfire.com
 * @since     Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Bonfire Base Model
 *
 * The Base model implements standard CRUD functions that can be
 * used and overriden by module models. This helps to maintain
 * a standard interface to program to, and makes module creation
 * faster.
 *
 * @package    Bonfire
 * @subpackage MY_Model
 * @category   Models
 * @author     <NAME>
 * @link       http://cibonfire.com/docs/guides/models.html
 *
 */
class BF_Model extends CI_Model
{

	/**
	 * Stores custom errors that can be used in UI error reporting.
	 *
	 * @var string
	 */
	public $error 		= '';

	/**
	 * The name of the db table this model primarily uses.
	 *
	 * @var string
	 * @access protected
	 */
	protected $table 	= '';

	/**
	 * The primary key of the table. Used as the 'id' throughout.
	 *
	 * @var string
	 * @access protected
	 */
	protected $key		= 'id';

	/**
	 * Field name to use to the created time column in the DB table.
	 *
	 * @var string
	 * @access protected
	 */
	protected $created_field = 'created_on';

	/**
	 * Field name to use to the modified time column in the DB table.
	 *
	 * @var string
	 * @access protected
	 */
	protected $modified_field = 'modified_on';

	/**
	 * Whether or not to auto-fill a 'created_on' field on inserts.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $set_created	= TRUE;

	/**
	 * Whether or not to auto-fill a 'modified_on' field on updates.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $set_modified = TRUE;

	/*
		Var: $log_user
		If TRUE, will log user id for 'created_by', 'modified_by' and 'deleted_by'.

		Access:
			Protected
	*/
	protected $log_user = FALSE;

	/*
		Var: $created_by_field
		Field name to use to the created by column in the DB table.

		Access:
			Protected
	*/
	protected $created_by_field = 'created_by';

	/*
		Var: $modified_by_field
		Field name to use to the modified by column in the DB table.

		Access:
			Protected
	*/
	protected $modified_by_field = 'modified_by';

	/*
		Var: $deleted_by_field
		Field name to use for the deleted by column in the DB table.

		Access:
			Protected
	*/
	protected $deleted_by_field = 'deleted_by';

	/**
	 * The type of date/time field used for created_on and modified_on fields.
	 * Valid types are: 'int', 'datetime', 'date'
	 *
	 * @var string
	 * @access protected
	 */
	protected $date_format = 'int';

	/**
	 * If FALSE, the delete() method will perform a TRUE delete of that row.
	 * If TRUE, a 'deleted' field will be set to 1.
	 *
	 * @var boolean
	 * @access protected
	 */
	protected $soft_deletes = FALSE;

	/**
	 * Stores any selects here for use by the find* functions.
	 *
	 * @var string
	 * @access protected
	 */
	protected $selects = '';

	/*
	Var: $escape
	If FALSE, the select() method will not try to protect your field or table names with backticks.
	This is useful if you need a compound select statement.

	Access:
		Protected
	*/
	protected $escape = TRUE;


	/**
	 * DB Connection details (string or array)
	 *
	 * @var mixed
	 */
	protected $db_con = '';

	//---------------------------------------------------------------

	/**
	 * Setup the DB connection if it doesn't exist
	 *
	 */
	public function __construct()
	{
		parent::__construct();

		// if there are specific DB connection settings used in a model
		// load the database using those settings.
		if (!empty($this->db_con)) {

			$this->db = $this->load->database($this->db_con, TRUE);
		}

		// If we're loading the model, then we probably need the
		// database, so make sure it's loaded.
		if (!isset($this->db))
		{
			$this->load->database();
		}

	}//end __construct()

	//---------------------------------------------------------------

	/**
	 * Searches for a single row in the database.
	 *
	 * @param string $id The primary key of the record to search for.
	 *
	 * @return mixed An object representing the db row, or FALSE.
	 */
	public function find($id='')
	{
		if ($this->_function_check($id) === FALSE)
		{
			return FALSE;
		}

		$this->set_selects();

		$query = $this->db->get_where($this->table, array($this->table.'.'. $this->key => $id));

		if ($query->num_rows())
		{
			return $query->row();
		}

		return FALSE;

	}//end find()

	//---------------------------------------------------------------

	/**
	 * Returns all records in the table.
	 *
	 * By default, there is no 'where' clause, but you can filter
	 * the results that are returned by using either CodeIgniter's
	 * Active Record functions before calling this function, or
	 * through method chaining with the where() method of this class.
	 *
	 * @return mixed An array of objects representing the results, or FALSE on failure or empty set.
	 */
	public function find_all()
	{
		if ($this->_function_check() === FALSE)
		{
			return FALSE;
		}

		$this->set_selects();

		$this->db->from($this->table);

		$query = $this->db->get();

		if (!empty($query) && $query->num_rows() > 0)
		{
			return $query->result();
		}

		$this->error = $this->lang->line('bf_model_bad_select');
		$this->logit('['. get_class($this) .': '. __METHOD__ .'] '. $this->lang->line('bf_model_bad_select'));
		return FALSE;

	}//end find_all()

	//---------------------------------------------------------------

	/**
	 * A convenience method that combines a where() and find_all() call into a single call.
	 *
	 * @param mixed  $field The table field to search in.
	 * @param mixed  $value The value that field should be.
	 * @param string $type  The type of where clause to create. Either 'and' or 'or'.
	 *
	 * @return bool|mixed An array of objects representing the results, or FALSE on failure or empty set.
	 */
	public function find_all_by($field=NULL, $value=NULL, $type='and')
	{
		if (empty($field)) return FALSE;

		// Setup our field/value check
		if (is_array($field))
		{
			foreach ($field as $key => $value)
			{
				if ($type == 'or')
				{
					$this->db->or_where($key, $value);
				}
				else
				{
					$this->db->where($key, $value);
				}
			}
		}
		else
		{
			$this->db->where($field, $value);
		}

		$this->set_selects();

		return $this->find_all();

	}//end find_all_by()

	//--------------------------------------------------------------------

	/**
	 * Returns the first result that matches the field/values passed.
	 *
	 * @param string $field Either a string or an array of fields to match against. If an array is passed it, the $value parameter is ignored since the array is expected to have key/value pairs in it.
	 * @param string $value The value to match on the $field. Only used when $field is a string.
	 * @param string $type  The type of where clause to create. Either 'and' or 'or'.
	 *
	 * @return bool|mixed An object representing the first result returned.
	 */
	public function find_by($field='', $value='', $type='and')
	{
		if (empty($field) || (!is_array($field) && empty($value)))
		{
			$this->error = $this->lang->line('bf_model_find_error');
			$this->logit('['. get_class($this) .': '. __METHOD__ .'] '. $this->lang->line('bf_model_find_error'));
			return FALSE;
		}

		if (is_array($field))
		{
			foreach ($field as $key => $value)
			{
				if ($type == 'or')
				{
					$this->db->or_where($key, $value);
				}
				else
				{
					$this->db->where($key, $value);
				}
			}
		}
		else
		{
			$this->db->where($field, $value);
		}

		$this->set_selects();

		$query = $this->db->get($this->table);

		if ($query && $query->num_rows() > 0)
		{
			return $query->row();
		}

		return FALSE;

	}//end find_by()

	//---------------------------------------------------------------

	/**
	 * Inserts a row of data into the database.
	 *
	 * @param array $data an array of key/value pairs to insert.
	 *
	 * @return bool|mixed Either the $id of the row inserted, or FALSE on failure.
	 */
	public function insert($data=NULL)
	{
		if ($this->_function_check(FALSE, $data) === FALSE)
		{
			return FALSE;
		}

		// Add the created field
		if ($this->set_created === TRUE && !array_key_exists($this->created_field, $data))
		{
			$data[$this->created_field] = $this->set_date();
		}

		if ($this->set_created === TRUE && $this->log_user === TRUE && !array_key_exists($this->created_by_field, $data))
		{
			$data[$this->created_by_field] = $this->auth->user_id();
		}

		// Insert it
		$status = $this->db->insert($this->table, $data);

		if ($status != FALSE)
		{
			return $this->db->insert_id();
		}
		else
		{
			$this->error = mysql_error();
			return FALSE;
		}

	}//end insert()

	//---------------------------------------------------------------
	
	/**
	 * Inserts a batch of data into the database.
	 *
	 * @param array $data an array of key/value pairs to insert.
	 *
	 * @return bool|mixed Either the $id of the row inserted, or FALSE on failure.
	 */
	public function insert_batch($data=NULL)
	{
		if ($this->_function_check(FALSE, $data) === FALSE)
		{
			return FALSE;
		}
		
		$set = array();

		// Add the created field
		if ($this->set_created === TRUE )
		{
			$set[$this->created_field] = $this->set_date();
		} 

		if ($this->set_created === TRUE && $this->log_user === TRUE)
		{
			$set[$this->created_by_field] = $this->auth->user_id();
		}

		if(!empty($set))
		{
			foreach($data as $key => $record)
			{
				$data[$key] = array_merge($set,$data[$key]);
			}
		}


		// Insert it
		$status = $this->db->insert_batch($this->table, $data);

		if ($status === FALSE)
		{
			$this->error = mysql_error();
			return FALSE;
		}

		return TRUE;

	}//end insert_batch()

	//---------------------------------------------------------------

	/**
	 * Updates an existing row in the database.
	 *
{'fraction_non_alphanumeric': 0.1980772089796323, 'fraction_numerical': 0.0012884682095247535, 'mean_word_length': 2.352159468438538, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 193, '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': '7541554', 'n_tokens_mistral': 7046, 'n_tokens_neox': 6159, 'n_words': 2005}