Files
nextcloud-analytics/analyticshub/lib/Service/DatabaseService.php
WLTBAgent 8a445c4d46 Fix: Rename app folder to match app ID
- Renamed analytics-hub/ → analyticshub/
- App ID in info.xml is 'analyticshub' (no hyphen)
- Nextcloud requires folder name to match app ID exactly
- Fixes 'Could not download app analyticshub' error during installation

Installation:
- Upload analyticshub/ folder to /var/www/nextcloud/apps/
- Folder name must match app ID in info.xml
2026-02-13 18:21:39 +00:00

142 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\AnalyticsHub\Service;
use OCA\AnalyticsHub\Model\ClientConfig;
use OCP\IDBConnection;
use OCP\AppFramework\Utility\SimplePDO;
/**
* Database Service
* Handles report storage in Nextcloud database
*/
class DatabaseService {
private IDBConnection $db;
public function __construct(IDBConnection $db) {
$this->db = $db;
}
/**
* Initialize database tables
*/
public function initialize(): void {
$this->createReportsTable();
}
/**
* Create reports table
*/
private function createReportsTable(): void {
$sql = "
CREATE TABLE IF NOT EXISTS *PREFIX*analytics_reports (
id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
client_name TEXT NOT NULL,
report_date TEXT NOT NULL,
file_path TEXT NOT NULL,
file_size INTEGER,
created_at TEXT NOT NULL
)
";
$this->db->executeUpdate($sql);
}
/**
* Save report to database
*/
public function saveReport(int $clientId, string $clientName, string $markdown, string $filePath): int {
$sql = "
INSERT INTO *PREFIX*analytics_reports
(client_id, client_name, report_date, file_path, file_size, created_at)
VALUES (?, ?, ?, ?, ?, ?)
";
$this->db->executeUpdate($sql, [
$clientId,
$clientName,
date('Y-m-d'),
$filePath,
strlen($markdown),
date('Y-m-d H:i:s')
]);
return (int)$this->db->lastInsertId();
}
/**
* Get report by ID
*/
public function getReportById(int $id): ?array {
$sql = "
SELECT * FROM *PREFIX*analytics_reports
WHERE id = ?
";
$result = $this->db->executeQuery($sql, [$id])->fetch();
return $result ? json_decode(json_encode($result), true) : null;
}
/**
* Get all reports
*/
public function getAllReports(): array {
$sql = "
SELECT * FROM *PREFIX*analytics_reports
ORDER BY created_at DESC
LIMIT 100
";
$result = $this->db->executeQuery($sql)->fetchAll();
return $result;
}
/**
* Get reports by client
*/
public function getReportsByClient(int $clientId): array {
$sql = "
SELECT * FROM *PREFIX*analytics_reports
WHERE client_id = ?
ORDER BY created_at DESC
";
$result = $this->db->executeQuery($sql, [$clientId])->fetchAll();
return $result;
}
/**
* Get latest report
*/
public function getLatestReport(): ?array {
$sql = "
SELECT * FROM *PREFIX*analytics_reports
ORDER BY created_at DESC
LIMIT 1
";
$result = $this->db->executeQuery($sql)->fetch();
return $result ? json_decode(json_encode($result), true) : null;
}
/**
* Delete old reports (cleanup)
*/
public function deleteOldReports(int $days = 90): int {
$sql = "
DELETE FROM *PREFIX*analytics_reports
WHERE created_at < datetime('now', '-' . $days . ' days')
";
return $this->db->executeUpdate($sql);
}
}