From c460cb51f332122d5214e7a86246b48ef7c58ad3 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 24 Aug 2016 21:28:22 +0800 Subject: [PATCH] added wiki functionality --- .../admin/page_contents_controller.rb | 4 + .../admin/wiki_pages_controller.rb | 64 +++++++++++++ app/controllers/page_contents_controller.rb | 18 +++- app/models/wiki_page.rb | 13 +++ app/views/admin/page_contents/_index.html.erb | 1 + app/views/admin/page_contents/new.html.erb | 96 +++++++++++++++++-- app/views/admin/wiki_pages/_form.html.erb | 59 ++++++++++++ app/views/admin/wiki_pages/_index.html.erb | 38 ++++++++ app/views/admin/wiki_pages/edit.html.erb | 76 +++++++++++++++ app/views/admin/wiki_pages/index.html.erb | 5 + app/views/admin/wiki_pages/new.html.erb | 76 +++++++++++++++ config/locales/en.yml | 2 + config/locales/zh_tw.yml | 2 + config/routes.rb | 3 + lib/page_content/engine.rb | 2 +- 15 files changed, 446 insertions(+), 13 deletions(-) create mode 100644 app/controllers/admin/wiki_pages_controller.rb create mode 100644 app/models/wiki_page.rb create mode 100644 app/views/admin/wiki_pages/_form.html.erb create mode 100644 app/views/admin/wiki_pages/_index.html.erb create mode 100644 app/views/admin/wiki_pages/edit.html.erb create mode 100644 app/views/admin/wiki_pages/index.html.erb create mode 100644 app/views/admin/wiki_pages/new.html.erb diff --git a/app/controllers/admin/page_contents_controller.rb b/app/controllers/admin/page_contents_controller.rb index ea2fde2..f444fba 100644 --- a/app/controllers/admin/page_contents_controller.rb +++ b/app/controllers/admin/page_contents_controller.rb @@ -23,6 +23,10 @@ class Admin::PageContentsController < OrbitAdminController @page = Page.find(params[:page_id]) if can_edit_or_delete?(@page) @page_content = PageContext.new + @options = {} + @site_in_use_locales.each do |l| + @options[l.to_s] = @page.wiki_pages.collect{|wp|[wp.title_translations[l], wp.uid]} + end else render_401 end diff --git a/app/controllers/admin/wiki_pages_controller.rb b/app/controllers/admin/wiki_pages_controller.rb new file mode 100644 index 0000000..cf4d6da --- /dev/null +++ b/app/controllers/admin/wiki_pages_controller.rb @@ -0,0 +1,64 @@ +class Admin::WikiPagesController < OrbitAdminController + before_action ->(module_app = @app_title) { set_variables module_app } + + def initialize + super + @app_title = "page_content" + end + + def index + @table_fields = [:title, :created_at, "page_content.parent_page", :last_modified] + @filter_fields = {} + @page = Page.find(params[:page_id]) rescue nil + if !@page.nil? + @wikis = @page.wiki_pages.order_by(sort) + @wikis = search_data(@wikis,[:name, :page_id]).page(params[:page]).per(10) + else + @wikis = WikiPage.all.order_by(sort) + @wikis = search_data(@wikis,[:title]).page(params[:page]).per(10) + end + render :partial => "index" if request.xhr? + end + + def create_on_the_go + wiki = WikiPage.create(wiki_page_params) + render :json => {"title" => wiki.title_translations, "id" => wiki.id.to_s}.to_json + end + + def new + @page = Page.find(params[:page_id]) rescue nil + @wiki = WikiPage.new + @options = {} + @site_in_use_locales.each do |l| + @options[l.to_s] = @page.wiki_pages.collect{|wp|[wp.title_translations[l], wp.uid]} + end + end + + def edit + @page = Page.find(params[:page_id]) rescue nil + @wiki = WikiPage.find(params[:id]) + @options = {} + @site_in_use_locales.each do |l| + t = @page.wiki_pages.collect{|wp|[wp.title_translations[l], wp.uid] if wp.id != @wiki.id} + t.delete(nil) + @options[l.to_s] = t + end + end + + def create + wiki = WikiPage.create(wiki_page_params) + redirect_to admin_wiki_pages_path(:page_id => wiki.page.id) + end + + def update + wiki = WikiPage.find(params[:id]) + wiki.update_attributes(wiki_page_params) + redirect_to admin_wiki_pages_path(:page_id => wiki.page.id) + end + + private + + def wiki_page_params + params.require(:wiki_page).permit! + end +end \ No newline at end of file diff --git a/app/controllers/page_contents_controller.rb b/app/controllers/page_contents_controller.rb index 20b64f7..06a9eb6 100644 --- a/app/controllers/page_contents_controller.rb +++ b/app/controllers/page_contents_controller.rb @@ -1,11 +1,21 @@ class PageContentsController < OrbitAdminController def index params = OrbitHelper.params - page = Page.where(:page_id => params[:page_id]).first - url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/page_contents/new?page_id=#{page.id.to_s}" : "" + if params["wiki"].present? + page = WikiPage.where(:uid => params["wiki"]).first + name = page.title rescue "" + html = page.content rescue "" + url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/wiki_pages/#{page.id}/edit?page_id=#{page.page.id.to_s}" : "" + else + page = Page.where(:page_id => params[:page_id]).first + name = page.name rescue "" + html = page.page_contexts.last.content rescue "" + url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/page_contents/new?page_id=#{page.id.to_s}" : "" + end + { - "html" => (page.page_contexts.last.content rescue ""), - "title" => (page.name rescue ""), + "html" => html, + "title" => name, "view_count" => (page.view_count rescue ""), "view-count-head" =>t('page_content.view_count'), "url_to_edit" => url_to_edit diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb new file mode 100644 index 0000000..2a34b0e --- /dev/null +++ b/app/models/wiki_page.rb @@ -0,0 +1,13 @@ +class WikiPage + include Mongoid::Document + include Mongoid::Timestamps + include OrbitModel::Impression + include Slug + + field :update_user_id + field :view_count, :type => Integer, :default => 0 + field :content, localize: true + field :title, localize: true + + belongs_to :page +end \ No newline at end of file diff --git a/app/views/admin/page_contents/_index.html.erb b/app/views/admin/page_contents/_index.html.erb index 3c3db4f..516139c 100644 --- a/app/views/admin/page_contents/_index.html.erb +++ b/app/views/admin/page_contents/_index.html.erb @@ -16,6 +16,7 @@ diff --git a/app/views/admin/page_contents/new.html.erb b/app/views/admin/page_contents/new.html.erb index d9ad743..1fecb02 100644 --- a/app/views/admin/page_contents/new.html.erb +++ b/app/views/admin/page_contents/new.html.erb @@ -15,7 +15,16 @@
<% @site_in_use_locales.each_with_index do |locale, i| %> - +
+
+ <%= label_tag(locale, t("page_content.wiki_pages"), :class=>"control-label muted") %> +
+ <%= select_tag "wikis[#{locale.to_s}]", options_for_select(@options[locale.to_s]), {:prompt => "----Select a WIKI----"} %> + Create + " >Insert +
+
+
<%= f.fields_for :content_translations do |con| %> @@ -30,12 +39,83 @@
<% end %>
-
- <%= f.hidden_field :page_id, :value=>@page.id.to_s %> - <%= f.hidden_field :version, :value=>((@page.page_contexts.last.version + 1) rescue 1)%> - - <%= f.submit t("page_content.save"), :class=> "btn btn-primary bt-form-save" %> -
+
+ <%= f.hidden_field :page_id, :value=>@page.id.to_s %> + <%= f.hidden_field :version, :value=>((@page.page_contexts.last.version + 1) rescue 1)%> + + <%= f.submit t("page_content.save"), :class=> "btn btn-primary bt-form-save" %> +
-<% end %> \ No newline at end of file +<% end %> + + + \ No newline at end of file diff --git a/app/views/admin/wiki_pages/_form.html.erb b/app/views/admin/wiki_pages/_form.html.erb new file mode 100644 index 0000000..a3fe68b --- /dev/null +++ b/app/views/admin/wiki_pages/_form.html.erb @@ -0,0 +1,59 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> +<% end %> + +
+ + +
+ + <% @site_in_use_locales.each_with_index do |locale, i| %> +
+
+ <%= f.fields_for :title_translations do |con| %> + <%= label_tag(locale, t(:title), :class=>"control-label muted") %> +
+ <%= con.text_field locale, :class => "input-block-level", :value => (@wiki.title_translations[locale] rescue nil)%> +
+ <% end %> +
+
+
+
+ <%= label_tag(locale, t("page_content.wiki_pages"), :class=>"control-label muted") %> +
+ <%= select_tag "wikis[#{locale.to_s}]", options_for_select(@options[locale.to_s]), {:prompt => "----Select a WIKI----"} %> + Create + " >Insert +
+
+
+
+
+ <%= f.fields_for :content_translations do |con| %> + <%= label_tag(locale, t(:content), :class=>"control-label muted") %> +
+
+ <%= con.text_area locale, :class => "ckeditor input-block-level", :value => (@wiki.content_translations[locale] rescue nil)%> +
+
+ <% end %> +
+
+ <% end %> +
+ +
+ +
+ <%= f.hidden_field :update_user_id, :value=>current_user.id.to_s %> + <%= f.hidden_field :page_id, :value=>params[:page_id] %> + + <%= f.submit t("page_content.save"), :class=> "btn btn-primary bt-form-save" %> +
diff --git a/app/views/admin/wiki_pages/_index.html.erb b/app/views/admin/wiki_pages/_index.html.erb new file mode 100644 index 0000000..cc221f9 --- /dev/null +++ b/app/views/admin/wiki_pages/_index.html.erb @@ -0,0 +1,38 @@ + + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @wikis.each do |page| %> + + + + + + + <% end %> + +
+ <%= page.title %> +
+ +
+
<%= format_value page.created_at rescue nil %><%= page.page.page_id rescue "" %><%= User.find(page.update_user_id).user_name rescue nil %>
+ + +
+ <%= content_tag :div, paginate(@wikis), class: "pagination pagination-centered" %> +
+ <% if !@page.nil? && can_edit_or_delete?(@page) %> + Add + <% end %> +
+
\ No newline at end of file diff --git a/app/views/admin/wiki_pages/edit.html.erb b/app/views/admin/wiki_pages/edit.html.erb new file mode 100644 index 0000000..8b1ea24 --- /dev/null +++ b/app/views/admin/wiki_pages/edit.html.erb @@ -0,0 +1,76 @@ +<%= form_for @wiki, :url => {:action => :update}, :html => {:class => 'form-horizontal main-forms'} do |f| %> +
+ <%= render :partial => "form", :locals => {:f => f} %> +
+<% end %> + + + \ No newline at end of file diff --git a/app/views/admin/wiki_pages/index.html.erb b/app/views/admin/wiki_pages/index.html.erb new file mode 100644 index 0000000..66bb688 --- /dev/null +++ b/app/views/admin/wiki_pages/index.html.erb @@ -0,0 +1,5 @@ +<%= render_filter @filter_fields, "index_table" %> + + <%= render 'index'%> + + diff --git a/app/views/admin/wiki_pages/new.html.erb b/app/views/admin/wiki_pages/new.html.erb new file mode 100644 index 0000000..7024966 --- /dev/null +++ b/app/views/admin/wiki_pages/new.html.erb @@ -0,0 +1,76 @@ +<%= form_for @wiki, :url => {:action => :create}, :html => {:class => 'form-horizontal main-forms'} do |f| %> +
+ <%= render :partial => "form", :locals => {:f => f} %> +
+<% end %> + + + \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 02f3767..39b001c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,6 +5,8 @@ en: view_count: View count save: Save all: All + wiki: Wiki Pages + parent_page: Parent Page Id create_page_content_success: Page content was successfully created editing_page_content: Editing page content frontend: diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 9e83ce2..1ce6856 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -4,6 +4,8 @@ zh_tw: context: 內文 view_count: 瀏覽人次 save: 送出 + wiki: Wiki Pages + parent_page: Parent Page Id create_page_content_success: 建立頁面內容成功 editing_page_content: 編輯頁面內容 all: All diff --git a/config/routes.rb b/config/routes.rb index 9c581fe..674fa2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,6 +9,9 @@ Rails.application.routes.draw do get "view" => "page_contents#view" end end + resources :wiki_pages + post "/wiki_pages/create_on_the_go", to: 'wiki_pages#create_on_the_go' + post "/page_contents/save_category" => "page_contents#save_category" end end diff --git a/lib/page_content/engine.rb b/lib/page_content/engine.rb index baaa5e6..bec55fe 100644 --- a/lib/page_content/engine.rb +++ b/lib/page_content/engine.rb @@ -11,7 +11,7 @@ module PageContent side_bar do head_label_i18n 'page_content.page', icon_class: "icons-newspaper" available_for "users" - active_for_controllers (['admin/page_contents']) + active_for_controllers (['admin/page_contents', 'admin/wiki_pages']) head_link_path "admin_page_contents_path" context_link 'page_content.all',