This repository has been archived on 2023-09-12. You can view files and clone it, but cannot push or open issues or pull requests.
nccu-announcement/app/controllers/announcements_controller.rb

242 lines
10 KiB
Ruby
Raw Normal View History

2014-11-07 08:32:00 +00:00
class AnnouncementsController < ApplicationController
def index
params = OrbitHelper.params
announcements = nil
if params[:keywords]
2015-01-09 06:30:21 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2014-12-30 01:49:18 +00:00
announcements = Bulletin.where(:title.ne => "").any_of(:title => /#{params[:keywords].to_s}/i).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:keywords].nil?
2015-03-06 06:44:13 +00:00
announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "搜尋標題有關 ' " + params[:keywords] + " '"
2014-11-07 08:32:00 +00:00
elsif params[:unit]
2015-01-09 06:30:21 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2014-12-30 01:49:18 +00:00
announcements = Bulletin.where(:title.ne => "" ,:cache_dept => params[:unit].to_s).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:unit].nil?
2015-03-06 06:44:13 +00:00
announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "列出由:" + params[:unit] + "發佈的資料"
2014-11-07 11:53:07 +00:00
elsif params[:tag]
2015-01-09 06:30:21 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2015-03-06 01:26:36 +00:00
announcements = Bulletin.where(:title.ne => "" ,:is_preview.in=>[false,nil]).can_display.is_approved.order_by(sort).filter_by_categories(["all"]).filter_by_tags([params[:tag]])
keyword = "搜尋有關 ' " + Tag.where(:id => params[:tag]).first.name + " '"
elsif params[:category]
2015-01-09 06:30:21 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
category = Category.find_by(:uid => params["category"].split("-").last) rescue nil
2015-03-06 01:26:36 +00:00
announcements = Bulletin.all.where(:title.ne => "").can_display.is_approved.order_by(sort).filter_by_categories([category.id.to_s]) if !category.nil?
2015-03-06 06:44:13 +00:00
announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keywords = params[:category].split("-")
keywords.delete(keywords.last)
if keywords.kind_of?(Array)
keyword = "搜尋有關 ' " + keywords.join("-") + " '"
else
keyword = "搜尋有關 ' " + keywords + " '"
end
2014-11-07 08:32:00 +00:00
else
2015-01-09 06:30:21 +00:00
sort = ( !params[:sort].blank? ? {params[:sort].to_sym=>params[:order]} : {:is_top=>"desc",:postdate=>"desc"})
2014-11-11 02:58:22 +00:00
# announcements = Bulletin.where(:title.ne => "" ,:is_preview.in=>[false,nil]).can_display.is_approved.order_by(sort).filter_by_categories.filter_by_tags(OrbitHelper.page_tags)
if OrbitHelper.page_categories.first == "all"
2015-02-24 06:12:16 +00:00
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).can_display.is_approved.order_by(sort).filter_by_tags.page(params[:page_no]).per(OrbitHelper.page_data_count)
2014-11-11 02:58:22 +00:00
else
2015-03-06 01:26:36 +00:00
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).can_display.is_approved.filter_by_categories.filter_by_tags.order_by(sort).page(params[:page_no]).per(OrbitHelper.page_data_count)
2014-11-11 02:58:22 +00:00
end
2014-11-07 08:32:00 +00:00
end
page = Page.where(:module => "announcement").first rescue nil
ma = ModuleApp.find_by_key("announcement") rescue nil
2014-11-07 11:53:07 +00:00
max_tag_count = 0
tags =[]
ma.tags.each do |t|
max_tag_count = (t.taggings.count > max_tag_count ? t.taggings.count : max_tag_count)
tags << {
"tag-id" => t.id.to_s,
"tag-name" => t.name,
"tag-link" => "/#{I18n.locale.to_s + page.url}/?tag=#{t.id.to_s}",
"count" => t.taggings.count
}
end
tags.each do |t|
percent = (t["count"].to_i * 100) / max_tag_count
2014-11-11 02:58:22 +00:00
if percent >= 75
2014-11-07 11:53:07 +00:00
t["tag-class"] = "hot1"
elsif percent >= 50
t["tag-class"] = "hot2"
elsif percent >= 25
t["tag-class"] = "hot3"
else
t["tag-class"] = "hot4"
end
if t["tag-id"] == params[:tag]
t["tag-class"] = t["tag-class"] + " active"
end
end
2014-11-07 08:32:00 +00:00
categories = ma.categories.enabled.collect do |cat|
{
"category-name" => cat.title,
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
}
end
anns = announcements.collect do |a|
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
{
2015-01-13 02:34:56 +00:00
"title" => HTMLEntities.new.encode(a.title),
2014-11-07 08:32:00 +00:00
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
2014-11-11 02:58:22 +00:00
"link_to_show" => (params[:page_id] == "visitors" ? "/#{I18n.locale.to_s + page.url}/#{a.to_param}" : OrbitHelper.url_to_show(a.to_param)),
2014-11-07 08:32:00 +00:00
"img_src" => a.image.thumb.url || "http://placehold.it/100x100",
"img_description" => a.image_description,
"more" => t(:more_plus)
}
end
{
"announcements" => anns,
"categories" => categories,
2014-11-07 11:53:07 +00:00
"tag-cloud" => tags,
2014-11-07 08:32:00 +00:00
"extras" => {
2014-11-07 11:53:07 +00:00
"tag-cloud-title" => t("announcement.tag_cloud"),
2014-11-07 08:32:00 +00:00
"categories-title" => t("announcement.categories"),
"widget-title" =>t('announcement.announcement'),
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"status-head" => t('announcement.table.status'),
"subtitle-head" => t('announcement.table.sub_title'),
"category-head" => t('announcement.table.category'),
"announcements_count" => announcements_count,
"keyword" => keyword
},
"total_pages" => announcements.total_pages
}
end
def widget
2014-11-11 02:58:22 +00:00
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:postdate=>'desc').filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags)
# announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :category_id.in => OrbitHelper.widget_categories).can_display.is_approved.order_by(:postdate=>'desc').filter_by_tags(OrbitHelper.widget_tags).limit(OrbitHelper.widget_data_count)
# params = OrbitHelper.params
# announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :category_id.in => OrbitHelper.widget_categories).can_display.is_approved.order_by(:postdate=>'desc').filter_by_tags(OrbitHelper.widget_tags).page(params[:page_no]).per(OrbitHelper.widget_data_count) rescue nil
2014-11-07 08:32:00 +00:00
anns = announcements.collect do |a|
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
{
2015-01-13 02:34:56 +00:00
"title" => HTMLEntities.new.encode(a.title),
2014-11-07 08:32:00 +00:00
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"link_to_show" => OrbitHelper.widget_item_url(a.to_param),
"img_src" => a.image.thumb.url || "http://placehold.it/100x100",
"img_description" => a.image_description
}
end
{
"announcements" => anns,
"extras" => {
"more_url"=>OrbitHelper.widget_more_url,
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"status-head" => t('announcement.table.status'),
"subtitle-head" => t('announcement.table.sub_title'),
2014-11-11 02:58:22 +00:00
"category-head" => t('announcement.table.category'),
"total_pages" => announcements.total_pages
2014-11-07 08:32:00 +00:00
}
}
end
def show
params = OrbitHelper.params
2015-02-24 06:12:16 +00:00
announcement = Bulletin.find_by(:uid=>params[:uid])
2014-11-07 08:32:00 +00:00
if !announcement.nil?
url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level?
page = Page.where(:module => "announcement").first rescue nil
2015-01-13 01:17:53 +00:00
if !announcement.approved && (access_level != "manager" && access_level != "admin" && access_level != "sub_manager")
2014-11-07 08:32:00 +00:00
return {}
end
page = Page.where(:module => "announcement").first rescue nil
ma = ModuleApp.find_by_key("announcement") rescue nil
categories = ma.categories.enabled.collect do |cat|
{
"category-name" => cat.title,
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
}
end
2014-11-07 11:53:07 +00:00
max_tag_count = 0
tags =[]
ma.tags.each do |t|
max_tag_count = (t.taggings.count > max_tag_count ? t.taggings.count : max_tag_count)
tags << {
"tag-name" => t.name,
"tag-link" => "/#{I18n.locale.to_s + page.url}/?tag=#{t.id.to_s}",
"count" => t.taggings.count
}
end
tags.each do |t|
percent = (t["count"].to_i * 100) / max_tag_count
2014-11-11 02:58:22 +00:00
if percent >= 75
2014-11-07 11:53:07 +00:00
t["tag-class"] = "hot1"
elsif percent >= 50
t["tag-class"] = "hot2"
elsif percent >= 25
t["tag-class"] = "hot3"
else
t["tag-class"] = "hot4"
end
end
2014-11-07 08:32:00 +00:00
2014-11-07 11:53:07 +00:00
ann_tags = announcement.tags.map{|tag| {
2014-11-07 08:32:00 +00:00
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = announcement.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = announcement.update_user.member_profile.name rescue ""
{
2014-11-07 11:53:07 +00:00
"tags" => ann_tags,
2014-11-07 08:32:00 +00:00
"categories" => categories,
"bulletin_files" => files,
"bulletin_links" => links,
2014-11-07 11:53:07 +00:00
"tag-cloud" => tags,
2014-11-07 08:32:00 +00:00
"data" => {
2014-11-07 11:53:07 +00:00
"tag-cloud-title" => t("announcement.tag_cloud"),
2015-01-13 02:34:56 +00:00
"title" => HTMLEntities.new.encode(announcement.title),
2014-11-07 08:32:00 +00:00
"categories-title" => t("announcement.categories"),
"update_user" => update_user,
2014-12-29 08:11:53 +00:00
"updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
2014-11-07 08:32:00 +00:00
"body" =>announcement.text,
"image" => announcement.image.url,
"dept_link" => "/#{I18n.locale.to_s + page.url}/?unit=#{announcement.cache_dept}",
2015-01-13 09:12:08 +00:00
"cache_dept" => announcement.cache_dept,
2015-03-06 01:26:36 +00:00
"view_count" =>announcement.view_count,
"view-count-title" => t("news.view_count"),
"dept-title" => t("announcement.dept_title")
2014-11-07 08:32:00 +00:00
},
"impressionist" => (announcement.is_preview ? nil : announcement),
"url_to_edit"=>url_to_edit
}
else
{}
end
end
end