File: /var/www/heifetz/heifetz-app/models/AbstractDictionary.php
<?php
namespace Models;
use Core\Models\CoreHelper;
use Core\Models\RedisCache;
use Helpers\ArrayHelper;
use Traits\ModelGetQuery;
/**
* @property int $id
* @property ?string $key
* @property string $name
* @property int $deleted
* @property int $active
* @property int $protected
* @property int $company_id
*/
abstract class AbstractDictionary extends AbstractModel
{
use ModelGetQuery;
static bool $withBoolFields = false;
public static function save(int $entityId, array $data = []): int
{
$diff = ArrayHelper::getDiff(static::find($entityId), $data);
if (empty($diff)) {
return 0;
}
return static::update($data, ['id' => $entityId]);
}
protected static function getRules(): array
{
return [
'name' => 'required|size:255',
];
}
// TODO: rename it to getAttributeLabels
protected static function getTranslations(): array
{
return ['name' => Lang::__('fieldName')];
}
public static function setName(int $id, string $name): void
{
static::update(['name' => $name], ['id' => $id]);
}
public static function setDeleted($id, $deleted = true): void
{
static::update(['deleted' => (int) $deleted], ['id' => $id]);
}
// active
public static function setProtected($id, $protected = true): void
{
static::update(['protected' => (int) $protected], ['id' => $id]);
}
public static function add($word, $isProtected = false)
{
$data = ['name' => $word, 'company_id' => CoreHelper::$companyId];
if ($isProtected) {
$data['protected'] = in_array(static::$tableName, Dictionary::$tablesWithBool) ? true : 1;
}
$id = static::create($data);
$cache = RedisCache::create();
$cache->delete($cache->keys(static::$tableName . '*'));
UserLogs::logCreate(static::$tableName, $id, $data);
return $id;
}
public static function get($id): ?object
{
$sql = self::getQuery()->where('id = ?', (int) $id)->where('deleted = 0');
return $sql->fetchRow();
}
public static function isExists(string $word): bool
{
return (bool) static::$db->select('id')->from(static::$tableName)
->_where('name = ?', $word)
->fetchOne();
}
/**
* @param int|null $id TODO не должно быть ?int
* @param string|null $default
*
* @return string
*/
public static function getName(?int $id, string $default = null): string
{
$item = static::$db->select('name', 'deleted', 'active')->from(static::$tableName)->company()->where('id = ?', (int) $id)->fetchRow();
$name = empty($item->name) ? ($default ?? 'Не указано') : $item->name;
if (isset($item->deleted) && $item->deleted) {
$name .= ' 🔴';
}
if (isset($item->active) && !$item->active) {
$name .= ' 🟠';
}
return $name;
}
public static function getNames(bool $keyById = false, bool $showDeleted = false, array $ids = [], bool $onlyActive = false): array
{
$companyId = CoreHelper::$companyId;
$tableData = RedisCache::create()->getSet(static::$tableName . '_getNames_cid_' . $companyId, callback: fn() => static::$db->select('id', 'name', 'deleted', 'active')
->from(static::$tableName)
->company()
->order('name', 'ASC')
->fetchArray(), expiration: RedisCache::DAY);
if (!$showDeleted) {
$tableData = array_filter($tableData, fn($item) => $item['deleted'] == (static::$withBoolFields ? false : 0));
}
if ($onlyActive) {
$tableData = array_filter($tableData, fn($item) => $item['active'] == (static::$withBoolFields ? true : 1));
}
if (!empty($ids)) {
$tableData = array_filter($tableData, fn($item) => in_array($item['id'], $ids));
}
$result = [];
if ($keyById) {
foreach ($tableData as $item) {
if (isset($item['deleted']) && $item['deleted']) {
$item['name'] .= ' 🔴';
}
if (isset($item['active']) && !$item['active']) {
$item['name'] .= ' 🟠';
}
$result[$item['id']] = $item['name'];
}
$tableData = $result;
}
return $tableData;
}
/**
* @param bool $showDeleted
* @param bool $keyById
* @param bool $onlyActive
*
* @return array|object[]|static[]
*/
public static function getTableData(bool $showDeleted = false, bool $keyById = false, bool $onlyActive = false): array
{
$companyId = CoreHelper::$companyId;
$key = static::$tableName . '_getTableData_company_id_' . $companyId;
$sql = static::$db->select()->from(static::$tableName)->order('name', 'ASC')->company();
$tableData = RedisCache::create()->getSet($key, callback: fn() => $sql->fetchAll(), expiration: RedisCache::DAY);
if (!$showDeleted) {
$tableData = array_filter($tableData, fn($item) => $item->deleted == (static::$withBoolFields ? false : 0));
}
if ($onlyActive) {
$tableData = array_filter($tableData, fn($item) => $item->active == (static::$withBoolFields ? true : 1));
}
if (empty($tableData)) {
return [];
}
if (isset($keyById)) {
$tableData = ArrayHelper::keyByField($tableData);
}
return $tableData;
}
/**
* @param string $name
*
* @return static|object|null
*/
public static function getByName(string $name): ?object
{
$entity = static::$db->select()->from(static::$tableName)->where('name = ?', $name)->fetchRow();
return empty($entity) ? null : $entity;
}
}