From c78772e2c0f4e5b3968ca8dd2d8334191e2ef531 Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 18 Apr 2021 17:50:30 -0700 Subject: [PATCH] added Sorting class to handle organizing tags and archives organizing for page rendering. added empty Render class for rendering --- brain/api/v1/SettingsAPI.inc.php | 37 +++++---- brain/data/Auth.inc.php | 122 +++++++++++++++--------------- brain/data/Book.inc.php | 2 + brain/data/Render.inc.php | 8 ++ brain/utility/Sorting.inc.php | 83 ++++++++++++++++++++ brain/utility/StringTools.inc.php | 22 ++++++ brain/views/dash/page-edit.twig | 2 +- public/index.php | 6 +- 8 files changed, 203 insertions(+), 79 deletions(-) create mode 100644 brain/data/Render.inc.php create mode 100644 brain/utility/Sorting.inc.php diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index 89d7e09..e78bb61 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -15,25 +15,32 @@ class SettingsAPI case "publish": $view = Twig::fromRequest($request); - $template = "dash/start.twig"; - $pageOptions = [ - "title" => "Welcome to Fucking Fipamo", - "status" => false, - ]; + //$sortTags = Sorting::tags(); + //var_dump($sortTags); + $sortArchive = Sorting::archive(); + var_dump($sortArchive); - $html = $view->fetch($template, $pageOptions); + /* + $template = "dash/start.twig"; + $pageOptions = [ + "title" => "Welcome to Fucking Fipamo", + "status" => false, + ]; - $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); - } + $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); + } + */ $result = [ - "message" => "Site published. GOOD EFFORT", + "message" => "Items sorted. GOOD EFFORT", "type" => "TASK_NONE", ]; diff --git a/brain/data/Auth.inc.php b/brain/data/Auth.inc.php index b93579d..927c5de 100644 --- a/brain/data/Auth.inc.php +++ b/brain/data/Auth.inc.php @@ -1,74 +1,74 @@ secret; - } - public static function status() - { - $result = ""; - if (Session::active()) { - $result = true; - } else { - $result = false; + public static function sessionStatus() + { + if (isset($_SESSION["member"])) { + return true; + } else { + return false; + } + //return $this->secret; } - return $result; - } - public static function login($who) - { - //grab member list - $folks = (new Settings())->getFolks(); - $found = find($folks, ["handle" => $who["handle"]]); - - if ($found) { - //name is found, verify password - if (password_verify($who["password"], $found["password"])) { - $member = [ - "handle" => $found["handle"], - "email" => $found["email"], - "role" => $found["role"], - "avatar" => $found["avi"], - ]; - - $token = Token::create( - $found["id"], - $found["secret"], - time() + 3600, - "localhost" - ); //expires in an hour - Session::start(); - Session::set("member", $member); - Session::set("token", $token); - - $result = "good_login"; - } else { - $result = "bad_pass"; - } - } else { - //if name is not found - $result = "no_name"; + public static function status() + { + $result = ""; + if (Session::active()) { + $result = true; + } else { + $result = false; + } + return $result; } - return $result; - } - public static function logout() - { - Session::kill(); - } + public static function login($who) + { + //grab member list + $folks = (new Settings())->getFolks(); + $found = find($folks, ["handle" => $who["handle"]]); + + if ($found) { + //name is found, verify password + if (password_verify($who["password"], $found["password"])) { + $member = [ + "handle" => $found["handle"], + "email" => $found["email"], + "role" => $found["role"], + "avatar" => $found["avi"], + ]; + + $token = Token::create( + $found["id"], + $found["secret"], + time() + 3600, + "localhost" + ); //expires in an hour + Session::start(); + Session::set("member", $member); + Session::set("token", $token); + + $result = "good_login"; + } else { + $result = "bad_pass"; + } + } else { + //if name is not found + $result = "no_name"; + } + return $result; + } + + public static function logout() + { + Session::kill(); + } } diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index f840f4b..ae94b0a 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -253,6 +253,8 @@ class Book "updated" => date("Y M D d", $meta["updated"]), "rawCreated" => $meta["created"], "rawUpdated" => $meta["updated"], + "createdYear" => date("Y", $meta["created"]), + "createdMonth" => date("m", $meta["created"]), "deleted" => $meta["deleted"], "menu" => $meta["menu"], "featured" => $meta["featured"], diff --git a/brain/data/Render.inc.php b/brain/data/Render.inc.php new file mode 100644 index 0000000..6d4568d --- /dev/null +++ b/brain/data/Render.inc.php @@ -0,0 +1,8 @@ +getContents(); + + foreach ($pages as $page) { + $temp = []; + $temp = explode(",", $page["tags"]); + foreach ($temp as $tag) { + $label = trim($tag); + if (!find(self::$_tags, ["tag_name" => $label])) { + array_push(self::$_tags, [ + "tag_name" => $label, + "slug" => StringTools::safeString($label), + "count" => 1, + ]); + } else { + $item = find(self::$_tags, ["tag_name" => $label]); + //echo "TAG: " . $item["tag_name"] . "\n"; + $count = $item["count"]; + self::$_tags[$label]["count"] = $count + 1; + } + } + } + + return self::$_tags; + } + + public static function archive() + { + $pages = (new Book("../content/pages"))->getContents(); + $years = []; + $archive = []; + foreach ($pages as $page) { + $year = date("Y", date($page["rawCreated"])); + //echo $page["title"] . " : " . $year . "\n"; + if (!find($years, ["year" => $year])) { + $findPages = filter($pages, ["createdYear" => $year]); + //var_dump($findPages); + array_push($years, ["year" => $year, "count" => count($findPages)]); + } + } + foreach ($years as $year) { + $sorted = []; + $filtered = filter($pages, ["createdYear" => $year["year"]]); + + foreach ($filtered as $obj) { + $month = date("m", date($obj["rawCreated"])); + if (!find($sorted, ["month" => $month])) { + $perMonth = filter($pages, [ + "createdYear" => $year["year"], + "createdMonth" => $month, + "deleted" => false, + "published" => true, + "layout" => "page", + ]); + array_push($sorted, [ + "month" => $month, + "full_month" => date("F", date($obj["rawCreated"])), + "count" => count($perMonth), + "pages" => $perMonth, + ]); + } + } + array_push(self::$_archive, [ + "year" => $year["year"], + "year_data" => $sorted, + ]); + } + return self::$_archive; + } +} diff --git a/brain/utility/StringTools.inc.php b/brain/utility/StringTools.inc.php index 0888a7e..58487fb 100644 --- a/brain/utility/StringTools.inc.php +++ b/brain/utility/StringTools.inc.php @@ -23,6 +23,28 @@ class StringTools ); } + public static function safeString($string) + { + return strtolower( + trim( + preg_replace( + "~[^0-9a-z]+~i", + "_", + html_entity_decode( + preg_replace( + "~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i", + '$1', + htmlentities($string, ENT_QUOTES, "UTF-8") + ), + ENT_QUOTES, + "UTF-8" + ) + ), + "-" + ) + ); + } + public static function randomString(int $length) { $alphanum = diff --git a/brain/views/dash/page-edit.twig b/brain/views/dash/page-edit.twig index 29d93d0..4e33c22 100644 --- a/brain/views/dash/page-edit.twig +++ b/brain/views/dash/page-edit.twig @@ -17,7 +17,7 @@ {% set id = '' %} {% set uuid = '' %} {% set slug = '' %} - {% set layout = 'pages' %} + {% set layout = 'page' %} {% set feature = '' %} {% set title = '' %} {% set tags = '' %} diff --git a/public/index.php b/public/index.php index 967134f..5a0ef4e 100644 --- a/public/index.php +++ b/public/index.php @@ -8,12 +8,14 @@ use Slim\Views\Twig; use Slim\Views\TwigMiddleware; include "../brain/controller/RouteControl.inc.php"; -include "../brain/data/Auth.inc.php"; -include "../brain/utility/StringTools.inc.php"; +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/utility/StringTools.inc.php"; include "../brain/utility/FileUploader.inc.php"; include "../brain/utility/DocTools.inc.php"; +include "../brain/utility/Sorting.inc.php"; $app = AppFactory::create(); $twig = Twig::create("../brain/views/");