<?php
namespace Entity;

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

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

	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 COUNTRY CODE ---------------------------------------------------------------------
		if ($fields['countryCode']) {
			$countryEnt = \Entity\Country::select($fields['countryCode']);
			if (!$countryEnt->recordExists() or $countryEnt->deleted) {
				$currMessages->addWarningCode('countryNotValid', 'countryCode');
			}
		}


		// CHECK THAT (NAME AND COUNTRY CODE) IS UNIQUE -------------------------------------------
		// check nameStandard
		if ($fields['nameStandard'] and $fields['countryCode']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setCustomFilter('city.name = ? OR city.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]);
			$cityObj->setFilter('city.countryCode', 'LIKE', $fields['countryCode']);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('cityNameNotUnique', 'name');
			}

		}

		// check nameLocal
		if ($fields['nameLocal'] and $fields['countryCode']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setCustomFilter('city.name = ? OR city.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]);
			$cityObj->setFilter('city.countryCode', 'LIKE', $fields['countryCode']);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('cityNameNotUnique', 'name');
			}
		}

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

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

		}

		// CHECK THAT CODE LOCAL IS UNIQUE -------------------------------------------------------------
		if ($fields['codeLocal'] and $cfg['codeLocal']['unique']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setColumns(['city.name']);
			$cityObj->setFilter('city.codeLocal', 'LIKE', $fields['codeLocal']);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('code3CyrNotUnique', 'codeLocal', $cityArray[0]['name']);
			}
		}

	}


	protected function isAccessible_update()
	{
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Cities-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 city id
		$city = \Entity\City::selectForUpdate($currentIdEntity);
		if (!$city->recordExists()) {
			$currMessages->addErrorCode('update_invalidId');
		}
		elseif ($city->deleted) {
			$currMessages->addWarningCode('update_cityDeleted');
		}

	}

	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();

		$city = \Entity\City::select($currentIdEntity);

		// CHECK THAT (NAME AND COUNTRY CODE) IS UNIQUE -------------------------------------------
		// check nameStandard
		if ($fields['nameStandard']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setFilter('city.id', '!=', $currentIdEntity);
			$cityObj->setCustomFilter('city.name = ? OR city.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]);
			$cityObj->setFilter('city.countryCode', 'LIKE', $city->countryCode);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('cityNameNotUnique', 'name');
			}
		}

		// check nameLocal
		if ($fields['nameLocal']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setFilter('city.id', '!=', $currentIdEntity);
			$cityObj->setCustomFilter('city.name = ? OR city.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]);
			$cityObj->setFilter('city.countryCode', 'LIKE', $city->countryCode);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('cityNameNotUnique', 'name');
			}
		}

		// CHECK THAT CODE3 IS UNIQUE -------------------------------------------------------------
		if ($fields['code3'] and $cfg['code3']['unique']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setColumns(['city.name']);
			$cityObj->setFilter('city.id', '!=', $currentIdEntity);
			$cityObj->setFilter('city.code3', 'LIKE', $fields['code3']);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

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

		// CHECK THAT CODE3CYR IS UNIQUE -------------------------------------------------------------
		if ($fields['codeLocal'] and $cfg['codeLocal']['unique']) {
			$cityObj = new \App\EntityList\Cities();
			$cityObj->setColumns(['city.name']);
			$cityObj->setFilter('city.id', '!=', $currentIdEntity);
			$cityObj->setFilter('city.codeLocal', 'LIKE', $fields['codeLocal']);
			$cityObj->setLimit(1);
			$cityArray = $cityObj->exportRecords();

			if ($cityArray) {
				$currMessages->addWarningCode('code3CyrNotUnique', 'codeLocal', $cityArray[0]['name']);
			}
		}

	}

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

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

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

		$city = City::selectForUpdate($currentIdEntity);

		// check city id
		if (!$city->recordExists()) {
			$currMessages->addErrorCode('delete_invalidId');
		}
		elseif ($city->deleted) {
			$currMessages->addWarningCode('delete_cityDeleted');
		}

	}

}
