announcement-test/app/controllers/bulletins_controller.rb

160 lines
5.4 KiB
Ruby
Raw Normal View History

2015-02-13 06:24:51 +00:00
# encoding: utf-8
2015-09-14 12:04:56 +00:00
class BulletinsController < ApplicationController
2015-02-13 06:24:51 +00:00
before_filter :set_I18n
2022-04-18 15:32:19 +00:00
def annc_depts_translations
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:annc_depts_translations] rescue AnnouncementSetting.first.annc_depts_translations
else
AnnouncementSetting.first.annc_depts_translations rescue {}
end
end
def enable_annc_dept
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:enable_annc_dept] rescue AnnouncementSetting.first.enable_annc_dept
else
AnnouncementSetting.first.enable_annc_dept rescue false
end
end
def manually_sort
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:enable_manually_sort] rescue AnnouncementSetting.first.enable_manually_sort
else
AnnouncementSetting.first.enable_manually_sort rescue false
end
end
2015-02-13 06:24:51 +00:00
def get_bulletins
page = Page.where(:module => "announcement").first rescue nil
2015-02-13 06:24:51 +00:00
# 頁次
page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i
# 每頁顯示的則數
per_page = params[:per_page].blank? ? 10 : params[:per_page].to_i
per_page = per_page > 0 ? per_page : 10
2022-04-18 15:32:19 +00:00
annc_depts = []
tmp_enable_annc_dept = (enable_annc_dept rescue false)
if tmp_enable_annc_dept
annc_depts = annc_depts_translations[locale] rescue []
end
2015-09-14 08:14:32 +00:00
I18n.locale = :zh_tw
2022-09-19 04:01:17 +00:00
keyword = params[:keyword].to_s
if keyword.present?
keyword = Regexp.new(".*"+keyword+".*")
2015-02-13 06:24:51 +00:00
bulletins = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
else
bulletins = Bulletin.all
end
2015-09-14 08:14:32 +00:00
if !params[:category].blank?
module_id = ModuleApp.where(:key=>"announcement").first.id
category = Regexp.new(".*"+params[:category]+".*")
category_id = Category.where(:title => category, :module_app_id => module_id).first.id
bulletins = bulletins.where(:category_id => category_id)
else
bulletins = bulletins
end
2019-10-29 06:48:02 +00:00
2015-02-13 06:24:51 +00:00
bulletins = bulletins.where(:is_preview.in=>[false,nil])
bulletins = bulletins.where(:approved.ne => false , :rejected.ne => true)
bulletins = bulletins.where(:postdate.lt=>Time.now)
2022-04-18 15:32:19 +00:00
if (manually_sort rescue false)
bulletins = bulletins.order({:is_top => -1, :sort_number => 1, :postdate => -1}).page(page_num).per(per_page)
else
bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page)
end
bulletins = bulletins.to_a
2015-02-13 06:24:51 +00:00
bulletins = bulletins.collect do |b|
image = request.protocol + request.host_with_port + b.image.url rescue nil
2019-10-29 06:48:02 +00:00
2015-02-13 06:24:51 +00:00
links = b.bulletin_links.collect do |bl|
{
"title" => bl.title_translations,
"url" => bl.url
}
end rescue nil
files = b.bulletin_files.collect do |bf|
2019-10-29 06:48:02 +00:00
file = request.protocol + request.host_with_port + bf.file.url rescue nil
2015-02-13 06:24:51 +00:00
{
"title" => bf.title_translations,
"description" => bf.description_translations,
"file" => file
}
end rescue nil
ts = b.tags.collect do |t|
{
"name" => t.name_translations
}
end rescue nil
text = {"en" => "", "zh_tw" => ""}
text["en"] = (b.text_translations["en"].nil? ? "" :smart_convertor(b.text_translations["en"]))
text["zh_tw"] = (b.text_translations["zh_tw"].nil? ? "" : smart_convertor(b.text_translations["zh_tw"]))
2022-04-18 15:32:19 +00:00
author = tmp_enable_annc_dept ? annc_depts[b.annc_dept] : User.find(b.create_user_id).member_profile.name rescue ""
2015-02-13 06:24:51 +00:00
{
"id" => b.id.to_s,
"title" => b.title_translations,
"subtitle" => b.subtitle_translations,
"text" => text,
"postdate" => b.postdate,
"deadline" => b.deadline,
"category" => b.category.title_translations,
"tags" => ts,
"image" => image,
"links" => links,
"files" => files,
"author" => author,
"url" => "/#{I18n.locale.to_s + page.url}/#{b.to_param}"
2015-02-13 06:24:51 +00:00
}
end
# 計算總筆數 Start
2022-09-19 04:01:17 +00:00
keyword = params[:keyword].to_s
if keyword.present?
keyword = Regexp.new(".*"+keyword+".*")
bulletin_count = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
else
bulletin_count = Bulletin.all
end
bulletin_count = bulletin_count.where(:is_preview.in=>[false,nil])
bulletin_count = bulletin_count.where(:approved.ne => false , :rejected.ne => true)
bulletin_count = bulletin_count.where(:postdate.lt=>Time.now)
total_pages = bulletin_count.count
# End
2015-02-13 06:24:51 +00:00
render :json => {
"bulletins" => bulletins,
"bulletins_count" => bulletins.count,
"page_num" => page_num,
"total_pages" => total_pages,
2015-02-13 06:24:51 +00:00
}.to_json
end
def smart_convertor(text)
html_string = text
links = html_string.scan(/img.*?src="(.*?)"/i)
2019-10-29 06:48:02 +00:00
links.each do |link|
l = link.first
new_link = nil
if l.starts_with?("/")
new_link = request.protocol + request.host_with_port + l
elsif l.starts_with?("..")
l1 = l.gsub("../","")
new_link = request.protocol + request.host_with_port + "/" + l1
2015-02-13 06:24:51 +00:00
end
2019-10-29 06:48:02 +00:00
html_string = html_string.sub(l,new_link) if !new_link.nil?
end
2015-02-13 06:24:51 +00:00
return html_string
end
protected
def set_I18n
I18n.locale = params[:lang] if params[:lang].present?
end
end