now can import files and links with excel
This commit is contained in:
parent
402562f743
commit
fc325dce2d
|
@ -55,6 +55,11 @@ class Admin::AnnouncementsController < OrbitAdminController
|
|||
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_announcements_path
|
||||
end
|
||||
|
||||
def importanns
|
||||
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
require "net/http"
|
||||
require "uri"
|
||||
require 'json'
|
||||
|
||||
module Admin::AnnouncementsHelper
|
||||
def page_for_bulletin(bulletin)
|
||||
ann_page = nil
|
||||
|
@ -82,6 +86,31 @@ module Admin::AnnouncementsHelper
|
|||
value["zh_tw"] = val
|
||||
anns.text_translations = value
|
||||
value = {}
|
||||
when 16
|
||||
links = val.split(";")
|
||||
desc_en = row.cells[17].value.split(";")
|
||||
desc_zh_tw = row.cells[18].value.split(";")
|
||||
links.each_with_index do |link,i|
|
||||
bl = BulletinLink.new
|
||||
bl.url = link.strip
|
||||
bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]}
|
||||
bl.bulletin_id = anns.id
|
||||
bl.save
|
||||
end
|
||||
when 19
|
||||
files = val.split(";")
|
||||
desc_en = row.cells[20].value.split(";")
|
||||
desc_zh_tw = row.cells[21].value.split(";")
|
||||
alt_en = row.cells[22].value.split(";")
|
||||
alt_zh_tw = row.cells[23].value.split(";")
|
||||
files.each_with_index do |file, i|
|
||||
bf = BulletinFile.new
|
||||
bf.remote_file_url = file.strip rescue nil
|
||||
bf.title_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_zh_tw[i] rescue "")}
|
||||
bf.description_translations = {"en" => (alt_en[i] rescue ""), "zh_tw" => (alt_zh_tw[i] rescue "")}
|
||||
bf.bulletin_id = anns.id
|
||||
bf.save
|
||||
end
|
||||
end
|
||||
end
|
||||
anns.create_user_id = current_user.id.to_s
|
||||
|
@ -90,6 +119,43 @@ module Admin::AnnouncementsHelper
|
|||
anns.save
|
||||
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?
|
||||
announcements = []
|
||||
xml.xpath("//channel").xpath("//item").each do |anns|
|
||||
announcements << {
|
||||
:title => (anns>"title").text,
|
||||
:category => (anns>"category").text,
|
||||
:postdate => (anns>"pubDate").text,
|
||||
:text => (anns>"description").text,
|
||||
:rss2_sn => (anns>"link").text.split("=").last
|
||||
}
|
||||
end
|
||||
announcements.each do |anns|
|
||||
ma = ModuleApp.where(:key => "announcement").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 = Bulletin.where(:rss2_sn => anns[:rss2_sn]).first rescue nil
|
||||
if ann.nil?
|
||||
ann = Bulletin.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 load_access_level
|
||||
if (current_user.is_admin? rescue false)
|
||||
@access_level = "admin"
|
||||
|
|
|
@ -88,6 +88,36 @@ wb.add_worksheet(name: "Annoucement") do |sheet|
|
|||
row1 << "editor"
|
||||
row2 << ""
|
||||
|
||||
row << t("link")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';'. Example: http://rulingcom.com; http://google.com"
|
||||
|
||||
row << t("link") + " " + t("url_alt") + " - " + t("en")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine"
|
||||
row << t("link") + " " + t("url_alt") + " - " + t("zh_tw")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine"
|
||||
|
||||
row << t("file_")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';'. Example: http://www.example.com/images/example.png; http://www.example.com/images/example2.png"
|
||||
|
||||
row << t("file_") + " " + t("description") + " - " + t("en")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait"
|
||||
row << t("file_") + " " + t("description") + " - " + t("zh_tw")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait"
|
||||
|
||||
row << t("file_") + " " + t("alternative") + " - " + t("en")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2"
|
||||
row << t("file_") + " " + t("alternative") + " - " + t("zh_tw")
|
||||
row1 << "textfield"
|
||||
row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2"
|
||||
|
||||
|
||||
sheet.add_row row, :style => heading
|
||||
sheet.add_row row1
|
||||
sheet.add_row row2, :style => example
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" enctype="multipart/form-data">
|
||||
<% content_for :page_specific_javascript do %>
|
||||
<script type="text/javascript" src="/assets/validator.js"></script>
|
||||
<% end %>
|
||||
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xls" enctype="multipart/form-data">
|
||||
<h3 style="padding-left: 30px;">Import from Excel</h3>
|
||||
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<% if @module_app.categories.count > 0 %>
|
||||
|
@ -10,7 +14,7 @@
|
|||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted">Upload :</label>
|
||||
<div class="controls">
|
||||
<input type="file" id="import-anns" name="import_file" />
|
||||
<input type="file" id="import-anns" name="import_file" data-fv-validation="required;mustbexls;" data-fv-messages="Cannot be empty; Must be an excel file.;" />
|
||||
<span class="help-block">Please create all the tags and categories before hand. Only excel file is allowed.</span>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -28,3 +32,26 @@
|
|||
</div>
|
||||
<% end %>
|
||||
</form>
|
||||
<!-- <form action="<%= admin_announcement_import_from_xml_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xml" enctype="multipart/form-data">
|
||||
<h3 style="padding-left: 30px;">Import from XML</h3>
|
||||
<%#= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<div class="control-group">
|
||||
<label for="import-anns" class="control-label muted">URL :</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="import-anns" name="import_xml" data-fv-validation="required;url;" data-fv-messages="Cannot be empty; Must be an URL.;" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Import" class="btn btn-primary">
|
||||
</div>
|
||||
</form> -->
|
||||
<script type="text/javascript">
|
||||
var form = new FormValidator($("#import-anns-xls"));
|
||||
form.validate_functions.mustbexls = function(val){
|
||||
var t = val.split("."),
|
||||
ext = t[t.length - 1];
|
||||
return (ext == "xls" || ext == "xlsx")
|
||||
}
|
||||
</script>
|
|
@ -7,6 +7,7 @@ Rails.application.routes.draw do
|
|||
post 'announcement/preview', to: 'announcements#preview'
|
||||
post 'announcement/createfeed', to: 'announcements#createfeed'
|
||||
post 'announcement/importanns', to: 'announcements#importanns'
|
||||
post 'announcement/import_from_xml', to: 'announcements#import_from_xml'
|
||||
get 'announcement/excel_format', to: 'announcements#excel_format'
|
||||
patch 'announcement/updatefeed', to: 'announcements#updatefeed'
|
||||
delete 'announcement/deletefeed', to: 'announcements#deletefeed'
|
||||
|
|
Loading…
Reference in New Issue