require "net/http" require "uri" require 'json' module Admin::RecruitmentHelper module FormHelper extend self extend ActionView::Helpers::FormTagHelper extend ActionView::Helpers::FormOptionsHelper extend ActionView::Helpers::DateHelper extend ActionView::Helpers::TagHelper extend ActionView::Helpers::RenderingHelper extend ActionView::Context extend OrbitBasis::RenderAnywhere extend ActionView::Helpers::UrlHelper extend OrbitFormHelper extend Ckeditor::Helpers::FormHelper def available_locales @available_locales = @available_locales || Site.first.in_use_locales || I18n.available_locales end def set_input_name(input_name) @input_name = input_name end def get_input_name @input_name.to_s end def create_lang_panel(field) tmp2 = content_tag(:div,:class => 'btn-group', :data=>{:toggle=>"buttons-radio"}) do available_locales.collect do |key| link_entry_ary = ["##{field}","_#{key}"] link_entry = link_entry_ary.join link_to(I18n.t(key),link_entry,:data=>{:toggle=>"tab"},:class=>"btn #{(key == I18n.locale ? "active" : nil)}",:for=>key) end.join.html_safe end end def multiple_lang_tag(index1,type_of_tag,field,value=nil,custom_options={},combine_element='',exteral_options={},sortable=false) if !index1.nil? all_field = (get_input_name + "[#{index1}][#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'') else all_field = (get_input_name + "[#{field}][parant]").gsub(/\[/,'_').gsub(/\]/,'') end tmp = (available_locales.collect do |locale| active_flag = ((locale == I18n.locale) ? ' in active' : '') content_tag(:div,:class => "tab-content fade#{active_flag}",:id=>"#{all_field}_#{locale}") do value_locale = value[locale.to_s] rescue nil if !index1.nil? self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{index1}][#{field}][#{locale}]",value_locale,custom_options) else self.__send__("#{type_of_tag}_tag","#{get_input_name}[#{field}][#{locale}]",value_locale,custom_options) end end end.join + create_lang_panel(all_field)).html_safe + combine_element if sortable if exteral_options['style'].nil? exteral_options['style'] = 'display: flex;align-items: center;flex-wrap: nowrap;' else exteral_options['style'] = exteral_options['style'] + 'display: flex;align-items: center;flex-wrap: nowrap;' end content_tag(:div,{:class => "tab-panel border"}.merge(exteral_options)) do ("" +content_tag(:div) do tmp end).html_safe end else content_tag(:div,{:class => "tab-panel"}.merge(exteral_options)) do tmp end end end end def self.thead(field,center=false,enable_sort=true) sort = field.to_s.include?('.') ? field.to_s.split('.')[1] : field.to_s active = OrbitHelper.params[:sort].eql? sort order = active ? (["asc", "desc"]-[OrbitHelper.params[:order]]).first : "asc" arrow = (order.eql? "desc") ? "" : "" klass = field.eql?(:title) ? "span5" : "span2" th_data = (sort=="preview" || !enable_sort) ? RecruitmentCustomTitle.get_trans(field) : "#{RecruitmentCustomTitle.get_trans(field)} #{active ? arrow : ""}" "#{th_data}".html_safe end def page_for_recruitment(recruitment) ann_page = nil pages = Page.where(:module=>'recruitment_mod') pages.each do |page| if page.categories.count ==1 if page.categories.include?(recruitment.category.id.to_s) ann_page = page end end break if !ann_page.nil? end if ann_page.nil? pages.each do |page| if page.categories.include?(recruitment.category.id.to_s) ann_page = page end break if !ann_page.nil? end end ann_page = pages.first if ann_page.nil? request.protocol+(request.host_with_port+ann_page.url+'/'+recruitment.to_param).gsub('//','/') rescue "/" end def import_this_recruitment(row,categories,tags) value = {} anns = Recruitment.new row.cells.each_with_index do |cell,index| val = cell.nil? ? nil : cell.value next if val.nil? || val == "" case index when 0 anns.category = categories[val.to_i] when 1 new_tags = [] if (val.include?(",") rescue false) ts = val.split(",") ts.each do |t| new_tags << tags[t.to_i] end else new_tags << tags[val.to_i] end anns.tags=new_tags when 2 anns.recruitment_date = val when 3 anns.postdate = val when 4 anns.deadline = val when 5 anns.is_top = (val.to_i == 1 ? true : false) when 6 anns.is_hot = (val.to_i == 1 ? true : false) when 7 anns.is_hidden = (val.to_i == 1 ? true : false) when 8 anns.remote_image_url = val when 9 value["en"] = val anns.image_description_translations = value.clone when 10 value["zh_tw"] = val anns.image_description_translations = value.clone value = {} when 11 value["en"] = val anns.title_translations = value.clone when 12 value["zh_tw"] = val anns.title_translations = value.clone value = {} when 17 value["en"] = val anns.subtitle_translations = value.clone when 18 value["zh_tw"] = val anns.subtitle_translations = value.clone value = {} when 19 value["en"] = val anns.text_translations = value.clone when 20 value["zh_tw"] = val anns.text_translations = value.clone value = {} when 21 value["en"] = val anns.notes_translations = value.clone when 22 value["zh_tw"] = val anns.notes_translations = value.clone value = {} when 23 value["en"] = val anns.education_requirement_translations = value.clone when 24 value["zh_tw"] = val anns.education_requirement_translations = value.clone value = {} when 25 value["en"] = val anns.experience_requirement_translations = value.clone when 26 value["zh_tw"] = val anns.experience_requirement_translations = value.clone value = {} when 27 value["en"] = val anns.skills_requirement_translations = value.clone when 28 value["zh_tw"] = val anns.skills_requirement_translations = value.clone value = {} when 29 value["en"] = val anns.language_translations = value.clone when 30 value["zh_tw"] = val anns.language_translations = value.clone value = {} when 31 value["en"] = val anns.salary_info_translations = value.clone when 32 value["zh_tw"] = val anns.salary_info_translations = value.clone value = {} when 33 value["en"] = val anns.contact_info_translations = value.clone when 34 value["zh_tw"] = val anns.contact_info_translations = value.clone value = {} when 35 links = val.split(";") rescue [] desc_en = row.cells[36].value.split(";") rescue [] desc_zh_tw = row.cells[37].value.split(";") rescue [] links.each_with_index do |link,i| bl = RecruitmentLink.new bl.url = link.strip bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]} bl.recruitment_id = anns.id bl.save end when 38 files = val.split(";") rescue [] desc_en = row.cells[39].value.split(";") rescue [] desc_zh_tw = row.cells[40].value.split(";") rescue [] alt_en = row.cells[41].value.split(";") rescue [] alt_zh_tw = row.cells[42].value.split(";") rescue [] files.each_with_index do |file, i| bf = RecruitmentFile.new bf.remote_file_url = file.strip rescue nil bf.title_translations = {"en" => (alt_en[i] rescue ""), "zh_tw" => (alt_zh_tw[i] rescue "")} bf.description_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_zh_tw[i] rescue "")} bf.recruitment_id = anns.id bf.save end when 31 value["en"] = val anns.place_translations = value.clone when 32 value["zh_tw"] = val anns.place_translations = value.clone value = {} when 33 anns.event_end_date = val when 34 carousel_images = val.split(";") rescue [] desc_en = row.cells[35].value.split(";") rescue [] desc_zh_tw = row.cells[36].value.split(";") rescue [] carousel_images.each_with_index do |image, i| bc = RecruitmentCarouselImage.new bc.remote_file_url = image.strip rescue nil bc.description_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_zh_tw[i] rescue "")} bc.recruitment_id = anns.id bc.save end end end anns.create_user_id = current_user.id.to_s anns.update_user_id = current_user.id.to_s anns.approved = true anns.save end def send_rejection_email(recruitment,locale) user = User.find(recruitment.create_user_id) rescue nil if !user.nil? email = user.member_profile.email if !email.nil? && email != "" url = "http://#{request.host_with_port}/admin/recruitment/#{recruitment.id}/edit" datatosend = "

Hello #{user.name},

#{current_user.name} #{t("recruitment.rejected_recruitment")} : #{recruitment.rejection_reason} #{t("recruitment.click_here_to_see")}

" mail = Email.new(:mail_to => email, :mail_subject => "Recruitment rejected公告未通過 : #{recruitment.title_translations[locale]}.", :template => "email/recruitment_email.html.erb", :template_data => {"html" => datatosend}) mail.save mail.deliver rescue nil end end end def send_notification_mail_to_managers(recruitment, type, locale) users = [] if @recruitment_setting.email_to.include?("managers") authorizations = Authorization.where(:module_app_id => @module_app.id) users = authorizations.collect do |auth| auth.user end end if @recruitment_setting.email_to.include?("admins") wg = Workgroup.where(:key => "admin").first admins = User.where(:workgroup_id => wg.id) users.delete(nil) users = users.concat(admins.to_a) end if @recruitment_setting.email_to.include?("approvers") approvers = User.find(@recruitment_setting.approvers).to_a rescue [] auths = Authorization.where(:category_id => recruitment.category_id).collect{|a| a.user} users = users.concat(approvers & auths) end users.each do |user| email = user.member_profile.email if !email.nil? && email != "" send_email(user.name, email, recruitment, type, locale) # sleep(1) end end end def send_email(name, useremail, recruitment, type, locale) url = "http://#{request.host_with_port}/admin/recruitment?url=#{page_for_recruitment(recruitment).sub("http://" + request.host_with_port, "")}&id=#{recruitment.id}" case type when "approval" datatosend = "

#{t("recruitment.approval_mail_hi", :name => name)},

#{t("recruitment.submitted_new_recruitment", :poster => current_user.name)}

#{t("recruitment.approval_recruitment_title")} : #{recruitment.title_translations[locale]}
#{t("recruitment.click_here_to_see")} : #{url}

" when "reapproval" datatosend = "

#{t("recruitment.approval_mail_hi", :name => name)},

#{t("recruitment.updated_recruitment", :poster => current_user.name)}

#{t("recruitment.approval_recruitment_title")} : #{recruitment.title_translations[locale]}
#{t("recruitment.click_here_to_see")} : #{url}

" end email = Email.new(:mail_to => useremail, :mail_subject => " #{t("recruitment.recruitment_subject")} : #{recruitment.title_translations[locale]}.", :template => "email/recruitment_email.html.erb", :template_data => {"html" => datatosend}) email.save email.deliver rescue nil end def download_tmp_xml(url) xml = File.join(Rails.root, "tmp", "ann_cc_ntu.xml") open(xml, 'wb') do |fo| fo.print open(url).read end end def import_from_tmp_xml(file) xml = Nokogiri::XML(file) return if xml.nil? recruitment = [] xml.xpath("//channel").xpath("//item").each do |anns| recruitment << { :title => (anns>"title").text, :category => (anns>"category").text, :postdate => (anns>"pubDate").text, :text => (anns>"description").text, :rss2_sn => (anns>"link").text.split("=").last } end recruitment.each do |anns| ma = ModuleApp.where(:key => "recruitment").first cat = Category.where(:title => anns[:category]).first rescue nil if cat.nil? cat = Category.create(:title_translations => {"en" => anns[:category], "zh_tw" => anns[:category]}, :module_app_id => ma.id) end ann = Recruitment.where(:rss2_sn => anns[:rss2_sn]).first rescue nil if ann.nil? ann = Recruitment.new(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}, :rss2_sn => anns[:rss2_sn], :category_id => cat.id, :approved => true, :create_user_id => current_user.id) else ann.update_attributes(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}) end ann.save end File.delete(file) end def import_from_wordpress(xmlfile) xml_file = File.read(xmlfile) doc = Nokogiri::XML.parse(xml_file) doc.xpath("//channel").each do|channel_data| channel_data.xpath('//item').each do|itme| bu = Recruitment.where(:rss2_sn => itme.xpath('wp:post_id').text ).first rescue nil if bu.nil? bu = Recruitment.new bu.approved = true bu.rss2_sn = itme.xpath('wp:post_id').text bu.title_translations = {"en" => itme.xpath('title').text, "zh_tw" => itme.xpath('title').text} bu.text_translations = {"en" => itme.xpath('content:encoded').text, "zh_tw" => itme.xpath('content:encoded').text} bu.postdate = itme.xpath('wp:post_date').text itme.xpath('category').each do |i_cate| if i_cate["domain"].to_s == "category" cat = @module_app.categories.where(:title => i_cate.text.to_s).first rescue nil if cat.nil? cat = Category.new cat.module_app = @module_app cat.title_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s} cat.save end bu.category = cat elsif i_cate["domain"].to_s == "post_tag" tag = Tag.where(:name => i_cate.text.to_s ).first rescue nil if tag.nil? tag = Tag.new tag.name_translations = {"en" => i_cate.text.to_s, "zh_tw" => i_cate.text.to_s} tag.module_app_ids << @module_app.id tag.save end bu.tags = tag end end bu.save end end end File.delete(xmlfile) end def load_access_level if (current_user.is_admin? rescue false) @access_level = "admin" elsif (current_user.is_manager?(@module_app) rescue false) @access_level = "manager" else @access_level = "users" end end def user_can_approve?(anns=nil) can_approve = false setting = RecruitmentSetting.first case @access_level when "admin" can_approve = true when "manager" can_approve = true else can_approve = false end if !can_approve if !anns.nil? if setting.approvers.include?(current_user.id.to_s) if (current_user.approved_categories_for_module(@module_app).include?(anns.category) rescue false) can_approve = true end end else can_approve = setting.approvers.include?(current_user.id.to_s) end end can_approve end end