From f2215be917d6eaf0a9cb039200994cf35831e49c Mon Sep 17 00:00:00 2001 From: "BOYA,CHIU" Date: Mon, 23 Aug 2021 19:44:45 +0800 Subject: [PATCH] fix style error for show page and add setting for default field --- ...ews-default.jpg => event_news-default.jpg} | Bin .../admin/event_news_controller.rb | 16 ++++ app/controllers/event_news_controller.rb | 35 ++++---- app/helpers/admin/event_news_helper.rb | 78 ++++++++++++++++++ app/models/event_news_custom_title.rb | 17 ++++ app/views/admin/event_news/_form.html.erb | 8 +- app/views/admin/event_news/_index.html.erb | 2 +- .../event_news/custom_fields_title.html.erb | 26 ++++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 7 +- event_news_mod.gemspec | 1 + lib/event_news_mod/engine.rb | 18 +++- modules/event_news/show.html.erb | 2 +- 14 files changed, 184 insertions(+), 28 deletions(-) rename app/assets/images/{evenet_news-default.jpg => event_news-default.jpg} (100%) create mode 100644 app/models/event_news_custom_title.rb create mode 100644 app/views/admin/event_news/custom_fields_title.html.erb diff --git a/app/assets/images/evenet_news-default.jpg b/app/assets/images/event_news-default.jpg similarity index 100% rename from app/assets/images/evenet_news-default.jpg rename to app/assets/images/event_news-default.jpg diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index d5b6654..7a3ecd6 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -455,6 +455,22 @@ class Admin::EventNewsController < OrbitAdminController end end + def custom_fields_title + @event_news_custom_titles = EventNewsCustomTitle.get_map + end + + def update_custom_title + event_news_custom_title_params = params.require(:event_news_custom_title).permit! + event_news_custom_title_params.each do |k,event_news_custom_title_param| + EventNewsCustomTitle.find(event_news_custom_title_param['id']).update_attributes(event_news_custom_title_param) + end + Thread.new do + content = "UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi" + system(content) + end + redirect_to action: :index + end + private def load_settings diff --git a/app/controllers/event_news_controller.rb b/app/controllers/event_news_controller.rb index c95ca4e..a631490 100644 --- a/app/controllers/event_news_controller.rb +++ b/app/controllers/event_news_controller.rb @@ -68,11 +68,11 @@ class EventNewsController < ApplicationController "extras" => { "widget-title" =>t('event_news.event_news'), "title-head" => t('event_news.table.title'), - "speaker-head" => t('event_news.speaker'), - "place-head" => t('event_news.place'), - "host-head" => t('event_news.host'), + "speaker-head" => EventNewsCustomTitle.get_trans('speaker'), + "place-head" => EventNewsCustomTitle.get_trans('place'), + "host-head" => EventNewsCustomTitle.get_trans('host'), "notes-head" => t('event_news.notes'), - "event_date-head" => t('date_'), + "event_date-head" => EventNewsCustomTitle.get_trans('event_date'), "date-head" => t('event_news.table.date'), "status-head" => t('event_news.table.status'), "author-head" => t('event_news.table.author'), @@ -415,11 +415,11 @@ class EventNewsController < ApplicationController "main_picture" => mp, "main_picture_description" => mpd, "title-head" => t('event_news.table.title'), - "speaker-head" => t('event_news.speaker'), - "place-head" => t('event_news.place'), - "host-head" => t('event_news.host'), + "speaker-head" => EventNewsCustomTitle.get_trans('speaker'), + "place-head" => EventNewsCustomTitle.get_trans('place'), + "host-head" => EventNewsCustomTitle.get_trans('host'), "notes-head" => t('event_news.notes'), - "event_date-head" => t('date_'), + "event_date-head" => EventNewsCustomTitle.get_trans('event_date'), "date-head" => t('event_news.table.date'), "author-head" => t('event_news.table.author'), "status-head" => t('event_news.table.status'), @@ -561,10 +561,11 @@ class EventNewsController < ApplicationController "event_news_links" => links, "event_carousel_images" => event_carousel_images, "data" => { - "speaker-head" => t('event_news.speaker'), - "host-head" => t('event_news.host'), + "speaker-head" => EventNewsCustomTitle.get_trans('speaker'), + "host-head" => EventNewsCustomTitle.get_trans('host'), "notes-head" => t('event_news.notes'), "speaker" => event_news.speaker, + "speaker-css" => (event_news.speaker.blank? ? "display: none;" : "display: inline-block;"), "host" => event_news.host, "notes" => event_news.notes, "title" => event_news.title, @@ -583,7 +584,7 @@ class EventNewsController < ApplicationController "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}" : "" + "place_text" => (event_news.place.present? rescue false) ? "#{EventNewsCustomTitle.get_trans("place")}: #{event_news.place}" : "" }, "impressionist" => (event_news.is_preview ? nil : event_news), "url_to_edit"=>url_to_edit @@ -658,10 +659,10 @@ class EventNewsController < ApplicationController "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'), + "speaker-head" => EventNewsCustomTitle.get_trans('speaker'), + "host-head" => EventNewsCustomTitle.get_trans('host'), "notes-head" => t('event_news.notes'), - "event_date-head" => t('date_'), + "event_date-head" => EventNewsCustomTitle.get_trans('event_date'), "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 ""), @@ -777,10 +778,10 @@ class EventNewsController < ApplicationController "extras" => { "widget-title" =>t('event_news.event_news'), "title-head" => t('event_news.table.title'), - "speaker-head" => t('event_news.speaker'), - "host-head" => t('event_news.host'), + "speaker-head" => EventNewsCustomTitle.get_trans('speaker'), + "host-head" => EventNewsCustomTitle.get_trans('host'), "notes-head" => t('event_news.notes'), - "event_date-head" => t('date_'), + "event_date-head" => EventNewsCustomTitle.get_trans('event_date'), "date-head" => t('event_news.table.date'), "status-head" => t('event_news.table.status'), "author-head" => t('event_news.table.author'), diff --git a/app/helpers/admin/event_news_helper.rb b/app/helpers/admin/event_news_helper.rb index 57a5c6a..4da2df4 100644 --- a/app/helpers/admin/event_news_helper.rb +++ b/app/helpers/admin/event_news_helper.rb @@ -3,6 +3,84 @@ require "uri" require 'json' module Admin::EventNewsHelper + module FormHelper + extend self + extend ActionView::Helpers::FormTagHelper + extend ActionView::Helpers::FormOptionsHelper + extend ActionView::Helpers::DateHelper + extend ActionView::Helpers::TagHelper + extend ActionView::Helpers::RenderingHelper + extend ActionView::Context + extend OrbitBasis::RenderAnywhere + extend ActionView::Helpers::UrlHelper + extend OrbitFormHelper + extend Ckeditor::Helpers::FormHelper + def available_locales + @available_locales = @available_locales || Site.first.in_use_locales || I18n.available_locales + end + def set_input_name(input_name) + @input_name = input_name + end + + def get_input_name + @input_name.to_s + end + def create_lang_panel(field) + tmp2 = content_tag(:div,:class => 'btn-group', :data=>{:toggle=>"buttons-radio"}) do + available_locales.collect do |key| + link_entry_ary = ["##{field}","_#{key}"] + link_entry = link_entry_ary.join + link_to(I18n.t(key),link_entry,:data=>{:toggle=>"tab"},:class=>"btn #{(key == I18n.locale ? "active" : nil)}",:for=>key) + end.join.html_safe + end + end + def multiple_lang_tag(index1,type_of_tag,field,value=nil,custom_options={},combine_element='',exteral_options={},sortable=false) + if !index1.nil? + all_field = (get_input_name + "[#{index1}][#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'') + else + all_field = (get_input_name + "[#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'') + end + tmp = (available_locales.collect do |locale| + active_flag = ((locale == I18n.locale) ? ' in active' : '') + content_tag(:div,:class => "tab-content fade#{active_flag}",:id=>"#{all_field}_#{locale}") do + value_locale = value[locale.to_s] rescue nil + if !index1.nil? + self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{index1}][#{field}][#{locale}]",value_locale,custom_options) + else + self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{field}][#{locale}]",value_locale,custom_options) + end + end + end.join + create_lang_panel(all_field)).html_safe + combine_element + + if sortable + if exteral_options['style'].nil? + exteral_options['style'] = 'display: flex;align-items: center;flex-wrap: nowrap;' + else + exteral_options['style'] = exteral_options['style'] + 'display: flex;align-items: center;flex-wrap: nowrap;' + end + content_tag(:div,{:class => "tab-panel border"}.merge(exteral_options)) do + ("" +content_tag(:div) do + tmp + end).html_safe + end + else + content_tag(:div,{:class => "tab-panel"}.merge(exteral_options)) do + tmp + end + end + end + end + + def self.thead(field,center=false,enable_sort=true) + sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s + active = OrbitHelper.params[:sort].eql? sort + order = active ? (["asc", "desc"]-[OrbitHelper.params[:order]]).first : "asc" + arrow = (order.eql? "desc") ? "" : "" + klass = field.eql?(:title) ? "span5" : "span2" + th_data = (sort=="preview" || !enable_sort) ? EventNewsCustomTitle.get_trans(field) : "#{EventNewsCustomTitle.get_trans(field)} #{active ? arrow : ""}" + "#{th_data}".html_safe + end + def page_for_event_news(event_news) ann_page = nil pages = Page.where(:module=>'event_news') diff --git a/app/models/event_news_custom_title.rb b/app/models/event_news_custom_title.rb new file mode 100644 index 0000000..85689b4 --- /dev/null +++ b/app/models/event_news_custom_title.rb @@ -0,0 +1,17 @@ +class EventNewsCustomTitle + include Mongoid::Document + include Mongoid::Timestamps + field :key + field :title, type: String, localize: true + KEYS = ['event_date','speaker','place','host'] + def self.get_map + KEYS.map{|k| self.where(key: k).first || self.create(key: k,title_translations: I18n.available_locales.map{|l| [l,I18n.with_locale(l){I18n.t("event_news.#{k}")}]}.to_h)} + end + def default_title + I18n.t("event_news.#{self.key}") + end + def self.get_trans(key) + TitleMap[key][I18n.locale] rescue I18n.t("event_news.#{key}") + end + TitleMap = self.get_map.map{|v| [v.key,v.title_translations]}.to_h +end diff --git a/app/views/admin/event_news/_form.html.erb b/app/views/admin/event_news/_form.html.erb index 3b38c82..41b05ef 100644 --- a/app/views/admin/event_news/_form.html.erb +++ b/app/views/admin/event_news/_form.html.erb @@ -65,7 +65,7 @@
- +
<%= f.datetime_picker :event_date, :no_label => true, :new_record => @event_news.new_record?, :data=>{"picker-type" => "range", "range" => "start"} %>
@@ -382,7 +382,7 @@
- +
<%= f.fields_for :speaker_translations do |f| %> <%= f.text_field locale, class: "input-block-level", placeholder: t("event_news.speaker"), value: (@event_news.speaker_translations[locale] rescue nil) %> @@ -391,7 +391,7 @@
- +
<%= f.fields_for :place_translations do |f| %> <%= f.text_field locale, class: "input-block-level", placeholder: t("event_news.place"), value: (@event_news.place_translations[locale] rescue nil) %> @@ -400,7 +400,7 @@
- +
<%= f.fields_for :host_translations do |f| %> <%= f.text_field locale, class: "input-block-level", placeholder: t("event_news.host"), value: (@event_news.host_translations[locale] rescue nil) %> diff --git a/app/views/admin/event_news/_index.html.erb b/app/views/admin/event_news/_index.html.erb index e06c9ea..f8ea6c0 100644 --- a/app/views/admin/event_news/_index.html.erb +++ b/app/views/admin/event_news/_index.html.erb @@ -6,7 +6,7 @@ <% @table_fields.each do |f| %> - <%= thead(f) %> + <%= tmp = f.to_s.split('.')[-1]; EventNewsCustomTitle::KEYS.include?(tmp) ? Admin::EventNewsHelper.thead(tmp): thead(f) %> <% end %> diff --git a/app/views/admin/event_news/custom_fields_title.html.erb b/app/views/admin/event_news/custom_fields_title.html.erb new file mode 100644 index 0000000..eb3d4d0 --- /dev/null +++ b/app/views/admin/event_news/custom_fields_title.html.erb @@ -0,0 +1,26 @@ + +<%= form_tag({action: 'update_custom_title',method: 'post'}, {class: "form-horizontal main-forms previewable"}) do %> + <% Admin::EventNewsHelper::FormHelper.set_input_name("event_news_custom_title") %> + <% @event_news_custom_titles.each_with_index do |v,i| %> +
+ <%= label_tag :title , v.default_title, :class=>"control-label muted" %> +
+ <%= hidden_field_tag "event_news_custom_title[#{i}][id]", v.id %> + <%= Admin::EventNewsHelper::FormHelper.multiple_lang_tag(i,'text_field','title_translations',v.title_translations,{placeholder: v.default_title}) %> +
+
+ <% end %> +
+ <%= submit_tag t(:submit),class: 'btn btn-primary' %> +
+<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index fdf5456..c9a8bc3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,6 +2,7 @@ en: module_name: event_news: Event News event_news: + custom_fields_title: Custom Fields Title 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 diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index eadff9a..4659675 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -2,6 +2,7 @@ zh_tw: module_name: event_news: 活動公告 event_news: + custom_fields_title: 欄位名稱設定 custom_carousel_image_width_hint: "未填寫,則使用預設寬度" carousel_image_width: 輪播圖片寬度 default_carousel_image_width: 預設輪播圖片寬度 diff --git a/config/routes.rb b/config/routes.rb index c590180..db303c7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,7 +22,12 @@ Rails.application.routes.draw do patch 'event_news/updatesettings', to: 'event_news#updatesettings' post 'event_news/import_from_wp', to: 'event_news#import_from_wp' post 'event_news/generate_iframe_url' => 'event_news#generate_iframe_url' - resources :event_news + resources :event_news do + collection do + get 'custom_fields_title' + post 'update_custom_title' + end + end end resources :event_news do diff --git a/event_news_mod.gemspec b/event_news_mod.gemspec index a227c98..0c2b505 100644 --- a/event_news_mod.gemspec +++ b/event_news_mod.gemspec @@ -14,6 +14,7 @@ if bundle_update_flag if !folder.include?('mobile') info_json_file = "#{folder}modules/event_news/info.json" if File.exist?(info_json_file) + Bundler.with_clean_env{system ('cp -f '+ app_path + '/modules/event_news/show.html.erb ' + "#{folder}modules/event_news/.")} begin file_text = File.read(info_json_file) rescue "" encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???") diff --git a/lib/event_news_mod/engine.rb b/lib/event_news_mod/engine.rb index c94923c..4247e40 100644 --- a/lib/event_news_mod/engine.rb +++ b/lib/event_news_mod/engine.rb @@ -75,6 +75,10 @@ module EventNewsMod if defined? EventNewsCache EventNewsCache.destroy_all end + require File.expand_path('../../../app/models/event_news_custom_title', __FILE__) + if defined? EventNewsCustomTitle + EventNewsCustomTitle.get_map + end rescue => e puts ['error in event_news',e] end @@ -125,19 +129,25 @@ module EventNewsMod :active_for_action=>{'admin/event_news'=>'tags'}, :active_for_tag => 'EventNewsModule', :available_for => 'managers' + context_link 'event_news.custom_fields_title', + :link_path=>"custom_fields_title_admin_event_news_index_path" , + :priority=>5, + :active_for_action=>{'admin/event_news'=>'custom_fields_title'}, + :available_for => 'managers' + context_link 'event_news.feed_list', :link_path=>"admin_event_news_feed_path" , - :priority=>5, + :priority=>6, :active_for_action=>{'admin/event_news'=>'feed'}, :available_for => 'managers' - context_link 'event_news.import', + context_link 'event_news.import', :link_path=>"admin_event_news_import_path" , - :priority=>6, + :priority=>7, :active_for_action=>{'admin/event_news'=>'import'}, :available_for => 'managers' context_link 'event_news.settings', :link_path=>"admin_event_news_settings_path" , - :priority=>6, + :priority=>8, :active_for_action=>{'admin/event_news'=>'settings'}, :available_for => 'managers' end diff --git a/modules/event_news/show.html.erb b/modules/event_news/show.html.erb index 67275b3..2d2574e 100644 --- a/modules/event_news/show.html.erb +++ b/modules/event_news/show.html.erb @@ -157,7 +157,7 @@
  • - + {{speaker-head}}: {{speaker}}