From 9283a7f5b371889fa2b2e78bba1e8e71e8da6a3f Mon Sep 17 00:00:00 2001 From: Ro Date: Wed, 21 Apr 2021 18:41:32 -0700 Subject: [PATCH] tag html rendering implemented --- .gitignore | 1 + brain/api/v1/SettingsAPI.inc.php | 27 ++---------------- brain/controller/IndexControl.inc.php | 19 +++++++++---- brain/data/Book.inc.php | 1 + brain/data/Render.inc.php | 41 +++++++++++++++++++++++++++ brain/data/Settings.inc.php | 6 ++++ brain/utility/Sorting.inc.php | 39 ++++++++++++++++--------- brain/views/dash/start.twig | 2 +- public/index.php | 1 + 9 files changed, 92 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 85257c2..f044de1 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ public/assets/images/* content/ vendor/ cache/ +_temp .ftpconfig .vscode/ *.swp diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index e78bb61..9155a80 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -13,31 +13,8 @@ class SettingsAPI $task = $args["fourth"]; switch ($task) { case "publish": - $view = Twig::fromRequest($request); - - //$sortTags = Sorting::tags(); - //var_dump($sortTags); - $sortArchive = Sorting::archive(); - var_dump($sortArchive); - - /* - $template = "dash/start.twig"; - $pageOptions = [ - "title" => "Welcome to Fucking Fipamo", - "status" => false, - ]; - - $html = $view->fetch($template, $pageOptions); - - $location = "../content/test.html"; - if (!is_file($location)) { - file_put_contents($location, $html); - } else { - ($new = fopen($location, "w")) or die("Unable to open file!"); - fwrite($new, $html); - fclose($new); - } - */ + $render = new Render(); + $render->renderTags(); $result = [ "message" => "Items sorted. GOOD EFFORT", diff --git a/brain/controller/IndexControl.inc.php b/brain/controller/IndexControl.inc.php index d9ad0f1..e26d526 100644 --- a/brain/controller/IndexControl.inc.php +++ b/brain/controller/IndexControl.inc.php @@ -12,11 +12,18 @@ class IndexControl ): ResponseInterface { //unset($_SESSION); $view = Twig::fromRequest($request); - return $view->render($response, "front/start.twig", [ - "title" => "Fipamo Dash", - "status" => false, - "pages" => [], - "totalPages" => 0, - ]); + + $html = file_get_contents("../public/index.html"); + $response->getBody()->write($html); + return $response; + + /* + return $view->render($response, "front/start.twig", [ + "title" => "Fipamo Dash", + "status" => false, + "pages" => [], + "totalPages" => 0, + ]); + */ } } diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index ae94b0a..db08ece 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -150,6 +150,7 @@ class Book //once saved, update menu $body["path"] = $path; Settings::updateMenu($body); + Settings::updateTags(); } else { $response = [ "message" => "Uh oh. File save problem. Don't panic", diff --git a/brain/data/Render.inc.php b/brain/data/Render.inc.php index 6d4568d..7dac5cb 100644 --- a/brain/data/Render.inc.php +++ b/brain/data/Render.inc.php @@ -2,7 +2,48 @@ class Render { + public $loader; + public $twig; + public $pageInfo; + public $menu; + public $background; public function __construct() { + $config = new Settings(); + $this->loader = new \Twig\Loader\FilesystemLoader("../content/themes"); + $this->twig = new \Twig\Environment($this->loader, []); + $settings = $config->getSettings(); + $this->menu = $settings["menu"]; + $this->pageInfo = [ + "keywords" => $settings["global"]["keywords"], + "description" => $settings["global"]["descriptions"], + "image" => $settings["global"]["background"], + ]; + } + + public function renderTags() + { + $list = Sorting::tags(); + foreach ($list as $item) { + $template = "fipamo-default/tags.twig"; + $pageOptions = [ + "title" => "Pages Tagged as " . $item["tag_name"], + "background" => $this->pageInfo["image"], + "tag_list" => $item["pages"], + "info" => $this->pageInfo, + "menu" => $this->menu, + ]; + + $html = $this->twig->render($template, $pageOptions); + + $location = "../public/tags/" . $item["slug"] . ".html"; + if (!is_file($location)) { + file_put_contents($location, $html); + } else { + ($new = fopen($location, "w")) or die("Unable to open file!"); + fwrite($new, $html); + fclose($new); + } + } } } diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index 137a6cb..883ab6a 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -164,4 +164,10 @@ class Settings } DocTools::writeSettings("../config/settings.json", $settings); } + + public static function updateTags() + { + $tags = Sorting::tags(); + DocTools::writeSettings("../config/tags.json", $tags); + } } diff --git a/brain/utility/Sorting.inc.php b/brain/utility/Sorting.inc.php index ca71670..0f47ce4 100644 --- a/brain/utility/Sorting.inc.php +++ b/brain/utility/Sorting.inc.php @@ -13,7 +13,6 @@ class Sorting public static function tags() { $pages = (new Book("../content/pages"))->getContents(); - foreach ($pages as $page) { $temp = []; $temp = explode(",", $page["tags"]); @@ -23,13 +22,8 @@ class Sorting array_push(self::$_tags, [ "tag_name" => $label, "slug" => StringTools::safeString($label), - "count" => 1, + "pages" => self::tagPages($label, $pages), ]); - } else { - $item = find(self::$_tags, ["tag_name" => $label]); - //echo "TAG: " . $item["tag_name"] . "\n"; - $count = $item["count"]; - self::$_tags[$label]["count"] = $count + 1; } } } @@ -37,18 +31,38 @@ class Sorting return self::$_tags; } + private static function tagPages($tag, $pages) + { + $tagged = []; + foreach ($pages as $page) { + if (strpos($page["tags"], $tag) !== false) { + array_push($tagged, [ + "title" => $page["title"], + "slug" => $page["slug"], + "path" => $page["path"], + ]); + } + } + + return $tagged; + } + public static function archive() { $pages = (new Book("../content/pages"))->getContents(); $years = []; $archive = []; foreach ($pages as $page) { - $year = date("Y", date($page["rawCreated"])); + //$year = date("Y", date($page["rawCreated"])); + $date = explode("/", $page["path"]); //echo $page["title"] . " : " . $year . "\n"; - if (!find($years, ["year" => $year])) { - $findPages = filter($pages, ["createdYear" => $year]); + if (!find($years, ["year" => trim($date[0])])) { + $findPages = filter($pages, ["createdYear" => trim($date[0])]); //var_dump($findPages); - array_push($years, ["year" => $year, "count" => count($findPages)]); + array_push($years, [ + "year" => trim($date[0]), + "count" => count($findPages), + ]); } } foreach ($years as $year) { @@ -59,8 +73,7 @@ class Sorting $month = date("m", date($obj["rawCreated"])); if (!find($sorted, ["month" => $month])) { $perMonth = filter($pages, [ - "createdYear" => $year["year"], - "createdMonth" => $month, + "path" => $year["year"] . "/" . $month, "deleted" => false, "published" => true, "layout" => "page", diff --git a/brain/views/dash/start.twig b/brain/views/dash/start.twig index 86cd36a..833090e 100644 --- a/brain/views/dash/start.twig +++ b/brain/views/dash/start.twig @@ -5,7 +5,7 @@ {% endblock %} {% block stylesheets %} - + {% endblock %} {% block mainContent %} diff --git a/public/index.php b/public/index.php index 5a0ef4e..63323b6 100644 --- a/public/index.php +++ b/public/index.php @@ -12,6 +12,7 @@ include "../brain/data/Settings.inc.php"; include "../brain/data/Session.inc.php"; include "../brain/data/Member.inc.php"; include "../brain/data/Auth.inc.php"; +include "../brain/data/Render.inc.php"; include "../brain/utility/StringTools.inc.php"; include "../brain/utility/FileUploader.inc.php"; include "../brain/utility/DocTools.inc.php";