* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved. * @license GNU General Public License version 3 or later; see LICENSE * SPDX-License-Identifier: GPL-3.0-or-later */ namespace Joomla\Component\MokoSuiteCross\Administrator\Controller; defined('_JEXEC') or die; use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\Session\Session; use Joomla\Component\MokoSuiteCross\Administrator\Helper\AnalyticsHelper; class AnalyticsController extends BaseController { /** * Return heatmap grid data as JSON. * * Query params: service_type (string), days (int, default 90) */ public function heatmap(): void { if (!Session::checkToken('get')) { echo json_encode(['success' => false, 'error' => 'Invalid token']); $this->app->close(); return; } $user = $this->app->getIdentity(); if (!$user->authorise('core.manage', 'com_mokosuitecross')) { echo json_encode(['success' => false, 'error' => 'Permission denied']); $this->app->close(); return; } $serviceType = $this->input->getCmd('service_type', ''); $days = $this->input->getInt('days', 90); $grid = AnalyticsHelper::getHeatmapData($serviceType, $days); $bestTimes = AnalyticsHelper::getBestTimes($serviceType, 3); $this->app->setHeader('Content-Type', 'application/json; charset=utf-8'); echo json_encode([ 'success' => true, 'grid' => $grid, 'best_times' => $bestTimes, ]); $this->app->close(); } /** * Return the top posting times as JSON. * * Query params: service_type (string), limit (int, default 5) */ public function besttimes(): void { if (!Session::checkToken('get')) { echo json_encode(['success' => false, 'error' => 'Invalid token']); $this->app->close(); return; } $user = $this->app->getIdentity(); if (!$user->authorise('core.manage', 'com_mokosuitecross')) { echo json_encode(['success' => false, 'error' => 'Permission denied']); $this->app->close(); return; } $serviceType = $this->input->getCmd('service_type', ''); $limit = $this->input->getInt('limit', 5); $bestTimes = AnalyticsHelper::getBestTimes($serviceType, $limit); $serviceBreakdown = AnalyticsHelper::getServiceBreakdown(); $this->app->setHeader('Content-Type', 'application/json; charset=utf-8'); echo json_encode([ 'success' => true, 'best_times' => $bestTimes, 'service_breakdown' => $serviceBreakdown, ]); $this->app->close(); } }