<?php

namespace Entity;

class Validator_ActivityDomainCategory 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 THAT CATEGORY NAME IS UNIQUE -----------------------------------------------------
		if ($fields['name']) {
		    $domainCategoryObj = new \App\EntityList\ActivityDomainCategories();
		    $domainCategoryObj->setFilter('adc.name', 'LIKE', $fields['name']);
		    $domainCategoryObj->setLimit(1);
		    $domainCategoryArray = $domainCategoryObj->exportRecords();

		    if ($domainCategoryArray) {
		        $currMessages->addWarningCode('insert_categoryNotUnique', 'name');
		    }
		}

	}

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

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

	    $category = \Entity\ActivityDomainCategory::selectForUpdate($currentIdEntity);

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

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

		// CHECK THAT CATEGORY NAME IS UNIQUE -----------------------------------------------------
		if ($fields['name']) {
            $activityDomainCategoryObj = new \App\EntityList\ActivityDomainCategories();
            $activityDomainCategoryObj->setFilter('adc.id', '!=', $currentIdEntity);
            $activityDomainCategoryObj->setFilter('adc.name', 'LIKE', $fields['name']);
            $activityDomainCategoryObj->setLimit(1);
            $activityDomainCategoryArray = $activityDomainCategoryObj->exportRecords();

            if ($activityDomainCategoryArray) {
                $currMessages->addWarningCode('update_categoryNotUnique', 'name');
            }
		}

	}


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

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

		// CHECK ID -------------------------------------------------------------------------------
		$cat = \Entity\ActivityDomainCategory::selectForUpdate($currentIdEntity);
		if (!$cat->recordExists()) {
			$currMessages->addErrorCode('delete_invalidId');
		}
		elseif ($cat->deleted) {
			$currMessages->addWarningCode('delete_catDeleted');
		}

		// CHECK THAT THE CURRENT CATEGORY DOES NOT HAVE ANY DOMAINS ------------------------------
		$domListObj = new \App\EntityList\ActivityDomains();
		$domListObj->setColumn('name');
		$domListObj->setFilter('idCategory', '=', $currentIdEntity);
		$domListObj->setLimit(1);
		if ($domListObj->exportRecords()) {
			$currMessages->addWarningCode('delete_catHasDomains');
		}
	}

}

