<?php
/**
 * @package     VikRentItems
 * @subpackage  com_vikrentitems
 * @author      Alessio Gaggii - e4j - Extensionsforjoomla.com
 * @copyright   Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 * @link        https://vikwp.com
 */

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

/**
 * Class handler for conditional rule "items".
 * 
 * @since 	1.4.0
 */
class VikRentItemsConditionalRuleItems extends VikRentItemsConditionalRule
{
	/**
	 * Class constructor will define the rule name, description and identifier.
	 */
	public function __construct()
	{
		// call parent constructor
		parent::__construct();

		$this->ruleName = JText::translate('VRIPVIEWORDERSTHREE');
		$this->ruleDescr = JText::translate('VRI_CONDTEXT_RULE_ROOMS_DESCR');
		$this->ruleId = basename(__FILE__);
	}

	/**
	 * Displays the rule parameters.
	 * 
	 * @return 	void
	 */
	public function renderParams()
	{
		$this->vri_app->loadSelect2();
		$items = $this->loadItems();
		$current_items = $this->getParam('items', array());
		$current_items = !is_array($current_items) ? array() : $current_items;

		?>
		<div class="vri-param-container">
			<div class="vri-param-label"><?php echo JText::translate('VRIITEMSASSIGNED'); ?></div>
			<div class="vri-param-setting">
				<select name="<?php echo $this->inputName('items', true); ?>" id="<?php echo $this->inputID('items'); ?>" multiple="multiple">
				<?php
				foreach ($items as $item) {
					?>
					<option value="<?php echo $item['id']; ?>"<?php echo in_array($item['id'], $current_items) ? ' selected="selected"' : ''; ?>><?php echo $item['name']; ?></option>
					<?php
				}
				?>
				</select>
			</div>
		</div>
		
		<script type="text/javascript">
			jQuery(function() {
				jQuery('#<?php echo $this->inputID('items'); ?>').select2();
			});
		</script>
		<?php
	}

	/**
	 * Tells whether the rule is compliant.
	 * 
	 * @return 	bool 	True on success, false otherwise.
	 */
	public function isCompliant()
	{
		$items_booked = $this->getProperty('items', array());
						
		if (!is_array($items_booked) || !count($items_booked)) {
			return false;
		}

		$allowed_items = $this->getParam('items', array());
		
		$one_found = false;
		foreach ($items_booked as $ibs) {
			foreach($ibs as $ib){
				if (!isset($ib['info']['id'])) {
					continue;
				}
				if (in_array($ib['info']['id'], $allowed_items)) {
					$one_found = true;
					break;
				}
			}
		}

		// return true if at least one room booked is in the parameters
		return $one_found;
	}

	/**
	 * Internal function for this rule only.
	 * 
	 * @return 	array
	 */
	protected function loadItems()
	{
		$dbo = JFactory::getDbo();
		$q = "SELECT `id`, `name` FROM `#__vikrentitems_items` ORDER BY `name` ASC;";
		$dbo->setQuery($q);

		return $dbo->loadAssocList();
	}

	/**
	 * Internal function for this rule only.
	 * 
	 * @param 	int 	$i 			the room unit index to get.
	 * @param 	array 	$features 	the list of room features.
	 * 
	 * @return 	array
	 */
	protected function getFirstFeature($i, $features = [])
	{
		if (!is_array($features) || !isset($features[$i])) {
			return $i;
		}

		foreach ($features[$i] as $fkey => $fval) {
			if (!empty($fkey) && !empty($fval)) {
				return "#$i - " . JText::translate($fkey) . ': ' . $fval;
			}
		}

		return "#$i";
	}
}
