<?php
/**
 * @version        5.6.0
 * @package        Joomla
 * @subpackage     Joom Donation
 * @author         Tuan Pham Ngoc
* @copyright      Copyright (C) 2009 - 2023 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

class DonationModelField extends OSFModelAdmin
{

	public static $protectedFields = array('first_name', 'email');

	/**
	 * Store custom field
	 *
	 * @param OSFInput $input
	 * @param array    $ignore
	 *
	 * @return bool|void
	 */
	public function store($input, $ignore = array())
	{
		$row     = $this->getTable();
		$fieldId = $input->getInt('id', 0);
		if ($fieldId)
		{
			$row->load($fieldId);
		}
		if (in_array($row->name, self::$protectedFields))
		{
			$ignore = array('field_type', 'published', 'validation_rules', 'required');
		}
		parent::store($input, $ignore);
	}

	/**
	 *
	 * Publish, unpublish custom fields
	 *
	 * @param array $pks
	 * @param int   $value
	 */
	public function publish($pks, $value = 1)
	{
		if (count($pks))
		{
			$db    = $this->getDbo();
			$query = $db->getQuery(true);
			$query->select('id')
				->from('#__jd_fields')
				->where('name IN ("' . implode('","', self::$protectedFields) . '")');
			$db->setQuery($query);
			$protectedFieldIds = $db->loadColumn();
			$pks               = array_diff($pks, $protectedFieldIds);
			if (count($pks))
			{
				parent::publish($pks, $value);
			}
		}
	}

	/**
	 * Method to remove  fields
	 *
	 * @access    public
	 * @return    boolean    True on success
	 */
	public function delete($cid = array())
	{
		if (count($cid))
		{
			$db    = $this->getDbo();
			$query = $db->getQuery(true);
			$query->select('id')
				->from('#__jd_fields')
				->where('name IN ("' . implode('","', self::$protectedFields) . '")');
			$db->setQuery($query);
			$protectedFieldIds = $db->loadColumn();
			$cid               = array_diff($cid, $protectedFieldIds);
			if (count($cid))
			{
				$query->clear();
				$query->delete('#__jd_field_value')->where('field_id IN (' . implode(',', $cid) . ')');
				$db->setQuery($query);
				$db->execute();
				parent::delete($cid);
			}
		}
	}

	/**
	 * Change require status
	 *
	 * @param array $cid
	 * @param int   $state
	 *
	 * @return boolean
	 */
	public function required($cid, $state)
	{
		if (count($cid))
		{
			$db    = $this->getDbo();
			$query = $db->getQuery(true);
			$query->select('id')
				->from('#__jd_fields')
				->where('name IN ("' . implode('","', self::$protectedFields) . '")');
			$db->setQuery($query);
			$protectedFieldIds = $db->loadColumn();
			$cid               = array_diff($cid, $protectedFieldIds);
			if (count($cid))
			{
				$query->clear();
				$query->update('#__jd_fields')
					->set('required = ' . $state)
					->where('id IN (' . implode(',', $cid) . ' )');
				$db->setQuery($query);
				$db->execute();
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}