<?php
namespace Entity;

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

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

		// check user access
		if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Departments-insert')) {
			$currMessages->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 NAME IS UNIQUE --------------------------------------------------------------
		if ($fields['name']) {
			$departmentObj = new \App\EntityList\Departments();
			$departmentObj->setFilter('dept.name', 'LIKE', $fields['name']);
			$departmentObj->setLimit(1);
			$departmentArray = $departmentObj->exportRecords();

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

		// CHECK THAT CODE IS UNIQUE --------------------------------------------------------------
		if ($cfg['code']['unique'] and $fields['code']) {
			$departmentObj = new \App\EntityList\Departments();
			$departmentObj->setFilter('dept.code', 'LIKE', $fields['code']);
			$departmentObj->setLimit(1);
			$departmentArray = $departmentObj->exportRecords();

			if ($departmentArray) {
				$currMessages->addWarningCode('insert_codeNotUnique', '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/Departments-update')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

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

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

		// lock the record (if it is the case)
		$dept = \Entity\Department::selectForUpdate($currentIdEntity);

		if (!$dept->recordExists()) {
			$currMessages->addErrorCode('update_invalidDeptId');
		}
		elseif ($dept->deleted)  {
			$currMessages->addErrorCode('update_deletedDept');
		}
	}

	/**
	 * 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 IS UNIQUE --------------------------------------------------------------
		if ($fields['name']) {
			$departmentObj = new \App\EntityList\Departments();
			$departmentObj->setFilter('dept.id', '!=', $currentIdEntity);
			$departmentObj->setFilter('dept.name', 'LIKE', $fields['name']);
			$departmentObj->setLimit(1);
			$departmentArray = $departmentObj->exportRecords();

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

		// CHECK THAT CODE IS UNIQUE --------------------------------------------------------------
		if ($cfg['code']['unique'] and $fields['code']) {
			$departmentObj = new \App\EntityList\Departments();
			$departmentObj->setFilter('dept.id', '!=', $currentIdEntity);
			$departmentObj->setFilter('dept.code', 'LIKE', $fields['code']);
			$departmentObj->setLimit(1);
			$departmentArray = $departmentObj->exportRecords();

			if ($departmentArray) {
				$currMessages->addWarningCode('update_codeNotUnique', '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/Departments-delete')) {
			$currMessages->addWarningCode('noAccessToOption');
		}

	}

	/**
	 * Check if the operation can be performed (without considering class fields).
	 */
	protected function isAvailable_delete()
	{

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

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

		// CHECK THAT THE CURRENT DEPARTMENT IS NOT ALREADY DELETED -------------------------------
		$dept = \Entity\Department::selectForUpdate($currentIdEntity);

		if (!$dept->recordExists()) {
			$currMessages->addWarningCode('delete_deptIdInvalid');
		}
		elseif ($dept->deleted) {
			$currMessages->addWarningCode('delete_deptDeleted');
		}

		// CHECK THAT DEPARTMENT HASN'T GOT ANY USERS ASSOCIATED ----------------------------------
		$userObj = new \App\EntityList\Users();
		$userObj->setFilter('user.idDepartment', '=', $currentIdEntity);
		$userObj->setLimit(1);
		$userArray = $userObj->exportRecords();

		if ($userArray) {
			$currMessages->addWarningCode('delete_deptHasUsers');
		}

	}

}
