<?php
namespace Entity; class Validator_Department extends \App\Entity\Utils\Extension_ValidateDefaultAbstract { protected function isAccessible_insert() { $currMessages = $this->entity->getMessages(); if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Departments-insert')) { $currMessages->addWarningCode('noAccessToOption'); } } protected function check_insert() { $cfg = $this->entity->getCFG(); $fields = $this->entity->getFields(); $currMessages = $this->entity->getMessages(); 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'); } } 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'); } } } protected function isAccessible_update() { $currMessages=$this->entity->getMessages(); if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Departments-update')) { $currMessages->addWarningCode('noAccessToOption'); } } protected function isAvailable_update() { $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); $dept = \Entity\Department::selectForUpdate($currentIdEntity); if (!$dept->recordExists()) { $currMessages->addErrorCode('update_invalidDeptId'); } elseif ($dept->deleted) { $currMessages->addErrorCode('update_deletedDept'); } } protected function check_update() { $cfg = $this->entity->getCFG(); $fields = $this->entity->getFields(); $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); 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'); } } 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'); } } } protected function isAccessible_delete() { $currMessages = $this->entity->getMessages(); if (!\App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToOption('Modules/Admin/Departments-delete')) { $currMessages->addWarningCode('noAccessToOption'); } } protected function isAvailable_delete() { $currentIdEntity = $this->entity->getId(); $currMessages = $this->entity->getMessages(); $dept = \Entity\Department::selectForUpdate($currentIdEntity); if (!$dept->recordExists()) { $currMessages->addWarningCode('delete_deptIdInvalid'); } elseif ($dept->deleted) { $currMessages->addWarningCode('delete_deptDeleted'); } $userObj = new \App\EntityList\Users(); $userObj->setFilter('user.idDepartment', '=', $currentIdEntity); $userObj->setLimit(1); $userArray = $userObj->exportRecords(); if ($userArray) { $currMessages->addWarningCode('delete_deptHasUsers'); } } } 