From: LeonardoBizzoni Date: Mon, 18 Apr 2022 17:17:46 +0000 (+0200) Subject: Vtubers live page ability to watch and add X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=153c473d5ba80792e6b8eeaa3fabbeb4a6a88eef;p=highschool-graduation-project Vtubers live page ability to watch and add --- diff --git a/www/Migrations/m_1650287984_vtuberTable.php b/www/Migrations/m_1650287984_vtuberTable.php index 899bca9..c4a2839 100644 --- a/www/Migrations/m_1650287984_vtuberTable.php +++ b/www/Migrations/m_1650287984_vtuberTable.php @@ -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); } } diff --git a/www/controllers/SiteController.php b/www/controllers/SiteController.php index 432a4f0..d15cd64 100644 --- a/www/controllers/SiteController.php +++ b/www/controllers/SiteController.php @@ -1,12 +1,16 @@ "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]); } } diff --git a/www/core/Application.php b/www/core/Application.php index a0abfe3..dec27fb 100644 --- a/www/core/Application.php +++ b/www/core/Application.php @@ -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); diff --git a/www/core/BaseModel.php b/www/core/BaseModel.php index 0a6e6d8..9960ffe 100644 --- a/www/core/BaseModel.php +++ b/www/core/BaseModel.php @@ -63,7 +63,6 @@ abstract class BaseModel { } } - echo " "; return empty($this->errors); } diff --git a/www/core/Router.php b/www/core/Router.php index 1bda1fc..d493236 100644 --- a/www/core/Router.php +++ b/www/core/Router.php @@ -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(); } } -?> diff --git a/www/models/Vtubers.php b/www/models/Vtubers.php index 894f66d..aeb0db4 100644 --- a/www/models/Vtubers.php +++ b/www/models/Vtubers.php @@ -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"; } } diff --git a/www/pub/index.php b/www/pub/index.php index a877dce..eafdda7 100644 --- a/www/pub/index.php +++ b/www/pub/index.php @@ -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 index 2df0027..0000000 --- a/www/views/contact.php +++ /dev/null @@ -1,20 +0,0 @@ -

Contact page

- -
-
-
- - -
We'll never share your email with anyone else.
-
-
- - -
-
- - -
- -
-
diff --git a/www/views/layouts/live.php b/www/views/layouts/live.php new file mode 100644 index 0000000..e3e8a30 --- /dev/null +++ b/www/views/layouts/live.php @@ -0,0 +1,18 @@ + + + + + + + WeebSite - Live! + + + + {{content}} + + + diff --git a/www/views/live.php b/www/views/live.php index c918670..79b0b81 100644 --- a/www/views/live.php +++ b/www/views/live.php @@ -1,8 +1,35 @@ -

Live page

+Live page"; -
- - field($model, "Link"); ?> - - + $form = app\core\forms\Form::begin("", "post"); + echo $form->field($model, "Link"); + echo ''; + app\core\forms\Form::end(); + + echo ""; +} else { + foreach ($params[0] as $vtuber) { + if ($_GET["id"] == $vtuber["id"]) { + echo " +
+
+ +
+
+ +
+
+"; + } + } +}