pagecontent/app/controllers/admin/page_contents_controller.rb

75 lines
2.0 KiB
Ruby

class Admin::PageContentsController < OrbitAdminController
before_action ->(module_app = @app_title) { set_variables module_app }
def index
@table_fields = [:page_id, :name,:version,:update_at,:last_modified, :category]
@filter_fields = {}
if params[:sort].blank? && params[:keywords].blank?
@pages = get_pages_sorted(Page.root)
@pages = Kaminari.paginate_array(@pages).page(params[:page]).per(10)
else
@pages = Page.where(:module=>"page_content").order_by(sort)
@pages = search_data(@pages,[:name, :page_id]).page(params[:page]).per(10)
end
@categories = @module_app.categories.enabled.authorized(current_user).collect{|c| [c.title, c.id]}
render :partial => "index" if request.xhr?
end
def show
@page_context = PageContext.find(params[:id])
end
def new
@page = Page.find(params[:page_id])
if can_edit_or_delete?(@page)
@page_content = PageContext.new
else
render_401
end
end
def create
@page_content = PageContext.new(update_params)
@page_content.update_user_id = current_user.id
@page_content.save
redirect_to params['referer_url']
end
def view
@table_fields = ["Name","Version","Updated At","Last Modified By"]
@page = Page.find(params[:id])
@page_contexts = @page.page_contexts.desc(:version)
end
def save_category
page_id = params[:page_id]
category_id = params[:category_id]
page = Page.find(page_id) rescue nil
if !page.nil?
page.category_id = category_id
page.save
end
render :json => {"success" => true}.to_json
end
private
def update_params
params.require(:page_context).permit!
end
def get_pages_sorted(page)
pages = []
page.child_page.asc(:number).each do |cp|
if cp.child_page.blank? || cp.child_page.nil?
pages << cp if cp.module == "page_content"
else
pages << cp if cp.module == "page_content"
pages.concat(get_pages_sorted(cp))
end
end
pages
end
end