From 15cccb43865cb8b78bd99253af976ff6366ef097 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 19 Jul 2016 03:41:16 -0400 Subject: add themes, switch pages and sites to use them --- src/guff/apis.cr | 3 +- src/guff/model-set.cr | 1 + src/guff/models/page.cr | 41 ++++++++++------- src/guff/models/site.cr | 1 + src/guff/models/theme.cr | 102 +++++++++++++++++++++++++++++++++++++++++++ src/guff/views/admin-page.cr | 17 ++++++++ src/views/admin-page.ecr | 37 +++++----------- 7 files changed, 159 insertions(+), 43 deletions(-) create mode 100644 src/guff/models/theme.cr (limited to 'src') diff --git a/src/guff/apis.cr b/src/guff/apis.cr index dccc01c..bbedd3f 100644 --- a/src/guff/apis.cr +++ b/src/guff/apis.cr @@ -41,7 +41,8 @@ module Guff::APIs name: params["name"]?, body: params["body"]?, - layout: params["layout"]?, + have_theme_id: true, + theme_id: (params["theme_id"]? && params["theme_id"].size > 0) ? params["theme_id"].to_i32 : nil, ) nil diff --git a/src/guff/model-set.cr b/src/guff/model-set.cr index afc7c5f..cf3c769 100644 --- a/src/guff/model-set.cr +++ b/src/guff/model-set.cr @@ -22,5 +22,6 @@ class Guff::ModelSet role: Models::RoleModel, state: Models::StateModel, file: Models::FileModel, + theme: Models::ThemeModel, }) end diff --git a/src/guff/models/page.cr b/src/guff/models/page.cr index 9be3b3a..0ce8d5b 100644 --- a/src/guff/models/page.cr +++ b/src/guff/models/page.cr @@ -12,8 +12,8 @@ class Guff::Models::PageModel < Guff::Models::Model ON (d.state_id = b.state_id) JOIN users e ON (e.user_id = b.created_by) - JOIn layouts f - ON (f.layout_id = c.layout_id) + LEFT JOIN themes f + ON (f.theme_id = c.theme_id) WHERE a.site_id = ? AND b.slug = ? @@ -30,9 +30,10 @@ class Guff::Models::PageModel < Guff::Models::Model b.slug, b.name, b.body, - f.layout, e.user_id, - e.name AS user_name + e.name AS user_name, + c.theme_id, + a.theme_id AS site_theme_id FROM sites a JOIN posts b @@ -43,8 +44,6 @@ class Guff::Models::PageModel < Guff::Models::Model ON (d.state_id = b.state_id) JOIN users e ON (e.user_id = b.created_by) - JOIn layouts f - ON (f.layout_id = c.layout_id) WHERE a.site_id = ? AND b.slug = ? @@ -59,13 +58,19 @@ class Guff::Models::PageModel < Guff::Models::Model ", add: " - INSERT INTO pages(post_id, layout_id) - VALUES (?, (SELECT layout_id FROM layouts WHERE layout = 'default')) + INSERT INTO pages(post_id) + VALUES (?) ", set: " UPDATE pages - SET layout_id = (SELECT layout_id FROM layouts WHERE layout = ?) + SET theme_id = (SELECT theme_id FROM themes WHERE theme_id = ?) + WHERE post_id = ? + ", + + set_default: " + UPDATE pages + SET theme_id = NULL WHERE post_id = ? ", @@ -79,15 +84,16 @@ class Guff::Models::PageModel < Guff::Models::Model a.slug_lock, a.name, a.body, - d.layout + b.theme_id, + d.theme_id AS site_theme_id FROM posts a JOIN pages b ON (b.post_id = a.post_id) JOIN states c ON (c.state_id = a.state_id) - JOIN layouts d - ON (d.layout_id = b.layout_id) + JOIN sites d + ON (d.site_id = a.site_id) WHERE a.post_id = ? ", @@ -166,7 +172,8 @@ class Guff::Models::PageModel < Guff::Models::Model name : String? = nil, body : String? = nil, - layout : String? = nil, + have_theme_id : Bool = false, + theme_id : Int32? = nil, ) db = @context.dbs.rw @@ -190,8 +197,12 @@ class Guff::Models::PageModel < Guff::Models::Model body: body, ) - if layout - db.query(SQL[:set], [layout, post_id.to_s]) + if have_theme_id + if theme_id + db.query(SQL[:set], [theme_id.to_s, post_id.to_s]) + else + db.query(SQL[:set_default], [post_id.to_s]) + end end end end diff --git a/src/guff/models/site.cr b/src/guff/models/site.cr index 8ab4162..99c09dd 100644 --- a/src/guff/models/site.cr +++ b/src/guff/models/site.cr @@ -38,6 +38,7 @@ class Guff::Models::SiteModel < Guff::Models::Model get_sites: " SELECT site_id, name, + theme_id, is_active, is_default diff --git a/src/guff/models/theme.cr b/src/guff/models/theme.cr new file mode 100644 index 0000000..a8682f7 --- /dev/null +++ b/src/guff/models/theme.cr @@ -0,0 +1,102 @@ +class Guff::Models::ThemeModel < Guff::Models::Model + SQL = { + all: " + SELECT a.theme_id, + a.theme_name, + a.theme_version, + a.theme_date, + a.is_system, + + (SELECT COUNT(*) + FROM sites + WHERE theme_id = a.theme_id) AS num_sites, + (SELECT COUNT(*) + FROM pages + WHERE theme_id = a.theme_id) AS num_pages + + FROM themes a + + ORDER BY LOWER(a.theme_name), a.theme_date + ", + + get_data: " + SELECT a.theme_id, + a.data_key, + a.data_val + + FROM theme_data a + JOIN theme_data_types b + ON (b.type_id = a.type_id) + + WHERE b.name = ? + AND a.theme_id IN (%s) + ", + + get: " + SELECT a.theme_id, + a.theme_name, + a.theme_version, + a.theme_date, + a.is_system, + + (SELECT COUNT(*) + FROM sites + WHERE theme_id = a.theme_id) AS num_sites, + (SELECT COUNT(*) + FROM pages + WHERE theme_id = a.theme_id) AS num_pages + FROM themes a + + ORDER BY LOWER(a.theme_name), a.theme_date + ", + } + + def all(with_metadata : Bool = false) + # get rows + rows = [] of Hash(String, String) + + @context.dbs.ro.all(SQL[:all]) do |row| + rows << row.reduce({} of String => String) do |r, kv| + r[kv[0]] = kv[1].to_s + r + end + end + + if with_metadata + # get metadata lut + lut = get_data("metadata", rows.map { |row| row["theme_id"].to_i }) + + # build and return result + rows.map { |row| + lut[row["theme_id"]]? ? row.merge(lut[row["theme_id"]]) : row + } + else + rows + end + end + + def get(theme_id : Int32) + id = theme_id.to_s + r = @context.dbs.ro.row(SQL[:get], [id]).not_nil! + lut = get_data("metadata", [theme_id]) + lut[id]? ? r.merge(lut[id]) : r + end + + def get_data(type : String, theme_ids : Array(Int32)) + r = {} of String => Hash(String, String) + + if theme_ids.size > 0 + @context.dbs.ro.all(SQL[:get_data] % [ + (["?"] * theme_ids.size).join(",") + ], [type].concat(theme_ids.map { |id| id.to_s })) do |row| + r[row["theme_id"].to_s] ||= {} of String => String + r[row["theme_id"].to_s][ + "%s/%s" % [type, row["data_key"].to_s] + ] = row["data_val"].to_s + end + end + + # return results + r + end +end diff --git a/src/guff/views/admin-page.cr b/src/guff/views/admin-page.cr index 094b5f7..7761994 100644 --- a/src/guff/views/admin-page.cr +++ b/src/guff/views/admin-page.cr @@ -198,5 +198,22 @@ class Guff::Views::AdminPageView < Guff::Views::HTMLView end end + private def theme_options + [{ + "id" => "site-default", + "name" => "Site Default", + }].concat(@context.models.theme.all.map { |row| + { + "id" => row["theme_id"], + "name" => "%s (%s)" % %w{ + name + version + }.map { |k| row["theme_#{k}"] }, + } + }).map { |row| + TEMPLATES[:option] % %w{id name}.map { |k| row[k] } + }.join("") + end + ECR.def_to_s("src/views/admin-page.ecr") end diff --git a/src/views/admin-page.ecr b/src/views/admin-page.ecr index b441014..8581c01 100644 --- a/src/views/admin-page.ecr +++ b/src/views/admin-page.ecr @@ -1185,37 +1185,20 @@
-
-- cgit v1.2.3