diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 5644b54..adf9e5d 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -1,4 +1,6 @@ +require 'rubyXL' class Admin::GalleriesController < OrbitAdminController + include Admin::GalleriesHelper before_filter :setup_vars before_action :authenticate_user, :except => "imgs" before_action :log_user_action @@ -60,6 +62,36 @@ class Admin::GalleriesController < OrbitAdminController 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 if params[:set_cover] == "true" album = Album.find(params[:album_id]) diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index c9110ca..d68a7f5 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -69,6 +69,7 @@ class GalleriesController < ApplicationController "alt_title" => alt_text, "thumb-src" => a.file.thumb.url, "thumb-large-src" => a.file.thumb_large.url, + "image_description" => a.description, "mobile-src" => a.file.mobile.url, "theater-src" => a.file.theater.url } diff --git a/app/helpers/admin/galleries_helper.rb b/app/helpers/admin/galleries_helper.rb index 63810a1..14fc6fd 100644 --- a/app/helpers/admin/galleries_helper.rb +++ b/app/helpers/admin/galleries_helper.rb @@ -1,2 +1,21 @@ 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 diff --git a/app/views/admin/galleries/excel_format.xlsx.axlsx b/app/views/admin/galleries/excel_format.xlsx.axlsx new file mode 100644 index 0000000..7efb55c --- /dev/null +++ b/app/views/admin/galleries/excel_format.xlsx.axlsx @@ -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 \ No newline at end of file diff --git a/app/views/admin/galleries/import.html.erb b/app/views/admin/galleries/import.html.erb new file mode 100644 index 0000000..4c76fd6 --- /dev/null +++ b/app/views/admin/galleries/import.html.erb @@ -0,0 +1,33 @@ +<% content_for :page_specific_javascript do %> + +<% end %> +
+

Import from Excel for album <%= @album.name %>

+ <%= hidden_field_tag :authenticity_token, form_authenticity_token %> +
+ +
+ +
+ + Only excel file is allowed. Max 200 entries. +
+
+
+
+ +
+
+ + \ No newline at end of file diff --git a/app/views/admin/galleries/show.html.erb b/app/views/admin/galleries/show.html.erb index 65fd38c..2bf3c08 100644 --- a/app/views/admin/galleries/show.html.erb +++ b/app/views/admin/galleries/show.html.erb @@ -20,6 +20,8 @@ <% if can_edit_or_delete?(@album) %> Delete Photo Add Tags + Import + Edit Edit Order diff --git a/config/locales/en.yml b/config/locales/en.yml index a9e3998..f682298 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -27,6 +27,8 @@ en: no_description: No Decription photo_tag: Photo Tag pic_not_found: Picture Not Found + img_link: Image Link + img_description: Image Description save: Save save_changes: Save Changes search_tags: Search Tags diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 4739fba..08475a2 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -25,6 +25,8 @@ zh_tw: no_description: 沒有描述 photo_tag: 相片標籤 pic_not_found: 找不到圖片 + img_link: Image Link + img_description: Image Description save: 儲存 save_changes: 儲存變更 search_tags: 搜尋標籤 diff --git a/config/routes.rb b/config/routes.rb index 533c672..1fb1b14 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,12 @@ Rails.application.routes.draw do post "galleries/order" => "images#changeorder" resources :galleries do - get "imgs" => "galleries#imgs" + get "imgs" => "galleries#imgs" + member do + get "import" + get "excel_format" + post "importimages" + end end resources :images post "galleries/upload_image" => "galleries#upload_image"