<?php

namespace Entity;


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

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

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

		// CHECK CATEGORY ID ----------------------------------------------------------------------
		if ($fields['idCategory']) {
			$categ = \Entity\ActivityDomainCategory::selectForUpdate($fields['idCategory']);

			if (!$categ->recordExists()){
				$currMessages->addErrorCode('insert_invalidCategSelected');
			}
			elseif ($categ->deleted) {
				$currMessages->addWarningCode('insert_selectedCategDeleted');
			}
		}

		// CHECK THAT DOMAIN NAME IS UNIQUE -------------------------------------------------------
		if ($fields['name'] && $fields['idCategory']) {
			$activityDomainObj = new \App\EntityList\ActivityDomains();
			$activityDomainObj->setFilter('ad.name', 'LIKE', $fields['name']);
			$activityDomainObj->setFilter('ad.idCategory', '=', $fields['idCategory']);
			$activityDomainObj->setLimit(1);
			$activityDomainArray = $activityDomainObj->exportRecords();

			if ($activityDomainArray) {
			    $currMessages->addWarningCode('insert_domainNotUnique', 'name');
			}
		}
	}

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

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

	   $activityDomain = \Entity\ActivityDomain::selectForUpdate($currentIdEntity);

	   if (!$activityDomain->recordExists()) {
	       $currMessages->addErrorCode('update_invalidId');
	   }
	   elseif ($activityDomain->deleted) {
	       $currMessages->addWarningCode('update_actDomDeleted');
	   }
	}

	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();
		$ad = \Entity\ActivityDomain::selectForUpdate($currentIdEntity);

		// CHECK THAT DOMAIN NAME IS UNIQUE -------------------------------------------------------
		if ($fields['name']) {
            $activityDomainObj = new \App\EntityList\ActivityDomains();
            $activityDomainObj->setFilter('ad.id', '!=', $currentIdEntity);
            $activityDomainObj->setFilter('ad.name', 'LIKE', $fields['name']);
            $activityDomainObj->setFilter('ad.idCategory', '=', $ad->idCategory);
            $activityDomainObj->setLimit(1);
            $activityDomainArray = $activityDomainObj->exportRecords();

            if ($activityDomainArray) {
                $currMessages->addWarningCode('update_domainNotUnique', 'name');
            }
		}

	}

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

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

		// CHECK THAT THE CURRENT ACTIVITY IS NOT ALREADY DELETED ------------------------------------
		$activityDomain = \Entity\ActivityDomain::selectForUpdate($currentIdEntity);
		if (!$activityDomain->recordExists()) {
			$currMessages->addErrorCode('delete_activityDomainIdInvalid');
		}
		elseif ($activityDomain->deleted) {
			$currMessages->addWarningCode('delete_activityDomainDeleted');
		}

	}

}
