<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Revision_Model
 *
 * @package HostCMS
 * @subpackage Revision
 * @version 7.x
 * @copyright © 2005-2026, https://www.hostcms.ru
 */
class Revision_Model extends Core_Entity
{
	/**
	 * Backend property
	 * @var mixed
	 */
	public $name = NULL;

	/**
	 * Column consist item's name
	 * @var string
	 */
	protected $_nameColumn = 'entity_id';

	/**
	 * Belongs to relations
	 * @var array
	 */
	protected $_belongsTo = array(
		'user' => array()
	);

	/**
	 * Constructor.
	 * @param int $id entity ID
	 */
	public function __construct($id = NULL)
	{
		parent::__construct($id);

		if (is_null($id) && !$this->loaded())
		{
			$oUser = Core_Auth::getCurrentUser();
			$this->_preloadValues['user_id'] = is_null($oUser) ? 0 : $oUser->id;
		}
	}

	/**
	 * Get Model for revision
	 * @return Core_Entity
	 */
	public function getModel()
	{
		return Core_Entity::factory($this->model, $this->entity_id);
	}

	/**
	 * Rollback Revision
	 * @return self
	 */
	public function rollback()
	{
		$oModel = $this->getModel();
		$oModel->backupRevision();
		$oModel->rollbackRevision($this->id);
		return $this;
	}

	/**
	 * Print decoded JSON
	 * @param mixed $value json value
	 */
	protected function _printJson($value)
	{
		if (is_array($value))
		{
			foreach($value as $key => $tmp)
			{
				?><div class="row"><div class="col-xs-4 semi-bold"><?php
				echo htmlspecialchars($key) . ': ';
				?></div><div class="col-xs-8 small"><?php
				$this->_printJson($tmp);
				?></div></div><?php
			}
		}
		elseif (is_string($value) || is_numeric($value))
		{
			$iLen = strlen($value);
			echo '<span class="pre-wrap">' . ($iLen <= 102400 ? htmlspecialchars($value) : Core_Str::getTextSize($iLen)) . '</span>';
		}
		elseif (is_object($value))
		{
			echo $value;
		}
	}

	/**
	 * Backend callback method
	 * @param Admin_Form_Field_Model $oAdmin_Form_Field
	 * @param Admin_Form_Controller $oAdmin_Form_Controller
	 * @return string
	 */
	public function nameBackend($oAdmin_Form_Field, $oAdmin_Form_Controller)
	{
		$this->printValue();

		return '<a id="revision' . $this->id . '" href="javascript:void(0);">' . htmlspecialchars($this->name) . '</a>';
	}

	/**
	 * Print revision value
	 * @return self
	 */
	public function printValue()
	{
		$oRevision = Core_Entity::factory('Revision', $this->id);

		$aValue = json_decode($oRevision->value, TRUE);

		?><div id="revision<?php echo $this->id?>" class="hidden"><div class="revision-details"><?php
		$this->_printJson($aValue);
		?></div></div>
		<script>
		$(function() {
			$('a#revision<?php echo $this->id?>').on('click', function (){
				var dialog = bootbox.dialog({
					title: $.escapeHtml('<?php echo Core_Str::escapeJavascriptVariable($this->name)?> <?php echo Core_Date::sql2datetime($this->datetime)?>'),
					message: $('#revision<?php echo $this->id?>').html(),
					backdrop: true,
					size: 'large'
				});
				dialog.modal('show');
			});
		});
		</script>
		<?php

		return $this;
	}

	/**
	 * Backend callback method
	 * @param Admin_Form_Field_Model $oAdmin_Form_Field
	 * @param Admin_Form_Controller $oAdmin_Form_Controller
	 * @return string
	 */
	public function sizeBackend($oAdmin_Form_Field, $oAdmin_Form_Controller)
	{
		$oRevision = Core_Entity::factory('Revision', $this->id);

		$size = strlen($oRevision->value);

		if ($size)
		{
			return Core_Str::getTextSize($size);
		}
	}

	/**
	 * Get entity description
	 * @return string
	 */
	public function getTrashDescription()
	{
		return htmlspecialchars(
			Core_Str::cut($this->model, 255)
		);
	}

	/**
	 * Get Related Site
	 * @return Site_Model|NULL
	 * @hostcms-event advertisement_group_list.onBeforeGetRelatedSite
	 * @hostcms-event advertisement_group_list.onAfterGetRelatedSite
	 */
	public function getRelatedSite()
	{
		Core_Event::notify($this->_modelName . '.onBeforeGetRelatedSite', $this);

		$oSite = isset($this->entity_id)
			? $this->getModel()->getRelatedSite()
			: NULL; // List of revisions doesn't have entity_id

		Core_Event::notify($this->_modelName . '.onAfterGetRelatedSite', $this, array($oSite));

		return $oSite;
	}
}