<?php
namespace Entity;

class Validator_AgencyCreditCard extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{
	/**
	 * Check if the user access to this option
	 */
	protected function isAccessible_insert()
	{
		// current collected messages
		$currentMessages = $this->entity->getMessages();

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

	protected function check_insert()
	{
		$fields = $this->entity->getFields();

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

		// check name uniqueness
		if ($fields['name']) {
			$agencyCC = new \App\EntityList\AgencyCreditCards();
			$agencyCC->setCustomFilter('agencyCreditCards.name = ?', [$fields['name']]);
			$agencyCC->setLimit(1);
			$agencyCCArray = $agencyCC->exportRecords();

			if ($agencyCCArray) {
				$currentMessages->addWarningCode('insert_notUniqueName');
			}
		}

	}

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

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

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

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

		$agencyCC = \Entity\AgencyCreditCard::selectForUpdate($currentEntityId);

		if (!$agencyCC->recordExists()) {
			$currentMessages->addErrorCode("update_invalidId");
		}
		elseif ($agencyCC->deleted) {
			$currentMessages->addWarningCode("update_deletedId");
		}
	}

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

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

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

		if ($fields['name']) {
			$agencyCC = new \App\EntityList\AgencyCreditCards();
			$agencyCC->setFilter('agencyCreditCards.id', '!=', $currentEntityId);
			$agencyCC->setCustomFilter('agencyCreditCards.name = ?', [$fields['name']]);
			$agencyCC->setLimit(1);
			$agencyCCArray = $agencyCC->exportRecords();

			if ($agencyCCArray) {
				$currentMessages->addWarningCode('update_notUniqueName');
			}
		}

	}

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

		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/AgencyCreditCards-delete')) {
			$currentMessages->addWarningCode('noAccessToOption');
		}
	}

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

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

		$agencyCC = \Entity\AgencyCreditCard::selectForUpdate($currentEntityId);

		if (!$agencyCC->recordExists()) {
			$currentMessages->addErrorCode("delete_invalidId");
		}
		elseif ($agencyCC->deleted) {
			$currentMessages->addWarningCode("delete_deletedId");
		}
	}
}