File: /var/www/heifetz/heifetz-app/models/UserLogs.php
<?php
namespace Models;
use Core\DbLib\DbModel;
use Core\Models\CoreHelper;
use Core\Models\OldUser;
use Helpers\Data;
use Traits\ModelGetQuery;
/**
* @property int $id
* @property ?int $user_id
* @property ?string $entity
* @property ?int $entity_id
* @property ?string $column
* @property ?string $value
* @property string $created_at
* @property ?string $action
*/
class UserLogs extends DbModel
{
use ModelGetQuery;
protected static bool $hasCompanyRelation = true;
const ACTION_CREATE = 'CREATE';
const ACTION_UPDATE = 'UPDATE';
const ACTION_DELETE = 'DELETE';
const ACTION_SOFT_DELETE = 'SOFT_DELETE';
const ACTION_REPLACE = 'REPLACE';
public static array $actions = [
self::ACTION_CREATE => 'Создание',
self::ACTION_UPDATE => 'Обновление',
self::ACTION_DELETE => 'Удаление',
self::ACTION_SOFT_DELETE => 'Безопасное удаление',
self::ACTION_REPLACE => 'Замена',
];
static string $tableName = 'user_logs';
public static function log($entity, $entityId, $column, $value, $action = null)
{
return;
$entityId = is_array($entityId) ? Data::jsonEncode($entityId) : $entityId;
$value = is_array($value) ? Data::jsonEncode($value) : $value;
self::create(
[
'user_id' => OldUser::getRealId(),
'entity' => $entity,
'entity_id' => $entityId,
'column' => $column,
'value' => $value,
'action' => $action,
], false
);
}
public static function logCreate($entity, $entityId, $data)
{
self::log($entity, $entityId, 'record', $data, self::ACTION_CREATE);
}
public static function logUpdate($entity, $entityId, $column, $value)
{
self::log($entity, $entityId, $column, $value, self::ACTION_UPDATE);
}
public static function logDelete($entity, $entityId)
{
self::log($entity, $entityId, 'record', '', self::ACTION_DELETE);
}
public static function logSoftDelete($entity, $entityId)
{
self::log($entity, $entityId, 'record', '', self::ACTION_SOFT_DELETE);
}
/**
* @param $entity
* @param $entityId
* @param array|object $array
* @param null $action
*
* @depricated Use UserLogs::log()
*/
public static function logRow($entity, $entityId, $array, $action = null)
{
self::log($entity, $entityId, 'record', json_encode($array), $action);
}
}