nccu-news/app/controllers/news_controller.rb

230 lines
9.7 KiB
Ruby
Raw Normal View History

2014-11-07 08:44:11 +00:00
class NewsController < ApplicationController
def index
params = OrbitHelper.params
news = nil
if params[:keywords]
2015-01-09 06:29:00 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2015-02-24 06:13:42 +00:00
news = NewsBulletin.where(:title.ne => "").any_of(:title => /#{params[:keywords].to_s}/i).can_display_postdate.is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:keywords].nil?
2015-03-06 06:45:17 +00:00
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
keyword = "搜尋標題有關 ' " + params[:keywords] + " '"
2014-11-10 02:03:21 +00:00
elsif params[:department]
2015-01-09 06:29:00 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2014-11-07 08:44:11 +00:00
dept = Department.find_by(:uid => params["department"].split("-").last) rescue nil
2015-02-24 06:13:42 +00:00
news = NewsBulletin.where(:title.ne => "" ,:department => dept).can_display_postdate.is_approved.order_by(sort).filter_by_categories(["all"]) if !dept.nil?
2015-03-06 06:45:17 +00:00
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
2014-11-10 02:03:21 +00:00
keywords = params[:department].split("-")
keywords.delete(keywords.last)
if keywords.kind_of?(Array)
keyword = "列出由:" + keywords.join("-") + "發佈的資料"
2014-11-10 02:03:21 +00:00
else
keyword = "列出由:" + keywords + "發佈的資料"
2014-11-10 02:03:21 +00:00
end
elsif params[:unit]
2014-11-07 08:44:11 +00:00
unit = Unit.find_by(:uid => params["unit"].split("-").last) rescue nil
2015-01-09 06:29:00 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2015-02-24 06:13:42 +00:00
news = NewsBulletin.where(:title.ne => "" ,:unit => unit).can_display_postdate.is_approved.order_by(sort).filter_by_categories(["all"]) if !unit.nil?
2015-03-06 06:45:17 +00:00
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
2014-11-10 02:03:21 +00:00
keywords = params[:unit].split("-")
keywords.delete(keywords.last)
if keywords.kind_of?(Array)
keyword = "列出由:" + keywords.join("-") + "發佈的資料"
2014-11-10 02:03:21 +00:00
else
keyword = "列出由:" + keywords + "發佈的資料"
2014-11-10 02:03:21 +00:00
end
2014-11-07 08:44:11 +00:00
elsif params["category"]
2015-01-09 06:29:00 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2014-11-07 08:44:11 +00:00
category = Category.find_by(:uid => params["category"].split("-").last) rescue nil
2015-02-24 06:13:42 +00:00
news = NewsBulletin.all.where(:title.ne => "").can_display_postdate.is_approved.order_by(sort).filter_by_categories([category.id.to_s]) if !category.nil?
2015-03-06 06:45:17 +00:00
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
2014-11-10 02:03:21 +00:00
keywords = params[:category].split("-")
keywords.delete(keywords.last)
if keywords.kind_of?(Array)
keyword = "搜尋有關 ' " + keywords.join("-") + " '"
2014-11-10 02:03:21 +00:00
else
keyword = "搜尋有關 ' " + keywords + " '"
2014-11-10 02:03:21 +00:00
end
2014-11-07 08:44:11 +00:00
end
2015-01-09 06:29:00 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
2015-02-24 06:13:42 +00:00
news = NewsBulletin.where(:title.ne => "" ,:is_preview.in=>[false,nil]).can_display_postdate.is_approved.order_by(sort).filter_by_categories.filter_by_tags(OrbitHelper.params['tags']) if news.nil?
2014-11-07 08:44:11 +00:00
page = Page.where(:module => "news").first rescue nil
2014-11-10 02:03:21 +00:00
ma = ModuleApp.find_by_key("news") rescue nil
2014-11-07 08:44:11 +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
2015-02-24 06:13:42 +00:00
2014-11-07 08:44:11 +00:00
anns = news.collect do |a|
2015-03-06 01:31:21 +00:00
if a.image_description.blank?
2015-01-12 06:40:36 +00:00
image_description = "裝飾性圖片"
else
image_description = a.image_description
end
2015-03-06 01:31:21 +00:00
2014-11-07 08:44:11 +00:00
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
{
2015-01-13 02:35:42 +00:00
"title" => HTMLEntities.new.encode(a.title),
2014-11-07 08:44:11 +00:00
"subtitle" => a.subtitle,
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate.strftime('%Y-%m-%d'),
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
2014-11-11 08:31:16 +00:00
"img_src" => a.image.thumb.url || "/assets/news_default_image.jpg",
2015-01-12 06:40:36 +00:00
"img_description" => image_description,
2014-11-07 08:44:11 +00:00
"more" => t(:more_plus)
}
end
{
"news" => anns,
"categories" => categories,
"extras" => {
"categories-title" => t("news.categories"),
"widget-title" =>t('news.news'),
"title-head" => t('news.table.title'),
"date-head" => t('news.table.date'),
"status-head" => t('news.table.status'),
"subtitle-head" => t('news.table.sub_title'),
"category-head" => t('news.table.category'),
"news_count" => news_count,
"keyword" => keyword
},
"total_pages" => news.total_pages
}
end
def widget
2014-11-12 10:04:20 +00:00
params = OrbitHelper.params
2015-01-09 08:37:02 +00:00
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
news = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil],:is_hot => true).can_display.is_approved.order_by(sort).filter_by_widget_categories if news.nil?
2014-11-13 02:05:41 +00:00
2014-11-07 08:44:11 +00:00
page = Page.where(:module => "news").first rescue nil
ma = ModuleApp.find_by_key("news") rescue nil
categories = ma.categories.enabled.collect do |cat|
{
"name" => cat.title,
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
}
end
categories.unshift({"name" => t("news.all"), "category-link" => "/#{I18n.locale.to_s + page.url}/"})
anns = news.collect do |a|
2015-03-06 01:31:21 +00:00
if a.image_description.blank?
2015-01-12 06:40:36 +00:00
image_description = "裝飾性圖片"
else
image_description = a.image_description
end
2014-11-07 08:44:11 +00:00
statuses = a.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
2015-01-30 03:10:58 +00:00
2015-02-06 10:02:08 +00:00
if I18n.locale.to_s == 'zh_tw'
subtitle = a.subtitle.truncate(100)
elsif I18n.locale.to_s == 'en'
subtitle = a.subtitle.truncate(200)
end
2014-11-07 08:44:11 +00:00
{
2015-01-13 02:35:42 +00:00
"title" => HTMLEntities.new.encode(a.title),
2015-02-06 10:02:08 +00:00
"subtitle" => subtitle,
2014-11-07 08:44:11 +00:00
"statuses" => statuses,
"category" => a.category.title,
"postdate" => a.postdate,
"link_to_show" => OrbitHelper.widget_item_url(a.to_param),
2014-11-11 08:31:16 +00:00
"img_src" => a.image.thumb.url || "/assets/news_default_image.jpg",
2015-01-12 06:40:36 +00:00
"img_description" => image_description
2014-11-07 08:44:11 +00:00
}
end
{
"news" => anns,
"categories" => categories,
"extras" => {
"more_url"=>OrbitHelper.widget_more_url,
"title-head" => t('news.table.title'),
"date-head" => t('news.table.date'),
"status-head" => t('news.table.status'),
"subtitle-head" => t('news.table.sub_title'),
"category-head" => t('news.table.category')
}
}
end
def show
params = OrbitHelper.params
news = NewsBulletin.find_by(:uid=>params[:uid])
url_to_edit = OrbitHelper.user_can_edit?(news) ? "/admin/news/#{news.id.to_s}/edit" : ""
access_level = OrbitHelper.user_access_level?
page = Page.where(:module => "news").first rescue nil
2015-01-13 01:19:12 +00:00
if !news.approved && (access_level != "manager" && access_level != "admin" && access_level != "sub_manager")
2014-11-07 08:44:11 +00:00
return {}
end
ma = ModuleApp.find_by_key("news") 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
tags = news.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
files = news.news_bulletin_files.collect{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title) } } rescue []
links = news.news_bulletin_links.collect{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
update_user = news.update_user.member_profile.name rescue ""
department = news.department rescue ""
2015-07-03 12:55:23 +00:00
request = OrbitHelper.request
meta_desc = news.subtitle.nil? || news.subtitle == "" ? news.text[0..200] : news.subtitle
OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => news.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}#{news.image.url}"},{"property" => "og:type", "content" => "Article"}])
2014-11-07 08:44:11 +00:00
{
"tags" => tags,
"news_bulletin_files" => files,
"news_bulletin_links" => links,
"categories" => categories,
"data" => {
2015-03-06 01:31:21 +00:00
"categories-title" => t("news.category"),
2015-01-13 02:35:42 +00:00
"title" => HTMLEntities.new.encode(news.title),
2014-11-07 08:44:11 +00:00
"category" => news.category.title,
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{news.category.to_param}",
2015-02-13 02:01:01 +00:00
"unit" => (news.unit.name rescue ""),
"unit-link" => "/#{I18n.locale.to_s + page.url}/?unit=#{(news.unit.to_param rescue "")}",
2014-11-07 08:44:11 +00:00
"department" => (department.name rescue ""),
"department-link" => "/#{I18n.locale.to_s + page.url}/?department=#{(department.to_param rescue "")}",
"update_user" => update_user,
2014-12-29 08:09:44 +00:00
"updated_at" => news.postdate.strftime('%Y-%m-%d %H:%M'),
2014-11-12 10:04:20 +00:00
"body" => news.text == "" ? "Content unavailable for this language." : news.text,
2014-11-07 08:44:11 +00:00
"image" => news.image.url,
2015-03-06 01:31:21 +00:00
"view_count" =>news.view_count,
"unit-title" => t("news.unit"),
"view-count-title" => t("news.view_count")
2014-11-07 08:44:11 +00:00
},
"impressionist" => (news.is_preview ? nil : news),
"url_to_edit"=>url_to_edit
}
end
end