diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 172fa29..1517437 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -179,6 +179,14 @@ class Admin::AnnouncementsController < OrbitAdminController @bulletin = Bulletin.new @bulletin.email_sentdate = Time.now @reach_limit = @bulletin.check_status_limit(current_user,true) + if defined? Calendar + categories = user_authenticated_categories rescue ['all'] + if categories.first == "all" + @calendar_categories = CalendarType.all + else + @calendar_categories = CalendarType.where(:category_id.in => categories) rescue [] + end + end end def create @@ -200,6 +208,20 @@ class Admin::AnnouncementsController < OrbitAdminController end bulletin = Bulletin.new(bps) + if !defined?(Calendar).nil? + if bps[:add_to_calendar] == '0' && !bps[:event_id].blank? + Event.find(bps[:event_id]).destroy rescue nil + bps[:event_id] = nil + elsif bps[:add_to_calendar] == '1' + event = Event.find(bps[:event_id]) rescue Event.new(create_user_id: current_user.id) + e_start = bps[:calendar_start_date].blank? ? bps[:postdate] : bps[:calendar_start_date] + e_start = Time.now.to_datetime if e_start.blank? + e_end = bps[:calendar_end_date].blank? ? bps[:deadline] : bps[:calendar_end_date] + e_end = Time.now.to_datetime + 1.year if e_end.blank? + event.update_attributes(bulletin_id: bulletin.id,start: e_start,end: e_end,update_user_id: current_user.id,all_day: bps[:calendar_all_day],calendar_type_id: bps[:calendar_type_id],title: bps[:title_translations][I18n.locale],note: bps[:subtitle_translations][I18n.locale]) + bps[:event_id] = event.id + end + end bulletin.create_user_id = current_user.id bulletin.update_user_id = current_user.id if AnnouncementSetting.is_pro? @@ -239,6 +261,14 @@ class Admin::AnnouncementsController < OrbitAdminController @reach_limit = @bulletin.check_status_limit(current_user,true) @tags = @module_app.tags @categories = @module_app.categories.enabled + if defined? Calendar + categories = user_authenticated_categories rescue ['all'] + if categories.first == "all" + @calendar_categories = CalendarType.all + else + @calendar_categories = CalendarType.where(:category_id.in => categories) rescue [] + end + end @statuses = [] @bulletin.email_sentdate = Time.now if @bulletin.email_sent == false else @@ -268,6 +298,20 @@ class Admin::AnnouncementsController < OrbitAdminController bps[:is_hot] = bulletin.is_hot bps[:is_hidden] = bulletin.is_hidden end + if !defined?(Calendar).nil? + if bps[:add_to_calendar] == '0' && !bps[:event_id].blank? + Event.find(bps[:event_id]).destroy rescue nil + bps[:event_id] = nil + elsif bps[:add_to_calendar] == '1' + event = Event.find(bps[:event_id]) rescue Event.new(create_user_id: current_user.id) + e_start = bps[:calendar_start_date].blank? ? bps[:postdate] : bps[:calendar_start_date] + e_start = Time.now.to_datetime if e_start.blank? + e_end = bps[:calendar_end_date].blank? ? bps[:deadline] : bps[:calendar_end_date] + e_end = Time.now.to_datetime + 1.year if e_end.blank? + event.update_attributes(bulletin_id: bulletin.id,start: e_start,end: e_end,update_user_id: current_user.id,all_day: bps[:calendar_all_day],calendar_type_id: bps[:calendar_type_id],title: bps[:title_translations][I18n.locale],note: bps[:subtitle_translations][I18n.locale]) + bps[:event_id] = event.id + end + end bulletin.update_attributes(bps) bulletin.update_user_id = current_user.id if bulletin.rejected diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index c70e628..57853ab 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -15,6 +15,13 @@ class Bulletin before_destroy do AnnsCache.all.destroy end + field :add_to_calendar,type: Boolean,default: false + field :calendar_start_date, :type => DateTime + field :calendar_end_date, :type => DateTime + field :calendar_all_day,type: Boolean,default: false + field :calendar_type_id + field :event_id + field :title, type: String, localize: true field :subtitle, localize: true field :text, localize: true @@ -58,6 +65,17 @@ class Bulletin before_create :set_expire before_save :check_limit + + def calendar_type + CalendarType.where(:category_id.in => self.calendar_type_id) + end + def event + if !self.event_id.nil? + Event.where(:id => self.event_id).first + else + nil + end + end def check_limit check_status_limit(update_user) end diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 74d4785..3ec3379 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -22,7 +22,15 @@ <%= javascript_include_tag "lib/module-area" %> <%= javascript_include_tag "form" %> <% end %> - +
@@ -30,6 +38,9 @@
+ + <% if defined? Calendar %> +
+
+ +
+ <%= f.check_box :add_to_calendar,onchange: 'trigger_on_add_calendar(this)' %> +
+
+
> +
+ +
+ <%= f.select :calendar_type_id, @calendar_categories.collect{|t| [ t.title, t.id ]} %> +
+
+
+
+ + +
+
+ <%= f.datetime_picker :calendar_start_date, :new_record => @bulletin.new_record?, :no_label => true, :data=>{"picker-type" => "range", "range" => "start"} %> +
+
+
+
+ + +
+
+ <%= f.datetime_picker :calendar_end_date, :new_record => @bulletin.new_record?, :no_label => true, :data=>{"picker-type" => "range", "range" => "end"} %> +
+
+
+ +
+ <%= f.check_box :calendar_all_day %> +
+
+
+ <%= f.hidden_field :event_id %> +
+ <% end %> <% if((!AnnouncementSetting.first.only_manager_can_edit_status) || (AnnouncementSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index f3dd6ac..6c1e5d1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,8 @@ en: feed: Feed import: Import announcement: + add_to_calendar: Add to calendar + blank_to_set: (blank to use announcement setting) stime: start time etime: end time select_prompt: --select category-- diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 2eba1e2..295a71d 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -3,6 +3,8 @@ zh_tw: feed: 供給 import: 匯入 announcement: + add_to_calendar: 加入行事曆 + blank_to_set: (留白則使用公告設定) stime: 開始時間 etime: 結束時間 select_prompt: --選取類別--