]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Can add fav vtuber on click
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 9 May 2022 08:24:11 +0000 (10:24 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 9 May 2022 08:24:11 +0000 (10:24 +0200)
dockerfiles/web/nginx.conf
www/Migrations/m_1652080803_favoriteVtuber.php [new file with mode: 0644]
www/models/Vtubers.php
www/pub/js/index.js [new file with mode: 0644]
www/views/live.php

index 4723ea59180f3e036a10df583f050df0423cebd1..9a37f1246ec58c687c48b1901c657645abfff7fe 100644 (file)
@@ -9,8 +9,8 @@ events {
 
 
 http {
-fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MY_CACHE:100m inactive=60m;
-fastcgi_cache_key "$scheme$request_method$host$request_uri";
+fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MY_CACHE:100m inactive=60m;
+fastcgi_cache_key "$scheme$request_method$host$request_uri";
 
      server {
           listen 80 default_server;
@@ -26,10 +26,10 @@ fastcgi_cache_key "$scheme$request_method$host$request_uri";
 
           location ~ \.php$ {
 
-fastcgi_ignore_headers Expires Cache-Control;
-fastcgi_cache MY_CACHE;
-        fastcgi_cache_valid 200 60m;
-        add_header X-Cache $upstream_cache_status;
+fastcgi_ignore_headers Expires Cache-Control;
+fastcgi_cache MY_CACHE;
+        fastcgi_cache_valid 200 60m;
+        add_header X-Cache $upstream_cache_status;
 
                fastcgi_read_timeout 300;
                fastcgi_pass      app:9000;
diff --git a/www/Migrations/m_1652080803_favoriteVtuber.php b/www/Migrations/m_1652080803_favoriteVtuber.php
new file mode 100644 (file)
index 0000000..7f80447
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+use app\core\Application;
+
+class m_1652080803_favoriteVtuber
+{
+    public function up()
+    {
+        $db = Application::$app->db;
+        $sql = "CREATE TABLE favoriteVtuber (
+                id INT AUTO_INCREMENT PRIMARY KEY,
+                _vtuberID INT NOT NULL,
+                _userID INT NOT NULL,
+
+                FOREIGN KEY(_vtuberID) references `vtubers`(`id`),
+                FOREIGN KEY(_userID) references `users`(`id`)
+              ) ENGINE=INNODB;";
+        $db->pdo->exec($sql);
+    }
+
+    public function down()
+    {
+        $db = Application::$app->db;
+        $sql = "DROP TABLE favoriteVtuber;";
+        $db->pdo->exec($sql);
+    }
+}
+?>
index cf1f680eda4560f3d9c79af5df84fce4856960e7..95f2d86447a1ada394b29a4a498d69ade388d5cd 100644 (file)
@@ -93,50 +93,56 @@ class Vtubers extends DbModel
 
     public function isLive(string $login, string $link)
     {
-        if (str_contains($link, "twitch.tv")) {
-            $clientID = Application::$app->config["twitch"]["clientid"] ?? "";
-            $token = Application::$app->config["twitch"]["token"] ?? "";
+        // if (str_contains($link, "twitch.tv")) {
+        //     $clientID = Application::$app->config["twitch"]["clientid"] ?? "";
+        //     $token = Application::$app->config["twitch"]["token"] ?? "";
 
-            $url = "https://api.twitch.tv/helix/streams?user_login=$login";
+        //     $url = "https://api.twitch.tv/helix/streams?user_login=$login";
 
-            $ch = curl_init($url);
-            curl_setopt($ch, CURLOPT_URL, $url);
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Client-ID: $clientID", "Authorization: Bearer $token"));
+        //     $ch = curl_init($url);
+        //     curl_setopt($ch, CURLOPT_URL, $url);
+        //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        //     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Client-ID: $clientID", "Authorization: Bearer $token"));
 
-            $result = get_object_vars(json_decode(curl_exec($ch)));
-            curl_close($ch);
+        //     $result = get_object_vars(json_decode(curl_exec($ch)));
+        //     curl_close($ch);
 
-            return count($result["data"]) ? $result["data"] : [];
-        }
+        //     return count($result["data"]) ? $result["data"] : [];
+        // }
 
-        if (str_contains($link, "youtube.com")) {
-            $url = "https://www.youtube.com/channel/$login/live";
+        // if (str_contains($link, "youtube.com")) {
+        //     $url = "https://www.youtube.com/channel/$login/live";
 
-            $ch = curl_init($url);
-            curl_setopt($ch, CURLOPT_URL, $url);
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        //     $ch = curl_init($url);
+        //     curl_setopt($ch, CURLOPT_URL, $url);
+        //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
-            $result = curl_exec($ch);
-            curl_close($ch);
+        //     $result = curl_exec($ch);
+        //     curl_close($ch);
 
-            $doc = new DOMDocument();
-            libxml_use_internal_errors(true);
-            $doc->loadHTML($result);
+        //     $doc = new DOMDocument();
+        //     libxml_use_internal_errors(true);
+        //     $doc->loadHTML($result);
 
-            $result = $doc->getElementsByTagName("link");
-            $length = $result->length;
+        //     $result = $doc->getElementsByTagName("link");
+        //     $length = $result->length;
 
-            for ($i = 0; $i < $length; $i++) {
-                $tag = $result->item($i)->getAttribute("href");
-                if (str_contains($tag, "https://www.youtube.com/watch?v=")) {
-                    return [str_replace("https://www.youtube.com/watch?v=", "",  $tag)];
-                }
-            }
+        //     for ($i = 0; $i < $length; $i++) {
+        //         $tag = $result->item($i)->getAttribute("href");
+        //         if (str_contains($tag, "https://www.youtube.com/watch?v=")) {
+        //             return [str_replace("https://www.youtube.com/watch?v=", "",  $tag)];
+        //         }
+        //     }
 
-            unset($doc);
-        }
+        //     unset($doc);
+        // }
 
         return [];
     }
+
+    public function addToFav()
+    {
+        $stmt = parent::prepare("INSERT INTO favoriteVtuber(_vtuberID, _userID) values (".$_GET["id"].", ".Application::$app->user->id.")");
+        $stmt->execute();
+    }
 }
diff --git a/www/pub/js/index.js b/www/pub/js/index.js
new file mode 100644 (file)
index 0000000..e69de29
index 8a13a2cf7e64d68522303928d2ddeccd73a39e17..09a4e548c5fff59757a0ad531e9ca5d5e6ae2b0c 100644 (file)
@@ -1,12 +1,40 @@
 <?php
+if (isset($_GET["aggiungi"])) {
+    $model->addToFav();
+    exit;
+}
+
 if (!isset($_GET["id"])) {
     echo "<h1>Live page</h1>";
 
-    $form = app\core\forms\Form::begin("", "post");
-    echo $form->field($model, "Link");
-    echo '<button type="submit" class="btn btn-primary">Submit</button>';
-    app\core\forms\Form::end();
+?>
+
+    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">Add vtuber to the list</button>
+
+    <!-- Modal -->
+    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
+        <div class="modal-dialog">
+            <div class="modal-content">
+                <div class="modal-header">
+                    <h5 class="modal-title" id="exampleModalLabel">Add vtuber to the list</h5>
+                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
+                </div>
+                <div class="modal-body">
+                    <?php
+                    $form = app\core\forms\Form::begin("", "post");
+                    echo $form->field($model, "Link");
+                    ?>
+                </div>
+                <div class="modal-footer">
+                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
+                    <button type="submit" class="btn btn-primary">Submit</button>
+                    <?php app\core\forms\Form::end(); ?>
+                </div>
+            </div>
+        </div>
+    </div>
 
+    <?php
     echo "<h2>Currently live</h2>";
     echo "<ul>";
     foreach ($params[0] as $idol) {
@@ -30,7 +58,14 @@ if (!isset($_GET["id"])) {
     <div>
     <nav class=\"navbar bg-dark text-white\">
         <div class=\"container-fluid\">
-                <h1>" . ucfirst($vtuber[0]["username"]) . "</h1><img class=\"idolLogo\" src=\"" . $vtuber[0]["img"] . "\"/>
+                <h1>" . ucfirst($vtuber[0]["username"]) . "</h1>";
+
+            # TODO: controllo con session se l'utente รจ loggato
+            // echo "<button id='add' class='btn btn-primary' name='add'>Add to favorites</button>";
+            echo "<button id='add'>Add to favorites</button>";
+            # -------------------------------------------------
+
+            echo "<img class=\"idolLogo\" src=\"" . $vtuber[0]["img"] . "\"/>
         </div>
     </nav>
 
@@ -39,12 +74,23 @@ if (!isset($_GET["id"])) {
 <div id=\"child\">";
 
             if (str_contains($vtuber[0]["link"], "twitch.tv")) {
-                echo "<iframe src=\"https://player.twitch.tv/?channel=".$vtuber[0]["login"]."&parent=localhost\" frameborder=\"0\" allowfullscreen=\"true\" scrolling=\"no\"></iframe>";
+                echo "<iframe src=\"https://player.twitch.tv/?channel=" . $vtuber[0]["login"] . "&parent=localhost\" frameborder=\"0\" allowfullscreen=\"true\" scrolling=\"no\"></iframe>";
             } else {
-                echo "<iframe src=\"https://www.youtube.com/embed/".$vtuber[1][0]."\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
+                echo "<iframe src=\"https://www.youtube.com/embed/" . $vtuber[1][0] . "\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
             }
+    ?>
+            </div>
+            </div>
+            <script>
+                document.getElementById('add').onclick = function() {
+                    let req = new XMLHttpRequest();
 
-            echo "</div></div>";
+                    req.open("get", "/live?aggiungi=1&id="+ <?php echo $_GET["id"] ?>, true);
+                    req.send();
+                };
+            </script>
+<?php
         }
     }
 }
+?>