|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BNETDocs\Controllers\Analytics; |
| 4 | + |
| 5 | +use \BNETDocs\Libraries\Core\HttpCode; |
| 6 | +use \DateTimeImmutable; |
| 7 | +use \DateTimeInterface; |
| 8 | +use \PDOStatement; |
| 9 | + |
| 10 | +class Dashboard extends \BNETDocs\Controllers\Base |
| 11 | +{ |
| 12 | + public function __construct() |
| 13 | + { |
| 14 | + $this->model = new \BNETDocs\Models\Analytics\Dashboard(); |
| 15 | + } |
| 16 | + |
| 17 | + public function invoke(?array $args): bool |
| 18 | + { |
| 19 | + $this->model->acl_allowed = ($this->model->active_user && $this->model->active_user->getOption( |
| 20 | + \BNETDocs\Libraries\User\User::OPTION_ACL_EVENT_LOG_VIEW // TODO : Use correct ACL permission bit |
| 21 | + )); |
| 22 | + |
| 23 | + if (!$this->model->acl_allowed) |
| 24 | + { |
| 25 | + $this->model->_responseCode = HttpCode::HTTP_FORBIDDEN; |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + $this->handleDateRange(); |
| 30 | + $this->getAnalytics(); |
| 31 | + $this->model->_responseCode = HttpCode::HTTP_OK; |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + private function getAnalytics(): void |
| 36 | + { |
| 37 | + $tables = [ |
| 38 | + 'comments' => 'count_comments', |
| 39 | + 'documents' => 'count_documents', |
| 40 | + 'event_log' => 'count_event_log', |
| 41 | + 'news_posts' => 'count_news_posts', |
| 42 | + 'packets' => 'count_packets', |
| 43 | + 'servers' => 'count_servers', |
| 44 | + 'tags' => 'count_tags', |
| 45 | + 'user_profiles' => 'count_user_profiles', |
| 46 | + 'users' => 'count_users', |
| 47 | + ]; |
| 48 | + |
| 49 | + foreach ($tables as $table => $property) |
| 50 | + { |
| 51 | + $this->model->$property = $this->countTableRows($table); |
| 52 | + } |
| 53 | + |
| 54 | + $start = $this->model->date_start; |
| 55 | + $end = $this->model->date_end; |
| 56 | + |
| 57 | + if ($start && $end) |
| 58 | + { |
| 59 | + $timestamp_columns = [ |
| 60 | + 'comments' => 'created_datetime', |
| 61 | + 'documents' => 'created_datetime', |
| 62 | + 'event_log' => 'event_datetime', |
| 63 | + 'news_posts' => 'created_datetime', |
| 64 | + 'packets' => 'created_datetime', |
| 65 | + 'servers' => 'created_datetime', |
| 66 | + 'users' => 'created_datetime', |
| 67 | + // excluded: 'tags', 'user_profiles' |
| 68 | + ]; |
| 69 | + |
| 70 | + foreach ($timestamp_columns as $table => $timestamp_column) |
| 71 | + { |
| 72 | + $property = 'interval_new_' . $table; |
| 73 | + $this->model->$property = $this->countNewRowsInRange($table, $timestamp_column, $start, $end); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private function countNewRowsInRange( |
| 79 | + string $table, string $timestamp_column, DateTimeInterface $start, DateTimeInterface $end |
| 80 | + ): int |
| 81 | + { |
| 82 | + $pdo = \BNETDocs\Libraries\Db\MariaDb::instance(); |
| 83 | + $count = 0; |
| 84 | + |
| 85 | + $query = "SELECT COUNT(*) FROM `{$table}` WHERE `{$timestamp_column}` BETWEEN :start AND :end;"; |
| 86 | + |
| 87 | + $params = [ |
| 88 | + ':start' => $start->format('Y-m-d 00:00:00'), |
| 89 | + ':end' => $end->format('Y-m-d 23:59:59'), |
| 90 | + ]; |
| 91 | + |
| 92 | + try |
| 93 | + { |
| 94 | + $stmt = $pdo->prepare($query); |
| 95 | + if ($stmt && $stmt->execute($params) && $stmt->rowCount() === 1) |
| 96 | + { |
| 97 | + $count = (int) $stmt->fetchColumn(); |
| 98 | + } |
| 99 | + } |
| 100 | + finally |
| 101 | + { |
| 102 | + if (isset($stmt) && $stmt instanceof PDOStatement) $stmt->closeCursor(); |
| 103 | + } |
| 104 | + |
| 105 | + return $count; |
| 106 | + } |
| 107 | + |
| 108 | + private function countTableRows(string $table): int |
| 109 | + { |
| 110 | + $pdo = \BNETDocs\Libraries\Db\MariaDb::instance(); |
| 111 | + $count = 0; |
| 112 | + |
| 113 | + try |
| 114 | + { |
| 115 | + $stmt = $pdo->prepare("SELECT COUNT(*) FROM `{$table}`;"); |
| 116 | + if ($stmt && $stmt->execute() && $stmt->rowCount() === 1) |
| 117 | + { |
| 118 | + $count = (int) $stmt->fetchColumn(); |
| 119 | + } |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + if (isset($stmt) && $stmt instanceof PDOStatement) $stmt->closeCursor(); |
| 124 | + } |
| 125 | + |
| 126 | + return $count; |
| 127 | + } |
| 128 | + |
| 129 | + private function handleDateRange(): void |
| 130 | + { |
| 131 | + $data = \BNETDocs\Libraries\Core\Router::query(); |
| 132 | + |
| 133 | + if (isset($data['date_start']) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $data['date_start'])) |
| 134 | + { |
| 135 | + $this->model->date_start = new DateTimeImmutable($data['date_start']); |
| 136 | + } |
| 137 | + |
| 138 | + if (isset($data['date_end']) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $data['date_end'])) |
| 139 | + { |
| 140 | + $this->model->date_end = new DateTimeImmutable($data['date_end']); |
| 141 | + } |
| 142 | + |
| 143 | + // Fallback: if only one is set, assume the other is today |
| 144 | + if (!$this->model->date_end) |
| 145 | + { |
| 146 | + $this->model->date_end = new DateTimeImmutable('today'); |
| 147 | + } |
| 148 | + |
| 149 | + if (!$this->model->date_start && $this->model->date_end) |
| 150 | + { |
| 151 | + // Default to 30-day window |
| 152 | + $this->model->date_start = $this->model->date_end->sub(new \DateInterval('P30D')); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments