added excel import for announcements
This commit is contained in:
parent
daa42b0ce2
commit
2983b2b691
|
@ -1,4 +1,5 @@
|
|||
# encoding: utf-8
|
||||
require 'rubyXL'
|
||||
class Admin::AnnouncementsController < OrbitAdminController
|
||||
include Admin::AnnouncementsHelper
|
||||
before_action ->(module_app = @app_title) { set_variables module_app }
|
||||
|
@ -43,6 +44,36 @@ class Admin::AnnouncementsController < OrbitAdminController
|
|||
end
|
||||
end
|
||||
|
||||
def import
|
||||
end
|
||||
|
||||
def excel_format
|
||||
respond_to do |format|
|
||||
format.xlsx {
|
||||
response.headers['Content-Disposition'] = 'attachment; filename="announcement_import_format.xlsx"'
|
||||
}
|
||||
end
|
||||
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
|
||||
next if v == "" || v.nil?
|
||||
import_this_announcement(row, categories, tags)
|
||||
end
|
||||
redirect_to admin_announcements_path
|
||||
else
|
||||
redirect_to admin_announcements_path(:error => "1")
|
||||
end
|
||||
end
|
||||
|
||||
def createsettings
|
||||
setting = AnnouncementSetting.new(settings_params)
|
||||
setting.save
|
||||
|
|
|
@ -25,6 +25,71 @@ module Admin::AnnouncementsHelper
|
|||
request.protocol+(request.host_with_port+ann_page.url+'/'+bulletin.to_param).gsub('//','/') rescue "/"
|
||||
end
|
||||
|
||||
def import_this_announcement(row,categories,tags)
|
||||
value = {}
|
||||
anns = Bulletin.new
|
||||
row.cells.each_with_index do |cell,index|
|
||||
next if cell.nil?
|
||||
val = 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.postdate = val
|
||||
when 3
|
||||
anns.deadline = val
|
||||
when 4
|
||||
anns.is_top = (val.to_i == 1 ? true : false)
|
||||
when 5
|
||||
anns.is_hot = (val.to_i == 1 ? true : false)
|
||||
when 6
|
||||
anns.is_hidden = (val.to_i == 1 ? true : false)
|
||||
when 7
|
||||
anns.remote_image_url = val
|
||||
when 8
|
||||
value["en"] = val
|
||||
when 9
|
||||
value["zh_tw"] = val
|
||||
anns.image_description_translations = value
|
||||
value = {}
|
||||
when 10
|
||||
value["en"] = val
|
||||
when 11
|
||||
value["zh_tw"] = val
|
||||
anns.title_translations = value
|
||||
value = {}
|
||||
when 12
|
||||
value["en"] = val
|
||||
when 13
|
||||
value["zh_tw"] = val
|
||||
anns.subtitle_translations = value
|
||||
value = {}
|
||||
when 14
|
||||
value["en"] = val
|
||||
when 15
|
||||
value["zh_tw"] = val
|
||||
anns.text_translations = value
|
||||
value = {}
|
||||
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 load_access_level
|
||||
if (current_user.is_admin? rescue false)
|
||||
@access_level = "admin"
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
# encoding: utf-8
|
||||
|
||||
wb = xlsx_package.workbook
|
||||
|
||||
wb.add_worksheet(name: "Annoucement") do |sheet|
|
||||
|
||||
heading = sheet.styles.add_style(:b => true, :locked => true)
|
||||
example = sheet.styles.add_style(:i => true)
|
||||
row = []
|
||||
row1 = []
|
||||
row2 = []
|
||||
|
||||
row << t("category")
|
||||
row1 << "select"
|
||||
t = ""
|
||||
@module_app.categories.asc(:created_at).each_with_index do |cat,i|
|
||||
t = t + "#{i}" + " -> " + cat.title + ", "
|
||||
end
|
||||
if @module_app.categories.count > 0
|
||||
t = t + " Example : 0"
|
||||
else
|
||||
t = "Leave this field blank"
|
||||
end
|
||||
row2 << t
|
||||
|
||||
row << t("tags")
|
||||
row1 << "select"
|
||||
t = ""
|
||||
@module_app.tags.asc(:created_at).each_with_index do |tag,i|
|
||||
t = t + "#{i}" + " -> " + tag.name + ", "
|
||||
end
|
||||
if @module_app.tags.count > 0
|
||||
t = t + " Example : 0,1,2"
|
||||
else
|
||||
t = "Leave this field blank"
|
||||
end
|
||||
row2 << t
|
||||
|
||||
row << t("start_date")
|
||||
row1 << "date"
|
||||
row2 << "Format: YYYY/MM/DD, Example: 2015/12/10"
|
||||
|
||||
row << t("end_date")
|
||||
row1 << "date"
|
||||
row2 << "Format: YYYY/MM/DD, Example: 2015/12/12"
|
||||
|
||||
row << t("top")
|
||||
row1 << "boolean"
|
||||
row2 << "0 for false, 1 for true"
|
||||
|
||||
row << t("hot")
|
||||
row1 << "boolean"
|
||||
row2 << "0 for false, 1 for true"
|
||||
|
||||
row << t("hide")
|
||||
row1 << "boolean"
|
||||
row2 << "0 for false, 1 for true "
|
||||
|
||||
row << t("image")
|
||||
row1 << "url"
|
||||
row2 << "http://www.example.com/images/example.png"
|
||||
|
||||
row << t("image") + " " + t("description") + " - " + t("en")
|
||||
row1 << "textfield"
|
||||
row2 << ""
|
||||
row << t("image") + " " + t("description") + " - " + t("zh_tw")
|
||||
row1 << "textfield"
|
||||
row2 << ""
|
||||
|
||||
row << t("title") + " - " + t("en")
|
||||
row1 << "textfield"
|
||||
row2 << ""
|
||||
row << t("title") + " - " + t("zh_tw")
|
||||
row1 << "textfield"
|
||||
row2 << ""
|
||||
|
||||
row << t("subtitle") + " - " + t("en")
|
||||
row1 << "textarea"
|
||||
row2 << ""
|
||||
row << t("subtitle") + " - " + t("zh_tw")
|
||||
row1 << "textarea"
|
||||
row2 << ""
|
||||
|
||||
row << t("content") + " - " + t("en")
|
||||
row1 << "editor"
|
||||
row2 << ""
|
||||
row << t("content") + " - " + t("zh_tw")
|
||||
row1 << "editor"
|
||||
row2 << ""
|
||||
|
||||
sheet.add_row row, :style => heading
|
||||
sheet.add_row row1
|
||||
sheet.add_row row2, :style => example
|
||||
|
||||
end
|
|
@ -0,0 +1,30 @@
|
|||
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" enctype="multipart/form-data">
|
||||
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
|
||||
<div class="input-area">
|
||||
<% if @module_app.categories.count > 0 %>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<a href="<%= admin_announcement_excel_format_path(:format => "xlsx") %>">Download example sheet here.</a>
|
||||
</div>
|
||||
</div>
|
||||
<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" />
|
||||
<span class="help-block">Please create all the tags and categories before hand. Only excel file is allowed.</span>
|
||||
</div>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<h4>Please create atleast one category before importing.</h4>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% if @module_app.categories.count > 0 %>
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Import" class="btn btn-primary">
|
||||
</div>
|
||||
<% end %>
|
||||
</form>
|
|
@ -18,6 +18,7 @@ en:
|
|||
approve: Approve
|
||||
all_articles: All Articles
|
||||
settings: Settings
|
||||
import: Import
|
||||
announcement: Announcement
|
||||
approval_setting: Approval Setting
|
||||
approve_bulletin_fail: Approval Fail
|
||||
|
|
|
@ -15,6 +15,7 @@ zh_tw:
|
|||
approve: 通過
|
||||
feed_name: Feed 標題
|
||||
settings: Settings
|
||||
import: Import
|
||||
rssfeed: Rss Feed Link
|
||||
feed_list: 訂閱清單
|
||||
all_articles: 文章列表
|
||||
|
|
|
@ -6,6 +6,8 @@ Rails.application.routes.draw do
|
|||
namespace :admin do
|
||||
post 'announcement/preview', to: 'announcements#preview'
|
||||
post 'announcement/createfeed', to: 'announcements#createfeed'
|
||||
post 'announcement/importanns', to: 'announcements#importanns'
|
||||
get 'announcement/excel_format', to: 'announcements#excel_format'
|
||||
patch 'announcement/updatefeed', to: 'announcements#updatefeed'
|
||||
delete 'announcement/deletefeed', to: 'announcements#deletefeed'
|
||||
get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview'
|
||||
|
@ -13,6 +15,7 @@ Rails.application.routes.draw do
|
|||
get 'announcement/feed', to: 'announcements#feed'
|
||||
get 'announcements/feedform', to: 'announcements#feedform'
|
||||
get 'announcement/settings', to: 'announcements#settings'
|
||||
get 'announcement/import', to: 'announcements#import'
|
||||
post 'announcement/createsettings', to: 'announcements#createsettings'
|
||||
patch 'announcement/updatesettings', to: 'announcements#updatesettings'
|
||||
resources :announcements
|
||||
|
|
|
@ -49,6 +49,11 @@ module Announcement
|
|||
:priority=>5,
|
||||
:active_for_action=>{'admin/announcements'=>'feed'},
|
||||
:available_for => 'managers'
|
||||
context_link 'announcement.import',
|
||||
:link_path=>"admin_announcement_import_path" ,
|
||||
:priority=>6,
|
||||
:active_for_action=>{'admin/announcements'=>'import'},
|
||||
:available_for => 'managers'
|
||||
context_link 'announcement.settings',
|
||||
:link_path=>"admin_announcement_settings_path" ,
|
||||
:priority=>6,
|
||||
|
|
Loading…
Reference in New Issue