web-base/Core/API/VisitorsAPI.class.php

122 lines
3.9 KiB
PHP
Raw Normal View History

2020-07-01 21:10:25 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2020-07-01 21:10:25 +02:00
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2020-07-01 21:10:25 +02:00
2022-02-20 16:53:26 +01:00
abstract class VisitorsAPI extends Request {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false, array $params = []) {
parent::__construct($context, $externalCall, $params);
2022-02-20 16:53:26 +01:00
}
2020-07-01 21:10:25 +02:00
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Visitors {
2020-07-01 21:10:25 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\API\VisitorsAPI;
use Core\Driver\SQL\Expression\Count;
2023-01-16 21:47:23 +01:00
use Core\Driver\SQL\Query\Insert;
use Core\Objects\DatabaseEntity\Group;
2020-07-01 21:10:25 +02:00
use DateTime;
2022-11-18 18:06:46 +01:00
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Expression\Add;
use Core\Driver\SQL\Query\Select;
use Core\Driver\SQL\Strategy\UpdateStrategy;
use Core\Objects\Context;
2020-07-01 21:10:25 +02:00
2021-01-07 16:02:52 +01:00
class ProcessVisit extends VisitorsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
2021-01-07 16:02:52 +01:00
"cookie" => new StringType("cookie")
));
$this->isPublic = false;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2021-01-07 16:02:52 +01:00
$cookie = $this->getParam("cookie");
$day = (new DateTime())->format("Ymd");
$sql->insert("Visitor", array("cookie", "day"))
->addRow($cookie, $day)
->onDuplicateKeyStrategy(new UpdateStrategy(
array("day", "cookie"),
array("count" => new Add("Visitor.count", 1))))
->execute();
return $this->success;
}
}
2020-07-01 21:10:25 +02:00
class Stats extends VisitorsAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, bool $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-07-01 21:10:25 +02:00
'type' => new StringType('type', 32),
'date' => new Parameter('date', Parameter::TYPE_DATE, true, new DateTime())
));
}
2022-02-21 13:01:03 +01:00
private function setConditions(string $type, DateTime $date, Select $query): bool {
2020-07-01 21:10:25 +02:00
if ($type === "yearly") {
$yearStart = $date->format("Y0000");
$yearEnd = $date->modify("+1 year")->format("Y0000");
$query->where(new Compare("day", $yearStart, ">="));
$query->where(new Compare("day", $yearEnd, "<"));
} else if($type === "monthly") {
$monthStart = $date->format("Ym00");
$monthEnd = $date->modify("+1 month")->format("Ym00");
$query->where(new Compare("day", $monthStart, ">="));
$query->where(new Compare("day", $monthEnd, "<"));
} else if($type === "weekly") {
$weekStart = ($date->modify("monday this week"))->format("Ymd");
$weekEnd = ($date->modify("sunday this week"))->format("Ymd");
$query->where(new Compare("day", $weekStart, ">="));
$query->where(new Compare("day", $weekEnd, "<="));
} else {
2022-02-21 13:01:03 +01:00
return $this->createError("Invalid scope: $type");
2020-07-01 21:10:25 +02:00
}
2022-02-21 13:01:03 +01:00
return true;
}
2020-07-01 21:10:25 +02:00
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2020-07-01 21:10:25 +02:00
$date = $this->getParam("date");
$type = $this->getParam("type");
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$query = $sql->select(new Count(), "day")
2020-07-01 21:10:25 +02:00
->from("Visitor")
->whereGt("count", 1)
2020-07-01 21:10:25 +02:00
->groupBy("day")
->orderBy("day")
->ascending();
2022-02-21 13:01:03 +01:00
$this->success = $this->setConditions($type, $date, $query);
2020-07-01 21:10:25 +02:00
if (!$this->success) {
return false;
}
$res = $query->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$this->result["type"] = $type;
$this->result["visitors"] = array();
foreach($res as $row) {
$day = DateTime::createFromFormat("Ymd", $row["day"])->format("Y/m/d");
$count = $row["count"];
$this->result["visitors"][$day] = $count;
}
}
return $this->success;
}
2023-01-16 21:47:23 +01:00
public static function getDefaultACL(Insert $insert): void {
2024-03-27 14:12:01 +01:00
$insert->addRow(self::getEndpoint(), [Group::ADMIN, Group::SUPPORT], "Allows users to view visitor statistics", true);
2023-01-16 21:47:23 +01:00
}
2020-07-01 21:10:25 +02:00
}
}