<?php
namespace Entity; class Validator_Country extends \App\Entity\Utils\Extension_ValidateDefaultAbstract { protected function isAccessible_insert() { if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-insert')) { $this->entity->getMessages()->addWarningCode('noAccessToOption'); } } protected function check_insert() { $cfg = $this->entity->getCFG(); $fields = $this->entity->getFields(); $currMessages = $this->entity->getMessages(); if ($fields['code']) { $country = \Entity\Country::selectForUpdate($fields['code']); if ($country->recordExists() && !$country->deleted) { $currMessages->addWarningCode('insert_codeNotUnique', 'code', $country->name); } } if ($fields['nameStandard']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('insert_nameNotUnique', 'name'); } } if ($fields['nameLocal']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('insert_nameNotUnique', 'name'); } } if ($cfg['code3']['unique'] and $fields['code3']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setColumns(['country.name']); $countryObj->setFilter('country.code3', 'LIKE', $fields['code3']); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('insert_code3NotUnique', 'code3', $countryArray[0]['name']); } } } protected function isAccessible_update() { if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-update')) { $this->entity->getMessages()->addWarningCode('noAccessToOption'); } } protected function isAvailable_update() { $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); $country = Country::selectForUpdate($currentIdEntity); if (!$country->recordExists()) { $currMessages->addErrorCode('update_cntrDetailsBadId'); } elseif ($country->deleted) { $currMessages->addWarningCode('update_cntrDeleted'); } } protected function check_update() { $cfg = $this->entity->getCFG(); $fields = $this->entity->getFields(); $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); if ($fields['nameStandard']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setFilter('country.code', '!=', $currentIdEntity); $countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameStandard'], $fields['nameStandard']]); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('update_nameNotUnique', 'name'); } } if ($fields['nameLocal']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setFilter('country.code', '!=', $currentIdEntity); $countryObj->setCustomFilter('country.name = ? OR country.name2 = ?', [$fields['nameLocal'], $fields['nameLocal']]); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('update_nameNotUnique', 'name'); } } if ($cfg['code3']['unique'] and $fields['code3']) { $countryObj = new \App\EntityList\Countries(); $countryObj->setColumns(['country.name']); $countryObj->setFilter('country.code', '!=', $currentIdEntity); $countryObj->setFilter('country.code3', 'LIKE', $fields['code3']); $countryObj->setLimit(1); $countryArray = $countryObj->exportRecords(); if ($countryArray) { $currMessages->addWarningCode('insert_code3NotUnique', 'code3', $countryArray[0]['name']); } } } protected function isAccessible_delete() { if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Countries-delete')) { $this->entity->getMessages()->addWarningCode('noAccessToOption'); } } protected function isAvailable_delete() { $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); $country = Country::selectForUpdate($currentIdEntity); if (!$country->recordExists()) { $currMessages->addErrorCode('delete_cntrDetailsBadId'); } elseif ($country->deleted) { $currMessages->addWarningCode('delete_cntrDeleted'); } $citiesList=new \App\EntityList\Cities(); $citiesList->setFilter('city.countryCode', '=', $currentIdEntity); $citiesList->setLimit(1); if ($citiesList->exportRecords()) { $currMessages->addWarningCode('delete_cntrHasCities'); } } } 