Orbit/app/controllers/admin/pages_controller.rb

203 lines
5.8 KiB
Ruby

class Admin::PagesController < Admin::ItemsController
helper Admin::PagePartsHelper
include Admin::FrontendWidgetInterface
def show
@item = Page.find(params[:id])
respond_to do |format|
format.html {
render 'admin/items/index'
}
format.js {}
end
end
def new
@item = Page.new
@item.parent = Item.find(params[:parent_id]) rescue nil
# @apps = ModuleApp.for_frontend_select
@designs = Design.all.entries
@themes = Design.first.themes
@module_apps = ModuleApp.for_frontend_select
@app_frontend_urls = nil
@categories = nil
@tags = nil
@page_frontend_data_counts = nil
@frontend_styles = nil
@selected={
:design => @designs.first,
:theme=> @themes.first,
:module_app=>nil,#@module_apps.first
:app_frontend_url=> nil, #@module_apps.first
:category=>nil,
:tag=>nil,
:page_frontend_data_count=>nil,
:frontend_style => nil
}
end
def edit
@item = Page.find(params[:id])
@designs = Design.all.entries
@themes = @item.design ? @item.design.themes : @designs.first.themes
@module_apps = ModuleApp.for_frontend_select
@categories = nil
@tags = nil
@page_frontend_data_counts = nil
@frontend_styles = nil
@selected={
:design => @item.design || @designs.first,
:theme=> @item.theme,
:module_app=>@item.module_app,#@module_apps.first
:app_frontend_url=> @item.app_frontend_url, #@module_apps.first
:category=>nil, #fetch by method: get_categories_and_tags
:tag=>nil, #fetch by method: get_categories_and_tags
:page_frontend_data_count=>nil,
:frontend_style => nil
}
@app_frontend_urls = @item.module_app ? select_array_for_app_frontend_urls(@item.module_app.app_pages) : nil
get_categories_and_tags
get_frontend_data_count
end
def reload_themes
design = Design.find(params[:design_id])
@themes = design.themes
respond_to do |format|
format.js {}
end
end
def reload_frontend_pages
@item = Page.find(params[:id]) rescue Page.new
@selected={
:design => nil,
:theme=> nil,
:module_app=>(ModuleApp.find(params[:module_app_id]) rescue nil),
:app_frontend_url=> @item.app_frontend_url || 'default_widget', #@module_apps.first
:category=>nil, #fetch by method: get_categories_and_tags
:tag=>nil, #fetch by method: get_categories_and_tags
:page_frontend_data_count=>nil,
:frontend_style => nil
}
@app_frontend_urls = @selected[:module_app] ? select_array_for_app_frontend_urls(@selected[:module_app].app_pages) : nil
get_categories_and_tags
get_frontend_data_count
get_frontend_styles
respond_to do |format|
format.js {}
end
end
def reload_front_end_setting
@item = Page.find(params[:id]) rescue Page.new
@categories = nil
@tags = nil
@page_frontend_data_counts = nil
@selected={
# :design => @item.design || @designs.first,
# :theme=> @item.theme,
:module_app=>ModuleApp.find(params[:module_app_id]),
:app_frontend_url=> params[:frontend] ,
:category=>nil, #fetch by method: get_categories_and_tags
:tag=>nil, #fetch by method: get_categories_and_tags
:page_frontend_data_count=>nil,
:frontend_style => nil
}
@app_frontend_urls = @selected[:module_app].nil? ? nil : @selected[:module_app].app_pages.each{|name,data| [I18n.t(data["i18n"]),name]}
@frontend_styles = @selected[:module_app].app_pages[params["frontend"]]["style"] rescue nil
end
def reload_widget_field
@index = params[:field_seri].to_i
@item = Page.find(params[:id]) rescue Page.new
@selected = {
:module_app=> (ModuleApp.find(params[:module_app_id]) || @item.module_app ),
:default_widget_field=>params[:widget_field_value]
}
# @choosen_field = params[:widget_field_value]
# @module_app = @item.module_app
# @module_app = ModuleApp.find params[:module_app_id] if @module_app.nil?
end
def create
clean_tags_and_category_params
@item = Page.new(params[:page])
if @item.module_app && @item.module_app.key == 'page_content'
@item.page_contexts.build(:create_user_id => current_user.id, :update_user_id => current_user.id )
end
if @item.save
flash.now[:notice] = t('create.success.page')
respond_to do |format|
format.html {
redirect_to admin_page_url(@item)
}
format.js {}
end
else
flash.now[:error] = t('create.error.page')
@designs = Design.all.entries
@design = Design.first
render :action => "new"
end
end
def update
clean_tags_and_category_params
@item = Page.find(params[:id])
if @item.module_app && @item.module_app.key == 'page_content' && @item.page_contexts.blank?
@item.page_contexts.build(:create_user_id => current_user.id, :update_user_id => current_user.id )
end
if @item.update_attributes(params[:page])
flash[:notice] = t('update.success.page')
respond_to do |format|
format.html {
redirect_to admin_page_url(@item)
}
format.js {}
end
else
render :action => "edit"
end
end
def destroy
@item = Page.find(params[:id])
@item.destroy
respond_to do |format|
format.html {
redirect_to admin_items_url( :parent_id => @item.parent_id )
}
format.js { redirect_to admin_items_url }
end
end
def delete
@item = Page.find(params[:id])
@item.destroy
respond_to do |format|
format.html {}
format.js { @item = @item.parent }
end
end
protected
def get_frontend_styles
has_old_value = @selected[:module_app].app_pages[@selected[:app_frontend_url]]
@frontend_styles = has_old_value ? has_old_value["style"] : (@selected[:module_app].app_pages.first[1]["style"] rescue nil)
end
end