aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/guff/apis.cr3
-rw-r--r--src/guff/model-set.cr1
-rw-r--r--src/guff/models/page.cr41
-rw-r--r--src/guff/models/site.cr1
-rw-r--r--src/guff/models/theme.cr102
-rw-r--r--src/guff/views/admin-page.cr17
-rw-r--r--src/views/admin-page.ecr37
7 files changed, 159 insertions, 43 deletions
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 @@
<div class='row'>
<div class='col-md-6'>
- <label>
- Layout
+ <label for='page-edit-theme'>
+ Theme
</label>
- <div
- id='page-edit-layout'
- class='btn-group btn-group-justified state-buttons'
- >
- <a
- href='#'
- class='btn btn-default'
- title='Use blank layout for this page.'
- data-val='blank'
- >
- <i class='fa fa-file-o'></i>
- Blank
- </a>
-
- <a
- href='#'
- class='btn btn-primary'
- title='Use default layout for this page.'
- data-val='default'
- >
- <i class='fa fa-newspaper-o'></i>
- Default
- </a>
- </div><!-- btn-group -->
+ <select
+ id='page-edit-theme'
+ class='form-control'
+ title='Choose theme for this page.'
+ ><%=
+ theme_options
+ %></select>
<p class='help-block'>
- Layout for this page.
+ Theme for this page.
</p>
</div><!-- col-md-6 -->