# encoding: utf-8 require 'rubyXL' class Admin::EventNewsController < OrbitAdminController include Admin::EventNewsHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_event_news, only: [:edit, :destroy] before_action :load_access_level, :load_settings def initialize super @app_title = "event_news_mod" end def update_sort ids = params[:ids] ids.each_with_index do |id,i| EventNews.where(id: id).update(sort_number: i) end EventNewsCache.all.delete edit_sort render 'update_sort',layout: false end def update_sort_setting setting = @event_news_setting setting.update_attributes(settings_params) if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] = setting.enable_manually_sort end redirect_to edit_sort_admin_event_news_index_path end def edit_sort @setting = @event_news_setting @event_news = EventNews.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1}) @table_fields = ['event_news.table.sort_number','event_news.table.title','event_news.event_date','event_news.start_date'] end def index EventNews.remove_expired_status @tags = @module_app.tags @table_fields = [:status, :category, :title, "event_news.event_date", "event_news.start_date", "event_news.end_date", :last_modified] @current_user = current_user if params[:sort].blank? params[:sort] = 'event_date' params[:order] = 'desc' end if EventNewsSetting.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) @event_news = EventNews.where(:category_id.ne=>nil, :create_user_id=>current_user.id,:is_preview.in=>[false,nil]) .order_by(sort) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) else @event_news = EventNews.where(:category_id.ne=>nil, :uid=>nil).order_by(sort) @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) end else @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @event_news = EventNews.where(:category_id.ne=>nil, :is_preview.in=>[false,nil]) .order_by(sort) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) end @event_news = search_data(@event_news,[:title]).page(params[:page]).per(10) if request.xhr? render :partial => "index" end end def feed @table_feed_fields = ["event_news.feed_name", :tags, :categories, "event_news.rssfeed", "event_news.jsonfeed"] @feeds = EventNewsFeed.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_news/widget/sync_data?'+url_params.to_param end def settings @setting = @event_news_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_news_import_format.xlsx"' } end end def export_excel @event_news = EventNews.where(:category_id.ne=>nil).desc(:created_at) respond_to do |format| format.xlsx { response.headers['Content-Disposition'] = 'attachment; filename="event_news_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_news_index_path end def import end def import_from_wp import_from_wordpress params["import_xml"].tempfile redirect_to admin_event_news_index_path end def importanns 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 rescue nil next if v == "" || v.nil? import_this_event_news(row, categories, tags) end redirect_to admin_event_news_index_path else redirect_to admin_event_news_index_path(:error => "1") end end def createsettings setting = EventNewsSetting.new(settings_params) setting.save redirect_to admin_event_news_settings_path end def updatesettings setting = @event_news_setting ids = params['event_news_setting']['anns_status_settings'].to_a.collect do |i,v| v['_id'] end.compact AnnsStatusSetting.where(:id.nin=>ids).destroy setting.update_attributes(settings_params) setting.save redirect_to admin_event_news_settings_path end def feedform if params[:type] == "new" @event_news_feed = EventNewsFeed.new render :partial => "feed_form" else params[:type] == "edit" @event_news_feed = EventNewsFeed.find(params[:id]) render :partial => "edit_feed_form" end end def createfeed event_news_feed = EventNewsFeed.new(feed_params) event_news_feed.save feeds = EventNewsFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def updatefeed ann_feed = EventNewsFeed.find(params[:id]) ann_feed.update_attributes(feed_params) ann_feed.save feeds = EventNewsFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def deletefeed ann_feed = EventNewsFeed.find(params[:id]) ann_feed.destroy feeds = EventNewsFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def new @tags = @module_app.tags @statuses = [] @event_news = EventNews.new @event_news.email_sentdate = Time.now @reach_limit = @event_news.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 = event_news_params event_news = EventNews.new(bps) if !bps['event_news_links_attributes'].nil? bps['event_news_links_attributes'].each do |idx,link| bps['event_news_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!EventNewsSetting.first.only_manager_can_edit_status) || (EventNewsSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !EventNewsSetting.check_limit_for_user(event_news.create_user_id, event_news.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? # event.update_attributes(event_news_id: event_news.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 event_news.create_user_id = current_user.id event_news.update_user_id = current_user.id if EventNewsSetting.is_pro? if user_can_approve? event_news.approved = true else send_notification_mail_to_managers(event_news,"approval",I18n.locale) end else event_news.approved = true end event_news.save build_email(event_news,I18n.locale) redirect_to params['referer_url'] end def approve_event_news id = params[:id] event_news = EventNews.find(id) if params["approved"] == "true" event_news.approved = true event_news.rejected = false event_news.reapproval = false else event_news.rejected = true event_news.reapproval = false event_news.rejection_reason = params["reason"] send_rejection_email(event_news,I18n.locale) end event_news.save redirect_to admin_event_news_index_path end def edit if can_edit_or_delete?(@event_news) @reach_limit = @event_news.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 = [] @event_news.email_sentdate = Time.now if @event_news.email_sent == false else render_401 end end def update uid = params[:id].split('-').last event_news = EventNews.find_by(:uid=>uid) bps = event_news_params bps[:tags] = bps[:tags].blank? ? [] : bps[:tags] bps[:email_member_ids] = bps[:email_member_ids].blank? ? [] : bps[:email_member_ids] if !bps['event_news_links_attributes'].nil? bps['event_news_links_attributes'].each do |idx,link| bps['event_news_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!EventNewsSetting.first.only_manager_can_edit_status) || (EventNewsSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !EventNewsSetting.check_limit_for_user(event_news.create_user_id, event_news.id) bps[:is_top] = "0" bps[:top_end_date] = nil end else bps[:is_top] = event_news.is_top bps[:is_hot] = event_news.is_hot bps[:is_hidden] = event_news.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(event_news_id: event_news.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 event_news.update_attributes(bps) event_news.update_user_id = current_user.id if event_news.rejected event_news.reapproval = true event_news.save send_notification_mail_to_managers(event_news,"reapproval",I18n.locale) else event_news.save end build_email(event_news,I18n.locale) now_event_news_page = EventNews.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort).map(&:id).map.with_index.select{|v,i| v==event_news.id}[0][1] rescue nil now_event_news_page = now_event_news_page.nil? ? 0 : ((now_event_news_page+1).to_f/10).ceil redirect_to admin_event_news_index_path(:page=>now_event_news_page) end def destroy @event_news.destroy redirect_to "/admin/event_news" end def delete if params[:ids] EventNews.any_in(:uid => params[:ids]).destroy_all end redirect_to "/admin/event_news" end def preview if params['preview_type'].eql?('edit') event_news_data = event_news_params org_event_news = EventNews.find(params['event_news_id']) event_news = org_event_news.clone event_news.generate_uid event_news.event_news_files = [] event_news.event_news_links = [] if event_news_data['image'].blank? event_news.image = org_event_news.image end if !event_news_data['event_news_files_attributes'].blank? event_news_data['event_news_files_attributes'].each do |key, event_news_file| next if !event_news_file['_destroy'].blank? file = nil if event_news_file['id'].blank? file = EventNewsFile.new(event_news_file) file.event_news_id = event_news.id file.save else org_file = EventNewsFile.find(event_news_file['id']) file = org_file.clone file.event_news_id = event_news.id file.file = org_file.file event_news_file.delete('id') event_news_file.delete('_destroy') file.update_attributes(event_news_file) end file.save event_news.event_news_files << file end end if !event_news_data['event_news_links_attributes'].blank? event_news_data['event_news_links_attributes'].each do |key, event_news_link| next if !event_news_link['_destroy'].blank? if event_news_link['id'].blank? link = EventNewsLink.new(event_news_link) link.event_news_id = event_news.id else link = EventNewsLink.find(event_news_link['id']).clone link.event_news_id = event_news.id event_news_link.delete('id') event_news_link.delete('_destroy') link.update_attributes(event_news_link) end link.save event_news.event_news_links << link end end event_news_data.delete('event_news_files_attributes') event_news_data.delete('event_news_links_attributes') event_news.update_attributes(event_news_data) else event_news = EventNews.new(event_news_params) end event_news.is_preview = true event_news.save render :text=>page_for_event_news(event_news) + "?preview=true" end def destroy_preview event_news = EventNews.find_by(:uid=>params['uid']) if event_news.is_preview event_news.destroy end render :json=>{'destroy'=>event_news.id.to_s} end def build_email(event_news,locale) if event_news.email_sent and !event_news.email_addresses.blank? if event_news.email.nil? email = Email.new email.save email.deliver rescue nil event_news.email_id = email.id event_news.save end is_sent = event_news.email.is_sent is_sent = !params[:resend_mail].eql?("true") if !params[:resend_mail].blank? doc = Nokogiri::HTML(event_news.title_translations[locale]) title = doc.text.empty? ? 'no content' : doc.text event_news.email.update_attributes( :create_user=>current_user, :mail_sentdate=>event_news.email_sentdate, :module_app=>@module_app, :mail_lang => locale, :mail_to=>event_news.email_addresses, :mail_subject=>title, :template=>'event_news/email', :template_data=>{ "host" => request.host_with_port, "title" => title, "url" => page_for_event_news(event_news) }, :is_sent=>is_sent ) event_news.email.deliver else event_news.email.destroy if !event_news.email.nil? 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 admin_event_news_index_path end private def load_settings @event_news_setting = EventNewsSetting.first rescue nil if @event_news_setting.nil? @event_news_setting = EventNewsSetting.create end end def set_event_news @event_news = EventNews.find(params[:id]) end def event_news_params params[:event_news][:email_sent] = params[:event_news][:email_sent].nil? ? 0 : params[:event_news][:email_sent] params.require(:event_news).permit! end def feed_params params.require(:event_news_feed).permit! end def settings_params params.require(:event_news_setting).permit! end end