<?php

// TODO: use entity list

namespace Entity;

class Validator_Trust 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/Partners/Trusts-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 NAME IS UNIQUE --------------------------------------------------------------
		if ($fields['name']) {
			$trustObj = new \App\EntityList\Trusts();
			$trustObj->setFilter('trusts.name', 'LIKE', $fields['name']);
			$trustObj->setFilter('trusts.type', 'LIKE', $fields['type']);
			$trustObj->setLimit(1);
			$trustArray = $trustObj->exportRecords();

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

	}

	protected function isAccessible_update()
	{
		// current collected messages
		$currMessages = $this->entity->getMessages();
		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Partners/Trusts-update')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

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

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

		$trust = \Entity\Trust::selectForUpdate($currentIdEntity);

		if (!$trust->recordExists()) {
			$currMessages->addErrorCode('delete_trustIdInvalid');
		}

		// check if deleted or can be deleted
		if ($trust->deleted) {
			$currMessages->addWarningCode('delete_trustDeleted');
		}
	}

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

		$trust = \Entity\Trust::selectForUpdate($currentIdEntity);

		// CHECK THAT NAME IS UNIQUE --------------------------------------------------------------
		if ($fields['name']) {
			$trustObj = new \App\EntityList\Trusts();
			$trustObj->setFilter('trusts.id', '!=', $currentIdEntity);
			$trustObj->setFilter('trusts.name', 'LIKE', $fields['name']);
			$trustObj->setFilter('trusts.type', 'LIKE', $trust->type);
			$trustObj->setLimit(1);
			$trustArray = $trustObj->exportRecords();

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

		}
	}

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

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

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

		// get trust details
		$trust = \Entity\Trust::selectForUpdate($currentIdEntity);

		if (!$trust->recordExists()) {
			$currMessages->addErrorCode('delete_trustIdInvalid');
		}

		// check if deleted or can be deleted
		if ($trust->deleted) {
			$currMessages->addWarningCode('delete_trustDeleted');
		}


		if ($trust->type == 'corporate') {
			$corporateObj = new \App\EntityList\Corporates();
			$corporateObj->setFilter('corp.idTrust', '=', $currentIdEntity);
			$corporateObj->setLimit(1);
			$corporateArray = $corporateObj->exportRecords();

			if ($corporateArray) {
				$currMessages->addWarningCode('delete_trustCantBeDeleted');
			}

		}
		elseif ($trust->type == 'supplier') {
			$supplierObj = new \App\EntityList\Suppliers();
			$supplierObj->setFilter('supp.idTrust', '=', $currentIdEntity);
			$supplierObj->setLimit(1);
			$supplierArray = $supplierObj->exportRecords();

			if ($supplierArray) {
				$currMessages->addWarningCode('delete_trustCantBeDeleted');
			}
		}

	}

}
