# encoding: utf-8 require 'rubyXL' class Admin::RecruitmentController < OrbitAdminController include Admin::RecruitmentHelper before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_recruitment, only: [:edit, :destroy] before_action :load_access_level, :load_settings def initialize super @app_title = "recruitment_mod" end def update_sort ids = params[:ids] ids.each_with_index do |id,i| Recruitment.where(id: id).update(sort_number: i) end RecruitmentCache.all.delete edit_sort render 'update_sort',layout: false end def update_sort_setting setting = @recruitment_setting setting.update_attributes(settings_params) update_enable_manually_sort(setting) redirect_to edit_sort_admin_recruitment_index_path end def edit_sort @setting = @recruitment_setting @recruitment = Recruitment.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1}) @table_fields = ['recruitment.table.sort_number','recruitment.table.title','recruitment.event_date','recruitment.start_date'] end def index Recruitment.remove_expired_status @tags = @module_app.tags @table_fields = [:status, :category, :title, "recruitment.event_date", "recruitment.start_date", "recruitment.end_date", :last_modified] @current_user = current_user if params[:sort].blank? params[:sort] = 'event_date' params[:order] = 'desc' end if RecruitmentSetting.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) @recruitment = Recruitment.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 @recruitment = Recruitment.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) @recruitment = Recruitment.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 @recruitment = search_data(@recruitment,[:title]).page(params[:page]).per(10) if request.xhr? render :partial => "index" end end def feed @table_feed_fields = ["recruitment.feed_name", :tags, :categories, "recruitment.rssfeed", "recruitment.jsonfeed"] @feeds = RecruitmentFeed.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/recruitment/widget/sync_data?'+url_params.to_param end def settings @setting = @recruitment_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="recruitment_import_format.xlsx"' } end end def export_excel @thread = Multithread.where(:key=>'export_recruitment').first update_flag = true if @thread.nil? @thread = Multithread.create(:key=>'export_recruitment',:status=>{:status=>'Processing'}) else update_flag = false if @thread.status[:status] == 'Processing' && @thread.respond_to?(:updated_at) && (@thread.updated_at > DateTime.now - 1.minute rescue false) if update_flag @thread.update(:status=>{:status=>'Processing'}) end end if update_flag Thread.new do begin @module_app = ModuleApp.where(:key => 'recruitment_mod').first @recruitment = Recruitment.where(:category_id.ne=>nil).desc(:created_at) last_updated = Recruitment.max(:updated_at).to_i filename = "public/recruitment_export_#{last_updated}.xlsx" if File.exist?(filename) @thread.update(:status=>{:status=>'finish','finish_percent'=>100,'info'=>I18n.t('recruitment.read_from_cache')}) else excel_contents = render_to_string( handlers: [:axlsx], formats: [:xlsx] , partial: 'export_excel.xlsx' ) File.open(filename, 'wb') do |f| f.write excel_contents end end @thread.status[:file] = filename @thread.status[:filename] = "recruitment_export_#{DateTime.now.in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y_%m_%d_%H%M')}.xlsx" @thread.save rescue => e @thread.status[:status] = 'error' # @thread.status[:info] = [e.to_s, e.backtrace] puts [e.to_s, e.backtrace] @thread.save end end end redirect_to admin_recruitment_import_path(:thread_id=>@thread.id.to_s) end def render_404 render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => 404, :formats => [:html] end def download_file_from_thread @thread = Multithread.where(:id=>params[:id]).first if params[:id].present? if @thread && @thread.status[:file] send_file(@thread.status[:file],:filename=>@thread.status[:filename]) else render_404 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_recruitment_index_path end def import @thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present? end def import_from_wp import_from_wordpress params["import_xml"].tempfile redirect_to admin_recruitment_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_recruitment(row, categories, tags) end redirect_to admin_recruitment_index_path else redirect_to admin_recruitment_index_path(:error => "1") end end def createsettings setting = RecruitmentSetting.new(settings_params) setting.save update_is_postdate_sort_first(setting) redirect_to admin_recruitment_settings_path end def updatesettings setting = @recruitment_setting ids = params['recruitment_setting']['anns_status_settings'].to_a.collect do |i,v| v['_id'] end.compact RecruitmentStatusSetting.where(:id.nin=>ids).destroy setting.update_attributes(settings_params) setting.save update_is_postdate_sort_first(setting) redirect_to admin_recruitment_settings_path end def feedform if params[:type] == "new" @recruitment_feed = RecruitmentFeed.new render :partial => "feed_form" else params[:type] == "edit" @recruitment_feed = RecruitmentFeed.find(params[:id]) render :partial => "edit_feed_form" end end def createfeed recruitment_feed = RecruitmentFeed.new(feed_params) recruitment_feed.save feeds = RecruitmentFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def updatefeed ann_feed = RecruitmentFeed.find(params[:id]) ann_feed.update_attributes(feed_params) ann_feed.save feeds = RecruitmentFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def deletefeed ann_feed = RecruitmentFeed.find(params[:id]) ann_feed.destroy feeds = RecruitmentFeed.all.asc(:created_at) render :partial => "feed", :collection => feeds end def new @tags = @module_app.tags @statuses = [] @recruitment = Recruitment.new @recruitment.email_sentdate = Time.now @reach_limit = @recruitment.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 = recruitment_params recruitment = Recruitment.new(bps) if !bps['recruitment_links_attributes'].nil? bps['recruitment_links_attributes'].each do |idx,link| bps['recruitment_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!RecruitmentSetting.first.only_manager_can_edit_status) || (RecruitmentSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !RecruitmentSetting.check_limit_for_user(recruitment.create_user_id, recruitment.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(recruitment_id: recruitment.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 recruitment.create_user_id = current_user.id recruitment.update_user_id = current_user.id if RecruitmentSetting.is_pro? if user_can_approve? recruitment.approved = true else send_notification_mail_to_managers(recruitment,"approval",I18n.locale) end else recruitment.approved = true end recruitment.save build_email(recruitment,I18n.locale) Thread.new do recruitment.notify_feed("create") end redirect_to params['referer_url'] end def approve_recruitment id = params[:id] recruitment = Recruitment.find(id) if params["approved"] == "true" recruitment.approved = true recruitment.rejected = false recruitment.reapproval = false else recruitment.rejected = true recruitment.reapproval = false recruitment.rejection_reason = params["reason"] send_rejection_email(recruitment,I18n.locale) end recruitment.save redirect_to admin_recruitment_index_path end def edit if can_edit_or_delete?(@recruitment) @reach_limit = @recruitment.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 = [] @recruitment.email_sentdate = Time.now if @recruitment.email_sent == false else render_401 end end def update uid = params[:id].split('-').last recruitment = Recruitment.find_by(:uid=>uid) bps = recruitment_params bps[:tags] = bps[:tags].blank? ? [] : bps[:tags] bps[:email_member_ids] = bps[:email_member_ids].blank? ? [] : bps[:email_member_ids] if !bps['recruitment_links_attributes'].nil? bps['recruitment_links_attributes'].each do |idx,link| bps['recruitment_links_attributes'].delete(idx.to_s) if link['url'].blank? end end if((!RecruitmentSetting.first.only_manager_can_edit_status) || (RecruitmentSetting.first.only_manager_can_edit_status && (@current_user.is_admin? || @current_user.is_manager?(@module_app))) ) if bps[:is_top] == "1" && !RecruitmentSetting.check_limit_for_user(recruitment.create_user_id, recruitment.id) bps[:is_top] = "0" bps[:top_end_date] = nil end else bps[:is_top] = recruitment.is_top bps[:is_hot] = recruitment.is_hot bps[:is_hidden] = recruitment.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(recruitment_id: recruitment.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 recruitment.update_attributes(bps) recruitment.update_user_id = current_user.id if recruitment.rejected recruitment.reapproval = true recruitment.save send_notification_mail_to_managers(recruitment,"reapproval",I18n.locale) else recruitment.save end build_email(recruitment,I18n.locale) Thread.new do recruitment.notify_feed("update") end now_recruitment_page = Recruitment.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort).map(&:id).map.with_index.select{|v,i| v==recruitment.id}[0][1] rescue nil now_recruitment_page = now_recruitment_page.nil? ? 0 : ((now_recruitment_page+1).to_f/10).ceil redirect_to admin_recruitment_index_path(:page=>now_recruitment_page) end def destroy @recruitment.destroy Thread.new do @recruitment.notify_feed("destroy") end redirect_to "/admin/recruitment" end def delete if params[:ids] Recruitment.any_in(:uid => params[:ids]).destroy_all Thread.new do Recruitment.notify_feed_delete(params[:ids]) end end redirect_to "/admin/recruitment" end def preview if params['preview_type'].eql?('edit') recruitment_data = recruitment_params org_recruitment = Recruitment.find(params['recruitment_id']) recruitment = org_recruitment.clone recruitment.generate_uid recruitment.recruitment_files = [] recruitment.recruitment_links = [] if recruitment_data['image'].blank? recruitment.image = org_recruitment.image end if !recruitment_data['recruitment_files_attributes'].blank? recruitment_data['recruitment_files_attributes'].each do |key, recruitment_file| next if !recruitment_file['_destroy'].blank? file = nil if recruitment_file['id'].blank? file = RecruitmentFile.new(recruitment_file) file.recruitment_id = recruitment.id file.save else org_file = RecruitmentFile.find(recruitment_file['id']) file = org_file.clone file.recruitment_id = recruitment.id file.file = org_file.file recruitment_file.delete('id') recruitment_file.delete('_destroy') file.update_attributes(recruitment_file) end file.save recruitment.recruitment_files << file end end if !recruitment_data['recruitment_links_attributes'].blank? recruitment_data['recruitment_links_attributes'].each do |key, recruitment_link| next if !recruitment_link['_destroy'].blank? if recruitment_link['id'].blank? link = RecruitmentLink.new(recruitment_link) link.recruitment_id = recruitment.id else link = RecruitmentLink.find(recruitment_link['id']).clone link.recruitment_id = recruitment.id recruitment_link.delete('id') recruitment_link.delete('_destroy') link.update_attributes(recruitment_link) end link.save recruitment.recruitment_links << link end end recruitment_data.delete('recruitment_files_attributes') recruitment_data.delete('recruitment_links_attributes') recruitment.update_attributes(recruitment_data) else recruitment = Recruitment.new(recruitment_params) end recruitment.is_preview = true recruitment.save render :text=>page_for_recruitment(recruitment) + "?preview=true" end def destroy_preview recruitment = Recruitment.find_by(:uid=>params['uid']) if recruitment.is_preview recruitment.destroy end render :json=>{'destroy'=>recruitment.id.to_s} end def build_email(recruitment,locale) if recruitment.email_sent and !recruitment.email_addresses.blank? if recruitment.email.nil? email = Email.new email.save email.deliver rescue nil recruitment.email_id = email.id recruitment.save end is_sent = recruitment.email.is_sent is_sent = !params[:resend_mail].eql?("true") if !params[:resend_mail].blank? doc = Nokogiri::HTML(recruitment.title_translations[locale]) title = doc.text.empty? ? 'no content' : doc.text recruitment.email.update_attributes( :create_user=>current_user, :mail_sentdate=>recruitment.email_sentdate, :module_app=>@module_app, :mail_lang => locale, :mail_to=>recruitment.email_addresses, :mail_subject=>title, :template=>'recruitment/email', :template_data=>{ "host" => request.host_with_port, "title" => title, "url" => page_for_recruitment(recruitment) }, :is_sent=>is_sent ) recruitment.email.deliver else recruitment.email.destroy if !recruitment.email.nil? end end def custom_fields_title @recruitment_custom_titles = RecruitmentCustomTitle.get_map end def update_custom_title recruitment_custom_title_params = params.require(:recruitment_custom_title).permit! recruitment_custom_title_params.each do |k,recruitment_custom_title_param| RecruitmentCustomTitle.find(recruitment_custom_title_param['id']).update_attributes(recruitment_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_recruitment_index_path end private def load_settings @recruitment_setting = RecruitmentSetting.first rescue nil if @recruitment_setting.nil? @recruitment_setting = RecruitmentSetting.create end end def set_recruitment @recruitment = Recruitment.find(params[:id]) end def recruitment_params params[:recruitment][:email_sent] = params[:recruitment][:email_sent].nil? ? 0 : params[:recruitment][:email_sent] params.require(:recruitment).permit! end def feed_params params.require(:recruitment_feed).permit! end def settings_params params.require(:recruitment_setting).permit! end def update_enable_manually_sort(setting) if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['recruitment_mod'][:enable_manually_sort] = setting.enable_manually_sort end end def update_is_postdate_sort_first(setting) if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['recruitment_mod'][:is_postdate_sort_first] = setting.is_postdate_sort_first feeds_time_field = (RecruitmentHelper.is_postdate_sort_first ? ['postdate', 'event_date'] : ['event_date', 'postdate']) @module_app.feeds_time_field = feeds_time_field @module_app.save if defined?(Feeds) Feeds::Migrate.sync_module_apps end end end end