<?php
namespace Entity;

class Validator_ExtResSystem extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{

	protected function isAccessible_insert()
	{
		// current collected messages
		$currMessages=$this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedOrders/SystemManagement-insert')) {
			$currMessages->addWarningCode('noAccessToOption');
		}
	}

	protected function check_insert()
	{

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

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

		// CHECK THAT THE NAME IS UNIQUE ----------------------------------------------------------
		if ($fields['name']) {
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setFilter('extResSys.name', 'LIKE', $fields['name']);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('nameNotUnique', 'name');
			}
		}

		// CHECK THAT THE CODE IS UNIQUE ----------------------------------------------------------
		if ($fields['code']) {
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setFilter('extResSys.code', 'LIKE', $fields['code']);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('codeNotUnique', 'code');
			}

		}

		// CHECK THE USERNAME AND TOKEN ---------------------------------------------------------
		if (($fields['username'] && !$fields['token']) || (!$fields['username'] && $fields['token'])) {
			$currMessages->addWarningCode('usernameAndTokenInvalid', 'username');
		}

		// check token
		if ($fields['token'] && preg_match('/%_/', $fields['token'])) {
			$currMessages->addWarningCode("invalidToken", 'token');
		}

		if ($fields['username']) {
			// check uniqueness
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setColumns(['name', 'code']);
			$externalSystemObject->setFilter('username', '=', $fields['username']);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('usernameNotUnique', 'username', [$externalSystemArray[0]['name'], $externalSystemArray[0]['code']]);
			}
		}

	}

	/**
	 * Check if the user has access to this option.
	 */
	protected function isAccessible_update()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedOrders/SystemManagement-update')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

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

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

		$system = \Entity\ExtResSystem::selectForUpdate($currentIdEntity);

		if (!$system->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		}
		elseif ($system->deleted) {
			$currMessages->addWarningCode('delete_sysDeleted');
		}

	}

	protected function check_update()
	{
		// get current fields details
		$fields = $this->entity->getFields();

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

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

		// CHECK THAT THE NAME IS UNIQUE ----------------------------------------------------------
		if ($fields['name']) {
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setFilter('extResSys.id', '!=', $currentIdEntity);
			$externalSystemObject->setFilter('extResSys.name', 'LIKE', $fields['name']);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('nameNotUnique', 'name');
			}
		}

		// CHECK THAT THE CODE IS UNIQUE ----------------------------------------------------------
		if ($fields['code']) {
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setFilter('extResSys.id', '!=', $currentIdEntity);
			$externalSystemObject->setFilter('extResSys.code', 'LIKE', $fields['code']);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('codeNotUnique', 'code');
			}
		}

		if (!array_key_exists('token', $fields)) {
			$token = \Entity\ExtResSystem::selectForUpdate($currentIdEntity)->token;
		}
		else {
			$token = $fields['token'];
		}

		// CHECK USERNAME AND TOKEN -------------------------------------------------------------
		if ((array_key_exists('username', $fields) && !$fields['username'] && $token) || ($fields['username'] && !$token)) {
			$currMessages->addWarningCode('usernameAndTokenInvalid', 'username');
		}

		if ($token && preg_match("/^[^_%&]+$/", htmlUnclean($token)) == 0) {
			$currMessages->addWarningCode("invalidToken", 'token');
		}

		if ($fields['username']) {
			// check uniqueness
			$externalSystemObject = new \App\EntityList\ImportedOrdersSystems();
			$externalSystemObject->setColumns(['name', 'code']);
			$externalSystemObject->setFilter('username', '=', $fields['username']);
			$externalSystemObject->setFilter('extResSys.id', '!=', $currentIdEntity);
			$externalSystemObject->setLimit(1);
			$externalSystemArray = $externalSystemObject->exportRecords();

			if ($externalSystemArray) {
				$currMessages->addWarningCode('usernameNotUnique', 'username', [$externalSystemArray[0]['name'], $externalSystemArray[0]['code']]);
			}
		}

	}

	/**
	 * Check if the user has access to this option.
	 */
	protected function isAccessible_delete()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Config/ImportedOrders/SystemManagement-delete')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

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

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

		$system = \Entity\ExtResSystem::selectForUpdate($currentIdEntity);

		if (!$system->recordExists()) {
			$currMessages->addErrorCode('invalidId');
		}
		elseif ($system->deleted) {
			$currMessages->addWarningCode('delete_sysDeleted');
		}

	}

}
