305 lines
13 KiB
Ruby
305 lines
13 KiB
Ruby
class NewsController < ApplicationController
|
|
|
|
def index
|
|
params = OrbitHelper.params
|
|
news = nil
|
|
|
|
if params[:keywords]
|
|
!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]).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?
|
|
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
|
|
keyword = "搜尋標題有關 ' " + params[:keywords] + " '"
|
|
elsif params[:department]
|
|
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
|
|
dept = Department.find_by(:uid => params["department"].split("-").last) rescue nil
|
|
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?
|
|
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
|
|
|
|
keywords = params[:department].split("-")
|
|
keywords.delete(keywords.last)
|
|
if keywords.kind_of?(Array)
|
|
keyword = "列出由:" + keywords.join("-") + "發佈的資料"
|
|
else
|
|
keyword = "列出由:" + keywords + "發佈的資料"
|
|
end
|
|
|
|
elsif params[:unit]
|
|
unit = Unit.find_by(:uid => params["unit"].split("-").last) rescue nil
|
|
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
|
|
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?
|
|
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
|
|
|
|
keywords = params[:unit].split("-")
|
|
keywords.delete(keywords.last)
|
|
if keywords.kind_of?(Array)
|
|
keyword = "列出由:" + keywords.join("-") + "發佈的資料"
|
|
else
|
|
keyword = "列出由:" + keywords + "發佈的資料"
|
|
end
|
|
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
|
|
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?
|
|
news_count = ",共搜尋到" + news.count.to_s + "筆資料"
|
|
|
|
keywords = params[:category].split("-")
|
|
keywords.delete(keywords.last)
|
|
if keywords.kind_of?(Array)
|
|
keyword = "搜尋有關 ' " + keywords.join("-") + " '"
|
|
else
|
|
keyword = "搜尋有關 ' " + keywords + " '"
|
|
end
|
|
end
|
|
!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]).can_display_postdate.is_approved.order_by(sort).filter_by_categories.filter_by_tags(OrbitHelper.params['tags']) if news.nil?
|
|
page = Page.where(:module => "news").first rescue nil
|
|
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
|
|
|
|
anns = news.collect do |a|
|
|
if a.image_description.blank?
|
|
image_description = "裝飾性圖片"
|
|
else
|
|
image_description = a.image_description
|
|
end
|
|
|
|
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.strftime('%Y-%m-%d'),
|
|
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
|
|
"img_src" => a.image.thumb.url || "/assets/news_default_image.jpg",
|
|
"img_description" => image_description,
|
|
"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" => "更多新聞+"}
|
|
params = OrbitHelper.params
|
|
!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?
|
|
|
|
page = Page.where(:module => "news").first rescue nil
|
|
ma = ModuleApp.find_by_key("news") rescue nil
|
|
|
|
current_categories = OrbitHelper.widget_categories
|
|
categories_for_url = ma.categories.enabled.collect do |cat|
|
|
if current_categories.include?(cat.id.to_s)
|
|
{
|
|
"name" => cat.title,
|
|
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{cat.to_param}"
|
|
}
|
|
end
|
|
end
|
|
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}/"})
|
|
|
|
anns = news.collect do |a|
|
|
if a.image_description.blank?
|
|
image_description = "裝飾性圖片"
|
|
else
|
|
image_description = a.image_description
|
|
end
|
|
|
|
statuses = a.statuses_with_classname.collect do |status|
|
|
{
|
|
"status" => status["name"],
|
|
"status-class" => "status-#{status['classname']}"
|
|
}
|
|
end
|
|
|
|
if I18n.locale.to_s == 'zh_tw'
|
|
subtitle = a.subtitle.truncate(100)
|
|
elsif I18n.locale.to_s == 'en'
|
|
subtitle = a.subtitle.truncate(200)
|
|
end
|
|
|
|
{
|
|
"title" => HTMLEntities.new.encode(a.title),
|
|
"subtitle" => 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 || "/assets/news_default_image.jpg",
|
|
"img_description" => image_description
|
|
}
|
|
end
|
|
{
|
|
"news" => anns,
|
|
"categories" => categories,
|
|
"categories_for_url" => categories_for_url,
|
|
"extras" => {
|
|
"more_stories" => more_stories[I18n.locale.to_s],
|
|
"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 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)
|
|
hot_news = hot_news.first
|
|
categories.each_with_index do |category,index|
|
|
if hot_news.nil?
|
|
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)
|
|
else
|
|
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)
|
|
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
|
|
|
|
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
|
|
|
|
if !news.approved && (access_level != "manager" && access_level != "admin" && access_level != "sub_manager")
|
|
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 ""
|
|
|
|
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"}])
|
|
{
|
|
"tags" => tags,
|
|
"news_bulletin_files" => files,
|
|
"news_bulletin_links" => links,
|
|
"categories" => categories,
|
|
"data" => {
|
|
"categories-title" => t("news.category"),
|
|
"title" => HTMLEntities.new.encode(news.title),
|
|
"category" => news.category.title,
|
|
"category-link" => "/#{I18n.locale.to_s + page.url}/?category=#{news.category.to_param}",
|
|
"unit" => (news.unit.name rescue ""),
|
|
"unit-link" => "/#{I18n.locale.to_s + page.url}/?unit=#{(news.unit.to_param rescue "")}",
|
|
"department" => (department.name rescue ""),
|
|
"department-link" => "/#{I18n.locale.to_s + page.url}/?department=#{(department.to_param rescue "")}",
|
|
"update_user" => update_user,
|
|
"updated_at" => news.postdate.strftime('%Y-%m-%d %H:%M'),
|
|
"body" => news.text == "" ? "Content unavailable for this language." : news.text,
|
|
"image" => news.image.url,
|
|
"view_count" =>news.view_count,
|
|
"unit-title" => t("news.unit"),
|
|
"view-count-title" => t("news.view_count")
|
|
},
|
|
"impressionist" => (news.is_preview ? nil : news),
|
|
"url_to_edit"=>url_to_edit
|
|
}
|
|
end
|
|
|
|
end
|