orbit4-5/app/controllers/pages_controller.rb

388 lines
13 KiB
Ruby
Raw Normal View History

# 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-07-09 02:05:02 +00:00
require 'zhconv'
require "uri"
2014-04-01 07:10:21 +00:00
class PagesController < ApplicationController
before_action :get_key
2014-04-01 07:10:21 +00:00
layout :get_layout
include PagesHelper
before_filter :check_frontend_open, :only => [:home,:show]
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)
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
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
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]
2014-06-30 10:08:48 +00:00
s = CGI.unescape(path[-1])
s.encode!('UTF-8')
params[:slug] = s
uid = path[-1].split("-").last
uid = uid.split("?").first
params[:uid] = uid
params[:target_action] = params[:method] || "show"
display_type = "show"
2014-04-01 12:40:03 +00:00
else
params[:page_id] = path[-1]
params[:slug] = nil
params[:uid] = nil
params[:target_action] = params[:method] || "index"
display_type = "index"
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])
if !page.nil?
if !page.enabled_for_mobile && !$mobile.blank?
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
return
end
if page.enabled_for.include? I18n.locale.to_s
module_app = page.module.downcase.pluralize
params[:target_controller] = "#{module_app}"
if display_type == "index"
params[:layout_type] = params[:method] || page.layout || "index"
end
params[:url] = page.url
@manifest = @key
@dataApi = nil
OrbitHelper.set_params params
OrbitHelper.set_site_locale locale
OrbitHelper.set_this_module_app module_app.singularize
OrbitHelper.set_page_categories page.categories || []
2014-07-11 13:12:08 +00:00
OrbitHelper.set_page_tags page.tags || []
OrbitHelper.set_page_data_count page.data_count
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)
render :html => render_final_page("#{module_app}/#{params[:target_action]}",page,layout).html_safe
else
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
end
else
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
end
2014-04-01 12:40:03 +00:00
end
2014-04-01 07:10:21 +00:00
end
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
def destroy
page = Page.find(params[:id])
@parent_pages = page.parent_pages_without_root
page.destroy
respond_to do |format|
format.js
end
end
def get_categories
module_app = ModuleApp.find_by_key(params[:module]);
if module_app.key.eql?("member")
role_status = module_app.role_status.collect do |cat|
{
"title" => "#{cat.role.title} - #{cat.title}",
"id" => cat.id.to_s
}
end
else
categories = module_app.categories.collect do |cat|
{
"title" => cat.title,
"id" => cat.id.to_s
}
end
end
role_status = role_status.nil? ? [] : role_status.sort_by{|status| status['title']}
categories = categories.nil? ? [] : categories
if module_app.data_count.nil?
render :json => {"categories" => categories, "tags" => module_app.tags,"role_status" => role_status,"layouts" => (get_layouts module_app.key),"data_count" => {"present"=>false}}.to_json
else
render :json => {"categories" => categories, "tags" => module_app.tags,"role_status" => role_status,"layouts" => (get_layouts module_app.key),"data_count" => {"present"=>true,"start"=>module_app.data_count.begin, "end" => module_app.data_count.end}}.to_json
end
end
2014-04-01 07:10:21 +00:00
def new
@page = Page.new(
:enabled_for_mobile=>true,
:menu_enabled_for=>["en", "zh_tw"],
:enabled_for=>["en", "zh_tw"],
:enabled_for_sitemap=>["en", "zh_tw"]
)
2014-04-01 07:10:21 +00:00
@pages = Page.where(:page_id.ne => "" , :page_id.exists => true)
@modules = ModuleApp.all.frontend_enabled.order_by(:key=>'asc')
2014-04-01 07:10:21 +00:00
end
def edit
@page = Page.find(params[:id])
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 []
2014-07-11 13:12:08 +00:00
@tags = @module_app.tags rescue []
if @module_app.key.eql?("member")
@status = @module_app.role_status.collect do |cat|
{
"title" => "#{cat.role.title} - #{cat.title}",
"id" => cat.id.to_s
}
end
@status = @status.sort_by{|status| status['title']}
else
@status = []
end
@layout_types = get_layouts @module_app.key
end
end
def update
@page = Page.find(params[:id])
@page.update_attributes(page_update_params)
@page.save
@parent_pages = @page.parent_pages_without_root
respond_to do |format|
format.js
end
end
2014-04-01 07:10:21 +00:00
def create
params['page']['page_id'] = params['page']['page_id'].gsub('-','_')
@page = Page.new(page_params)
2014-04-01 07:10:21 +00:00
@page.save!
@parent_pages = @page.parent_pages_without_root
respond_to do |format|
format.js
end
2014-04-01 07:10:21 +00:00
end
private
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
def render_final_page(original_view=get_view,page,layout)
final_html_for_render = ""
if layout
2014-05-29 10:32:27 +00:00
parts = $mobile.blank? ? (page.page_parts rescue []) : (page.mobile_page_parts rescue [])
@part_partials = {}
2014-05-29 10:32:27 +00:00
parts.each do |part|
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
OrbitHelper.set_widget_title subpart.title
2014-07-11 13:12:08 +00:00
OrbitHelper.set_widget_categories subpart.categories || []
OrbitHelper.set_widget_tags subpart.tags || []
2014-05-29 10:32:27 +00:00
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
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
@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")
else
2014-05-29 10:32:27 +00:00
@file = File.join('../templates', "#{@key}", "/home/page.html.erb")
end
@layout_html = render_to_string(@file)
doc = Nokogiri::HTML(@layout_html, nil, "UTF-8")
2014-04-01 07:10:21 +00:00
@part_partials.each do |key, partial|
html_string = ""
partial.each do |p|
html_string = html_string + p
end
pp = doc.css("*[#{key}]")
if !pp.blank?
pp = pp[0]
pp.inner_html = html_string
end
end
2014-04-01 07:10:21 +00:00
2014-05-29 10:32:27 +00:00
if original_view != "home"
viewarea = doc.css("*[data-content='true']")[0]
viewarea.inner_html = render_to_string(original_view) rescue "<div></div>"
2014-04-01 07:10:21 +00:00
end
link = doc.css("link")[0]
link.attributes["href"].value = current_site.favicon.url.nil? ? "/assets/favicon.ico" : current_site.favicon.url
final_html_for_render = doc.to_html
2014-04-01 07:10:21 +00:00
else
final_html_for_render = render_to_string(original_view) rescue "<div></div>"
2014-04-07 07:57:06 +00:00
end
final_html_for_render = change_to_language(final_html_for_render)
final_html_for_render
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
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
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
page = Page.find(params[:page][:parent_page])
page.url = page.url == "/" ? "" : page.url
@url = page.url + "/#{params[:page][:page_id]}"
2014-07-11 13:12:08 +00:00
p = params.require(:page).permit(:number, :page_type, :page_id, :module, :layout, :parent_page, :data_count, :enabled_for_mobile, enabled_for_sitemap: [], enabled_for: [], menu_enabled_for: [], categories: [], tags: [], name_translations: [:en, :zh_tw],external_url_translations: [:en, :zh_tw])
2014-04-01 07:10:21 +00:00
p["url"] = @url
if p["external_url_translations"]
p["external_url_translations"]["en"] = p["external_url_translations"]["en"].sub("http://" + request.host_with_port,"")
p["external_url_translations"]["zh_tw"] = p["external_url_translations"]["zh_tw"].sub("http://" + request.host_with_port,"")
end
2014-04-01 07:10:21 +00:00
p
end
def page_update_params
2014-07-11 13:12:08 +00:00
p = params.require(:page).permit(:number, :page_type, :page_id, :module, :layout, :parent_page, :data_count, :enabled_for_mobile, enabled_for_sitemap: [], enabled_for: [],menu_enabled_for: [], categories: [], tags: [], name_translations: [:en, :zh_tw], external_url_translations: [:en, :zh_tw])
p["enabled_for"] = p["enabled_for"] || []
p["menu_enabled_for"] = p["menu_enabled_for"] || []
p["enabled_for_mobile"] = p["enabled_for_mobile"] || 0
if p["external_url_translations"]
p["external_url_translations"]["en"] = p["external_url_translations"]["en"].sub("http://" + request.host_with_port,"")
p["external_url_translations"]["zh_tw"] = p["external_url_translations"]["zh_tw"].sub("http://" + request.host_with_port,"")
end
p
end
2014-04-01 07:10:21 +00:00
def get_layout
if request[:action] == "edit_view"
page = Page.find(params[:id])
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
def to_bool(str)
return true if str == "true"
return false if str == "false"
end
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
def check_authorization
if current_user.blank? or !current_user.is_admin?
redirect_to '/admin/dashboards'
end
end
def change_to_language(final_html)
if session[:zh_cn]
final_html = ZhConv.convert("zh-cn", final_html)
final_html.gsub!('/zh_tw/','/zh_cn/')
2014-07-19 19:05:30 +00:00
final_html.sub!('<a href="'+request.path+'">繁体中文</a>','<a href="'+(request.path.sub('/zh_cn/','/zh_tw/'))+'">繁体中文</a>')
end
final_html
end
2014-04-01 07:10:21 +00:00
end