'json']; public const CREATED = 'created'; public const UPDATED = 'updated'; public const DELETED = 'deleted'; public const ACTION = 'action'; public const LOGIN = 'login'; public const LOGOUT = 'logout'; /** * Подписи действий для страницы журнала. * * @var array */ public const LABELS = [ self::CREATED => 'создание', self::UPDATED => 'изменение', self::DELETED => 'удаление', self::ACTION => 'действие', self::LOGIN => 'вход', self::LOGOUT => 'выход', ]; /** * Цвет бейджа под действие — по строке в общем списке проще отличить * удаление от входа, чем читать подпись у каждой. * * @var array */ public const BADGES = [ self::CREATED => 'ok', self::UPDATED => 'info', self::DELETED => 'error', self::ACTION => 'muted', self::LOGIN => 'ok', self::LOGOUT => 'muted', ]; /** * @param array $changes поле => [было, стало] */ public static function write( string $action, string $entity, int|string $entityId, string $description, array $changes = [], int $userId = 0, string $login = '', string $ip = '' ): void { $entry = new self(); $entry->forceFill([ 'user_id' => $userId, 'user_login' => $login, 'action' => $action, 'entity' => $entity, 'entity_id' => (string) $entityId, 'description' => mb_substr($description, 0, 500), 'changes' => $changes === [] ? null : json_encode($changes, JSON_UNESCAPED_UNICODE), 'ip' => $ip, 'created_at' => Connection::now(), ])->save(); } public static function label(string $action): string { return self::LABELS[$action] ?? $action; } public static function badge(string $action): string { return self::BADGES[$action] ?? 'muted'; } /** * Чистка по сроку — зовётся воркером. */ public static function purge(int $days): int { if ($days <= 0) { return 0; } return self::query() ->where('created_at', '<', date('Y-m-d H:i:s', time() - $days * 86400)) ->forceDelete(); } }