2014-05-05 06:04:50 +00:00
|
|
|
# pass the layout=false for not rendering the layouts and also can specify the methods to in the backend controller.
|
|
|
|
# data-layout-content="arrayname" in layouts can be used to render data in the array
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
class PagesController < ApplicationController
|
2014-05-20 02:31:44 +00:00
|
|
|
before_action :get_key
|
2014-04-01 07:10:21 +00:00
|
|
|
layout :get_layout
|
|
|
|
include PagesHelper
|
2014-05-20 02:31:44 +00:00
|
|
|
before_filter :check_frontend_open, :only => [:home,:show]
|
2014-05-29 04:08:56 +00:00
|
|
|
before_filter :check_authorization, :except => [:home,:show]
|
2014-04-01 07:10:21 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
@pages = Page.all
|
|
|
|
# render json: @pages
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_view
|
|
|
|
@manifest = @key
|
|
|
|
@dataApi = "edit"
|
|
|
|
|
|
|
|
# @qq = render_to_string(:partial => @p, :layout => false)
|
2014-05-22 11:19:25 +00:00
|
|
|
view = get_view
|
|
|
|
if File.exists?(view)
|
|
|
|
render view
|
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
2014-04-07 07:57:06 +00:00
|
|
|
def home
|
|
|
|
@manifest = @key
|
|
|
|
@dataApi = nil
|
2014-04-14 03:27:09 +00:00
|
|
|
page = Page.first
|
2014-05-15 11:32:51 +00:00
|
|
|
impressionist(page)
|
2014-04-07 07:57:06 +00:00
|
|
|
OrbitHelper.set_params params
|
|
|
|
OrbitHelper.set_site_locale locale
|
2014-05-29 10:32:27 +00:00
|
|
|
render :html => render_final_page("home",page,true).html_safe
|
2014-04-07 07:57:06 +00:00
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def show
|
2014-06-06 08:30:56 +00:00
|
|
|
display_type = ""
|
2014-05-28 12:05:39 +00:00
|
|
|
path = request.path.split('/')
|
2014-05-29 03:44:35 +00:00
|
|
|
if path.size <= 2
|
2014-04-01 15:38:17 +00:00
|
|
|
redirect_to root_path
|
2014-04-01 12:40:03 +00:00
|
|
|
else
|
|
|
|
if path.last.include? '-'
|
|
|
|
params[:page_id] = path[-2]
|
|
|
|
params[:slug] = path[-1]
|
2014-05-05 06:04:50 +00:00
|
|
|
uid = path[-1].split("-").last
|
|
|
|
uid = uid.split("?").first
|
|
|
|
params[:uid] = uid
|
|
|
|
params[:target_action] = params[:method] || "show"
|
2014-06-06 08:30:56 +00:00
|
|
|
display_type = "show"
|
2014-04-01 12:40:03 +00:00
|
|
|
else
|
|
|
|
params[:page_id] = path[-1]
|
|
|
|
params[:slug] = nil
|
|
|
|
params[:uid] = nil
|
2014-05-05 06:04:50 +00:00
|
|
|
params[:target_action] = params[:method] || "index"
|
2014-06-06 08:30:56 +00:00
|
|
|
display_type = "index"
|
2014-06-04 12:36:12 +00:00
|
|
|
OrbitHelper.set_page_number params[:page_no].to_i || 1
|
2014-04-01 12:40:03 +00:00
|
|
|
end
|
|
|
|
page = Page.find_by_param(params[:page_id])
|
2014-04-21 07:19:29 +00:00
|
|
|
if !page.nil?
|
|
|
|
if page.enabled_for.include? I18n.locale.to_s
|
|
|
|
module_app = page.module.downcase.pluralize
|
|
|
|
params[:target_controller] = "#{module_app}"
|
2014-06-06 08:30:56 +00:00
|
|
|
if display_type == "index"
|
|
|
|
params[:layout_type] = params[:method] || page.layout || "index"
|
|
|
|
end
|
2014-04-21 07:19:29 +00:00
|
|
|
params[:url] = page.url
|
|
|
|
|
|
|
|
@manifest = @key
|
|
|
|
@dataApi = nil
|
|
|
|
OrbitHelper.set_params params
|
|
|
|
OrbitHelper.set_site_locale locale
|
2014-05-14 11:47:25 +00:00
|
|
|
OrbitHelper.set_this_module_app module_app.singularize
|
2014-04-21 07:19:29 +00:00
|
|
|
OrbitHelper.set_page_categories page.categories || []
|
|
|
|
OrbitHelper.set_page_data_count page.data_count
|
2014-05-05 06:04:50 +00:00
|
|
|
if params[:layout].kind_of?(String)
|
|
|
|
layout = to_bool(params[:layout])
|
|
|
|
else
|
|
|
|
layout = true
|
|
|
|
end
|
|
|
|
|
2014-05-15 11:32:51 +00:00
|
|
|
impressionist(page)
|
2014-05-05 06:04:50 +00:00
|
|
|
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe
|
2014-04-21 07:19:29 +00:00
|
|
|
else
|
2014-05-20 02:31:44 +00:00
|
|
|
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
|
2014-04-21 07:19:29 +00:00
|
|
|
end
|
|
|
|
else
|
2014-05-20 02:31:44 +00:00
|
|
|
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
|
2014-04-21 07:19:29 +00:00
|
|
|
end
|
2014-04-01 12:40:03 +00:00
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
2014-04-21 07:19:29 +00:00
|
|
|
def update_item_position
|
|
|
|
@page = Page.find(params[:id])
|
|
|
|
new_parent_page = Page.find(params[:parent_id])
|
|
|
|
old_parent_page_id = @page.parent_page_id
|
|
|
|
@page.parent_page_id = new_parent_page.id
|
|
|
|
if new_parent_page.id != old_parent_page_id
|
|
|
|
url = (new_parent_page.url == "/" ? "" : new_parent_page.url)
|
|
|
|
@page.url = url + "/#{@page.page_id}"
|
|
|
|
end
|
|
|
|
@page.save
|
|
|
|
params["children_ids"].each_with_index do |child,i|
|
|
|
|
page = Page.find(child)
|
|
|
|
page.number = i
|
|
|
|
page.save
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_parent_page.id != old_parent_page_id
|
|
|
|
old_parent_page = Page.find(old_parent_page_id)
|
|
|
|
|
|
|
|
old_parent_page.child_page.each_with_index do |page,i|
|
|
|
|
page.number = i
|
|
|
|
page.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
render :json => {"success"=>true}.to_json
|
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def preview
|
|
|
|
render render_final_page
|
|
|
|
end
|
|
|
|
|
2014-04-11 11:38:56 +00:00
|
|
|
def destroy
|
|
|
|
page = Page.find(params[:id])
|
|
|
|
page.destroy
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-21 07:19:29 +00:00
|
|
|
def get_categories
|
|
|
|
module_app = ModuleApp.find_by_key(params[:module]);
|
|
|
|
@categories = module_app.categories.collect do |cat|
|
|
|
|
{
|
|
|
|
"title" => cat.title,
|
|
|
|
"id" => cat.id.to_s
|
|
|
|
}
|
|
|
|
end
|
2014-06-06 08:30:56 +00:00
|
|
|
render :json => {"categories" => @categories,"layouts" => (get_layouts module_app.key)}.to_json
|
2014-04-21 07:19:29 +00:00
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
|
|
|
|
def new
|
|
|
|
@page = Page.new
|
|
|
|
@pages = Page.where(:page_id.ne => "" , :page_id.exists => true)
|
2014-05-13 10:41:29 +00:00
|
|
|
@modules = ModuleApp.all.frontend_enabled
|
2014-04-01 07:10:21 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-21 07:19:29 +00:00
|
|
|
def edit
|
|
|
|
@page = Page.find(params[:id])
|
2014-06-09 10:58:44 +00:00
|
|
|
if params[:type] == "page"
|
|
|
|
@pages = Page.where(:page_id.ne => "" , :page_id.exists => true)
|
|
|
|
@modules = ModuleApp.all.frontend_enabled
|
|
|
|
module_app = ModuleApp.find_by_key(@page.module) rescue nil
|
|
|
|
@categories = module_app.categories rescue []
|
|
|
|
@layout_types = get_layouts module_app.key
|
|
|
|
end
|
2014-04-21 07:19:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@page = Page.find(params[:id])
|
|
|
|
@page.update_attributes(page_update_params)
|
|
|
|
@page.save
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def create
|
2014-05-22 09:57:58 +00:00
|
|
|
params['page']['page_id'] = params['page']['page_id'].gsub('-','_')
|
|
|
|
@page = Page.new(page_params)
|
2014-04-01 07:10:21 +00:00
|
|
|
@page.save!
|
2014-04-11 11:38:56 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-06-06 08:30:56 +00:00
|
|
|
def get_layouts(module_app)
|
|
|
|
layout_types = []
|
|
|
|
Dir.glob("#{Rails.root}/app/templates/#{@key}/modules/#{module_app}/*").each do |w|
|
|
|
|
next if File.ftype(w).eql?("directory")
|
|
|
|
w = File.basename(w, ".*")
|
|
|
|
w = File.basename(w, ".*")
|
|
|
|
if w[0,1] != "_" && w[0,1] != "s"
|
|
|
|
layout_types << w
|
|
|
|
end
|
|
|
|
end
|
|
|
|
layout_types
|
|
|
|
end
|
|
|
|
|
2014-05-05 06:04:50 +00:00
|
|
|
def render_final_page(original_view=get_view,page,layout)
|
2014-05-22 11:19:25 +00:00
|
|
|
if layout
|
2014-05-29 10:32:27 +00:00
|
|
|
parts = $mobile.blank? ? (page.page_parts rescue []) : (page.mobile_page_parts rescue [])
|
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
@part_partials = {}
|
|
|
|
|
2014-05-29 10:32:27 +00:00
|
|
|
parts.each do |part|
|
2014-06-04 08:22:48 +00:00
|
|
|
subparts = part.sub_parts.asc(:created_at)
|
2014-05-29 10:32:27 +00:00
|
|
|
partials = []
|
|
|
|
subparts.each do |subpart|
|
|
|
|
if subpart.kind == "module_widget"
|
|
|
|
OrbitHelper.set_widget_data_count subpart.data_count
|
|
|
|
OrbitHelper.set_widget_categories subpart.categories
|
|
|
|
OrbitHelper.set_widget_module_app subpart.module
|
|
|
|
OrbitHelper.set_widget_item_url subpart
|
|
|
|
custom_value = subpart.custom_string_field || subpart.custom_array_field rescue nil
|
|
|
|
if !custom_value.nil?
|
|
|
|
OrbitHelper.set_widget_custom_value custom_value
|
|
|
|
end
|
|
|
|
partials << render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type)
|
|
|
|
elsif subpart.kind == "text"
|
|
|
|
partials << subpart.content
|
2014-05-22 11:19:25 +00:00
|
|
|
end
|
|
|
|
end
|
2014-05-29 10:32:27 +00:00
|
|
|
@part_partials["data-pp='#{part.part_id}'"] = partials
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
2014-05-28 12:05:39 +00:00
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
@file = nil
|
|
|
|
@layout_html = nil
|
2014-05-29 10:32:27 +00:00
|
|
|
if original_view == "home"
|
|
|
|
@file = File.join('../templates', "#{@key}", "/home/index.html.erb")
|
2014-05-22 11:19:25 +00:00
|
|
|
else
|
2014-05-29 10:32:27 +00:00
|
|
|
@file = File.join('../templates', "#{@key}", "/home/page.html.erb")
|
2014-05-22 11:19:25 +00:00
|
|
|
end
|
|
|
|
@layout_html = render_to_string(@file)
|
|
|
|
doc = Nokogiri::HTML(@layout_html, nil, "UTF-8")
|
2014-04-01 07:10:21 +00:00
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
@part_partials.each do |key, partial|
|
|
|
|
html_string = ""
|
|
|
|
partial.each do |p|
|
|
|
|
html_string = html_string + p
|
|
|
|
end
|
|
|
|
pp = doc.css("*[#{key}]")
|
2014-06-04 12:36:12 +00:00
|
|
|
if !pp.blank?
|
|
|
|
pp = pp[0]
|
|
|
|
pp.inner_html = html_string
|
|
|
|
end
|
2014-05-22 11:19:25 +00:00
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
|
2014-05-29 10:32:27 +00:00
|
|
|
if original_view != "home"
|
2014-05-22 11:19:25 +00:00
|
|
|
viewarea = doc.css("*[data-content='true']")[0]
|
|
|
|
viewarea.inner_html = render_to_string(original_view)
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
2014-05-27 10:18:23 +00:00
|
|
|
link = doc.css("link")[0]
|
|
|
|
link.attributes["href"].value = current_site.favicon.url.nil? ? "/assets/favicon.ico" : current_site.favicon.url
|
2014-05-22 11:19:25 +00:00
|
|
|
doc.to_html
|
2014-04-01 07:10:21 +00:00
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
else
|
|
|
|
render_to_string(original_view)
|
2014-04-07 07:57:06 +00:00
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_widget_path(widget)
|
|
|
|
file = File.join('../templates', "#{@key}", "modules/#{widget}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_view
|
2014-05-05 06:04:50 +00:00
|
|
|
page = Page.find(params[:id]) rescue Page.root
|
|
|
|
if page == Page.root
|
2014-05-29 10:32:27 +00:00
|
|
|
@view = File.join(Rails.root, 'app', 'templates', "#{@key}", "home/index.html.erb")
|
2014-04-01 07:10:21 +00:00
|
|
|
else
|
2014-04-15 04:49:38 +00:00
|
|
|
module_name = page.module.downcase.singularize
|
2014-05-29 10:32:27 +00:00
|
|
|
@view = File.join(Rails.root, 'app', 'templates', "#{@key}", "modules/#{module_name}/index.html.erb")
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def page_params
|
2014-05-02 10:19:57 +00:00
|
|
|
page = Page.find(params[:page][:parent_page])
|
|
|
|
page.url = page.url == "/" ? "" : page.url
|
|
|
|
@url = page.url + "/#{params[:page][:page_id]}"
|
2014-06-09 10:58:44 +00:00
|
|
|
p = params.require(:page).permit(:number, :page_type, :page_id, :module, :layout, :parent_page, :data_count, enabled_for_sitemap: [], enabled_for: [], menu_enabled_for: [], categories: [], name_translations: [:en, :zh_tw],external_url_translations: [:en, :zh_tw])
|
2014-04-01 07:10:21 +00:00
|
|
|
p["url"] = @url
|
|
|
|
p
|
|
|
|
end
|
|
|
|
|
2014-04-21 07:19:29 +00:00
|
|
|
def page_update_params
|
2014-06-09 10:58:44 +00:00
|
|
|
p = params.require(:page).permit(:number, :page_type, :page_id, :module, :layout, :parent_page, :data_count, enabled_for_sitemap: [], enabled_for: [],menu_enabled_for: [], categories: [], name_translations: [:en, :zh_tw], external_url_translations: [:en, :zh_tw])
|
2014-04-21 07:19:29 +00:00
|
|
|
p["enabled_for"] = p["enabled_for"] || []
|
|
|
|
p["menu_enabled_for"] = p["menu_enabled_for"] || []
|
|
|
|
p
|
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def get_layout
|
|
|
|
if request[:action] == "edit_view"
|
|
|
|
page = Page.find(params[:id])
|
2014-04-21 08:30:27 +00:00
|
|
|
if page.page_id == "" || page.page_id == nil
|
2014-04-01 07:10:21 +00:00
|
|
|
false
|
|
|
|
else
|
2014-05-29 10:32:27 +00:00
|
|
|
File.join("../../templates", "#{@key}", "/home/page.html.erb")
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
# elsif request[:action] == "show" || request[:action] == "moduleShow"
|
|
|
|
# File.join("../../templates", "themes", "#{@key}", '/home/page.html.erb')
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2014-05-05 06:04:50 +00:00
|
|
|
def to_bool(str)
|
|
|
|
return true if str == "true"
|
|
|
|
return false if str == "false"
|
|
|
|
end
|
2014-05-20 02:31:44 +00:00
|
|
|
|
|
|
|
def check_frontend_open
|
|
|
|
if !current_site.frontend_open && current_user.blank?
|
|
|
|
redirect_to '/admin/dashboards'
|
|
|
|
end
|
|
|
|
end
|
2014-05-28 12:05:39 +00:00
|
|
|
|
2014-05-29 04:08:56 +00:00
|
|
|
def check_authorization
|
|
|
|
if current_user.blank? or !current_user.is_admin?
|
|
|
|
redirect_to '/admin/dashboards'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|