announcement-test/app/controllers/announcements_controller.rb

139 lines
5.7 KiB
Ruby
Raw Normal View History

2014-04-01 07:12:43 +00:00
class AnnouncementsController < ApplicationController
def index
2014-12-27 07:54:43 +00:00
if !OrbitHelper.params['tags'].blank?
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:postdate=>'desc').filter_by_tags(OrbitHelper.params['tags']).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count)
2014-12-25 02:54:58 +00:00
else
2014-12-27 07:37:29 +00:00
announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).can_display.is_approved.order_by(:postdate=>'desc').filter_by_categories.filter_by_tags
2014-12-25 02:54:58 +00:00
end
2014-07-11 10:30:52 +00:00
2014-05-01 09:28:20 +00:00
anns = announcements.collect do |a|
2014-06-18 09:59:03 +00:00
statuses = a.statuses_with_classname.collect do |status|
2014-06-06 09:48:33 +00:00
{
2014-06-18 09:59:03 +00:00
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
2014-06-06 09:48:33 +00:00
}
end
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
author = User.find(a.create_user_id).member_profile.name rescue ""
2014-05-01 09:28:20 +00:00
{
"bulletin_links" => links,
"bulletin_files" => files,
2014-05-01 09:28:20 +00:00
"title" => a.title,
"subtitle" => a.subtitle,
2014-06-06 09:48:33 +00:00
"statuses" => statuses,
2014-09-19 07:28:17 +00:00
"category" => a.category.title,
"postdate" => a.postdate,
"author" => author,
2014-05-01 09:28:20 +00:00
"link_to_show" => OrbitHelper.url_to_show(a.to_param),
2014-05-14 11:51:17 +00:00
"img_src" => a.image.thumb.url || "http://placehold.it/100x100",
"img_description" => a.image_description,
2014-11-19 09:50:08 +00:00
"more" => t(:more_plus),
"view_count" => a.view_count
2014-05-01 09:28:20 +00:00
}
2014-04-01 07:12:43 +00:00
end
2014-04-02 06:44:36 +00:00
{
2014-05-14 11:51:17 +00:00
"announcements" => anns,
2014-07-11 10:30:52 +00:00
"extras" => {
2014-09-19 07:28:17 +00:00
"widget-title" =>t('announcement.announcement'),
2014-09-19 07:54:41 +00:00
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"status-head" => t('announcement.table.status'),
"author-head" => t('announcement.table.author'),
2014-09-19 07:54:41 +00:00
"subtitle-head" => t('announcement.table.sub_title'),
2015-01-16 09:01:33 +00:00
"category-head" => t('announcement.table.category'),
"link-head" => t('announcement.table.link'),
"file-head" => t('announcement.table.file')
2014-07-11 10:30:52 +00:00
},
"total_pages" => announcements.total_pages
2014-11-19 09:50:08 +00:00
}
2014-04-01 07:12:43 +00:00
end
def widget
2014-12-15 06:57:31 +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)
page = Page.where(:module => "announcement").first rescue nil
2014-07-11 10:30:52 +00:00
2014-04-02 06:44:36 +00:00
anns = announcements.collect do |a|
2014-06-18 09:59:03 +00:00
statuses = a.statuses_with_classname.collect do |status|
2014-06-06 09:48:33 +00:00
{
2014-06-18 09:59:03 +00:00
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
2014-06-06 09:48:33 +00:00
}
end
files = a.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title rescue '') } } rescue []
links = a.bulletin_links.map{|link| { "link_url" => link.url, "link_title" => (link.title.blank? ? link.url : link.title) } } rescue []
2014-11-25 10:25:54 +00:00
author = User.find(a.create_user_id).member_profile.name rescue ""
2014-04-01 07:12:43 +00:00
{
"bulletin_links" => links,
"bulletin_files" => files,
2014-04-01 07:12:43 +00:00
"title" => a.title,
2014-05-01 09:28:20 +00:00
"subtitle" => a.subtitle,
2014-06-06 09:48:33 +00:00
"statuses" => statuses,
2014-09-19 07:54:41 +00:00
"category" => a.category.title,
"postdate" => a.postdate,
"author" => author,
2014-05-24 11:36:23 +00:00
"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
2014-04-01 07:12:43 +00:00
}
end
2014-12-03 10:02:47 +00:00
mp = (anns[0]["img_src"] rescue "")
mpd = (anns[0]["img_description"] rescue "")
2014-04-02 06:44:36 +00:00
{
"announcements" => anns,
2014-07-11 10:30:52 +00:00
"extras" => {
2014-09-19 07:32:54 +00:00
"more_url"=>OrbitHelper.widget_more_url,
2014-12-03 10:07:52 +00:00
"main_picture" => mp,
"main_picture_description" => mpd,
2014-09-19 07:54:41 +00:00
"title-head" => t('announcement.table.title'),
"date-head" => t('announcement.table.date'),
"author-head" => t('announcement.table.author'),
2014-09-19 07:54:41 +00:00
"status-head" => t('announcement.table.status'),
"subtitle-head" => t('announcement.table.sub_title'),
"category-head" => t('announcement.table.category'),
"read_more" => "/#{I18n.locale.to_s + page.url}"
2014-07-11 10:30:52 +00:00
}
2014-11-19 09:50:08 +00:00
}
2014-04-01 07:12:43 +00:00
end
2014-11-19 09:50:08 +00:00
2014-04-01 07:12:43 +00:00
def show
params = OrbitHelper.params
2014-05-02 10:21:51 +00:00
announcement = Bulletin.can_display.find_by(:uid=>params[:uid])
2014-08-05 06:37:33 +00:00
url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : ""
2014-09-22 08:32:30 +00:00
access_level = OrbitHelper.user_access_level?
if !announcement.approved && (access_level != "manager" && access_level != "admin")
return {}
end
2014-12-11 09:41:42 +00:00
return {} if announcement.category.disable
2014-07-11 10:30:52 +00:00
tags = announcement.tags.map{|tag| {
"tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag)
} } rescue []
2014-10-06 09:59:40 +00:00
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 []
2014-05-14 11:51:17 +00:00
update_user = announcement.update_user.member_profile.name rescue ""
2014-05-05 07:48:49 +00:00
{
"tags" => tags,
2014-05-05 07:48:49 +00:00
"bulletin_files" => files,
"bulletin_links" => links,
"data" => {
2014-05-02 10:21:51 +00:00
"title" => announcement.title,
2014-05-14 11:51:17 +00:00
"update_user" => update_user,
2014-12-27 03:20:50 +00:00
"updated_at" => announcement.postdate.strftime('%Y-%m-%d %H:%M'),
2014-05-05 07:48:49 +00:00
"body" =>announcement.text,
"image" => announcement.image.url
2014-05-15 11:27:05 +00:00
},
2014-08-05 06:37:33 +00:00
"impressionist" => (announcement.is_preview ? nil : announcement),
"url_to_edit"=>url_to_edit
2014-11-19 09:50:08 +00:00
}
2014-06-17 10:46:52 +00:00
end
2014-11-19 09:50:08 +00:00
2014-04-01 07:12:43 +00:00
end