nccu-news/app/controllers/news_controller.rb

305 lines
13 KiB
Ruby
Raw Permalink 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-08-11 10:43:51 +00:00
news = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).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-08-11 10:43:51 +00:00
news = NewsBulletin.where(:title.ne => "" ,:department => dept,:is_preview.in=>[false,nil]).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-08-11 10:43:51 +00:00
news = NewsBulletin.where(:title.ne => "" ,:unit => unit,:is_preview.in=>[false,nil]).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-08-11 10:43:51 +00:00
news = NewsBulletin.all.where(:title.ne => "",:is_preview.in=>[false,nil]).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
more_stories = {"en" => "More Stories+", "zh_tw" => "更多新聞+"}
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
2015-07-28 09:13:44 +00:00
current_categories = OrbitHelper.widget_categories
2015-07-28 09:37:23 +00:00
categories_for_url = ma.categories.enabled.collect do |cat|
2015-07-28 09:13:44 +00:00
if current_categories.include?(cat.id.to_s)
2014-11-07 08:44:11 +00:00
{
"name" => cat.title,
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
}
2015-07-28 09:13:44 +00:00
end
2014-11-07 08:44:11 +00:00
end
2015-07-28 09:37:23 +00:00
categories_for_url.delete(nil)
categories_for_url = {"name" => "ALL", "category-link" => "/#{I18n.locale.to_s + page.url}/"} if categories_for_url.count != 1
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}/"})
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
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,
2015-07-28 09:37:23 +00:00
"categories_for_url" => categories_for_url,
2014-11-07 08:44:11 +00:00
"extras" => {
"more_stories" => more_stories[I18n.locale.to_s],
2014-11-07 08:44:11 +00:00
"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
2015-08-13 11:50:49 +00:00
def widget2
more_stories = {"en" => "More Stories+", "zh_tw" => "更多新聞+"}
categories = OrbitHelper.widget_categories
ma = ModuleApp.find_by_key("news")
page = Page.where(:module => "news").first rescue nil
if categories.first == "all" || categories.count == 0
categories = ma.categories.enabled.limit(3)
else
if categories.count > 3
categories = Category.find(categories.sample(3))
else
categories = Category.find(categories)
end
end
categories_data = []
categories_for_dropdown = ma.categories.enabled.collect do |cat|
link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
{
"name" => cat.title,
"category-link" => link
}
end
link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/"
categories_for_dropdown.unshift({"name" => t("news.all"), "category-link" => link})
hot_news = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_hot => true).can_display.is_approved.desc(:postdate).limit(1)
2015-08-13 11:50:49 +00:00
hot_news = hot_news.first
categories.each_with_index do |category,index|
if hot_news.nil?
2015-08-17 09:49:04 +00:00
nbs = NewsBulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :category_id => category.id).can_display.is_approved.desc(:postdate).limit(OrbitHelper.widget_data_count)
2015-08-13 11:50:49 +00:00
else
2015-08-17 09:49:04 +00:00
nbs = NewsBulletin.where(:id.ne => hot_news.id, :title.ne => "",:is_preview.in=>[false,nil], :category_id => category.id).can_display.is_approved.desc(:postdate).limit(OrbitHelper.widget_data_count)
2015-08-13 11:50:49 +00:00
end
temp = nbs.collect do |nb|
{
"title" => nb.title,
"link_to_show" => OrbitHelper.widget_item_url(nb.to_param)
}
end
link = page.nil? ? "#" : "/#{I18n.locale.to_s + page.url}/?category=#{category.to_param}"
categories_data << {
"name" => category.title,
"category-link" => link,
"news" => temp
}
end
image_description = hot_news.nil? || hot_news.image_description.blank? ? "裝飾性圖片" : hot_news.image_description
{
"categorized_news" => categories_data,
"categories" => categories_for_dropdown,
"extras" => {
"more_stories" => more_stories[I18n.locale.to_s],
"hot_news_title" => (hot_news.nil? ? "" : hot_news.title),
"hot_news_subtitle" => (hot_news.nil? ? "" : hot_news.subtitle),
"hot_news_image" => (hot_news.nil? ? "" : hot_news.image.mobile.url),
"hot_news_image_description" => image_description,
"hot_news_link_to_show" => (hot_news.nil? ? "" : OrbitHelper.widget_item_url(hot_news.to_param))
}
}
end
2014-11-07 08:44:11 +00:00
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