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

/**
 * Name:		blog_post_model
 *
 * Description:	This model handles all interactions with blog posts on site.
 *
 **/

/**
 * OVERLOADING NAILS' MODELS
 *
 * Note the name of this class; done like this to allow apps to extend this class.
 * Read full explanation at the bottom of this file.
 *
 **/

class NAILS_Blog_post_model extends NAILS_Model
{

	protected $_reserved;


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


	public function __construct()
	{
		parent::__construct();

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

		$this->_table				= NAILS_DB_PREFIX . 'blog_post';
		$this->_table_prefix		= 'bp';	//	Hard-coded throughout model; take care when changing this
		$this->_table_label_column	= 'title';
		$this->_destructive_delete	= FALSE;

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

		//	Define reserved words (for slugs, basically just controller methods)
		$this->_reserved = array( 'index', 'single', 'category','tag', 'archive' );
	}


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


	/**
	 * Creates a new object
	 *
	 * @access public
	 * @param array $data The data to create the object with
	 * @return mixed
	 **/
	public function create( $data = array() )
	{
		//	Prepare slug
		$_counter = 0;

		if ( empty( $data['title'] ) ) :

			$this->_set_error( 'Title missing' );
			return FALSE;

		endif;

		if ( empty( $data['blog_id'] ) ) :

			$this->_set_error( 'Blog ID missing' );
			return FALSE;

		endif;

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

		//	Generate a slug
		$_prefix = array_search( $data['title'], $this->_reserved ) !== FALSE ? 'post-' : '';
		$this->db->set( 'slug', $this->_generate_slug( $data['title'], $_prefix ) );

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

		//	Set data
		$this->db->set( 'blog_id', $data['blog_id'] );
		$this->db->set( 'title', $data['title'] );

		if ( isset( $data['body'] ) ) :				$this->db->set( 'body',				$data['body'] );			endif;
		if ( isset( $data['seo_title'] ) ) :		$this->db->set( 'seo_title',		$data['title'] );			endif;
		if ( isset( $data['seo_description'] ) ) :	$this->db->set( 'seo_description',	$data['seo_description'] );	endif;
		if ( isset( $data['seo_keywords'] ) ) :		$this->db->set( 'seo_keywords',		$data['seo_keywords'] );	endif;
		if ( isset( $data['is_published'] ) ) :		$this->db->set( 'is_published',		$data['is_published'] );	endif;

		//	Safety first!
		if ( array_key_exists( 'image_id', $data ) ) :

			$_image_id = (int) $data['image_id'];
			$_image_id = ! $_image_id ? NULL : $_image_id;

			$this->db->set( 'image_id', $_image_id );

		endif;

		//	Excerpt
		if ( ! empty( $data['excerpt'] ) ) :

			$this->db->set( 'excerpt', trim( strip_tags( $data['excerpt'] ) ) );

		elseif ( ! empty( $data['body'] ) ) :

			$this->db->set( 'excerpt', word_limiter( trim( strip_tags( $data['body'] ) ) ), 50 );

		endif;

		//	Publish date
		if ( ! empty( $data['is_published'] ) && isset( $data['published'] ) ) :

			//	Published with date set
			$_published = strtotime( $data['published'] );

			if ( $_published ) :

				$_published = user_rdatetime( $data['published'] );

				$this->db->set( 'published', $_published );

			else :

				//	Failed, use NOW();
				$this->db->set( 'published', 'NOW()', FALSE );

			endif;

		else :

			//	No date set, use NOW()
			$this->db->set( 'published', 'NOW()', FALSE );

		endif;

		$this->db->set( 'created',			'NOW()', FALSE );
		$this->db->set( 'modified',			'NOW()', FALSE );
		$this->db->set( 'created_by',		active_user( 'id' ) );
		$this->db->set( 'modified_by',		active_user( 'id' ) );

		$this->db->insert( NAILS_DB_PREFIX . 'blog_post' );

		if ( $this->db->affected_rows() ) :

			$_id = $this->db->insert_id();

			//	Add Gallery items, if any
			if ( ! empty( $data['gallery'] ) ) :

				$_data = array();

				foreach ( $data['gallery'] AS $order => $image_id ) :

					if ( (int) $image_id ) :

						$_data[] = array( 'post_id' => $_id, 'image_id' => $image_id, 'order' => $order );

					endif;

				endforeach;

				if ( $_data ) :

					$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_image', $_data );

				endif;

			endif;

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

			//	Add Categories and tags, if any
			if ( ! empty( $data['categories'] ) ) :

				$_data = array();

				foreach ( $data['categories'] AS $cat_id ) :

					$_data[] = array( 'post_id' => $_id, 'category_id' => $cat_id );

				endforeach;

				$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_category', $_data );

			endif;

			if ( ! empty( $data['tags'] ) ) :

				$_data = array();

				foreach ( $data['tags'] AS $tag_id ) :

					$_data[] = array( 'post_id' => $_id, 'tag_id' => $tag_id );

				endforeach;

				$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_tag', $_data );

			endif;

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

			//	Add associations, if any
			if ( ! empty( $data['associations'] ) ) :

				//	Fetch association config
				$_association = $this->config->item( 'blog_post_associations' );

				foreach ( $data['associations'] AS $index => $assoc ) :

					if ( ! isset( $_association[$index] ) ) :

						continue;

					endif;

					$_data = array();

					foreach( $assoc AS $id ) :

						$_data[] = array( 'post_id' => $_id, 'associated_id' => $id );

					endforeach;

					if ( $_data ) :

						$this->db->insert_batch( $_association[$index]->target, $_data );

					endif;

				endforeach;

			endif;

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

			return $_id;

		else :

			return FALSE;

		endif;
	}


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


	/**
	 * Updates an existing object
	 *
	 * @access public
	 * @param int $id The ID of the object to update
	 * @param array $data The data to update the object with
	 * @return bool
	 **/
	public function update( $id, $data = array() )
	{
		//	Prepare slug; the slug is regenrated if the blog post is in a transitioning state
		//	to published. We don't want to be changing slugs of published posts but while
		//	it's a draft we can do whatever (even if it was previously published, made a draft
		//	then republished).

		$this->db->select( 'is_published' );
		$this->db->where( 'id', $id );
		$_current = $this->db->get( NAILS_DB_PREFIX . 'blog_post' )->row();

		if ( ! $_current->is_published && $data['is_published'] ) :

			$_counter = 0;

			if ( ! isset( $data['title'] ) || ! $data['title'] ) :

				$this->_set_error( 'Title missing' );
				return FALSE;

			endif;

			//	Generate a slug
			$_prefix	= array_search( $data['title'], $this->_reserved ) !== FALSE ? 'post-' : '';
			$_slug		= $this->_generate_slug( $data['title'], $_prefix );

		else :

			$_slug = FALSE;

		endif;

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

		//	Set data
		if ( isset( $data['blog_id'] ) ) :			$this->db->set( 'blog_id',			$data['blog_id'] );			endif;
		if ( isset( $data['title'] ) ) :			$this->db->set( 'title',			$data['title'] );			endif;
		if ( isset( $data['body'] ) ) :				$this->db->set( 'body',				$data['body'] );			endif;
		if ( isset( $data['seo_title'] ) ) :		$this->db->set( 'seo_title',		$data['title'] );			endif;
		if ( isset( $data['seo_description'] ) ) :	$this->db->set( 'seo_description',	$data['seo_description'] );	endif;
		if ( isset( $data['seo_keywords'] ) ) :		$this->db->set( 'seo_keywords',		$data['seo_keywords'] );	endif;
		if ( isset( $data['is_published'] ) ) :		$this->db->set( 'is_published',		$data['is_published'] );	endif;
		if ( isset( $data['is_deleted'] ) ) :		$this->db->set( 'is_deleted',		$data['is_deleted'] );		endif;

		//	Safety first!
		if ( array_key_exists( 'image_id', $data ) ) :

			$_image_id = (int) $data['image_id'];
			$_image_id = ! $_image_id ? NULL : $_image_id;

			$this->db->set( 'image_id', $_image_id );

		endif;

		//	Excerpt
		if ( ! empty( $data['excerpt'] ) ) :

			$this->db->set( 'excerpt', trim( strip_tags( $data['excerpt'] ) ) );

		elseif ( ! empty( $data['body'] ) ) :

			$this->db->set( 'excerpt', word_limiter( trim( strip_tags( $data['body'] ) ) ), 50 );

		endif;

		//	Publish date
		if ( ! empty( $data['is_published'] ) && isset( $data['published'] ) ) :

			//	Published with date set
			$_published = strtotime( $data['published'] );
<commit_msg>No longer stripping tags from blog posts<commit_after><?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Name:		blog_post_model
 *
 * Description:	This model handles all interactions with blog posts on site.
 *
 **/

/**
 * OVERLOADING NAILS' MODELS
 *
 * Note the name of this class; done like this to allow apps to extend this class.
 * Read full explanation at the bottom of this file.
 *
 **/

class NAILS_Blog_post_model extends NAILS_Model
{

	protected $_reserved;


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


	public function __construct()
	{
		parent::__construct();

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

		$this->_table				= NAILS_DB_PREFIX . 'blog_post';
		$this->_table_prefix		= 'bp';	//	Hard-coded throughout model; take care when changing this
		$this->_table_label_column	= 'title';
		$this->_destructive_delete	= FALSE;

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

		//	Define reserved words (for slugs, basically just controller methods)
		$this->_reserved = array( 'index', 'single', 'category','tag', 'archive' );
	}


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


	/**
	 * Creates a new object
	 *
	 * @access public
	 * @param array $data The data to create the object with
	 * @return mixed
	 **/
	public function create( $data = array() )
	{
		//	Prepare slug
		$_counter = 0;

		if ( empty( $data['title'] ) ) :

			$this->_set_error( 'Title missing' );
			return FALSE;

		endif;

		if ( empty( $data['blog_id'] ) ) :

			$this->_set_error( 'Blog ID missing' );
			return FALSE;

		endif;

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

		//	Generate a slug
		$_prefix = array_search( $data['title'], $this->_reserved ) !== FALSE ? 'post-' : '';
		$this->db->set( 'slug', $this->_generate_slug( $data['title'], $_prefix ) );

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

		//	Set data
		$this->db->set( 'blog_id', $data['blog_id'] );
		$this->db->set( 'title', $data['title'] );

		if ( isset( $data['body'] ) ) :				$this->db->set( 'body',				$data['body'] );			endif;
		if ( isset( $data['seo_title'] ) ) :		$this->db->set( 'seo_title',		$data['title'] );			endif;
		if ( isset( $data['seo_description'] ) ) :	$this->db->set( 'seo_description',	$data['seo_description'] );	endif;
		if ( isset( $data['seo_keywords'] ) ) :		$this->db->set( 'seo_keywords',		$data['seo_keywords'] );	endif;
		if ( isset( $data['is_published'] ) ) :		$this->db->set( 'is_published',		$data['is_published'] );	endif;

		//	Safety first!
		if ( array_key_exists( 'image_id', $data ) ) :

			$_image_id = (int) $data['image_id'];
			$_image_id = ! $_image_id ? NULL : $_image_id;

			$this->db->set( 'image_id', $_image_id );

		endif;

		//	Excerpt
		if ( ! empty( $data['excerpt'] ) ) :

			$this->db->set( 'excerpt', trim( $data['excerpt'] ) );

		elseif ( ! empty( $data['body'] ) ) :

			$this->db->set( 'excerpt', word_limiter( trim( strip_tags( $data['body'] ) ) ), 50 );

		endif;

		//	Publish date
		if ( ! empty( $data['is_published'] ) && isset( $data['published'] ) ) :

			//	Published with date set
			$_published = strtotime( $data['published'] );

			if ( $_published ) :

				$_published = user_rdatetime( $data['published'] );

				$this->db->set( 'published', $_published );

			else :

				//	Failed, use NOW();
				$this->db->set( 'published', 'NOW()', FALSE );

			endif;

		else :

			//	No date set, use NOW()
			$this->db->set( 'published', 'NOW()', FALSE );

		endif;

		$this->db->set( 'created',			'NOW()', FALSE );
		$this->db->set( 'modified',			'NOW()', FALSE );
		$this->db->set( 'created_by',		active_user( 'id' ) );
		$this->db->set( 'modified_by',		active_user( 'id' ) );

		$this->db->insert( NAILS_DB_PREFIX . 'blog_post' );

		if ( $this->db->affected_rows() ) :

			$_id = $this->db->insert_id();

			//	Add Gallery items, if any
			if ( ! empty( $data['gallery'] ) ) :

				$_data = array();

				foreach ( $data['gallery'] AS $order => $image_id ) :

					if ( (int) $image_id ) :

						$_data[] = array( 'post_id' => $_id, 'image_id' => $image_id, 'order' => $order );

					endif;

				endforeach;

				if ( $_data ) :

					$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_image', $_data );

				endif;

			endif;

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

			//	Add Categories and tags, if any
			if ( ! empty( $data['categories'] ) ) :

				$_data = array();

				foreach ( $data['categories'] AS $cat_id ) :

					$_data[] = array( 'post_id' => $_id, 'category_id' => $cat_id );

				endforeach;

				$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_category', $_data );

			endif;

			if ( ! empty( $data['tags'] ) ) :

				$_data = array();

				foreach ( $data['tags'] AS $tag_id ) :

					$_data[] = array( 'post_id' => $_id, 'tag_id' => $tag_id );

				endforeach;

				$this->db->insert_batch( NAILS_DB_PREFIX . 'blog_post_tag', $_data );

			endif;

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

			//	Add associations, if any
			if ( ! empty( $data['associations'] ) ) :

				//	Fetch association config
				$_association = $this->config->item( 'blog_post_associations' );

				foreach ( $data['associations'] AS $index => $assoc ) :

					if ( ! isset( $_association[$index] ) ) :

						continue;

					endif;

					$_data = array();

					foreach( $assoc AS $id ) :

						$_data[] = array( 'post_id' => $_id, 'associated_id' => $id );

					endforeach;

					if ( $_data ) :

						$this->db->insert_batch( $_association[$index]->target, $_data );

					endif;

				endforeach;

			endif;

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

			return $_id;

		else :

			return FALSE;

		endif;
	}


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


	/**
	 * Updates an existing object
	 *
	 * @access public
	 * @param int $id The ID of the object to update
	 * @param array $data The data to update the object with
	 * @return bool
	 **/
	public function update( $id, $data = array() )
	{
		//	Prepare slug; the slug is regenrated if the blog post is in a transitioning state
		//	to published. We don't want to be changing slugs of published posts but while
		//	it's a draft we can do whatever (even if it was previously published, made a draft
		//	then republished).

		$this->db->select( 'is_published' );
		$this->db->where( 'id', $id );
		$_current = $this->db->get( NAILS_DB_PREFIX . 'blog_post' )->row();

		if ( ! $_current->is_published && $data['is_published'] ) :

			$_counter = 0;

			if ( ! isset( $data['title'] ) || ! $data['title'] ) :

				$this->_set_error( 'Title missing' );
				return FALSE;

			endif;

			//	Generate a slug
			$_prefix	= array_search( $data['title'], $this->_reserved ) !== FALSE ? 'post-' : '';
			$_slug		= $this->_generate_slug( $data['title'], $_prefix );

		else :

			$_slug = FALSE;

		endif;

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

		//	Set data
		if ( isset( $data['blog_id'] ) ) :			$this->db->set( 'blog_id',			$data['blog_id'] );			endif;
		if ( isset( $data['title'] ) ) :			$this->db->set( 'title',			$data['title'] );			endif;
		if ( isset( $data['body'] ) ) :				$this->db->set( 'body',				$data['body'] );			endif;
		if ( isset( $data['seo_title'] ) ) :		$this->db->set( 'seo_title',		$data['title'] );			endif;
		if ( isset( $data['seo_description'] ) ) :	$this->db->set( 'seo_description',	$data['seo_description'] );	endif;
		if ( isset( $data['seo_keywords'] ) ) :		$this->db->set( 'seo_keywords',		$data['seo_keywords'] );	endif;
		if ( isset( $data['is_published'] ) ) :		$this->db->set( 'is_published',		$data['is_published'] );	endif;
		if ( isset( $data['is_deleted'] ) ) :		$this->db->set( 'is_deleted',		$data['is_deleted'] );		endif;

		//	Safety first!
		if ( array_key_exists( 'image_id', $data ) ) :

			$_image_id = (int) $data['image_id'];
			$_image_id = ! $_image_id ? NULL : $_image_id;

			$this->db->set( 'image_id', $_image_id );

		endif;

		//	Excerpt
		if ( ! empty( $data['excerpt'] ) ) :

			$this->db->set( 'excerpt', trim( $data['excerpt'] ) );

		elseif ( ! empty( $data['body'] ) ) :

			$this->db->set( 'excerpt', word_limiter( trim( strip_tags( $data['body'] ) ) ), 50 );

		endif;

		//	Publish date
		if ( ! empty( $data['is_published'] ) && isset( $data['published'] ) ) :

			//	Published with date set
			$_published = strtotime( $data['published'] );

{'fraction_non_alphanumeric': 0.27600530779438065, 'fraction_numerical': 0.0006923210061731956, 'mean_word_length': 2.146487565801416, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 215, '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': '4016527', 'n_tokens_mistral': 6688, 'n_tokens_neox': 5710, 'n_words': 1328}