<commit_before><?php
/**
 * Model for content_stream plugin.
 *
 * PHP versions 5
 *
 * @package       jodel
 * @subpackage    jodel.content_stream.models
 */

/**
 * CsItem.
 *
 * Model that contains some methods for deal with finds and saves.
 *
 * @package       jodel
 * @subpackage    jodel.content_stream.models
 */
class CsItem extends ContentStreamAppModel
{
/**
 * Class name
 * 
 * @var string
 * @access public
 */
	var $name = 'CsItem';

/**
 * belongsTo relationship
 * 
 * @var array
 * @access public
 */
	var $belongsTo = array('ContentStream.CsContentStream');

/**
 * Attached behaviors
 * 
 * @var array
 * @access public
 */
	var $actsAs = array(
		'Containable',
		'JjUtils.Ordered' => array(
			'field' => 'order',
			'foreign_key' => 'cs_content_stream_id'
		)
	);

/**
 * afterFind callback.
 * 
 * Used to retreive all content from CsItems
 * 
 * @access public
 * @param array $results
 * @param boolean $primary
 * @return array The complete results
 */
	function afterFind($results, $primary)
	{
		if (isset($results[0]))
			foreach ($results as &$result)
				$result['CsItem'] += $this->getContent($result['CsItem']);
		
		return $results;
	}

/**
 * Method called by the BuroBurocrataController
 * 
 * This method receive the data from the ContentStream input and creates/updates
 * an CsItem.
 * 
 * @access public
 * @param array $data
 * @return boolean True if succefully saved. False otherwise.
 */
	function saveBurocrata($data)
	{
		$contentType = false;
		
		// If CsItem is already created
		if (!empty($data['CsItem']['id']))
		{
			$this->id = $data['CsItem']['id'];
			$contentType = $this->field('type');
		}
		// Else, need to create one item
		else if (isset($data[$this->alias]['type']))
		{
			$contentType = $data[$this->alias]['type'];
		}
		
		if (!$contentType)
			return false;
		
		// Start the transaction
		$dbo = $this->getDataSource();
		$dbo->begin($this);
		
		// Loads the config
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		
		$stream_config = $config['streams'][$contentType];
		$Model = ClassRegistry::init($stream_config['model']);
		
		$Model->create($data);
		if ($Model->save())
		{
			$data[$this->alias]['foreign_key'] = $Model->id;
			$this->create($data);
			if ($this->save())
			{
				$dbo->commit($this);
				return true;
			}
		}
		
		$dbo->rollback($this);
		return false;
	}

/**
 * Logic of duplicating an CsItem (and its content)
 * 
 * @access public
 * @param string $item_id
 * @return boolean True if succefully duplicated. False otherwise.
 */
	function duplicate($item_id)
	{
		$this->contain();
		$item = $this->findById($item_id);
		
		if (empty($item))
			return false;
		
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		$Model = ClassRegistry::init($config['streams'][$item[$this->alias]['type']]['model']);
		
		
		$item[$Model->alias] = $item[$this->alias][$Model->alias];
		unset($item[$this->alias][$Model->alias]);
		if (method_exists($Model, 'duplicate'))
		{
			if (!$Model->duplicate($item[$Model->alias][$Model->primaryKey]))
				return false;
			
			$item[$Model->alias][$Model->primaryKey] = $Model->id;
		}
		else
		{
			unset($item[$Model->alias][$Model->primaryKey]);
		}
		unset($item[$this->alias][$this->primaryKey]);
		unset($item[$this->alias]['foreign_key']);
		
		return $this->saveBurocrata($item);
	}

/**
 * Method that completes the content of an CsItem record.
 * 
 * @access public
 * @param array The CsItem data
 * @return array
 */
	function getContent($item)
	{
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		
		if (!isset($item['type']))
			return array();
		
		if (!isset($config['streams'][$item['type']])) {
			trigger_error('CsItem::getContent() - Found a item of an unknown type `'.$item['type'].'`.');
			return $item;
		}
		
		$stream_config = $config['streams'][$item['type']];
		$Model = ClassRegistry::init($stream_config['model']);
		$item['__title'] = $stream_config['title'];
		
		if (!$Model) {
			trigger_error('CsItem::getContent() - Model `'.$stream_config['model'].'` couldn be initialized.');
			return $item;<commit_msg>Fixed bugs on saving an CsItem.<commit_after><?php
/**
 * Model for content_stream plugin.
 *
 * PHP versions 5
 *
 * @package       jodel
 * @subpackage    jodel.content_stream.models
 */

/**
 * CsItem.
 *
 * Model that contains some methods for deal with finds and saves.
 *
 * @package       jodel
 * @subpackage    jodel.content_stream.models
 */
class CsItem extends ContentStreamAppModel
{
/**
 * Class name
 * 
 * @var string
 * @access public
 */
	var $name = 'CsItem';

/**
 * belongsTo relationship
 * 
 * @var array
 * @access public
 */
	var $belongsTo = array('ContentStream.CsContentStream');

/**
 * Attached behaviors
 * 
 * @var array
 * @access public
 */
	var $actsAs = array(
		'Containable',
		'JjUtils.Ordered' => array(
			'field' => 'order',
			'foreign_key' => 'cs_content_stream_id'
		)
	);

/**
 * afterFind callback.
 * 
 * Used to retreive all content from CsItems
 * 
 * @access public
 * @param array $results
 * @param boolean $primary
 * @return array The complete results
 */
	function afterFind($results, $primary)
	{
		if (isset($results[0]))
			foreach ($results as &$result)
				$result['CsItem'] += $this->getContent($result['CsItem']);
		
		return $results;
	}

/**
 * Method called by the BuroBurocrataController
 * 
 * This method receive the data from the ContentStream input and creates/updates
 * an CsItem.
 * 
 * @access public
 * @param array $data
 * @return boolean True if succefully saved. False otherwise.
 */
	function saveBurocrata($data)
	{
		$contentType = false;
		
		// If CsItem is already created, get its type
		if (!empty($data['CsItem']['id']))
		{
			$this->id = $data['CsItem']['id'];
			$contentType = $this->field('type');
		}
		// Else, need to get the type from the passed data
		else if (!empty($data[$this->alias]['type']))
		{
			$contentType = $data[$this->alias]['type'];
		}
		
		if (!$contentType)
			return false;
		
		$data[$this->alias]['type'] = $contentType;
		
		// Start the transaction
		$dbo = $this->getDataSource();
		$dbo->begin($this);
		
		// Loads the config
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		
		$stream_config = $config['streams'][$contentType];
		$Model = ClassRegistry::init($stream_config['model']);
		
		$Model->create($data);
		if ($Model->save())
		{
			$data[$this->alias]['foreign_key'] = $Model->id;
			$this->create($data);
			if ($this->save())
			{
				$dbo->commit($this);
				return true;
			}
		}
		
		$dbo->rollback($this);
		return false;
	}

/**
 * Logic of duplicating an CsItem (and its content)
 * 
 * @access public
 * @param string $item_id
 * @return boolean True if succefully duplicated. False otherwise.
 */
	function duplicate($item_id)
	{
		$this->contain();
		$item = $this->findById($item_id);
		
		if (empty($item))
			return false;
		
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		$Model = ClassRegistry::init($config['streams'][$item[$this->alias]['type']]['model']);
		
		
		$item[$Model->alias] = $item[$this->alias][$Model->alias];
		unset($item[$this->alias][$Model->alias]);
		if (method_exists($Model, 'duplicate'))
		{
			if (!$Model->duplicate($item[$Model->alias][$Model->primaryKey]))
				return false;
			
			$item[$Model->alias][$Model->primaryKey] = $Model->id;
		}
		else
		{
			unset($item[$Model->alias][$Model->primaryKey]);
		}
		unset($item[$this->alias][$this->primaryKey]);
		unset($item[$this->alias]['foreign_key']);
		
		return $this->saveBurocrata($item);
	}

/**
 * Method that completes the content of an CsItem record.
 * 
 * @access public
 * @param array The CsItem data
 * @return array
 */
	function getContent($item)
	{
		App::import('Lib', 'ContentStream.CsConfigurator');
		$config = CsConfigurator::getConfig();
		
		if (empty($item['type']) || empty($item['foreign_key']))
			return array();
		
		if (!isset($config['streams'][$item['type']])) {
			trigger_error('CsItem::getContent() - Found a item of an unknown type `'.$item['type'].'`.');
			return $item;
		}
		
		$stream_config = $config['streams'][$item['type']];
		$Model = ClassRegistry::init($stream_config['model']);
		$item['__title'] = $stream_config['title'];
		
		if (!$Model) {
			trigger_error('CsItem::getContent() - Model `'.$stream_config['model'].'` couldn be initialized.');
			return $item;
{'fraction_non_alphanumeric': 0.1856441430909516, 'fraction_numerical': 0.00046761748889408465, 'mean_word_length': 3.8580352072685975, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 80, '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': '412265', 'n_tokens_mistral': 3190, 'n_tokens_neox': 2784, 'n_words': 622}