diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index cfaf7be..9ebd14b 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -288,6 +288,10 @@ class Admin::AnnouncementsController < OrbitAdminController def update uid = params[:id].split('-').last bulletin = Bulletin.find_by(:uid=>uid) + Bulletin.where(:copy_id=>bulletin.id.to_s).destroy + bulletin.is_edit = true + bulletin.save + bulletin = bulletin.get_org_model bps = bulletin_params bps[:tags] = bps[:tags].blank? ? [] : bps[:tags] bps[:email_member_ids] = bps[:email_member_ids].blank? ? [] : bps[:email_member_ids] @@ -312,6 +316,8 @@ class Admin::AnnouncementsController < OrbitAdminController end bulletin.update_attributes(bps) bulletin.update_user_id = current_user.id + bulletin.expirable_created_at = nil + bulletin.is_preview = false if bulletin.rejected bulletin.reapproval = true bulletin.save @@ -397,6 +403,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin_data.delete('bulletin_files_attributes') bulletin_data.delete('bulletin_links_attributes') bulletin.update_attributes(bulletin_data) + bulletin.copy_id = params['bulletin_id'] else bulletin = Bulletin.new(bulletin_params) end @@ -406,7 +413,18 @@ class Admin::AnnouncementsController < OrbitAdminController #BulletinFeed.create_feed_cache(bulletin) render :text=>page_for_bulletin(bulletin) + "?preview=true" end - + def get_preview_action + bulletin = Bulletin.find_by(:uid=>params['uid']) + is_not_edit = (!bulletin.is_edit) + org_bulletin = bulletin.get_org_model + Bulletin.where(:copy_id=>org_bulletin.id.to_s).destroy + if is_not_edit + bulletin.destroy + render :json=> {:success=>true,:action=>"close"} + else + render :json=> {:success=>true,:action=>"redirect",:path=>edit_admin_announcement_path(:id=>org_bulletin.id)} + end + end def destroy_preview bulletin = Bulletin.find_by(:uid=>params['uid']) if bulletin.is_preview diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 1824630..d799367 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -26,6 +26,8 @@ class Bulletin before_destroy do AnnsCache.all.destroy end + field :is_edit, type: Boolean, default: false #use to check whether the preview record changed + field :copy_id 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 @@ -35,7 +37,7 @@ class Bulletin field :calendar_type_id field :event_id field :page_id - field :title, type: String, localize: true + field :title, as: :slug_title, type: String, localize: true field :subtitle, localize: true field :text, localize: true field :create_user_id @@ -88,6 +90,19 @@ class Bulletin before_save :check_limit index({postdate: 1}, { unique: false, background: true }) index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true }) + def get_org_model + if self.is_preview + org_model = nil + if self.copy_id + org_model = self.class.find(self.copy_id) rescue nil + else + org_model = self.class.where(:title=>self.title,:is_preview.ne=>true).desc(:updated_at).first + end + org_model.nil? ? self : org_model + else + self + end + end def to_calendar_param self.to_param end @@ -113,7 +128,7 @@ class Bulletin if status_settings.count != 0 reach_limit = status_settings.collect do |status_setting| status = status_setting.status - if status_setting.top_limit.to_i <= Bulletin.where(:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count + if status_setting.top_limit.to_i <= Bulletin.where(:is_preview.ne=>true,:update_user_id.in => Role.find(status_setting.role_id).member_profiles.collect(&:user).flatten.uniq.map{|v| v.id},status => true).count if !check_only if self[status] && !Bulletin.where(id:self.id).first[status] self[status] = false @@ -136,10 +151,6 @@ class Bulletin end reach_limit end - def slug_title - doc = Nokogiri::HTML(self.title) - title = doc.text.gsub('/','-') - end def set_expire self.expirable_created_at = Time.now if self.is_preview return true diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb index 9b7330d..3993f23 100644 --- a/app/views/admin/announcements/_form.html.erb +++ b/app/views/admin/announcements/_form.html.erb @@ -644,7 +644,11 @@ $('#remind-check').on('change', function() { $(this).prop('checked') ? $('.content-box').removeClass('hide'):$('.content-box').addClass('hide') }) - + try{ + if( self != top ){ + $('#button_for_preview,#show_preview').remove(); + } + }catch(e){} $('#button_for_preview').click(function(){ var method = $('.main-forms input[name="_method"]').val(); $('.main-forms input[name="_method"]').val("post"); @@ -666,11 +670,23 @@ if(window.location.protocol === "https:"){ data = data.replace("http:","https:"); } + window.preview_annc_uid = data.split("-").slice(-1)[0].split("?")[0]; $('.modal-body iframe').attr('src',data); $('#show_preview .modal').modal(); $('#show_preview .modal').height(function() { return $(window).height() * 0.7; }); + $('#show_preview .modal').on('hidden.bs.modal', function () { + $.ajax({ + type: "post", + url: '<%= admin_announcement_get_preview_action_path %>', + data: {uid: window.preview_annc_uid} + }).done(function(data){ + if(data["action"] == "redirect"){ + window.location.href = data["path"]; + } + }) + }); var slug = data.split('/')[(data.split('/').length-1)]; // $('#preview-iframe').on('load', function(){ diff --git a/config/routes.rb b/config/routes.rb index 481d6db..d68962a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do + post 'announcement/get_preview_action', to: 'announcements#get_preview_action' post 'announcement/preview', to: 'announcements#preview' post 'announcement/createfeed', to: 'announcements#createfeed' post 'announcement/importanns', to: 'announcements#importanns'