Orbit/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletin_categorys_controll...

210 lines
7.0 KiB
Ruby

class Panel::Announcement::BackEnd::BulletinCategorysController < OrbitBackendController
include OrbitControllerLib::DivisionForDisable
# <<<<<<< HEAD
# before_filter :for_app_manager,:except => [:index,:get_categorys_json]
# before_filter :set_module_app
# def set_module_app
# @module_app = ModuleApp.where(:title=>'Announcement').first
# end
# =======
before_filter :for_app_manager,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
before_filter :force_order_for_visitor,:only=>[:index,:get_categorys_json,:get_bulletins_json]
before_filter :force_order_for_user,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
before_filter :for_app_sub_manager,:except => [:index,:get_categorys_json,:get_bulletins_json, :get_bulletin_categories]
def index
@bulletin_categorys = get_categories_for_index("BulletinCategory")
#TODO 需要做 manager ,admin 才可以 all. 其他 available就好
@bulletin_category = BulletinCategory.new(:display => 'List')
@url = panel_announcement_back_end_bulletin_categorys_path
respond_to do |format|
format.html # index.html.erb
# format.xml { render :xml => @bulletins }
format.js
end
end
def get_categorys_json
categorys = BulletinCategory.all
data = Array.new
categorys.each do |c|
data << {
category: c.title,
# this is a json format
# link: "http://#{request.host_with_port}#{panel_announcement_back_end_bulletin_category_get_bulletins_json_path(c)}"
# remember to uncommand a rule in route.rb, too
link: "#{url_for( :action => "index",
:controller => "panel/announcement/front_end/bulletins",
:format => :rss,
:only_path => false,
:inner=>true,
:category_id => c )}"
}
end
#render :json => @data.to_json
#to print readable json
render :json => JSON.pretty_generate(data)
end
def get_bulletin_categories
I18n.locale = params[:locale] || :zh_tw
@categories = BulletinCategory.all.order_by(:title, :asc)
respond_to do |format|
format.json {
custom_categories = @categories.inject([]) do |data, category|
data << {
title: category.title,
link_html: "#{request.protocol}#{request.host_with_port}#{panel_announcement_front_end_bulletins_path(category_id: category.id, locale: I18n.locale)}",
link_json: "#{request.protocol}#{request.host_with_port}#{get_bulletins_panel_announcement_back_end_bulletins_path(category_id: category.id, locale: I18n.locale, format: 'json')}",
link_rss: "#{request.protocol}#{request.host_with_port}#{get_bulletins_panel_announcement_back_end_bulletins_path(category_id: category.id, locale: I18n.locale, format: 'rss')}"
}
end
render json: JSON.pretty_generate(custom_categories)
}
format.rss {}
end
end
def get_bulletins_json
bulletin = BulletinCategory.find(params[:bulletin_category_id]).bulletins
p bulletin
data = Array.new
bulletin.each do |b|
deadline = b.deadline
if not deadline.nil?
deadline = display_date_time(deadline)
else
deadline = I18n.t("no_deadline")
end
data << {
title: b.title,
link: "http://#{request.host_with_port}#{panel_announcement_front_end_bulletin_path(b, :category_id => b.bulletin_category.id)}",
postdate: display_date_time(b.postdate),
deadline: deadline,
tag: b.sorted_tags.to_a,
}
end
render :json => JSON.pretty_generate(data)
end
# GET /bulletins/1
# GET /bulletins/1.xml
def show
@bulletin_category = BulletinCategory.find(params[:id])
respond_to do |format|
format.html # show.html.erb
# format.xml { render :xml => @bulletin_category }
format.js
end
end
# GET /bulletins/new
# GET /bulletins/new.xml
def new
@bulletin_category = BulletinCategory.new(:display => 'List')
respond_to do |format|
format.html # new.html.erb
# format.xml { render :xml => @bulletin_category }
format.js
end
end
def quick_edit
# debugger
@bulletin_category = BulletinCategory.find(params[:bulletin_category_id])
@url = panel_announcement_back_end_bulletin_category_path(@bulletin_category)
respond_to do |format|
format.js
end
end
# GET /bulletins/1/edit
def edit
@bulletin_category = BulletinCategory.find(params[:id])
@url = panel_announcement_back_end_bulletin_category_path(@bulletin_category)
respond_to do |format|
format.html
format.js
end
end
# POST /bulletins
# POST /bulletins.xml
def create
@bulletin_category = BulletinCategory.new(params[:bulletin_category])
respond_to do |format|
if @bulletin_category.save
# format.html { redirect_to @bulletin_category, notice: 'Entry was successfully created.' }
format.html { redirect_to(panel_announcement_back_end_bulletin_categorys_url, :notice => t('announcement.create_bulletin_category_success')) }
# format.xml { render :xml => @bulletin_category, :status => :created, :location => @bulletin_category }
format.js
else
format.html { render :action => "new" }
# format.xml { render :xml => @bulletin_category.errors, :status => :unprocessable_entity }
format.js { render action: "new" }
end
end
end
# PUT /bulletins/1
# PUT /bulletins/1.xml
def update
@bulletin_category = BulletinCategory.find(params[:id])
@url = panel_announcement_back_end_bulletin_category_path(@bulletin_category)
respond_to do |format|
if @bulletin_category.update_attributes(params[:bulletin_category])
# format.html { redirect_to(panel_announcement_back_end_bulletin_category_url(@bulletin_category), :notice => t('announcement.update_bulletin_category_success')) }
# format.html { redirect_to(panel_announcement_back_end_bulletin_categorys_url, :notice => t('announcement.update_bulletin_category_success')) }
# format.xml { head :ok }
format.js
else
format.html { render :action => "edit" }
format.js { render :action => "edit" }
# format.xml { render :xml => @bulletin_category.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /bulletins/1
# DELETE /bulletins/1.xml
def destroy
@bulletin_category = BulletinCategory.find(params[:id])
@bulletin_category.disable = @bulletin_category.disable ? false : true
if @bulletin_category.save!
respond_to do |format|
flash[:notice] = t("update.success_")
# flash[:error] += @bulletin_category.disable ? t(:enable) : t(disable)
format.html { redirect_to(panel_announcement_back_end_bulletin_categorys_url) }
# format.xml { head :ok }
format.js
end
else
flash[:error] = t("update.fail")
format.html { render :action => "index" }
end
end
end