<?php
/**
* @package RSForm!Pro
* @copyright (C) 2020 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// no direct access
defined('_JEXEC') or die;

use Joomla\CMS\Table\Table;

class TableRSForm_Cleantalk extends Table
{
	public $form_id 	= null;
	public $merge_vars 	= '';
	public $published 	= 0;
	
	public function __construct(& $db)
	{
		parent::__construct('#__rsform_cleantalk', 'form_id', $db);
	}

	// Validate data before save
	public function check()
	{
		if (is_array($this->merge_vars))
		{
			$this->merge_vars = json_encode($this->merge_vars);
		}

		return true;
	}

	public function load($keys = null, $reset = true)
	{
		$result = parent::load($keys, $reset);

		if ($result)
		{
			$this->merge_vars = json_decode($this->merge_vars, true);

			if (!is_array($this->merge_vars))
			{
				$this->merge_vars = array();
			}
		}

		return $result;
	}

	public function hasPrimaryKey()
	{
		$db 	= $this->getDbo();
		$key 	= $this->getKeyName();
		$table	= $this->getTableName();

		$query = $db->getQuery(true)
			->select($db->qn($key))
			->from($db->qn($table))
			->where($db->qn($key) . ' = ' . $db->q($this->{$key}));

		return $db->setQuery($query)->loadResult() !== null;
	}
}