Browse Source

Visitor Count fix

Roman Hergenreder 3 years ago
parent
commit
e046ea7ed9
2 changed files with 31 additions and 9 deletions
  1. 29 0
      core/Api/VisitorsAPI.class.php
  2. 2 9
      core/Objects/User.class.php

+ 29 - 0
core/Api/VisitorsAPI.class.php

@@ -14,9 +14,38 @@ namespace Api\Visitors {
   use Api\VisitorsAPI;
   use DateTime;
   use Driver\SQL\Condition\Compare;
+  use Driver\SQL\Expression\Add;
   use Driver\SQL\Query\Select;
+  use Driver\SQL\Strategy\UpdateStrategy;
   use Objects\User;
 
+  class ProcessVisit extends VisitorsAPI {
+    public function __construct(User $user, bool $externalCall = false) {
+      parent::__construct($user, $externalCall, array(
+        "cookie" => new StringType("cookie")
+      ));
+      $this->isPublic = false;
+    }
+
+    public function execute($values = array()) {
+      if (!parent::execute($values)) {
+        return false;
+      }
+
+      $sql = $this->user->getSQL();
+      $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;
+    }
+  }
+
   class Stats extends VisitorsAPI {
     public function __construct(User $user, bool $externalCall = false) {
       parent::__construct($user, $externalCall, array(

+ 2 - 9
core/Objects/User.class.php

@@ -254,20 +254,13 @@ class User extends ApiObject {
 
   public function processVisit() {
     if ($this->sql && $this->sql->isConnected() && isset($_COOKIE["PHPSESSID"]) && !empty($_COOKIE["PHPSESSID"])) {
-
       if ($this->isBot()) {
         return;
       }
 
       $cookie = $_COOKIE["PHPSESSID"];
-      $day = (new DateTime())->format("Ymd");
-
-      $this->sql->insert("Visitor", array("cookie", "day"))
-        ->addRow($cookie, $day)
-        ->onDuplicateKeyStrategy(new UpdateStrategy(
-          array("month", "cookie"),
-          array("count" => new Add("Visitor.count", 1))))
-        ->execute();
+      $req = new \Api\Visitors\ProcessVisit($this);
+      $req->execute(array("cookie" => $cookie));
     }
   }