# encoding: utf-8 require 'rubyXL' class Admin::EventAnnsController < OrbitAdminController include Admin::EventAnnsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_bulletin, only: [:edit, :destroy] before_action :load_access_level, :load_settings def initialize super @app_title = "event_ann" end def index BulletinEvent.remove_expired_status @tags = @module_app.tags @table_fields = [:status, :category, :title,"event_anns.event_date", "event_anns.start_date", "event_anns.end_date", :last_modified] @current_user = current_user if EventAnnSetting.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 if current_user_is_sub_manager @categories = current_user.approved_categories.select{|c| c.module_app_id == @module_app.id} rescue [] @filter_fields = filter_fields(@categories, @tags) @bulletin_events = BulletinEvent.where(:create_user_id=>current_user.id,:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) else @bulletin_events = BulletinEvent.where(:uid=>nil) @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) end else @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @bulletin_events = BulletinEvent.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) end @bulletin_events = search_data(@bulletin_events,[:title]).page(params[:page]).per(10) if request.xhr? render :partial => "index" end end def feed @table_feed_fields = ["event_ann.feed_name", :tags, "event_ann.rssfeed", "event_ann.jsonfeed"] @feeds = BulletinFeedEvent.all.asc(:created_at) end def generate_iframe_url iframe_params = params.require(:iframe).permit! uids = iframe_params['member_ids'].to_a.map{|m_id| MemberProfile.find(m_id).uid rescue nil}.select{|uid| !uid.nil?} url_params = iframe_params.except(:member_ids) url_params['uids'] = uids if uids != [] render :text => '/xhr/panel/event_ann/widget/sync_data?'+url_params.to_param end def settings @setting = @event_ann_setting roles = Role.all @sorted_members = roles.inject({}) do |members,role| members_for_role = role.member_profiles.select{|m| (m.user.nil? ? false : m.user.approved)} members[role] = members_for_role members end @sorted_members['no_role'] = MemberProfile.any_in(:role_ids=>[nil,[]]).select{|m| (m.user.nil? ? false : m.user.approved)} @unapproved_members = User.where(:approved => false).map{|u| u.member_profile} end def import end def excel_format respond_to do |format| format.xlsx { response.headers['Content-Disposition'] = 'attachment; filename="event_ann_import_format.xlsx"' } end end def export_excel @event_anns = BulletinEvent.all.desc(:created_at) respond_to do |format| format.xlsx { response.headers['Content-Disposition'] = 'attachment; filename="event_ann_export.xlsx"' } end end def import_from_xml download_tmp_xml params["import_xml"] import_from_tmp_xml File.read(File.join(Rails.root, "tmp", "ann_cc_ntu.xml")) redirect_to admin_event_anns_path end def import end def import_from_wp import_from_wordpress params["import_xml"].tempfile redirect_to admin_event_anns_path end def importeanns workbook = RubyXL::Parser.parse(params["import_file"].tempfile) categories = @module_app.categories.asc(:created_at).to_a tags = @module_app.tags.asc(:created_at).to_a sheet = workbook[0] if sheet.count <= 503 sheet.each_with_index do |row, i| next if i < 3 v = row.cells.first.value next if v == "" || v.nil? import_this_event_ann(row, categories, tags) end redirect_to admin_event_anns_path else redirect_to admin_event_anns_path(:error => "1") end end def createsettings setting = EventAnnSetting.new(settings_params) setting.save redirect_to admin_event_ann_settings_path end def updatesettings setting = @event_ann_setting ids = params['event_ann_setting']['event_anns_status_settings'].to_a.collect do |i,v| v['_id'] end.compact EventAnnsStatusSetting.where(:id.nin=>ids).destroy setting.update_attributes(settings_params) setting.save redirect_to admin_event_ann_settings_path end def feedform if params[:type] == "new" @event_ann_feed = BulletinFeedEvent.new render :partial => "feed_form" else params[:type] == "edit" @event_ann_feed = BulletinFeedEvent.find(params[:id]) render :partial => "edit_feed_form" end end def createfeed event_ann_feed = BulletinFeedEvent.new(feed_params) event_ann_feed.save feeds = BulletinFeedEvent.all.asc(:created_at) render :partial => "feed", :collection => feeds end def updatefeed ann_feed = BulletinFeedEvent.find(params[:id]) ann_feed.update_attributes(feed_params) ann_feed.save feeds = BulletinFeedEvent.all.asc(:created_at) render :partial => "feed", :collection => feeds end def deletefeed ann_feed = BulletinFeedEvent.find(params[:id]) ann_feed.destroy feeds = BulletinFeedEvent.all.asc(:created_at) render :partial => "feed", :collection => feeds end def new @tags = @module_app.tags @statuses = [] @bulletin_event = BulletinEvent.new @bulletin_event.email_sentdate = Time.now @reach_limit = @bulletin_event.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 bps = bulletin_params bulletin_event = BulletinEvent.new(bps) if !bps['bulletin_event_links_attributes'].nil? bps['bulletin_event_links_attributes'].each do |idx,link| bps['bulletin_event_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!EventAnnSetting.first.only_manager_can_edit_status) || (EventAnnSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !EventAnnSetting.check_limit_for_user(bulletin_event.create_user_id, bulletin_event.id) bps[:is_top] = "0" bps[:top_end_date] = nil end else bps[:is_top] = false bps[:is_hot] = false bps[:is_hidden] = false 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? e_eventdate = bps[:calendar_end_date].blank? ? bps[:eventdate] : bps[:calendar_event_date] e_eventdate = Time.now.to_datetime + 1.year if e_eventdate.blank? event.update_attributes(bulletin_id: bulletin_event.id,start: e_start,end: e_end,event_date:e_eventdate,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],speaker: bps[:speaker_translations][I18n.locale],host: bps[:host_translations][I18n.locale],notes: bps[:notes_translations][I18n.locale]) bps[:event_id] = event.id end end bulletin_event.create_user_id = current_user.id bulletin_event.update_user_id = current_user.id if EventAnnSetting.is_pro? if user_can_approve? bulletin_event.approved = true else send_notification_mail_to_managers(bulletin_event,"approval",I18n.locale) end else bulletin_event.approved = true end bulletin_event.save build_email(bulletin_event,I18n.locale) redirect_to params['referer_url'] end def approve_bulletin id = params[:id] bulletin_event = BulletinEvent.find(id) if params["approved"] == "true" bulletin_event.approved = true bulletin_event.rejected = false bulletin_event.reapproval = false else bulletin_event.rejected = true bulletin_event.reapproval = false bulletin_event.rejection_reason = params["reason"] send_rejection_email(bulletin_event,I18n.locale) end bulletin_event.save redirect_to admin_event_anns_path end def edit if can_edit_or_delete?(@bulletin_event) @reach_limit = @bulletin_event.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_event.email_sentdate = Time.now if @bulletin_event.email_sent == false else render_401 end end def update uid = params[:id].split('-').last bulletin_event = BulletinEvent.find_by(:uid=>uid) bps = bulletin_params bps[:tags] = bps[:tags].blank? ? [] : bps[:tags] bps[:email_member_ids] = bps[:email_member_ids].blank? ? [] : bps[:email_member_ids] if !bps['bulletin_event_links_attributes'].nil? bps['bulletin_event_links_attributes'].each do |idx,link| bps['bulletin_event_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!EventAnnSetting.first.only_manager_can_edit_status) || (EventAnnSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !EventAnnSetting.check_limit_for_user(bulletin_event.create_user_id, bulletin_event.id) bps[:is_top] = "0" bps[:top_end_date] = nil end else bps[:is_top] = bulletin_event.is_top bps[:is_hot] = bulletin_event.is_hot bps[:is_hidden] = bulletin_event.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_event.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],speaker: bps[:speaker_translations][I18n.locale],host: bps[:host_translations][I18n.locale],notes: bps[:notes_translations][I18n.locale]) bps[:event_id] = event.id end end bulletin_event.update_attributes(bps) bulletin_event.update_user_id = current_user.id if bulletin_event.rejected bulletin_event.reapproval = true bulletin_event.save send_notification_mail_to_managers(bulletin_event,"reapproval",I18n.locale) else bulletin_event.save end build_email(bulletin_event,I18n.locale) now_bulletin_page = BulletinEvent.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort).map(&:id).map.with_index.select{|v,i| v==bulletin_event.id}[0][1] rescue nil now_bulletin_page = now_bulletin_page.nil? ? 0 : ((now_bulletin_page+1).to_f/10).ceil redirect_to "/zh_tw/admin/event_anns?page=#{now_bulletin_page}" end def destroy @bulletin_event.destroy redirect_to "/admin/event_anns" end def delete if params[:ids] BulletinEvent.any_in(:uid => params[:ids]).destroy_all end redirect_to "/admin/event_anns" end def preview if params['preview_type'].eql?('edit') bulletin_data = bulletin_params org_bulletin = BulletinEvent.find(params['bulletin_id']) bulletin_event = org_bulletin.clone bulletin_event.generate_uid bulletin_event.bulletin_event_files = [] bulletin_event.bulletin_event_links = [] if bulletin_data['image'].blank? bulletin_event.image = org_bulletin.image end if !bulletin_data['bulletin_event_files_attributes'].blank? bulletin_data['bulletin_event_files_attributes'].each do |key, bulletin_event_file| next if !bulletin_event_file['_destroy'].blank? file = nil if bulletin_event_file['id'].blank? file = BulletinFileEvent.new(bulletin_event_file) file.bulletin_id = bulletin_event.id file.save else org_file = BulletinFileEvent.find(bulletin_event_file['id']) file = org_file.clone file.bulletin_id = bulletin_event.id file.file = org_file.file bulletin_event_file.delete('id') bulletin_event_file.delete('_destroy') file.update_attributes(bulletin_event_file) end file.save bulletin_event.bulletin_event_files << file end end if !bulletin_data['bulletin_event_links_attributes'].blank? bulletin_data['bulletin_event_links_attributes'].each do |key, bulletin_event_link| next if !bulletin_event_link['_destroy'].blank? if bulletin_event_link['id'].blank? link = BulletinLinkEvent.new(bulletin_event_link) link.bulletin_id = bulletin_event.id else link = BulletinLinkEvent.find(bulletin_event_link['id']).clone link.bulletin_id = bulletin_event.id bulletin_event_link.delete('id') bulletin_event_link.delete('_destroy') link.update_attributes(bulletin_event_link) end link.save bulletin_event.bulletin_event_links << link end end bulletin_data.delete('bulletin_event_files_attributes') bulletin_data.delete('bulletin_event_links_attributes') bulletin_event.update_attributes(bulletin_data) else bulletin_event = BulletinEvent.new(bulletin_params) end bulletin_event.is_preview = true bulletin_event.save render :text=>page_for_bulletin(bulletin_event) + "?preview=true" end def destroy_preview bulletin_event = BulletinEvent.find_by(:uid=>params['uid']) if bulletin_event.is_preview bulletin_event.destroy end render :json=>{'destroy'=>bulletin_event.id.to_s} end def build_email(bulletin_event,locale) if bulletin_event.email_sent and !bulletin_event.email_addresses.blank? if bulletin_event.email.nil? email = Email.new email.save email.deliver rescue nil bulletin_event.email_id = email.id bulletin_event.save end is_sent = bulletin_event.email.is_sent is_sent = !params[:resend_mail].eql?("true") if !params[:resend_mail].blank? doc = Nokogiri::HTML(bulletin_event.title_translations[locale]) title = doc.text.empty? ? 'no content' : doc.text bulletin_event.email.update_attributes( :create_user=>current_user, :mail_sentdate=>bulletin_event.email_sentdate, :module_app=>@module_app, :mail_lang => locale, :mail_to=>bulletin_event.email_addresses, :mail_subject=>title, :template=>'event_anns/email', :template_data=>{ "host" => request.host_with_port, "title" => title, "url" => page_for_bulletin(bulletin_event) }, :is_sent=>is_sent ) bulletin_event.email.deliver else bulletin_event.email.destroy if !bulletin_event.email.nil? end end private def load_settings @event_ann_setting = EventAnnSetting.first rescue nil if @event_ann_setting.nil? @event_ann_setting = EventAnnSetting.create end end def set_bulletin @bulletin_event = BulletinEvent.find(params[:id]) end def bulletin_params params[:bulletin_event][:email_sent] = params[:bulletin_event][:email_sent].nil? ? 0 : params[:bulletin_event][:email_sent] params.require(:bulletin_event).permit! end def feed_params params.require(:bulletin_feed).permit! end def settings_params params.require(:event_ann_setting).permit! end end