<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
defined('_JEXEC') or die;

use com_eorisis_pixels\framework\lib\core\app\config as app_config;
use com_eorisis_pixels\framework\lib\core\cms\application;
use com_eorisis_pixels\framework\lib\core\cms\access;
use com_eorisis_pixels\framework\lib\core\model\section;
use com_eorisis_pixels\framework\lib\core\model\params;

if (!class_exists('EorisisTable', false))
{
	if (version_compare(JVERSION, '3.8', '>='))
	{
		JLoader::registerAlias('EorisisTable', '\\Joomla\\CMS\\Table\\Table');
	}
	else
	{
		JLoader::registerAlias('EorisisTable', 'JTable');
	}
}

// Preferences
class Eorisis_pixelsTablePref extends EorisisTable
{
	const VIEW_NAME = 'pref';

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

	public function __construct($db)
	{
		parent::__construct('#__'.app_config::TABLE_SECTIONS, 'id', $db);
	}

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

	protected function _getAssetName()
	{
		// Method to compute the default name of the asset.
		// The default name is in the form table_name.id
		// where id is the value of the primary key of the table.
		// @return  string
		// @deprecated  3.1.4 Class will be removed upon completion of transition to UCM

		$id = (int)params::get($this->_tbl_key, $this);
		if (!$id)
		{
			$id = section::id(self::VIEW_NAME);
		}

		return app_config::COMPONENT_ELEMENT.'.'.self::VIEW_NAME.'.'.$id;
	}

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

	protected function _getAssetTitle()
	{
		// Method to return the title to use for the asset table.
		// @return  string
		// @deprecated  3.1.4 Class will be removed upon completion of transition to UCM

		if (empty($this->title))
		{
			$this->title = 'Preferences';
		}

		return $this->title;
	}

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

	protected function _getAssetParentId(EorisisTable $table = null, $id = null)
	{
		// Method to get the parent asset id for the record
		// @param   Table    $table  A Table object (optional) for the asset parent
		// @param   integer  $id     The id (optional) of the content.
		// @return  integer

		if (version_compare(JVERSION, '3.8', '>='))
		{
			$class_prefix = '\\Joomla\\CMS\\Table\\';
		}
		else
		{
			$class_prefix = 'JTable';
		}

		// For simple cases, parent to the asset root.
		/** @var Asset $assets */
		$assets = self::getInstance('Asset', $class_prefix, ['dbo' => $this->getDbo()]);
		$root_id = $assets->getRootId();
		return empty($root_id) ? 1 : $root_id;
	}

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

	public function bind($src, $ignore = [])
	{
		// Method to bind an associative array or object to the JTable instance.This
		// method only binds properties that are publicly accessible and optionally
		// takes an array of properties to ignore when binding.
		// @param   array|object  $src     An associative array or object to bind to the JTable instance.
		// @param   array|string  $ignore  An optional array or space separated list of properties
		//										to ignore while binding.
		// @return  boolean  True on success.
		// @throws  InvalidArgumentException

		// Bind the rules
		if (isset($src['rules']) and is_array($src['rules']))
		{
			$rules = access::new_rules($src['rules']);
			$this->setRules($rules);
		}

		if (!$this->getRules())
		{
			$rules = access::asset_id($this->_getAssetName());
			$this->setRules($rules);
		}

		// Bind the params
		if (isset($src['params']) and is_array($src['params']))
		{
			$registry = application::new_registry();
			$registry->loadArray($src['params']);
			$src['params'] = (string)$registry;
		}

		return parent::bind($src, $ignore);
	}

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

	public function check()
	{
		// Overloaded check method to ensure data integrity
		// @return  boolean  True on success
		// @throws  UnexpectedValueException

		if (empty($this->params))
		{
			$this->params = '{}';
		}

		if (!$this->getRules())
		{
			$rules = access::asset_id($this->_getAssetName());
			$this->setRules($rules);
		}

		return true;
	}

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

	public function store($updateNulls = false)
	{
		// Overriden EorisisTable::store to set modified data
		// @param   boolean  $updateNulls  True to update fields even if they are null
		// @return  boolean  True on success
		return parent::store($updateNulls);
	}
}
