]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Favorites in home page
authorLeonardoBizzoni <leo2002714@gmail.com>
Thu, 26 May 2022 15:01:01 +0000 (17:01 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Thu, 26 May 2022 15:01:01 +0000 (17:01 +0200)
www/controllers/SiteController.php
www/views/live.php

index 8f2992a27b3f6923a1800bedc91a201e17466d2b..05226be9c85d84fe1d1b6169c5ce2fb6fe3088e5 100644 (file)
@@ -32,7 +32,7 @@ class SiteController extends BaseController
 
     public function live(Request $req)
     {
-        $params = [];
+        $vtuberLive = [];
         $singleVtuber = false;
         $vtuberModel = new Vtubers;
 
@@ -55,13 +55,22 @@ class SiteController extends BaseController
             $statement->execute();
 
             foreach ($statement->fetchAll() as $vtuber) {
-                $params[] = [$vtuber, $vtuberModel->isLive($vtuber["login"], $vtuber["link"])];
+                $vtuberLive[] = [$vtuber, $vtuberModel->isLive($vtuber["login"], $vtuber["link"])];
                 if ($singleVtuber) {
                     Application::$app->router->title = "WeebSite - " . $vtuber["username"];
                 }
             }
+
+            if (!Application::isGuest()) {
+                $statement = Application::$app->db->pdo->prepare("SELECT _vtuberID FROM favoriteVtuber where _userID=".Application::$app->user->id);
+                $statement->execute();
+
+                foreach ($statement->fetchAll() as $fav) {
+                    $favorites[] = $fav["_vtuberID"];
+                }
+            }
         }
 
-        return $this->render("live", ["model" => $vtuberModel, $params]);
+        return $this->render("live", ["model" => $vtuberModel, $vtuberLive, $favorites]);
     }
 }
index ca90ac31d783143135a704af7e4c8b98629bfbda..561f309d1acf4343dd841a9e844897f91dc77b1d 100644 (file)
@@ -38,6 +38,17 @@ if (count($model->errors) > 0) {
     </div>
 
     <?php
+    if (!Application::isGuest()) {
+        echo "<h2>Favorites</h2>";
+        echo "<ul class='list-group list-group-flush'>";
+        foreach ($params[0] as $idol) {
+            if (gettype($idol[1]) == "string" && in_array($idol[0]["id"], $params[1])) {
+                echo "<li class='list-group-item'><span class='badge bg-danger'>Live</span> <a href='/?id=" . $idol[0]["id"] . "'>" . ucfirst($idol[0]["username"]) . "</a></li>";
+            }
+        }
+        echo "</ul>";
+    }
+
     echo "<h2>Currently live</h2>";
     echo "<ul class='list-group list-group-flush'>";
     foreach ($params[0] as $idol) {
@@ -64,7 +75,7 @@ if (count($model->errors) > 0) {
         <div class=\"container-fluid\">
                 <h1>" . ucfirst($vtuber[0]["username"]) . "</h1>";
 
-            if (!Application::isGuest()) {
+            if (!Application::isGuest() && !in_array($vtuber[0]["id"], $params[1])) {
                 echo "<button id='add' type='button' class='btn btn-outline-light'>Add to favorites</button>";
             }
 
@@ -85,11 +96,14 @@ if (count($model->errors) > 0) {
             </div>
             </div>
             <script>
-                document.getElementById('add').onclick = function() {
+                let btn = document.getElementById('add');
+                btn.onclick = function() {
                     let req = new XMLHttpRequest();
 
                     req.open("get", "/?aggiungi=1&id=" + <?php echo $_GET["id"] ?>, true);
                     req.send();
+
+                    btn.style = "display: none";
                 };
             </script>
 <?php