Backup #768

ID768
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Infrastructure/Persistence/ContentRepository.php
Version14
Typ modified
Größe11.2 KB
Hashe1736b0152c11b5b910a14394d9935c1a32101cd3615175cd93c3041b2546557
Datum2025-12-23 08:04:14
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

namespace Infrastructure\Persistence;

use Domain\Repository\ContentRepositoryInterface;
use Infrastructure\Config\DatabaseFactory;
use Infrastructure\Traits\JsonDecodeTrait;

class ContentRepository implements ContentRepositoryInterface
{
    use JsonDecodeTrait;

    private \PDO $pdo;

    public function __construct(?\PDO $pdo = null)
    {
        $this->pdo = $pdo ?? DatabaseFactory::content();
    }

    // ==================== Orders ====================

    public function findAllOrders(array $filters = [], int $limit = 50, int $offset = 0): array
    {
        $sql = 'SELECT co.*,
                       ap.name as profile_name,
                       cc.name as contract_name,
                       cs.name as structure_name,
                       (SELECT COUNT(*) FROM content_versions WHERE order_id = co.id) as version_count
                FROM content_orders co
                LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = "author_profile"
                LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = "contract"
                LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = "structure"
                WHERE 1=1';

        $params = [];

        if (isset($filters['status']) && $filters['status'] !== '') {
            $sql .= ' AND co.status = :status';
            $params['status'] = $filters['status'];
        }

        if (isset($filters['profile_id']) && $filters['profile_id'] !== '') {
            $sql .= ' AND co.author_profile_id = :profile_id';
            $params['profile_id'] = $filters['profile_id'];
        }

        $sql .= ' ORDER BY co.updated_at DESC LIMIT :limit OFFSET :offset';

        $stmt = $this->pdo->prepare($sql);
        foreach ($params as $key => $value) {
            $stmt->bindValue(':' . $key, $value);
        }
        $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll();
    }

    public function findOrder(int $id): ?array
    {
        $stmt = $this->pdo->prepare('
            SELECT co.*,
                   ap.name as profile_name, ap.content as profile_config,
                   cc.name as contract_name, cc.content as contract_config,
                   cs.name as structure_name, cs.content as structure_config
            FROM content_orders co
            LEFT JOIN content_config ap ON co.author_profile_id = ap.id AND ap.type = "author_profile"
            LEFT JOIN content_config cc ON co.contract_id = cc.id AND cc.type = "contract"
            LEFT JOIN content_config cs ON co.structure_id = cs.id AND cs.type = "structure"
            WHERE co.id = :id
        ');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

        return $result !== false ? $result : null;
    }

    /**
     * Get settings from the last created order as defaults for new orders.
     *
     * @return array{model: string, collections: array<string>, context_limit: int, author_profile_id: int|null, contract_id: int|null, structure_id: int|null}
     */
    public function getLastOrderSettings(): array
    {
        $stmt = $this->pdo->query('
            SELECT model, collections, context_limit, author_profile_id, contract_id, structure_id
            FROM content_orders
            ORDER BY id DESC
            LIMIT 1
        ');
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);

        if (!$row) {
            return [
                'model' => 'claude-sonnet-4-20250514',
                'collections' => ['documents'],
                'context_limit' => 5,
                'author_profile_id' => null,
                'contract_id' => null,
                'structure_id' => null,
            ];
        }

        return [
            'model' => $row['model'] ?? 'claude-sonnet-4-20250514',
            'collections' => $this->decodeJsonArray($row['collections'] ?? null) ?: ['documents'],
            'context_limit' => (int) ($row['context_limit'] ?? 5),
            'author_profile_id' => $row['author_profile_id'] !== null ? (int) $row['author_profile_id'] : null,
            'contract_id' => $row['contract_id'] !== null ? (int) $row['contract_id'] : null,
            'structure_id' => $row['structure_id'] !== null ? (int) $row['structure_id'] : null,
        ];
    }

    public function createOrder(array $data): int
    {
        $stmt = $this->pdo->prepare("
            INSERT INTO content_orders
            (title, briefing, author_profile_id, contract_id, structure_id, model, collections, context_limit, status)
            VALUES (:title, :briefing, :profile_id, :contract_id, :structure_id, :model, :collections, :context_limit, 'draft')
        ");

        $stmt->execute([
            'title' => $data['title'],
            'briefing' => $data['briefing'],
            'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,
            'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,
            'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,
            'model' => $data['model'] ?? 'claude-sonnet-4-20250514',
            'collections' => $data['collections'] ?? '["documents"]',
            'context_limit' => $data['context_limit'] ?? 5,
        ]);

        return (int) $this->pdo->lastInsertId();
    }

    public function updateOrderStatus(int $id, string $status): void
    {
        $stmt = $this->pdo->prepare('
            UPDATE content_orders SET status = :status, updated_at = NOW() WHERE id = :id
        ');
        $stmt->execute(['id' => $id, 'status' => $status]);
    }

    public function updateOrder(int $id, array $data): bool
    {
        $stmt = $this->pdo->prepare('
            UPDATE content_orders SET
                title = :title,
                briefing = :briefing,
                author_profile_id = :profile_id,
                contract_id = :contract_id,
                structure_id = :structure_id,
                updated_at = NOW()
            WHERE id = :id
        ');

        return $stmt->execute([
            'id' => $id,
            'title' => $data['title'],
            'briefing' => $data['briefing'],
            'profile_id' => ($data['author_profile_id'] !== '' && $data['author_profile_id'] !== null) ? $data['author_profile_id'] : null,
            'contract_id' => ($data['contract_id'] !== '' && $data['contract_id'] !== null) ? $data['contract_id'] : null,
            'structure_id' => ($data['structure_id'] !== '' && $data['structure_id'] !== null) ? $data['structure_id'] : null,
        ]);
    }

    // ==================== Versions ====================

    public function findVersionsByOrder(int $orderId): array
    {
        $stmt = $this->pdo->prepare('
            SELECT * FROM content_versions
            WHERE order_id = :order_id
            ORDER BY version_number DESC
        ');
        $stmt->execute(['order_id' => $orderId]);

        return $stmt->fetchAll();
    }

    public function findLatestVersion(int $orderId): ?array
    {
        $stmt = $this->pdo->prepare('
            SELECT * FROM content_versions
            WHERE order_id = :order_id
            ORDER BY version_number DESC
            LIMIT 1
        ');
        $stmt->execute(['order_id' => $orderId]);
        $result = $stmt->fetch();

        return $result !== false ? $result : null;
    }

    public function findVersion(int $id): ?array
    {
        $stmt = $this->pdo->prepare('SELECT * FROM content_versions WHERE id = :id');
        $stmt->execute(['id' => $id]);
        $result = $stmt->fetch();

        return $result !== false ? $result : null;
    }

    // ==================== Critiques ====================

    public function findCritiquesByVersion(int $versionId): array
    {
        $stmt = $this->pdo->prepare('
            SELECT cc.*, c.name as critic_name
            FROM content_critiques cc
            JOIN critics c ON cc.critic_id = c.id
            WHERE cc.version_id = :version_id
            ORDER BY cc.round DESC, c.sort_order ASC
        ');
        $stmt->execute(['version_id' => $versionId]);

        $results = $stmt->fetchAll();

        // Parse feedback JSON and merge with row
        foreach ($results as &$row) {
            if (isset($row['feedback']) && $row['feedback'] !== '') {
                $feedback = $this->decodeJsonArray($row['feedback']);
                if ($feedback !== []) {
                    $row = array_merge($row, $feedback);
                }
            }
        }

        return $results;
    }

    // ==================== Sources ====================

    public function findSourcesByOrder(int $orderId): array
    {
        $stmt = $this->pdo->prepare('
            SELECT cs.*, c.content, d.filename as document_name
            FROM content_sources cs
            JOIN chunks c ON cs.chunk_id = c.id
            JOIN documents d ON c.document_id = d.id
            WHERE cs.order_id = :order_id
            ORDER BY cs.relevance_score DESC
        ');
        $stmt->execute(['order_id' => $orderId]);

        return $stmt->fetchAll();
    }

    // ==================== Profiles ====================

    public function findAllProfiles(): array
    {
        $stmt = $this->pdo->query("
            SELECT id, name, slug, content as config
            FROM content_config
            WHERE type = 'author_profile' AND status = 'active'
            ORDER BY name
        ");

        return $stmt->fetchAll();
    }

    // ==================== Contracts ====================

    public function findAllContracts(): array
    {
        $stmt = $this->pdo->query("
            SELECT id, name, slug, content as config
            FROM content_config
            WHERE type = 'contract' AND status = 'active'
            ORDER BY name
        ");

        return $stmt->fetchAll();
    }

    // ==================== Structures ====================

    public function findAllStructures(): array
    {
        $stmt = $this->pdo->query("
            SELECT id, name, slug, content as config
            FROM content_config
            WHERE type = 'structure' AND status = 'active'
            ORDER BY name
        ");

        return $stmt->fetchAll();
    }

    // ==================== Critics ====================

    public function findAllCritics(): array
    {
        $stmt = $this->pdo->query('
            SELECT * FROM critics WHERE is_active = 1 ORDER BY sort_order
        ');

        return $stmt->fetchAll();
    }

    // ==================== Statistics ====================

    public function getStatistics(): array
    {
        $stats = [];

        $stmt = $this->pdo->query('SELECT COUNT(*) FROM content_orders');
        $stats['total_orders'] = (int) $stmt->fetchColumn();

        $stmt = $this->pdo->query('SELECT status, COUNT(*) as count FROM content_orders GROUP BY status');
        $stats['by_status'] = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR);

        $stmt = $this->pdo->query('SELECT COUNT(*) FROM content_versions');
        $stats['total_versions'] = (int) $stmt->fetchColumn();

        $stmt = $this->pdo->query('SELECT COUNT(*) FROM content_critiques');
        $stats['total_critiques'] = (int) $stmt->fetchColumn();

        return $stats;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
2156 22 modified 4.9 KB 2025-12-30 22:22
1409 21 modified 5.1 KB 2025-12-25 16:58
1403 20 modified 4.9 KB 2025-12-25 16:58
1397 19 modified 12.7 KB 2025-12-25 16:58
1204 18 modified 12.7 KB 2025-12-25 10:34
1187 17 modified 12.7 KB 2025-12-25 10:32
1014 16 modified 12.0 KB 2025-12-24 01:29
975 15 modified 11.3 KB 2025-12-24 00:13
768 14 modified 11.2 KB 2025-12-23 08:04
578 13 modified 11.2 KB 2025-12-23 04:22
336 12 modified 11.2 KB 2025-12-22 08:09
335 11 modified 11.2 KB 2025-12-22 08:09
334 10 modified 11.2 KB 2025-12-22 08:09
333 9 modified 11.1 KB 2025-12-22 08:09
247 8 modified 11.0 KB 2025-12-22 01:48
206 7 modified 9.6 KB 2025-12-21 17:01
158 6 modified 9.3 KB 2025-12-21 02:59
154 5 modified 9.0 KB 2025-12-21 02:35
153 4 modified 9.0 KB 2025-12-21 02:34
152 3 modified 8.9 KB 2025-12-21 02:34
126 2 modified 8.9 KB 2025-12-20 19:40
33 1 modified 9.6 KB 2025-12-20 17:23

← Zurück zur Übersicht