import from excel for albums

This commit is contained in:
Harry Bomrah 2016-12-20 19:43:54 +08:00
parent 310297f01b
commit f384525d29
9 changed files with 125 additions and 1 deletions

View File

@ -1,4 +1,6 @@
require 'rubyXL'
class Admin::GalleriesController < OrbitAdminController class Admin::GalleriesController < OrbitAdminController
include Admin::GalleriesHelper
before_filter :setup_vars before_filter :setup_vars
before_action :authenticate_user, :except => "imgs" before_action :authenticate_user, :except => "imgs"
before_action :log_user_action before_action :log_user_action
@ -60,6 +62,36 @@ class Admin::GalleriesController < OrbitAdminController
end end
end end
def import
@album = Album.find(params[:id])
end
def importimages
album = Album.find(params[:id])
workbook = RubyXL::Parser.parse(params["import_file"].tempfile)
sheet = workbook[0]
if sheet.count <= 203
sheet.each_with_index do |row, i|
next if i < 3
v = row.cells.first.value
next if v == "" || v.nil?
import_this_image(row, album)
end
redirect_to admin_gallery_path(album.id)
else
redirect_to import_admin_gallery_path(:error => "1")
end
end
def excel_format
@album = Album.find(params[:id])
respond_to do |format|
format.xlsx {
response.headers['Content-Disposition'] = 'attachment; filename="gallery_import_format.xlsx"'
}
end
end
def set_cover def set_cover
if params[:set_cover] == "true" if params[:set_cover] == "true"
album = Album.find(params[:album_id]) album = Album.find(params[:album_id])

View File

@ -69,6 +69,7 @@ class GalleriesController < ApplicationController
"alt_title" => alt_text, "alt_title" => alt_text,
"thumb-src" => a.file.thumb.url, "thumb-src" => a.file.thumb.url,
"thumb-large-src" => a.file.thumb_large.url, "thumb-large-src" => a.file.thumb_large.url,
"image_description" => a.description,
"mobile-src" => a.file.mobile.url, "mobile-src" => a.file.mobile.url,
"theater-src" => a.file.theater.url "theater-src" => a.file.theater.url
} }

View File

@ -1,2 +1,21 @@
module Admin::GalleriesHelper module Admin::GalleriesHelper
def import_this_image(row,album)
value = {}
image = AlbumImage.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
image.remote_file_url = val
when 1
value["zh_tw"] = val
value["en"] = row.cells[index + 1].value rescue ""
image.description_translations = value
end
end
image.album = album
image.save
end
end end

View File

@ -0,0 +1,28 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: @album.name[0..100]) do |sheet|
sheet.merge_cells "A1:C1"
heading = sheet.styles.add_style(:b => true, :locked => true, :alignment=>{:horizontal => :center})
example = sheet.styles.add_style(:i => true)
row = []
row1 = []
row2 = []
row << "Import for album #{@album.name}"
row1 << t("gallery.img_link")
row2 << "http://www.example.com/example.jpg"
row1 << t("gallery.img_description") + "(" + t(:zh_tw) + ")"
row2 << "This is an image"
row1 << t("gallery.img_description") + "(" + t(:en) + ")"
row2 << "This is an image"
sheet.add_row row, :style => heading
sheet.add_row row1
sheet.add_row row2, :style => example
end

View File

@ -0,0 +1,33 @@
<% content_for :page_specific_javascript do %>
<script type="text/javascript" src="/assets/validator.js"></script>
<% end %>
<form action="<%= importimages_admin_gallery_path(@album.id) %>" method="post" class="form-horizontal main-forms" id="import-images-xls" enctype="multipart/form-data">
<h3 style="padding-left: 30px;">Import from Excel for album <i><%= @album.name %></i></h3>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area">
<div class="control-group">
<div class="controls">
<a href="<%= excel_format_admin_gallery_path(@album.id,:format => "xlsx") %>">Download example sheet here.</a>
</div>
</div>
<div class="control-group">
<label for="import-images" class="control-label muted">Upload :</label>
<div class="controls">
<input type="file" id="import-images" name="import_file" data-fv-validation="required;mustbexls;" data-fv-messages="Cannot be empty; Must be an excel file.;" />
<span class="help-block"> Only excel file is allowed. Max 200 entries.</span>
</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-images-xls"));
form.validate_functions.mustbexls = function(val){
var t = val.split("."),
ext = t[t.length - 1];
return (ext == "xls" || ext == "xlsx")
}
</script>

View File

@ -20,6 +20,8 @@
<% if can_edit_or_delete?(@album) %> <% if can_edit_or_delete?(@album) %>
<a href="#dialog" data-toggle="modal" class="btn btn-warning btn-small deletephoto hide"><i class="icons-cross-3"></i> Delete Photo</a> <a href="#dialog" data-toggle="modal" class="btn btn-warning btn-small deletephoto hide"><i class="icons-cross-3"></i> Delete Photo</a>
<a href="#view-photo-tags" class="btn btn-primary btn-small addtags open hide" for="batch"><i class="icons-tag"></i> Add Tags</a> <a href="#view-photo-tags" class="btn btn-primary btn-small addtags open hide" for="batch"><i class="icons-tag"></i> Add Tags</a>
<a href="<%= import_admin_gallery_path(@album.id) %>" class="btn btn-small btn-info"><i class="icons-download"></i> Import</a>
<b class="divider"></b>
<a href="<%= edit_admin_gallery_path(@album.id) %>" class="btn btn-small btn-success"><i class="icon-edit"></i> Edit</a> <a href="<%= edit_admin_gallery_path(@album.id) %>" class="btn btn-small btn-success"><i class="icon-edit"></i> Edit</a>
<a href="#" class="btn btn-small btn-info order-btn-class" id="edit-order-btn">Edit Order</a> <a href="#" class="btn btn-small btn-info order-btn-class" id="edit-order-btn">Edit Order</a>
<b class="divider"></b> <b class="divider"></b>

View File

@ -27,6 +27,8 @@ en:
no_description: No Decription no_description: No Decription
photo_tag: Photo Tag photo_tag: Photo Tag
pic_not_found: Picture Not Found pic_not_found: Picture Not Found
img_link: Image Link
img_description: Image Description
save: Save save: Save
save_changes: Save Changes save_changes: Save Changes
search_tags: Search Tags search_tags: Search Tags

View File

@ -25,6 +25,8 @@ zh_tw:
no_description: 沒有描述 no_description: 沒有描述
photo_tag: 相片標籤 photo_tag: 相片標籤
pic_not_found: 找不到圖片 pic_not_found: 找不到圖片
img_link: Image Link
img_description: Image Description
save: 儲存 save: 儲存
save_changes: 儲存變更 save_changes: 儲存變更
search_tags: 搜尋標籤 search_tags: 搜尋標籤

View File

@ -17,6 +17,11 @@ Rails.application.routes.draw do
resources :galleries do resources :galleries do
get "imgs" => "galleries#imgs" get "imgs" => "galleries#imgs"
member do
get "import"
get "excel_format"
post "importimages"
end
end end
resources :images resources :images
post "galleries/upload_image" => "galleries#upload_image" post "galleries/upload_image" => "galleries#upload_image"