From 40a270059f3a9ed6d812f45079296bfef5c34e45 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 19 Dec 2023 10:14:14 +0100 Subject: [PATCH] feat: :sparkles: pull hierarchical settings dict from post metadata --- www/mysql_adapter.php | 45 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/www/mysql_adapter.php b/www/mysql_adapter.php index b5370e7..bbb5b05 100644 --- a/www/mysql_adapter.php +++ b/www/mysql_adapter.php @@ -108,9 +108,47 @@ class MySQLAdapter { $post_content); } - function get_post_by_path($post_path, $with_subposts = true) { - $qry = "SELECT * FROM posts WHERE post_path = ?"; + function get_settings_for_path($post_path) { + $qry = " + WITH RECURSIVE settings_data (post_path, post_depth, json_settings) AS ( + SELECT post_path, post_path_depth, post_metadata + FROM posts + WHERE post_path = ? + + UNION ALL + + SELECT posts.post_path, posts.post_path_depth, posts.post_metadata + FROM posts, settings_data + WHERE posts.post_path = SUBSTRING_INDEX(settings_data.post_path, '/', settings_data.post_depth) + AND posts.post_path_depth = settings_data.post_depth - 1 + ) + SELECT post_depth, json_settings + FROM settings_data + ORDER BY post_depth ASC; + "; + + $out_settings = []; + + $settings_list = $this->_exec($qry, "s", $post_path)->fetch_all(MYSQLI_ASSOC); + + foreach($settings_list AS $setting) { + $setting = json_decode($setting['json_settings'], true); + + if(!isset($setting) || !isset($setting['settings'])) { + continue; + } + + $out_settings = array_merge($out_settings, $setting['settings']); + } + + return $out_settings; + } + + function get_post_by_path($post_path, + $with_subposts = true, $with_settings = true) { + + $qry = "SELECT * FROM posts WHERE post_path = ?"; $post_path = chop($post_path, '/'); $post_data = $this->_exec($qry, "s", $post_path)->fetch_assoc(); @@ -121,6 +159,9 @@ class MySQLAdapter { if($with_subposts) { $post_data['subposts'] = $this->get_subposts_by_path($post_path); } + if($with_settings) { + $post_data['settings'] = $this->get_settings_for_path($post_path); + } return $post_data; }