]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Vtubers live page ability to watch and add
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Apr 2022 17:17:46 +0000 (19:17 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Apr 2022 17:17:46 +0000 (19:17 +0200)
www/Migrations/m_1650287984_vtuberTable.php
www/controllers/SiteController.php
www/core/Application.php
www/core/BaseModel.php
www/core/Router.php
www/models/Vtubers.php
www/pub/index.php
www/views/contact.php [deleted file]
www/views/layouts/live.php [new file with mode: 0644]
www/views/live.php

index 899bca9548507513eda1d86cd3de2dcb70391b53..c4a283966d7692636001cc8ef0e49160c7323e24 100644 (file)
@@ -10,6 +10,8 @@ class m_1650287984_vtuberTable
         $sql = "CREATE TABLE vtubers (
                 id INT AUTO_INCREMENT PRIMARY KEY,
                 username VARCHAR(255) NOT NULL,
+                login VARCHAR(255) NOT NULL,
+                img VARCHAR(512) NOT NULL,
                 link VARCHAR(512) NOT NULL
               ) ENGINE=INNODB;";
         $db->pdo->exec($sql);
@@ -18,7 +20,7 @@ class m_1650287984_vtuberTable
     public function down()
     {
         $db = Application::$app->db;
-        $sql = "DROP TABLE vtuber";
+        $sql = "DROP TABLE vtubers";
         $db->pdo->exec($sql);
     }
 }
index 432a4f083c77cc2fc2c2cb499083af3bfa2c2494..d15cd64ca8332c6599129f150024922d78601376 100644 (file)
@@ -1,12 +1,16 @@
 <?php
+
 namespace app\controllers;
 
+use app\core\Application;
 use app\core\BaseController;
 use app\core\Request;
 use app\models\Vtubers;
 
-class SiteController extends BaseController {
-    public function home() {
+class SiteController extends BaseController
+{
+    public function home()
+    {
         $params = [
             "name" => "Leonardo"
         ];
@@ -14,8 +18,10 @@ class SiteController extends BaseController {
         return $this->render("home", $params);
     }
 
-    public function live(Request $req) {
+    public function live(Request $req)
+    {
         $errors = [];
+        $params = [];
         $vtuberModel = new Vtubers;
 
         if ($req->getMethod() == "post") {
@@ -25,7 +31,15 @@ class SiteController extends BaseController {
             if ($vtuberModel->validate() && $vtuberModel->register()) {
                 return "Success";
             }
+        } else if ($req->getMethod() == "get") {
+            $statement = Application::$app->db->pdo->prepare("SELECT * FROM vtubers;");
+            $statement->execute();
+
+            $params = $statement->fetchAll();
         }
-        return $this->render("live", [ "model" => $vtuberModel ]);
+
+        if (isset($_GET["id"]))
+            $this->setLayout("live");
+        return $this->render("live", ["model" => $vtuberModel, $params]);
     }
 }
index a0abfe32026ac47d90734203be8d1a49e7f39ada..dec27fbb05c0cc9df2b430046e797171c3a08ad8 100644 (file)
@@ -8,6 +8,7 @@ class Application {
     public Request $req;
     public Response $res;
     public Database $db;
+    public array $config;
 
     public static Application $app;
     public static string $ROOT_DIR;
@@ -16,6 +17,7 @@ class Application {
         self::$ROOT_DIR = $root;
         self::$app = $this;
 
+        $this->config = $config;
         $this->req = new Request();
         $this->res = new Response();
         $this->router = new Router($this->req, $this->res);
index 0a6e6d8cc0c631c82e41fe052ddeddad82f248f0..9960ffee9a0c372341057d96f36f56215108e0eb 100644 (file)
@@ -63,7 +63,6 @@ abstract class BaseModel {
             }
         }
 
-        echo " <script>alert('validate fatto!')</script>";
         return empty($this->errors);
     }
 
index 1bda1fc5e5ff9fbb4fd68a950d7b96f8ef32a420..d4932363c316b3e2c0b024ebdf46de8f01936591 100644 (file)
@@ -32,17 +32,19 @@ class Router
         $method = $this->req->getMethod();
         $callback = $this->routes[$method][$path] ?? false;
 
-        if (is_string($callback)) {
+        if (is_callable($callback)) {
+            return call_user_func($callback);
+        } else if (is_string($callback)) {
             return $this->renderView($callback);
         } else if (is_array($callback)) {
             Application::$app->setController(new $callback[0]);
             $callback[0] = Application::$app->getController();
+
+            return call_user_func($callback, $this->req);
         } else {
             $this->res->setStatusCode(404);
             return $this->renderView("404");
         }
-
-        return call_user_func($callback, $this->req);
     }
 
     public function renderView(string $view, array $params = [])
@@ -53,22 +55,23 @@ class Router
         return str_replace("{{content}}", $viewContent, $layoutContent);
     }
 
-    private function loadLayoutContent() {
+    private function loadLayoutContent()
+    {
         $layout = Application::$app->getController()->layout;
         ob_start();
-        include_once Application::$ROOT_DIR."/views/layouts/$layout.php";
+        include_once Application::$ROOT_DIR . "/views/layouts/$layout.php";
         return ob_get_clean();
     }
 
-    private function loadViewContent(string $view, array $params) {
+    private function loadViewContent(string $view, array $params)
+    {
         # modo epico per creare variabili con lo stesso nome assegnato nell'array!!
         foreach ($params as $key => $value) {
             $$key = $value;
         }
 
         ob_start();
-        include_once Application::$ROOT_DIR."/views/$view.php";
+        include_once Application::$ROOT_DIR . "/views/$view.php";
         return ob_get_clean();
     }
 }
-?>
index 894f66de8aa7295e068f82824b8d27d315dd3a05..aeb0db49a4db16e9a2108a4057913518f1c246fe 100644 (file)
@@ -2,11 +2,14 @@
 
 namespace app\models;
 
+use app\core\Application;
 use app\core\DbModel;
 
 class Vtubers extends DbModel
 {
     public string $username = "";
+    public string $login = "";
+    public string $img = "";
     public string $link = "";
 
     public function tableName(): string
@@ -14,8 +17,9 @@ class Vtubers extends DbModel
         return "vtubers";
     }
 
-    public function attributes(): array {
-        return [ "username", "link" ];
+    public function attributes(): array
+    {
+        return ["username", "login", "img", "link"];
     }
 
     public function register()
@@ -27,11 +31,38 @@ class Vtubers extends DbModel
     {
         return [
             "username" => [self::RULE_REQUIRED],
-            "link" => [self::RULE_REQUIRED, [self::RULE_UNIQUE, "class" => self::class ]],
+            "login" => [self::RULE_REQUIRED],
+            "img" => [self::RULE_REQUIRED],
+            "link" => [self::RULE_REQUIRED, [self::RULE_UNIQUE, "class" => self::class]],
         ];
     }
 
-    public function getVtuberName() {
+    public function getVtuberName()
+    {
+        $clientID = Application::$app->config["twitch"]["clientid"] ?? "";
+        $token = Application::$app->config["twitch"]["token"] ?? "";
+
+        if (str_contains($this->link, "twitch.tv")) {
+            $idol = str_replace("https://www.twitch.tv/", "", $this->link);
+            $url = "https://api.twitch.tv/helix/users?login=$idol";
+
+            $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($result["data"][0]);
+
+            $this->username = ucfirst($result["display_name"]);
+            $this->login= $result["login"];
+            $this->img = rtrim($result["profile_image_url"], "/ ");
+            return;
+        }
         $this->username = "i got you bro";
+        $this->login = "i got you bro";
+        $this->img = "i got you bro";
     }
 }
index a877dcefdb018677bbc6b00c14a1b695b33f97f2..eafdda796869931d91206852cc3df9fe84197de0 100644 (file)
@@ -12,6 +12,10 @@ $config = [
         "dsn" => $_ENV["DB_DSN"],
         "user" => $_ENV["DB_USER"],
         "password" => $_ENV["DB_PASSWORD"]
+    ],
+    "twitch" => [
+        "clientid" => $_ENV["TWITCH_CLIENTID"],
+        "token" => $_ENV["TWITCH_TOKEN"]
     ]
 ];
 
@@ -30,4 +34,3 @@ $app->router->get("/register", [AuthController::class, "register"]);
 $app->router->post("/register", [AuthController::class, "register"]);
 
 $app->run();
-?>
diff --git a/www/views/contact.php b/www/views/contact.php
deleted file mode 100644 (file)
index 2df0027..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<h1>Contact page</h1>
-
-<div class="container">
-<form action="" method="post">
-  <div class="mb-3">
-    <label for="exampleInputEmail1" class="form-label">Email address</label>
-    <input name="email" type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
-    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
-  </div>
-  <div class="mb-3">
-    <label for="exampleInputPassword1" class="form-label">Password</label>
-    <input name="pass" type="password" class="form-control" id="exampleInputPassword1">
-  </div>
-  <div class="mb-3 form-check">
-    <input name="check" type="checkbox" class="form-check-input" id="exampleCheck1">
-    <label class="form-check-label" for="exampleCheck1">Check me out</label>
-  </div>
-  <button type="submit" class="btn btn-primary">Submit</button>
-</form>
-</div>
diff --git a/www/views/layouts/live.php b/www/views/layouts/live.php
new file mode 100644 (file)
index 0000000..e3e8a30
--- /dev/null
@@ -0,0 +1,18 @@
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8" />
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
+    <style>
+        img {
+            width: 5%;
+        }
+    </style>
+    <title>WeebSite - Live!</title>
+</head>
+
+<body>
+    {{content}}
+</body>
+
+</html>
index c9186709716dbf4e7c24a7c568a46540aa3b7adc..79b0b814633d556f04898106ecea8981fd281a11 100644 (file)
@@ -1,8 +1,35 @@
-<h1>Live page</h1>
+<?php
+if (!isset($_GET["id"])) {
+    echo "<h1>Live page</h1>";
 
-<div class="container">
-    <?php $form = app\core\forms\Form::begin("", "post"); ?>
-    <?php echo $form->field($model, "Link"); ?>
-    <button type="submit" class="btn btn-primary">Submit</button>
-    <?php app\core\forms\Form::end(); ?>
+    $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();
+
+    echo "<ul>";
+    foreach ($params[0] as $idol) {
+        echo "<li><a href='/live?id=" . $idol["id"] . "'>" . ucfirst($idol["username"]) . "</a></li>";
+    }
+    echo "</ul>";
+} else {
+    foreach ($params[0] as $vtuber) {
+        if ($_GET["id"] == $vtuber["id"]) {
+            echo "
+<div id=\"parent\">
+    <div>
+      <nav class=\"navbar bg-dark text-white\">
+          <div class=\"container-fluid\">
+                  <h1>" . ucfirst($vtuber["username"]) . "</h1><img src='".$vtuber["img"]."'/>
+          </div>
+      </nav>
+    </div>
+</div>
+
+<div id=\"child\">
+<iframe src=\"https://player.twitch.tv/?channel=" . $vtuber["login"] . "&parent=localhost\" frameborder=\"0\" allowfullscreen=\"true\" scrolling=\"no\"></iframe>
 </div>
+";
+        }
+    }
+}