<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * VikRestaurants take-away menus attribute table.
 *
 * @since 1.8
 */
class VRETableTkattribute extends JTableVRE
{
	/**
	 * Class constructor.
	 *
	 * @param 	object 	$db  The database driver instance.
	 */
	public function __construct($db)
	{
		parent::__construct('#__vikrestaurants_takeaway_menus_attribute', 'id', $db);

		// register required fields
		$this->_requiredFields[] = 'name';
	}

	/**
	 * Method to bind an associative array or object to the Table 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 Table instance.
	 * @param   array|string  $ignore  An optional array or space separated list of properties to ignore while binding.
	 *
	 * @return  boolean  True on success.
	 */
	public function bind($src, $ignore = array())
	{
		$src = (array) $src;

		// fetch ordering for new attributes
		if ($src['id'] == 0)
		{
			$src['ordering'] = $this->getNextOrder();
		}

		// check if the font configuration attribute is an array
		if (isset($src['font']) && is_array($src['font']))
		{
			// JSON encode the font options
			$src['font'] = json_encode($src['font']);
		}

		// bind the details before save
		return parent::bind($src, $ignore);
	}

	/**
	 * Method to perform sanity checks on the Table instance properties to
	 * ensure they are safe to store in the database.
	 *
	 * @return  boolean  True if the instance is sane and able to be stored in the database.
	 * 
	 * @since   1.11
	 */
	public function check()
	{
		// check integrity using parent
		if (!parent::check())
		{
			return false;
		}

		// check icon type requirements
		if (isset($this->icontype))
		{
			if ($this->icontype === 'image')
			{
				if (!$this->icon)
				{
					// register error message
					$this->setError(JText::sprintf('VRE_INVALID_REQ_FIELD', JText::translate('VRMANAGEMEDIA4')));

					// missing image
					return false;
				}
			}
			else if ($this->icontype === 'fonticon')
			{
				if (!$this->font)
				{
					// register error message
					$this->setError(JText::sprintf('VRE_INVALID_REQ_FIELD', JText::translate('VRPAYMENTICONOPT1')));

					// missing font icon
					return false;
				}
			}
			else
			{
				// register error message
				$this->setError(JText::sprintf('VRE_INVALID_REQ_FIELD', JText::translate('VRMENUMEDIA')));

				// unsupported type
				return false;
			}
		}

		return true;
	}

	/**
	 * Helper method used to store the user data within the session.
	 *
	 * @param 	mixed 	$data  The array data to store.
	 *
	 * @return 	self    This object to support chaining.
	 * 
	 * @since   1.11
	 */
	public function setUserStateData($data = null)
	{
		if ($data)
		{
			$data = (array) $data;

			// make sure the font attribute is an object
			if (isset($data['font']) && !is_object($data['font']))
			{
				// decode from JSON or cast from ARRAY
				$data['font'] = is_string($data['font']) ? json_decode($data['font']) : (object) $data['font'];
			}
		}

		return parent::setUserStateData($data);
	}
}
