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

247 lines
11 KiB
Ruby

class AnnouncementsController < ApplicationController
def index
params = OrbitHelper.params
announcements = nil
if params[:keywords]
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
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?
announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "搜尋標題有關 ' " + params[:keywords] + " '"
elsif params[:unit]
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
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?
announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "列出由:" + params[:unit] + "發佈的資料"
elsif params[:tag]
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
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]
!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
announcements = Bulletin.all.where(:title.ne => "").can_display.is_approved.order_by(sort).filter_by_categories([category.id.to_s]) if !category.nil?
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
else
sort = ( !params[:sort].blank? ? {params[:sort].to_sym=>params[:order]} : {:is_top=>"desc",:postdate=>"desc"})
# 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"
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)
else
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)
end
end
page = Page.where(:module => "announcement").first rescue nil
ma = ModuleApp.find_by_key("announcement") rescue nil
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
if percent >= 75
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
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
{
"title" => HTMLEntities.new.encode(a.title),
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"link_to_show" => (params[:page_id] == "visitors" ? "/#{I18n.locale.to_s + page.url}/#{a.to_param}" : OrbitHelper.url_to_show(a.to_param)),
"img_src" => a.image.thumb.url || "http://placehold.it/100x100",
"img_description" => a.image_description,
"more" => t(:more_plus)
}
end
{
"announcements" => anns,
"categories" => categories,
"tag-cloud" => tags,
"extras" => {
"tag-cloud-title" => t("announcement.tag_cloud"),
"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
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
anns = announcements.collect do |a|
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
{
"title" => HTMLEntities.new.encode(a.title),
"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'),
"category-head" => t('announcement.table.category'),
"total_pages" => announcements.total_pages
}
}
end
def show
params = OrbitHelper.params
announcement = Bulletin.find_by(:uid=>params[:uid])
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
if !announcement.approved && (access_level != "manager" && access_level != "admin" && access_level != "sub_manager")
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
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
if percent >= 75
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
ann_tags = announcement.tags.map{|tag| {
"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 ""
request = OrbitHelper.request
meta_desc = announcement.subtitle.nil? || announcement.subtitle == "" ? announcement.text[0..200] : announcement.subtitle
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => announcement.title},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => "#{request.base_url}#{announcement.image.url}"},{"property" => "og:type", "content" => "Article"}])
{
"tags" => ann_tags,
"categories" => categories,
"bulletin_files" => files,
"bulletin_links" => links,
"tag-cloud" => tags,
"data" => {
"tag-cloud-title" => t("announcement.tag_cloud"),
"title" => HTMLEntities.new.encode(announcement.title),
"categories-title" => t("announcement.categories"),
"update_user" => update_user,
"updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
"body" =>announcement.text,
"image" => announcement.image.url,
"dept_link" => "/#{I18n.locale.to_s + page.url}/?unit=#{announcement.cache_dept}",
"cache_dept" => announcement.cache_dept,
"view_count" =>announcement.view_count,
"view-count-title" => t("news.view_count"),
"dept-title" => t("announcement.dept_title")
},
"impressionist" => (announcement.is_preview ? nil : announcement),
"url_to_edit"=>url_to_edit
}
else
{}
end
end
end