diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 03661d3..2567a1e 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,68 +1,8 @@ class AnnouncementsController < ApplicationController - + include AnnouncementsHelper def index Bulletin.remove_expired_status - params = OrbitHelper.params - locale = OrbitHelper.get_site_locale.to_s - feeds_anns = [] - top_anns = [] - if !params['tags'].blank? - announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.filter_by_tags(params['tags']).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count).to_a - else - if !params["source"].present? - announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_categories([],false).filter_by_tags.to_a - feeds_anns = get_feed_announcements("index") - else - announcements = [] - feeds_anns = get_feed_announcements("index",params["source"]) - end - end - # (OrbitHelper.page_number == 1 or OrbitHelper.page_number.nil?) && - if !params["source"].present? - if !params['tags'].blank? - top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories([],false).filter_by_tags(params['tags']).to_a - else - top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories([],false).filter_by_tags.to_a - end - end - - if !feeds_anns.blank? - announcements = announcements.concat(feeds_anns) - sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] } - sorted = top_anns + sorted - if params["keywords"].present? - sorted = sorted.find_all{|anns| - if anns["source-site"].present? - /#{params[:keywords].to_s}/i.match anns["title"] - else - /#{params[:keywords].to_s}/i.match anns.title - end - } - end - if params["postdate"].present? - sorted = sorted.find_all{|anns| - if anns["source-site"].present? - /#{params[:postdate].to_s}/i.match anns["postdate"].strftime("%Y-%m") if !anns["postdate"].nil? - else - /#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m") if !anns.postdate.nil? - end - } - end - sorted = Kaminari.paginate_array(sorted).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue [] - else - announcements = top_anns + announcements - if params["keywords"].present? - announcements = announcements.find_all{|anns| /#{params[:keywords].to_s}/i.match anns.title} - end - if params["postdate"].present? - announcements = announcements.find_all{|anns| - if !anns.postdate.nil? - /#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m") - end - } - end - sorted = Kaminari.paginate_array(announcements).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue [] - end + announcements,sorted = get_sorted_annc total_pages = sorted.total_pages anns = [] @@ -134,47 +74,6 @@ class AnnouncementsController < ApplicationController end - def get_feed_announcements(type,site_source=nil) - feed_anns = OrbitHelper.get_feed_for_module(type) - fans = [] - locale = OrbitHelper.get_site_locale.to_s - feed_anns.each do |fa| - next if !site_source.nil? && site_source != fa["source-site-title"] - status = { - "status" => "#{fa["source-site-title"]}", - "status-class" => "status-source" - } - - files = fa["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue [] - links = fa["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue [] - - x = { - "bulletin_links" => links, - "bulletin_files" => files, - "title" => fa["title_translations"][locale], - "subtitle" => fa["subtitle_translations"][locale], - "statuses" => [status], - "category" => fa["category"], - "postdate" => fa["postdate"], - "author" => fa["author"], - "is_top" => 0, - "source-site" => "#{fa["source-site-title"]}", - "source-site-title" => fa["source-site-title"], - "source-site-link" => fa["source-site"], - "link_to_show" => OrbitHelper.url_to_show(fa["params"]), - "target" => "_self", - "img_src" => fa["image"]["thumb"] || "/assets/announcement-default.jpg", - "img_description" => fa["image_description_translations"][locale], - "more" => t(:more_plus), - "view_count" => "" - } - if (!x["title"].empty? rescue false) - fans << x - end - end - fans - end - def random_announcement_widget uid = OrbitHelper.params[:uid] rescue "" tags = OrbitHelper.widget_tags diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb new file mode 100644 index 0000000..0197a37 --- /dev/null +++ b/app/helpers/announcements_helper.rb @@ -0,0 +1,111 @@ +module AnnouncementsHelper + def get_feed_announcements(type,site_source=nil) + feed_anns = OrbitHelper.get_feed_for_module(type) + fans = [] + locale = OrbitHelper.get_site_locale.to_s + feed_anns.each do |fa| + next if !site_source.nil? && site_source != fa["source-site-title"] + status = { + "status" => "#{fa["source-site-title"]}", + "status-class" => "status-source" + } + + files = fa["bulletin_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue [] + links = fa["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue [] + + x = { + "bulletin_links" => links, + "bulletin_files" => files, + "title" => fa["title_translations"][locale], + "subtitle" => fa["subtitle_translations"][locale], + "statuses" => [status], + "category" => fa["category"], + "postdate" => fa["postdate"], + "author" => fa["author"], + "is_top" => 0, + "source-site" => "#{fa["source-site-title"]}", + "source-site-title" => fa["source-site-title"], + "source-site-link" => fa["source-site"], + "link_to_show" => OrbitHelper.url_to_show(fa["params"]), + "target" => "_self", + "img_src" => fa["image"]["thumb"] || "/assets/announcement-default.jpg", + "img_description" => fa["image_description_translations"][locale], + "more" => t(:more_plus), + "view_count" => "" + } + if (!x["title"].empty? rescue false) + fans << x + end + end + fans + end + def test + 123 + end + def get_sorted_annc + params = OrbitHelper.params + locale = OrbitHelper.get_site_locale.to_s + page = Page.where(url:params['url']).first + feeds_anns = [] + top_anns = [] + tags = page.tags + if !tags.blank? + announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil).can_display.is_approved.filter_by_tags(tags).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count).to_a + else + if !params["source"].present? + announcements = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]).and(:title.ne => nil, :is_top.ne => true).can_display.is_approved.filter_by_categories([],false).filter_by_tags.to_a + feeds_anns = get_feed_announcements("index") + else + announcements = [] + feeds_anns = get_feed_announcements("index",params["source"]) + end + end + # (OrbitHelper.page_number == 1 or OrbitHelper.page_number.nil?) && + if !params["source"].present? + if !tags.blank? + top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories([],false).filter_by_tags(tags).to_a + else + top_anns = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil], :is_top => true).and(:title.ne => nil).can_display.filter_by_categories([],false).filter_by_tags.to_a + end + end + + if !feeds_anns.blank? + announcements = announcements.concat(feeds_anns) + sorted = announcements.sort{ |k,v| v["postdate"] <=> k["postdate"] } + sorted = top_anns + sorted + if params["keywords"].present? + sorted = sorted.find_all{|anns| + if anns["source-site"].present? + /#{params[:keywords].to_s}/i.match anns["title"] + else + /#{params[:keywords].to_s}/i.match anns.title + end + } + end + if params["postdate"].present? + sorted = sorted.find_all{|anns| + if anns["source-site"].present? + /#{params[:postdate].to_s}/i.match anns["postdate"].strftime("%Y-%m") if !anns["postdate"].nil? + else + /#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m") if !anns.postdate.nil? + end + } + end + sorted = Kaminari.paginate_array(sorted).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue [] + else + announcements = top_anns + announcements + if params["keywords"].present? + announcements = announcements.find_all{|anns| /#{params[:keywords].to_s}/i.match anns.title} + end + if params["postdate"].present? + announcements = announcements.find_all{|anns| + if !anns.postdate.nil? + /#{params[:postdate].to_s}/i.match anns.postdate.strftime("%Y-%m") + end + } + end + sorted = Kaminari.paginate_array(announcements).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count) rescue [] + end + [announcements,sorted] + end +end diff --git a/app/views/announcements/show.html.erb b/app/views/announcements/show.html.erb index 648b75c..d20fef9 100644 --- a/app/views/announcements/show.html.erb +++ b/app/views/announcements/show.html.erb @@ -1 +1,69 @@ -<%= render_view %> \ No newline at end of file +<% + require 'announcements_helper' + params = OrbitHelper.params + page = Page.where(url:params['url']).first + @show_back_and_next_flag = 0 + if page.methods.include? 'select_option_items'.to_sym + ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| + @show_option_items = modile_app.show_option_items rescue nil + end + page.select_option_items.each do |select_option_item| + if !(@show_option_items.nil?) && select_option_item.field_name == @show_option_items.keys.first.to_s + value = YAML.load(select_option_item.value) + if value[I18n.locale] == t('announcement.not_show') + @show_back_and_next_flag = 0 + elsif value[I18n.locale] == t('announcement.show_top') + @show_back_and_next_flag = 1 + elsif value[I18n.locale] == t('announcement.show_bottom') + @show_back_and_next_flag = 2 + end + end + end + end + if @show_back_and_next_flag != 0 + uid = params['uid'] + announcements,sorted = get_sorted_annc + now_index = sorted.to_enum.with_index.select{|v| v[0].uid==uid}[0][1] + if now_index != 0 + prev_result = sorted[now_index-1] + prev_url = params['url'] + '/' + prev_result.to_param + prev_content = "" + end + if now_index != sorted.length-1 + next_result = sorted[now_index+1] + next_url = params['url'] + '/' + next_result.to_param + next_content = "" + end + content = "
#{prev_content}#{next_content}
".html_safe + end +%> +<% if @show_back_and_next_flag!=0 %> + +<% end %> + +<% if @show_back_and_next_flag==1 %> +<%= content %> +<% end %> +<%= render_view %> +<% if @show_back_and_next_flag==2 %> +<%= content %> +<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 96ee16e..0e8db55 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,6 +7,12 @@ en: orignal_size: Original Size small_size: Small Size medium_size: Medium Size + showing_back_and_next: Show back and next + not_show: Not show + show_top: Show at top + show_bottom: Show at bottom + prev: previous + next: next table: title : Title date : Date diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 6a8cbd9..b118107 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,6 +7,12 @@ zh_tw: orignal_size: 原圖大小 small_size: 小張縮圖 medium_size: 中等縮圖 + showing_back_and_next: 顯示上下則 + not_show: 不顯示 + show_top: 顯示在最上面 + show_bottom: 顯示在最下面 + prev: 上一則 + next: 下一則 table: title : 標題 date : 張貼日期 diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index 1fe0061..0f63de2 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -15,6 +15,18 @@ module Announcement value3[t_data.keys[0]] = t_data.values[0]['announcement']['orignal_size'] end data[key1] = [value1,value2,value3] + data_item = {} + key_item1 = {} + value_item1 = {} + value_item2 = {} + value_item3 = {} + translate_data.each do |t_data| + key_item1[t_data.keys[0]] = t_data.values[0]['announcement']['showing_back_and_next'] + value_item1[t_data.keys[0]] = t_data.values[0]['announcement']['not_show'] + value_item2[t_data.keys[0]] = t_data.values[0]['announcement']['show_bottom'] + value_item3[t_data.keys[0]] = t_data.values[0]['announcement']['show_top'] + end + data_item[key_item1] = [value_item1,value_item2,value_item3] OrbitApp.registration "Announcement", :type => "ModuleApp" do module_label "announcement.announcement" base_url File.expand_path File.dirname(__FILE__) @@ -28,6 +40,7 @@ module Announcement data_count 1..30 begin show_options data + show_option_items data_item rescue => e puts ['there_was_no_show_option_method',e] end