From 6b29a2c2a1c53136a10b546c92e6ec9d7ce9da24 Mon Sep 17 00:00:00 2001 From: chiu Date: Thu, 30 Jul 2020 22:12:21 +0800 Subject: [PATCH] add comment feature for bulletin --- .../admin/announcements_controller.rb | 19 +++- app/controllers/announcements_controller.rb | 19 +++- app/controllers/bulletins_controller.rb | 1 - app/helpers/announcements_helper.rb | 21 +++++ app/models/bulletin.rb | 14 ++- app/models/bulletin_comment.rb | 21 +++++ .../admin/announcements/_comment.html.erb | 25 ++++++ app/views/admin/announcements/_form.html.erb | 47 +++++++++- app/views/admin/announcements/_index.html.erb | 1 + .../admin/announcements/comment.html.erb | 12 +++ app/views/announcements/show.html.erb | 87 +++++++++++++++++++ config/locales/en.yml | 10 +++ config/locales/zh_tw.yml | 10 +++ config/routes.rb | 5 +- 14 files changed, 283 insertions(+), 9 deletions(-) create mode 100644 app/models/bulletin_comment.rb create mode 100644 app/views/admin/announcements/_comment.html.erb create mode 100644 app/views/admin/announcements/comment.html.erb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 46f5623..a18802d 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -3,18 +3,31 @@ require 'rubyXL' class Admin::AnnouncementsController < OrbitAdminController include Admin::AnnouncementsHelper before_action ->(module_app = @app_title) { set_variables module_app } - before_action :set_bulletin, only: [:edit, :destroy] + before_action :set_bulletin, only: [:edit, :destroy, :comment] before_action :load_access_level, :load_settings def initialize super @app_title = "announcement" end - + def comment_hidden + b = BulletinComment.find(params[:id]) rescue nil + if !b.nil? + b.is_hidden = !b.is_hidden + b.save + @table_feed_fields = ["announcement.time", "announcement.comment", "announcement.account","ip","is_hidden"] + @comments = b.bulletin.bulletin_comments.reverse rescue [] + render partial: 'comment' + end + end + def comment + @table_feed_fields = ["announcement.time", "announcement.comment", "announcement.account","ip","is_hidden"] + @comments = @bulletin.bulletin_comments.reverse rescue [] + end def index Bulletin.remove_expired_status @tags = @module_app.tags - @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] + @table_fields = [:status, :category, :title, :start_date, :end_date, "announcement.comment", :last_modified] @current_user = current_user if AnnouncementSetting.first.is_display_edit_only && !current_user.is_admin? && !current_user.is_manager?(@module_app) current_user_is_sub_manager = !current_user.is_manager?(@module_app) && (current_user.is_sub_manager?(@module_app) || current_user.is_sub_manager_with_role?(@module_app)) rescue false diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index d7f5850..e1a5f14 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,5 +1,17 @@ class AnnouncementsController < ApplicationController include AnnouncementsHelper + helper_method :complementaryColor,:lighten_color + def comment + @bulletin = Bulletin.where(:uid=>params[:uid]).first + comment_val = params['comment'] + if !@bulletin.nil? && @bulletin.open_comment_for_user(OrbitHelper.current_user) && !comment_val.blank? + account_id = OrbitHelper.current_user.member_profile.id.to_s rescue 'visitor' + b = BulletinComment.new(ip: request.remote_ip,comment: comment_val,account_id: account_id) + b.bulletin_id = @bulletin.id + b.save + render :json => {} + end + end def index Bulletin.remove_expired_status sorted,total_pages = get_sorted_annc @@ -198,6 +210,7 @@ class AnnouncementsController < ApplicationController else announcement = Bulletin.can_display_and_sorted.where(:uid => uid).first end + @bulletin = announcement announcement = Bulletin.where(:uid => uid).first if announcement.nil? url_to_edit = OrbitHelper.user_can_edit?(announcement) ? "/admin/announcements/#{announcement.id.to_s}/edit" : "" @@ -241,7 +254,7 @@ class AnnouncementsController < ApplicationController subtitle_ann = announcement.subtitle if announcement.display_subtitle? img_src = (announcement.image.thumb.url || "/assets/announcement-default.jpg") if announcement.display_img? img_description = announcement.image_description if (announcement.image_description.present?) && (announcement.display_img?) - + show_comment_flag = announcement.open_comment_for_user(OrbitHelper.current_user) { "tags" => tags, "bulletin_files" => files, @@ -258,6 +271,8 @@ class AnnouncementsController < ApplicationController "hide_class" => announcement.display_img? ? '' : ' hide', "alt_title" => desc }, + "comments" => announcement.comments, + "show_comment_flag" => show_comment_flag, "impressionist" => (announcement.is_preview ? nil : announcement), "url_to_edit"=>url_to_edit } @@ -327,6 +342,8 @@ class AnnouncementsController < ApplicationController "hide_class" => announcement["display_img"] ? '' : ' hide', "alt_title" => desc }, + "comments" => [], + "show_comment_flag" => false, "impressionist" => nil, "url_to_edit" => url_to_edit } diff --git a/app/controllers/bulletins_controller.rb b/app/controllers/bulletins_controller.rb index da0b705..8db553c 100644 --- a/app/controllers/bulletins_controller.rb +++ b/app/controllers/bulletins_controller.rb @@ -1,7 +1,6 @@ # encoding: utf-8 class BulletinsController < ApplicationController before_filter :set_I18n - def get_bulletins page = Page.where(:module => "announcement").first rescue nil diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index bde1e22..38b0252 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -1,4 +1,25 @@ module AnnouncementsHelper + def complementaryColor(my_hex) + if my_hex[0] == '#' + my_hex = my_hex[1..-1] + end + rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} + comp = rgb.map{|a| (255 - a.to_i(16)).to_s(16).rjust(2,'0')} + '#'+comp.join + end + def lighten_color(my_hex,percent) + if my_hex[0] == '#' + my_hex = my_hex[1..-1] + end + rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} + comp = rgb.collect do |a| + tmp = a.to_i(16)*(1+percent/100.0) + tmp = 255 if tmp>255 + tmp = 0 if tmp < 0 + tmp.to_i.to_s(16).rjust(2,'0') + end + '#'+comp.join + end def set_image_version_for_widget subpart = OrbitHelper.get_current_widget @image_version = 'thumb' diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 57853ab..d79c294 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -49,12 +49,15 @@ class Bulletin field :other_mailaddress field :image_description, localize: true field :top_end_date, :type => DateTime - + field :open_comment, :type => Boolean, :default => false + field :comment_end_time, :type => DateTime + field :comment_role, :type => Array, :default => [] + mount_uploader :image, ImageUploader has_many :bulletin_links, :autosave => true, :dependent => :destroy has_many :bulletin_files, :autosave => true, :dependent => :destroy - + has_many :bulletin_comments, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :bulletin_files, :allow_destroy => true accepts_nested_attributes_for :bulletin_links, :allow_destroy => true @@ -166,4 +169,11 @@ class Bulletin def display_img? self.display_img rescue false end + def comments + self.bulletin_comments.select{|v| !v.is_hidden} + end + def open_comment_for_user(user) + role_ids = user.member_profile.roles.collect{|v| v.id.to_s} rescue ['visitor'] + !self.comment_end_time.blank? && self.open_comment && self.comment_end_time > Time.now && (self.comment_role.any?{|v| role_ids.include?(v)} || self.comment_role.include?('visitor') || (self.comment_role.include?('all_member') && role_ids[0] != 'visitor')) + end end diff --git a/app/models/bulletin_comment.rb b/app/models/bulletin_comment.rb new file mode 100644 index 0000000..9ca0570 --- /dev/null +++ b/app/models/bulletin_comment.rb @@ -0,0 +1,21 @@ +# encoding: utf-8 +class BulletinComment + include Mongoid::Document + include Mongoid::Timestamps + + field :ip + field :comment + field :account_id + field :is_hidden,type: Boolean,default: false + def time + self.created_at.strftime('%Y/%m/%d %H:%M') + end + def account + tmp = MemberProfile.where(:id => self.account_id).collect{|v| v.name}.join + tmp.blank? ? I18n.t('announcement_visitor') : tmp + end + def roles + MemberProfile.where(:id => self.account_id).collect{|v| v.roles}.flatten + end + belongs_to :bulletin +end \ No newline at end of file diff --git a/app/views/admin/announcements/_comment.html.erb b/app/views/admin/announcements/_comment.html.erb new file mode 100644 index 0000000..008fd92 --- /dev/null +++ b/app/views/admin/announcements/_comment.html.erb @@ -0,0 +1,25 @@ + + + + <% @table_feed_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @comments.each do |comment| %> + + + + + + + + <% end %> + +
<%= comment.time %><%= comment.comment.html_safe %> + <%= comment.account %> + <% comment.roles.each do |role| %> + <%= role.title rescue '' %> + <% end %> + <%= comment.ip %><%= button_tag (comment.is_hidden ? t('show') : t('is_hidden')), type: 'button',"data-href" => "/#{I18n.locale}/admin/annc-comment-hidden/#{comment.id}",:onclick => "update_status(this)" ,class: (comment.is_hidden ? 'btn btn-primary' : 'btn btn-info') %>
\ No newline at end of file diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 3ec3379..045b1d2 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -194,7 +194,33 @@ <%= f.datetime_picker :top_end_date, :no_label => true, :new_record => @bulletin.new_record? %> - +
+ +
+ <%= f.check_box :open_comment %><%= t('announcement.open') %> +
+
+
" data-for="open_comment"> + +
+ <%= f.datetime_picker :comment_end_time, :no_label => true, :new_record => @bulletin.new_record? %> +
+
+
" data-for="open_comment"> + +
+ <%= check_box_tag 'bulletin[comment_role][]','visitor',@bulletin.comment_role.include?('visitor') %> + <%= t('announcement.visitor') %> +
+ <%= check_box_tag 'bulletin[comment_role][]','all_member',@bulletin.comment_role.include?('all_member'),class: 'role_all_member' %> + <%= t('announcement.all_member') %> +
+ <% Role.all.each do |role| %> + <%= check_box_tag 'bulletin[comment_role][]',role.id.to_s,@bulletin.comment_role.include?(role.id.to_s),class: 'role' %> + <%= role.title %> + <% end %> +
+
<% end %> @@ -459,6 +485,25 @@ } $(function() { + $('#bulletin_open_comment').change(function(){ + $(this).prop('checked') ? $('div[data-for="open_comment"]').removeClass('hide') : $('div[data-for="open_comment"]').addClass('hide') + }) + $('.role_all_member').change(function(){ + if ($(this).prop('checked')){ + $('.role').each(function() { + $(this).prop("checked", true); + }) + } else{ + $('.role').each(function() { + $(this).prop("checked", false); + }) + } + }) + $('.role').change(function(){ + if (!$(this).prop('checked') && $('.role_all_member').prop('checked')){ + $('.role_all_member').prop('checked',false) + } + }) if (location.pathname.substr(-3)=='new'){ var getDate = new Date(); var toDay = getDate.getFullYear()+"/"+ (Appendzero(getDate.getMonth()+1))+"/"+Appendzero(getDate.getDate())+" "+Appendzero(getDate.getHours())+":"+Appendzero(getDate.getMinutes()); diff --git a/app/views/admin/announcements/_index.html.erb b/app/views/admin/announcements/_index.html.erb index abf77f2..fa05392 100644 --- a/app/views/admin/announcements/_index.html.erb +++ b/app/views/admin/announcements/_index.html.erb @@ -57,6 +57,7 @@ <%= format_value b.postdate %> "><%= format_value b.deadline %> + <%= link_to b.bulletin_comments.count.to_s,"/#{I18n.locale}/admin/announcements/#{b.id}/comment" %> <%= b.update_user.user_name rescue ""%> diff --git a/app/views/admin/announcements/comment.html.erb b/app/views/admin/announcements/comment.html.erb new file mode 100644 index 0000000..b28a5d3 --- /dev/null +++ b/app/views/admin/announcements/comment.html.erb @@ -0,0 +1,12 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag("admin/tags") %> +<% end %> + +<%= render 'comment' %> \ No newline at end of file diff --git a/app/views/announcements/show.html.erb b/app/views/announcements/show.html.erb index 6d7c32f..4352f3a 100644 --- a/app/views/announcements/show.html.erb +++ b/app/views/announcements/show.html.erb @@ -73,4 +73,91 @@ <%= render_view %> <% if @show_back_and_next_flag==2 %> <%= content %> +<% end %> +<% site = Site.first + hover_color = site.orbit_bar_background_hover_color rescue nil + hover_color = '#0095CF' if hover_color.blank? + bg_color = site.orbit_bar_background_color rescue nil + bg_color = bg_color.blank? ? '#3a3b3c' : lighten_color(bg_color,-40) + color1 = complementaryColor(bg_color) + color2 = lighten_color(color1,30) +%> + + +<% action_data['comments'].reverse.each do |comment| %> +
+
+ <%= comment.time %> +
+
+ <%= comment.comment.html_safe %> +
+
+
+<% end %> +<% if action_data['show_comment_flag'] %> +
+
+ +
<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 6c1e5d1..11bd2ca 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,16 @@ en: feed: Feed import: Import announcement: + time: Time + send_comment: Send Comment + comment: Comment + account: Account + open_comment: Open for Comment + open: Open + comment_end_time: Comment End Time + comment_role: Comment Role + visitor: Visitor + all_member: All Member add_to_calendar: Add to calendar blank_to_set: (blank to use announcement setting) stime: start time diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 295a71d..700004e 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -3,6 +3,16 @@ zh_tw: feed: 供給 import: 匯入 announcement: + time: 時間 + send_comment: 送出留言 + comment: 留言內容 + account: 帳號 + open_comment: 開放評論 + open: 開放 + comment_end_time: 停止開放時間 + comment_role: 留言身分 + visitor: 訪客 + all_member: 全部會員 add_to_calendar: 加入行事曆 blank_to_set: (留白則使用公告設定) stime: 開始時間 diff --git a/config/routes.rb b/config/routes.rb index 60e0700..e21e3a3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,11 +23,13 @@ Rails.application.routes.draw do post 'announcement/import_from_wp', to: 'announcements#import_from_wp' post 'announcement/generate_iframe_url' => 'announcements#generate_iframe_url' resources :announcements + get 'announcements/:id/comment'=> 'announcements#comment' + get 'annc-comment-hidden/:id' => 'announcements#comment_hidden' end resources :announcements do collection do - get ':slug_title-:uid', to: 'announcements#show', as: :display + get ':slug_title-:uid', to: 'announcements#show', as: :display end end @@ -36,6 +38,7 @@ Rails.application.routes.draw do get "/xhr/announcements/feeds" => "announcement_feeds#feeds" get '/xhr/announcements/announcement.json', to: 'bulletins#get_bulletins' get '/xhr/panel/announcement/widget/sync_data' => 'announcements#show_widget' + get '/xhr/announcements/:slug_title-:uid/comment', to: 'announcements#comment' end end