<?php
namespace Entity;

/**
 * The class is instantiated and the method validate() is executed in the Entity->check() method.
 *
 */
class Validator_Country extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{

	protected function isAccessible_insert()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-insert')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

	/**
	 * Validates the fields for inserting
	 */
	protected function check_insert()
	{
		// configs
		$cfg = $this->entity->getCFG();

		// get current fields details
		$fields = $this->entity->getFields();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THAT CODE (primary key) IS UNIQUE ------------------------------------------------
		if ($fields['code']) {
			$country = \Entity\Country::selectForUpdate($fields['code']);
			if ($country->recordExists() && !$country->deleted) {
				$currMessages->addWarningCode('insert_codeNotUnique', 'code', $country->name);
			}

		}

		// CHECK THAT NAME (standard) IS UNIQUE ---------------------------------------------------
		if ($fields['nameStandard']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('insert_nameNotUnique', 'name');
			}
		}
		// CHECK THAT NAME (local) IS UNIQUE ------------------------------------------------------
		if ($fields['nameLocal']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('insert_nameNotUnique', 'name');
			}
		}

		// CHECK THAT CODE3 IS UNIQUE -------------------------------------------------------------
		if ($cfg['code3']['unique'] and $fields['code3']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setColumns(['country.name']);
			$countryObj->setFilter('country.code3', 'LIKE', $fields['code3']);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('insert_code3NotUnique', 'code3', $countryArray[0]['name']);
			}

		}

	}

	protected function isAccessible_update()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-update')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

	protected function isAvailable_update()
	{
		// current entity id
		$currentIdEntity = $this->entity->getId();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THAT THE CURRENT COUNTRY IS NOT ALREADY DELETED ----------------------------------
		$country = Country::selectForUpdate($currentIdEntity);
		if (!$country->recordExists()) {
			$currMessages->addErrorCode('update_cntrDetailsBadId');
		}
		elseif ($country->deleted) {
			$currMessages->addWarningCode('update_cntrDeleted');
		}
	}

	/**
	 * Validates the fields for updating
	 */
	protected function check_update()
	{
		// configs
		$cfg = $this->entity->getCFG();

		// get current fields details
		$fields = $this->entity->getFields();

		// current entity id
		$currentIdEntity = $this->entity->getId();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THAT NAME (standard) IS UNIQUE ---------------------------------------------------
		if ($fields['nameStandard']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setFilter('country.code', '!=', $currentIdEntity);
			$countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('update_nameNotUnique', 'name');
			}
		}

		// CHECK THAT NAME (local) IS UNIQUE ------------------------------------------------------
		if ($fields['nameLocal']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setFilter('country.code', '!=', $currentIdEntity);
			$countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('update_nameNotUnique', 'name');
			}
		}

		// CHECK THAT CODE3 IS UNIQUE -------------------------------------------------------------
		if ($cfg['code3']['unique'] and $fields['code3']) {
			$countryObj = new \App\EntityList\Countries();
			$countryObj->setColumns(['country.name']);
			$countryObj->setFilter('country.code', '!=', $currentIdEntity);
			$countryObj->setFilter('country.code3', 'LIKE', $fields['code3']);
			$countryObj->setLimit(1);
			$countryArray = $countryObj->exportRecords();

			if ($countryArray) {
				$currMessages->addWarningCode('insert_code3NotUnique', 'code3', $countryArray[0]['name']);
			}

		}

	}

	protected function isAccessible_delete()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-delete')) {
			$this->entity->getMessages()->addWarningCode('noAccessToOption');
		}
	}

	/**
	 * Validates the fields for deleting
	 */
	protected function isAvailable_delete()
	{

		// current entity id
		$currentIdEntity = $this->entity->getId();

		// current collected messages
		$currMessages = $this->entity->getMessages();

		// CHECK THAT THE CURRENT COUNTRY IS NOT ALREADY DELETED ----------------------------------
		$country = Country::selectForUpdate($currentIdEntity);
		if (!$country->recordExists()) {
			$currMessages->addErrorCode('delete_cntrDetailsBadId');
		}
		elseif ($country->deleted) {
			$currMessages->addWarningCode('delete_cntrDeleted');
		}

		// CHECK THAT NO CITIES ARE ASSOCIATED TO THIS COUNTRY ------------------------------------

		$citiesList=new \App\EntityList\Cities();
		$citiesList->setFilter('city.countryCode', '=', $currentIdEntity);
		$citiesList->setLimit(1);

		// check if any cities on this country
		if ($citiesList->exportRecords()) {
			$currMessages->addWarningCode('delete_cntrHasCities');
		}

	}

}
