From f7039d6d1c8d9aa3814d8a3eb498a949f69f41c1 Mon Sep 17 00:00:00 2001 From: bohung Date: Mon, 9 Aug 2021 10:45:06 +0800 Subject: [PATCH] Add carousel images and event end date. --- app/controllers/event_news_controller.rb | 70 ++++- .../event_news_feeds_controller.rb | 2 + app/models/event_carousel_image.rb | 15 + app/models/event_news.rb | 39 ++- app/models/event_news_setting.rb | 2 +- app/views/admin/event_news/_form.html.erb | 54 +++- .../admin/event_news/_form_image.html.erb | 49 +++ app/views/admin/event_news/settings.html.erb | 6 + config/locales/en.yml | 6 + config/locales/zh_tw.yml | 6 + modules/event_news/show.html.erb | 295 +++++++++++++++++- 11 files changed, 524 insertions(+), 20 deletions(-) create mode 100644 app/models/event_carousel_image.rb create mode 100644 app/views/admin/event_news/_form_image.html.erb diff --git a/app/controllers/event_news_controller.rb b/app/controllers/event_news_controller.rb index fe7520d..c95ca4e 100644 --- a/app/controllers/event_news_controller.rb +++ b/app/controllers/event_news_controller.rb @@ -22,6 +22,10 @@ class EventNewsController < ApplicationController target = a.is_external_link ? "_blank" : "_self" doc = Nokogiri::HTML(a.title) title = doc.text.empty? ? 'no content' : doc.text + event_time = (a.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + if a.event_end_date + event_time = "#{event_time.split(" ")[0]} ~ #{a.event_end_date.strftime('%Y-%m-%d')}" + end { "department" => author, "event_news_links" => links, @@ -37,7 +41,7 @@ class EventNewsController < ApplicationController "subtitle" => a.subtitle, "statuses" => statuses, "category" => (a.category.title rescue ""), - "postdate" => a.event_date, + "postdate" => event_time, "author" => author, "is_top" => (a.is_top? ? 1 : 0), "link_to_show" => link_to_show+"\" title=\"#{title}\"", @@ -541,11 +545,21 @@ class EventNewsController < ApplicationController subtitle_ann = event_news.subtitle if event_news.display_subtitle? img_src = (event_news.image.thumb.url || "/assets/event_news-default.jpg") if event_news.display_img? img_description = event_news.image_description if (event_news.image_description.present?) && (event_news.display_img?) - + event_carousel_images = event_news.event_carousel_images.map{|image| {"src"=>image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }} + resume_btn_title = (I18n.locale.to_s =="zh_tw") ? "繼續播放" : "resume" + pause_btn_title = (I18n.locale.to_s =="zh_tw") ? "暫停播放" : "pause" + prev_btn_title = (I18n.locale.to_s =="zh_tw") ? "上一張" : "prev" + next_btn_title = (I18n.locale.to_s =="zh_tw") ? "下一張" : "next" + event_time = (event_news.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + if event_news.event_end_date + event_time = "#{event_time.split(" ")[0]} ~ #{event_news.event_end_date.strftime('%Y-%m-%d')}" + end + { "tags" => tags, "event_news_files" => files, "event_news_links" => links, + "event_carousel_images" => event_carousel_images, "data" => { "speaker-head" => t('event_news.speaker'), "host-head" => t('event_news.host'), @@ -556,13 +570,19 @@ class EventNewsController < ApplicationController "title" => event_news.title, "subtitle_ann" => subtitle_ann, "update_user" => update_user, - "updated_at" => (event_news.event_date.strftime('%Y-%m-%d %H:%M') rescue ""), + "updated_at" => event_time, "body" =>event_news.text, "image" => event_news.image.url, "img_src" => img_src, "img_description" => img_description, "hide_class" => event_news.display_img? ? event_news.image_display_class : ' hide', "alt_title" => desc, + "resume_btn_title" => resume_btn_title, + "pause_btn_title" => pause_btn_title, + "prev_btn_title" => prev_btn_title, + "next_btn_title" => next_btn_title, + "carousel_display_style" => (event_carousel_images.count == 0 ? 'display: none' : "width: #{event_news.carousel_image_width};margin: auto;"), + "carousel_count" => event_carousel_images.count, "place_text" => (event_news.place.present? rescue false) ? "#{I18n.t("event_news.place")}: #{event_news.place}" : "" }, "impressionist" => (event_news.is_preview ? nil : event_news), @@ -602,7 +622,11 @@ class EventNewsController < ApplicationController update_user = event_news["author"] desc = event_news["image_description_translations"][locale] rescue "" desc = (desc.nil? || desc == "" ? "EventNews image" : desc) - + img_description = nil + img_description = event_news["image_description_translations"][I18n.locale] if event_news['display_img'] + img_src = nil + img_src = (event_news['image']['original'] || "/assets/event_news-default.jpg") if event_news['display_img'] + subtitle_ann = event_news['subtitle_ann'] request = OrbitHelper.request if event_news["subtitle_translations"].present? meta_desc = event_news["subtitle_translations"][locale] != "" ? event_news["subtitle_translations"][locale] : event_news["text_translations"][locale][0..200] rescue "" @@ -612,13 +636,28 @@ class EventNewsController < ApplicationController OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_news["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => event_news["image"]["original"]},{"property" => "og:type", "content" => "Article"}]) - datetime = DateTime.parse(event_news["event_date"].to_s) rescue "" - + event_date = DateTime.parse(event_news["event_date"].to_s) rescue nil + event_end_date = DateTime.parse(event_news["event_end_date"].to_s) rescue nil + event_time = (event_date.strftime('%Y-%m-%d %H:%M') rescue "") + if event_end_date + event_time = "#{event_time.split(" ")[0]} ~ #{event_end_date.strftime('%Y-%m-%d')}" + end + event_carousel_images = Array(event_news["event_carousel_images"]) + resume_btn_title = (I18n.locale.to_s =="zh_tw") ? "繼續播放" : "resume" + pause_btn_title = (I18n.locale.to_s =="zh_tw") ? "暫停播放" : "pause" + prev_btn_title = (I18n.locale.to_s =="zh_tw") ? "上一張" : "prev" + next_btn_title = (I18n.locale.to_s =="zh_tw") ? "下一張" : "next" { "tags" => tags, "event_news_files" => files, "event_news_links" => links, + "event_carousel_images" => event_carousel_images, "data" => { + "title" => event_news["title_translations"][locale], + "subtitle_ann" => subtitle_ann, + "update_user" => update_user, + "updated_at" => event_time, + "body" => event_news["text_translations"][locale], "speaker-head" => t('event_news.speaker'), "host-head" => t('event_news.host'), "notes-head" => t('event_news.notes'), @@ -626,12 +665,17 @@ class EventNewsController < ApplicationController "speaker" => (event_news["speaker_translations"][locale].to_s rescue ""), "host" => (event_news["host_translations"][locale].to_s rescue ""), "notes" => (event_news["notes_translations"][locale].to_s rescue ""), - "title" => event_news["title_translations"][locale], - "update_user" => update_user, - "updated_at" => (datetime.strftime('%Y-%m-%d %H:%M') rescue ""), - "body" => event_news["text_translations"][locale], "image" => event_news["image"]["original"], + "img_src" => img_src, + "img_description" => img_description, + "hide_class" => event_news["display_img"] ? '' : ' hide', "alt_title" => desc, + "resume_btn_title" => resume_btn_title, + "pause_btn_title" => pause_btn_title, + "prev_btn_title" => prev_btn_title, + "next_btn_title" => next_btn_title, + "carousel_display_style" => (event_carousel_images.count == 0 ? 'display: none' : "width: #{EventNewsSetting.last.carousel_image_width};margin: auto;"), + "carousel_count" => event_carousel_images.count, "place_text" => (event_news["place"].present? rescue false) ? "#{I18n.t("event_news.place")}: #{event_news["place"]}" : "" }, "impressionist" => nil, @@ -687,6 +731,10 @@ class EventNewsController < ApplicationController target = a.is_external_link ? "_blank" : "_self" doc = Nokogiri::HTML(a.title) title = doc.text.empty? ? 'no content' : doc.text + event_time = (a.event_date.strftime('%Y-%m-%d %H:%M') rescue "") + if a.event_end_date + event_time = "#{event_time.split(" ")[0]} ~ #{a.event_end_date.strftime('%Y-%m-%d')}" + end { "department" => author, "event_news_links" => links, @@ -702,7 +750,7 @@ class EventNewsController < ApplicationController "subtitle" => a.subtitle, "statuses" => statuses, "category" => (a.category.title rescue ""), - "postdate" => a.event_date, + "postdate" => event_time, "author" => author, "is_top" => (a.is_top? ? 1 : 0), "link_to_show" => link_to_show+"\" title=\"#{title}\"", diff --git a/app/controllers/event_news_feeds_controller.rb b/app/controllers/event_news_feeds_controller.rb index 1c9f704..bb1adfd 100644 --- a/app/controllers/event_news_feeds_controller.rb +++ b/app/controllers/event_news_feeds_controller.rb @@ -125,6 +125,7 @@ class EventNewsFeedsController < ApplicationController a["text_translations"]["en"] = smart_convertor(anns.text_translations["en"]) if !anns.text_translations["en"].blank? a["text_translations"]["zh_tw"] = smart_convertor(anns.text_translations["zh_tw"]) if !anns.text_translations["zh_tw"].blank? a["event_date"] = anns.event_date + a["event_end_date"] = anns.event_end_date a["postdate"] = anns.event_date#anns.postdate a["image_description_translations"] = anns.image_description_translations a["image"] = {} @@ -138,6 +139,7 @@ class EventNewsFeedsController < ApplicationController a["params"] = anns.to_param a["event_news_links"] = [] a["event_news_files"] = [] + a["event_carousel_images"] = anns.event_carousel_images.map{|image| {"src"=>"#{request.base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }} anns.tags.each do |tag| if !tag_ids.include?(tag.id.to_s) tag_ids << tag.id.to_s diff --git a/app/models/event_carousel_image.rb b/app/models/event_carousel_image.rb new file mode 100644 index 0000000..3b646a2 --- /dev/null +++ b/app/models/event_carousel_image.rb @@ -0,0 +1,15 @@ +# encoding: utf-8 +class EventCarouselImage + + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :description, localize: true + + belongs_to :event_news + def description_text + Nokogiri::HTML(self.description.to_s).css("body").text() rescue "" + end +end diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 933fd28..2d21575 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -15,6 +15,7 @@ class EventNews before_destroy do EventNewsCache.all.destroy end + field :custom_carousel_image_width, type: String, default: "" field :image_display_class, type: String, default: "full-size-img" #3 choices: full-size-img , pull-left , pull-right field :add_to_calendar,type: Boolean,default: false field :calendar_start_date, :type => DateTime @@ -33,6 +34,7 @@ class EventNews field :update_user_id field :public, :type => Boolean, :default => true field :event_date , :type => DateTime, :default => Time.now + field :event_end_date , :type => DateTime, :default => Time.now field :postdate , :type => DateTime, :default => Time.now field :deadline , :type => DateTime field :rss2_sn @@ -59,10 +61,10 @@ class EventNews has_many :event_news_links, :autosave => true, :dependent => :destroy has_many :event_news_files, :autosave => true, :dependent => :destroy - + has_many :event_carousel_images, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :event_news_files, :allow_destroy => true accepts_nested_attributes_for :event_news_links, :allow_destroy => true - + accepts_nested_attributes_for :event_carousel_images, :allow_destroy => true before_destroy :destroy_email scope :can_display_and_sorted, ->{where(:is_hidden=>false,:is_preview => false).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil}).order(is_top: :desc,event_date: :desc,id: :desc)} @@ -171,4 +173,37 @@ class EventNews def display_img? self.display_img rescue false end + def statuses + statuses = [] + statuses << top_text if is_top? + statuses << hot_text if is_hot? + statuses << hidden_text if is_hidden? + statuses + end + def statuses_with_classname + statuses = [] + statuses << {"name" => top_text, "classname" => "top"} if is_top? + statuses << {"name" => hot_text, "classname" => "hot"} if is_hot? + statuses << {"name" => hidden_text, "classname" => "hidden"} if is_hidden? + statuses + end + def status_for_table + status = "" + status << "#{top_text} " if self.is_top + status << "#{hot_text} " if self.is_hot + status << "#{hidden_text}"if self.is_hidden + status.html_safe + end + def top_text + I18n.t("announcement.status.top") + end + def hot_text + I18n.t("announcement.status.hot") + end + def hidden_text + I18n.t("announcement.status.hidden") + end + def carousel_image_width + (self.custom_carousel_image_width.blank? ? AnnouncementSetting.last.carousel_image_width : self.custom_carousel_image_width) + end end diff --git a/app/models/event_news_setting.rb b/app/models/event_news_setting.rb index 2f46415..6f0a4a2 100644 --- a/app/models/event_news_setting.rb +++ b/app/models/event_news_setting.rb @@ -1,7 +1,7 @@ class EventNewsSetting include Mongoid::Document include Mongoid::Timestamps - + field :carousel_image_width, type: String, :default => "75%" field :top_limit, type: Integer, :default => 0 field :pro_enabled, type: Boolean, :default => false field :approvers, type: Array, :default => [] diff --git a/app/views/admin/event_news/_form.html.erb b/app/views/admin/event_news/_form.html.erb index 9fe2988..3b38c82 100644 --- a/app/views/admin/event_news/_form.html.erb +++ b/app/views/admin/event_news/_form.html.erb @@ -46,6 +46,7 @@ <% end %>
  • <%= t(:tags) %>
  • <%= t('event_news.image') %>
  • +
  • <%= t('event_news.carousel_image') %>
  • <%= t('event_news.email_reminder')%>
  • @@ -69,6 +70,12 @@ <%= f.datetime_picker :event_date, :no_label => true, :new_record => @event_news.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> +
    + +
    + <%= f.datetime_picker :event_end_date, :no_label => true, :new_record => @event_news.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %> +
    +
    @@ -116,12 +123,12 @@ <% image_display_class_relation = {"full_width"=>"full-size-img","up_left_corner"=>"pull-left","up_right_corner"=>"pull-right"} %>
    - <%= f.label :image_display_class, t("announcement.cover_image_display_setting"), :class => "control-label muted" %> + <%= f.label :image_display_class, t("event_news.cover_image_display_setting"), :class => "control-label muted" %>
    <% image_display_class_relation.each.with_index do |(key,value),i| %> <% end %>
    @@ -268,6 +275,33 @@
    + + +
    @@ -546,6 +580,22 @@ }); formTip(); }); + $(document).on('click', '#add_carousel_image', function(){ + var new_id = $(this).prev().attr('value'); + var old_id = new RegExp("new_event_carousel_images", "g"); + var on = $('.language-nav li.active').index(); + var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length; + $(this).prev().attr('value', parseInt(new_id) + 1); + $(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_image', f, :event_carousel_images) %>").replace(old_id, new_id)); + $(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() { + $(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active'); + }); + }); + $(document).on('click', '.fileupload-remove', function(){ + if($(this).find(".delete_image").length != 0){ + $(this).parents('.image_group').remove(); + } + }); $(document).on('click', '.delete_link', function(){ $(this).parents('.input-prepend').remove(); }); diff --git a/app/views/admin/event_news/_form_image.html.erb b/app/views/admin/event_news/_form_image.html.erb new file mode 100644 index 0000000..de4a786 --- /dev/null +++ b/app/views/admin/event_news/_form_image.html.erb @@ -0,0 +1,49 @@ + +
    +
    + +
    +
    +
    + <% if form_image.file.file %> + <%= image_tag form_image.file %> + <% else %> + + <% end %> +
    +
    + + <%= t(:select_image) %> + <%= t(:change) %> + <%= f.file_field :file %> + + <%= t(:cancel) %> +
    + +
    +
    +
    +
    + <% @site_in_use_locales.each do |locale| %> + <%= f.fields_for :description_translations do |f| %> +
    + +
    + <%= f.text_field locale, value: (form_image.description_translations[locale.to_s] rescue nil) %> +
    +
    + <% end %> + <% end %> +
    \ No newline at end of file diff --git a/app/views/admin/event_news/settings.html.erb b/app/views/admin/event_news/settings.html.erb index c4bedcb..9064ed9 100644 --- a/app/views/admin/event_news/settings.html.erb +++ b/app/views/admin/event_news/settings.html.erb @@ -127,6 +127,12 @@
    <%= t("event_news.click_on_submit") %>
    <%= form_for @setting, url: (@setting.new_record? ? admin_event_news_createsettings_path : admin_event_news_updatesettings_path), html: {class: "form-horizontal main-forms"} do |f| %>
    +
    + <%= f.label :carousel_image_width, t("event_news.default_carousel_image_width"), :class => "control-label muted" %> +
    + <%= f.text_field :carousel_image_width %> +
    +
    <%= f.label :only_manager_can_edit_status, t("event_news.only_manager_can_edit_status"), :class => "control-label muted" %>
    diff --git a/config/locales/en.yml b/config/locales/en.yml index f907c65..fdf5456 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,6 +2,9 @@ en: module_name: event_news: Event News event_news: + custom_carousel_image_width_hint: "If blank, width will be default value." + carousel_image_width: Carousel Image Width + default_carousel_image_width: Default Carousel Image Width place: Place cover_image_display_setting: Cover Image display setting full_width: Full width @@ -24,6 +27,7 @@ en: speaker: Speaker host: Host event_date: Event Date + event_end_date: Event End Date start_date: Start date end_date: End date add_to_calendar: Add to calendar @@ -37,6 +41,8 @@ en: 'yes': 'Yes' 'no': 'No' image: Cover Image + carousel_image: Carousel Image + carousel_image_title: Carousel Image(display at the bottom of show page) picture_showing_size: Picture Showing Size orignal_size: Original Size small_size: Small Size diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 813c1e0..64d036b 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -2,6 +2,9 @@ zh_tw: module_name: event_news: 活動公告 event_news: + custom_carousel_image_width_hint: "未填寫,則使用預設寬度" + carousel_image_width: 輪播圖片寬度 + default_carousel_image_width: 預設輪播圖片寬度 place: 地點 cover_image_display_setting: 封面圖片顯示設定 full_width: 滿版呈現 @@ -24,6 +27,7 @@ zh_tw: speaker: 演講者 host: 主持人 event_date: 事件日期 + event_end_date: 事件結束日期 start_date: 公告日期 end_date: 下架日期 add_to_calendar: 加入行事曆 @@ -37,6 +41,8 @@ zh_tw: 'yes': 是 'no': 否 image: 封面圖片 + carousel_image: 輪播圖片 + carousel_image_title: 輪播圖片(在show頁面底部顯示) picture_showing_size: 圖片顯示大小 orignal_size: 原圖大小 small_size: 小張縮圖 diff --git a/modules/event_news/show.html.erb b/modules/event_news/show.html.erb index 0341900..67275b3 100644 --- a/modules/event_news/show.html.erb +++ b/modules/event_news/show.html.erb @@ -1,3 +1,143 @@ + +

    {{title}}

    @@ -31,7 +171,7 @@
    -
    +
    {{img_description}}
    @@ -59,9 +199,53 @@
    - + {{link_to_edit}} - \ No newline at end of file