Orbit/app/controllers/admin/page_parts_controller.rb

158 lines
4.4 KiB
Ruby

class Admin::PagePartsController < OrbitBackendController
layout "site_editor"
before_filter :authenticate_user!
before_filter :is_admin?
before_filter :set_current_item
helper 'admin/items'
def show
@part = PagePart.find(params[:id])
respond_to do |format|
format.html {
render 'admin/items/index'
}
format.js {}
end
end
def new
end
def edit
@part = PagePart.find(params[:id])
@module_apps = ModuleApp.where(:key.in=>OrbitApp::Module::WidgetUtility.all.keys).order_by(:title, :asc)
@module_app = @part.module_app ? @part.module_app : nil
@user_choose = @part.widget_path
@r_tag = @part.public_r_tag.blank? ? LIST[:public_r_tags][0] : @part.public_r_tag
@tag_objects = @r_tag.classify.constantize.all rescue nil
if @module_app
@widget_path = @part.widget_path ? @part.widget_path : @module_app.widgets.keys[0]
@widget_style = @module_app.get_widget_style[@widget_path][:style] unless @widget_path.nil? or @module_app.nil?
end
unless @module_app.nil?
@categories = @module_app.get_categories
@tags = @module_app.get_tags
end
@widget_path = @part.widget_path ? @part.widget_path : (@module_app.widgets.keys[0] rescue nil)
if @module_app && @module_app.widgets.any?{|b| b.class == Array}
@widget_style = @module_app.widgets[@widget_path] if !@widget_path.blank? && !@module_app.widgets.blank?
end
end
def create
end
def update
@part = PagePart.find(params[:id])
# params[:page_part][:widget_field_type] = nil
# params[:page_part][:widget_field_is_link] = nil
if params[:page_part][:module_app].blank?
params[:page_part][:module_app] = nil
end
if params[:page_part][:tag].nil? || params[:page_part][:tag].include?("nil")
params[:page_part][:tag] = []
end
if params[:page_part][:category].nil? || params[:page_part][:category].include?("nil")
params[:page_part][:category] = []
end
# params[:page_part][:widget_field].each{|t| t[2] = (t[2]=="true" ? true : false )} if !params[:page_part][:widget_field].blank?
if @part.update_attributes(params[:page_part])
set_children_sub_menu(@part) if @part.public_r_tag && @part.public_r_tag.eql?('sub_menu')
flash.now[:notice] = t('update.success.content')
if @part.page.name == 'home'
expire_page '/'
else
expire_page page_path(@part.page.name)
end
respond_to do |format|
format.html {
redirect_to admin_page_url( @part.page )
}
format.js {
@item = @part.page
}
end
else
render :action => "edit"
end
end
def destroy
@item = Page.find(params[:id])
@item.destroy
redirect_to admin_items_url( :parent_id => @item.parent_id )
end
def reload_widget_field
@index = params[:field_seri].to_i
@part = PagePart.find params[:id]
@choosen_field = params[:widget_field_value]
@module_app = @part.module_app
@module_app = ModuleApp.find params[:module_app_id] if @module_app.nil?
end
def reload_widgets
@part = PagePart.find params[:id]
@module_app = ModuleApp.find(params[:module_app_id]) rescue nil
@categories =@module_app ? @module_app.get_categories : []
@tags =@module_app ? @module_app.get_tags : []
@widget_path = @module_app.widgets.keys[0] if (@module_app && @module_app.widgets[0].blank? )
@widget_style = @module_app.widgets[@widget_path] if ( !@widget_path.blank? )
@part.widget_path = @module_app.widgets.first if( @module_app)
respond_to do |format|
format.js {}
end
end
def reload_widget_styles
@module_app = ModuleApp.find(params[:module_app_id])
@part = PagePart.find params[:id]
@user_choose =@widget_path= params[:frontend]
@widget_style = @module_app.widgets[@widget_path]
respond_to do |format|
format.js {}
end
end
def reload_r_tag_options
@part = PagePart.find params[:id]
@r_tag = params[:type]
@tag_objects = @r_tag.classify.constantize.all rescue nil
respond_to do |format|
format.js {}
end
end
protected
def set_children_sub_menu(part)
part.page.children.each do |child|
child_part = child.page_parts.detect{ |x| x.name.eql?(part.name) } rescue nil
child_part.update_attributes(:kind => part.kind, :public_r_tag => part.public_r_tag, :public_r_tag_object_id => part.public_r_tag_object_id) rescue nil
end
end
end