Files
nextcloud-analytics/analyticshub/lib/Controller/AdminController.php
WLTBAgent 13c313352c Fix: Add proper Application.php and fix DI
- Created appinfo/Application.php (Nextcloud app bootstrap)
  - Extends OCP\AppFramework\App
  - Registers proper app ID: analyticshub
  - Loads CSS and JS files
- Removed obsolete lib/App.php file
- Updated all AppInfo::APP_NAME references to Application::APP_NAME
  - AdminController, ApiV1Controller
  - GoogleAnalyticsService, LLMService
- Fixed dependency injection in AdminController
  - Injected IConfig service properly
- Added missing use statements

This is the core fix for the app not appearing in Nextcloud.
Nextcloud requires appinfo/Application.php to initialize the app.
2026-02-13 18:38:35 +00:00

179 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\AnalyticsHub\Controller;
use OCP\IRequest;
use OCP\IResponse;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\AnalyticsHub\AppInfo\Application;
use OCA\AnalyticsHub\Service\GoogleAnalyticsService;
use OCA\AnalyticsHub\Service\LLMService;
use OCA\AnalyticsHub\Service\DataProcessor;
use OCP\IConfig;
/**
* Admin Settings Controller
* Handles app configuration via admin UI
*/
class AdminController {
private IConfig $config;
private GoogleAnalyticsService $gaService;
private LLMService $llmService;
private DataProcessor $dataProcessor;
public function __construct(
IConfig $config,
GoogleAnalyticsService $gaService,
LLMService $llmService,
DataProcessor $dataProcessor
) {
$this->config = $config;
$this->gaService = $gaService;
$this->llmService = $llmService;
$this->dataProcessor = $dataProcessor;
}
/**
* Index page - render admin UI
* GET /admin
*/
public function index(): TemplateResponse {
return new TemplateResponse('analyticshub', 'admin', [
'app_name' => Application::APP_NAME,
'version' => AppInfo::getVersion(),
'status' => $this->gaService->isConfigured() ? 'configured' : 'not_configured',
]);
}
/**
* Save configuration
* POST /admin/save
*/
public function save(IRequest $request): JSONResponse {
$params = $request->getParams();
// Validate required fields
if (!isset($params['google_client_id'])) {
return new JSONResponse([
'success' => false,
'error' => 'google_client_id is required'
], Http::STATUS_BAD_REQUEST);
}
if (!isset($params['google_client_secret'])) {
return new JSONResponse([
'success' => false,
'error' => 'google_client_secret is required'
], Http::STATUS_BAD_REQUEST);
}
if (!isset($params['anthropic_api_key'])) {
return new JSONResponse([
'success' => false,
'error' => 'anthropic_api_key is required'
], Http::STATUS_BAD_REQUEST);
}
if (!isset($params['clients_json'])) {
return new JSONResponse([
'success' => false,
'error' => 'clients_json is required'
], Http::STATUS_BAD_REQUEST);
}
try {
// Save Google OAuth config
$this->saveConfigValue('google_client_id', $params['google_client_id']);
$this->saveConfigValue('google_client_secret', $params['google_client_secret']);
$this->saveConfigValue('anthropic_api_key', $params['anthropic_api_key']);
// Save client configuration
$clientsJson = $params['clients_json'];
if (!json_decode($clientsJson)) {
return new JSONResponse([
'success' => false,
'error' => 'Invalid JSON format'
], Http::STATUS_BAD_REQUEST);
}
$this->saveConfigValue('clients_json', $clientsJson);
// Test connections
$gaConfigured = $this->gaService->isConfigured();
$llmConfigured = $this->llmService->isConfigured();
return new JSONResponse([
'success' => true,
'data' => [
'google_analytics_configured' => $gaConfigured,
'llm_configured' => $llmConfigured,
'message' => 'Configuration saved successfully'
]
]);
} catch (\Exception $e) {
return new JSONResponse([
'success' => false,
'error' => $e->getMessage()
], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
/**
* Get configuration
* GET /settings/load
*/
public function load(IRequest $request): DataResponse {
$config = [
'google_client_id' => $this->getConfigValue('google_client_id'),
'google_client_secret' => $this->getConfigValue('google_client_secret'),
'anthropic_api_key' => $this->getConfigValue('anthropic_api_key'),
'clients_json' => $this->getConfigValue('clients_json'),
];
return new DataResponse([
'success' => true,
'data' => $config
]);
}
/**
* Get app status
* GET /settings/status
*/
public function getStatus(IRequest $request): DataResponse {
$status = [
'app_name' => Application::APP_NAME,
'version' => AppInfo::getVersion(),
'status' => 'operational',
'google_analytics' => $this->gaService->isConfigured() ? 'configured' : 'not_configured',
'llm_service' => $this->llmService->isConfigured() ? 'configured' : 'not_configured',
'total_clients' => $this->gaService->getClientCount(),
'last_report_time' => $this->gaService->getLastReportTime()
];
return new DataResponse([
'success' => true,
'data' => $status
]);
}
/**
* Helper methods
*/
private function saveConfigValue(string $key, string $value): void {
$this->config->setAppValue(Application::APP_NAME, $key, $value);
}
private function getConfigValue(string $key): ?string {
return $this->config->getAppValue(Application::APP_NAME, $key);
}
}