<?php
namespace Entity;

abstract class Validator_PartnerContractAbstract extends \App\Entity\Utils\Extension_ValidateDefaultAbstract
{
	protected function check_insert()
	{
		// get current fields details
		$fields = $this->entity->getFields();

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

		// CHECK IF CONTRACT DATE IS < EXPIRE DATE /////////////////////////////////////////////////
		if ($fields['contractDate'] and $fields['expireDate']) {
			if ($fields['contractDate'] > $fields['expireDate']) {
				$currentMessages->addWarningCode('insert_invalidExpireDate');
			}
		}

		// CHECK FILE UPLOAD PATH /////////////////////////////////////////////////////////////////
		if ($fields['fileName']){
			if(!\Engine\Dir::create($this->entity->getFileStoragePath())){
				$currentMessages->addErrorCode('insert_contractFilePathErr');
			}
		}

		if (($fields['fileName'] and !$fields['fileContent']) or (!$fields['fileName'] and $fields['fileContent'])) {
			$currentMessages->addErrorCode('insert_fileIncorrect');
		}

		// CHECK IF NAME OF FILE NOT CONTAIN SPECIAL CHARACTERS ////////////////////////////////////
		if ($fields['fileName'] and $fields['fileName'] != \Engine\File::cleanName($fields['fileName'])) {
			$currentMessages->addWarningCode('insert_fileNameIncorrect');
		}

		// CHECK ID RESPONSIBLE USER IS VALID
		if ($fields['idResponsibleUser']) {
			$user = \Entity\User::select($fields['idResponsibleUser']);

			if (!$user->recordExists()) {
				$currentMessages->addErrorCode('insert_idResponsibleUserInvalid');
			}
			elseif ($user->deleted) {
				$currentMessages->addWarningCode('insert_idResponsibleUserDeleted');
			}
		}
	}

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

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

		$partnerRefColumnName = $this->entity->getPartnerRefColumnName();

		// CHECK PARTNER REF COLUMN NAME ID //////////////////////////////////////////////////////////////////////
		if (array_key_exists($partnerRefColumnName, $fields)) {
			throw new \Exception("The field '{$partnerRefColumnName}' cannot be updated in the `".__CLASS__."` entity.");
		}

		// CHECK FILE UPLOAD PATH -----------------------------------------------------------------
		if (array_key_exists('file', $fields)){
			if(!\Engine\Dir::create($this->entity->getFileStoragePath())){
				$currentMessages->addErrorCode('update_contractFilePathErr');
			}
		}

		// CHECK IF FILE NAME EXISTS AND ALSO FILE CONTENT
		if (($fields['fileName'] and !$fields['fileContent']) or (!$fields['fileName'] and $fields['fileContent'])) {
			$currentMessages->addWarningCode('insert_fileIncorrect');
		}

		// CHECK IF NAME OF FILE NOT CONTAIN SPECIAL CHARACTERS ////////////////////////////////////
		if ($fields['fileName'] != \Engine\File::cleanName($fields['fileName'])) {
			$currentMessages->addWarningCode('insert_fileNameIncorrect');
		}

		// CHECK ID RESPONSIBLE USER IS VALID
		if ($fields['idResponsibleUser']) {
			$user = \Entity\User::select($fields['idResponsibleUser']);

			if (!$user->recordExists()) {
				$currentMessages->addErrorCode('insert_idResponsibleUserInvalid');
			}
			elseif ($user->deleted) {
				$currentMessages->addWarningCode('insert_idResponsibleUserDeleted');
			}
		}
	}
}