File: /var/www/heifetz/heifetz-app/models/NewAbstractDictionary.php
<?php
namespace Models;
use Core\DbLib\NewDbModel;
use Core\Models\CoreHelper;
use Core\Models\RedisCache;
/**
* @property int $id
* @property ?string $key
* @property string $name
* @property int $deleted
* @property int $active
* @property int $protected
* @property int $company_id
*/
abstract class NewAbstractDictionary extends NewDbModel
{
static bool $withBoolFields = false;
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;
}
}