archive/app/controllers/admin/archive_files_controller.rb

239 lines
8.2 KiB
Ruby
Raw Normal View History

2014-05-14 11:52:06 +00:00
class Admin::ArchiveFilesController < OrbitAdminController
2021-06-16 03:52:20 +00:00
require 'action_dispatch/http/upload'
2019-09-16 12:16:53 +00:00
def show
2019-09-16 12:32:26 +00:00
module_pages = Page.where(:module => 'archive').collect{|p| p.url}
if module_pages.length<1
render :text => t('archive.no_page').to_s
else
redirect_to '/' + I18n.locale.to_s + module_pages[0] + '?title=' + params['title'].to_s
end
2019-09-16 12:16:53 +00:00
end
def save_categories_order
keys = params['data_keys']
values = params['data_values']
keys.each do |key|
key.slice! 'sort_number-'
end
keys.each_with_index do |key,index|
archivecategory = ArchiveCategory.where(category_id: key)
if !(values[index].empty?)
archivecategory.first.update_attributes(sort_number: values[index].to_i)
else
archivecategory.first.update_attributes(sort_number: nil)
end
end
render :json => {}.to_json
end
2014-05-08 06:03:33 +00:00
def index
if ArchiveSortOrder.count == 0
ArchiveSortOrder.create('sort_order' => false)
end
2019-09-16 11:46:22 +00:00
if !(params['order_asc'].nil?)
if params['order_asc'] == 'true'
ArchiveSortOrder.first.update_attributes('sort_order' => true)
elsif params['order_asc'] == 'false'
ArchiveSortOrder.first.update_attributes('sort_order' => false)
end
end
@choose = ArchiveSortOrder.first['sort_order']
2016-03-14 07:47:38 +00:00
@table_fields = [:status, :category, :title, :updated_at,:last_modified,"archive.downloaded_times"]
2021-12-13 14:42:38 +00:00
@categories = @module_app.categories.enabled
2014-05-14 11:52:06 +00:00
@tags = @module_app.tags
2014-08-07 10:46:24 +00:00
@filter_fields = filter_fields(@categories, @tags)
@archives = ArchiveFile.local_data.order_by(sort)
2014-08-07 10:46:24 +00:00
.with_categories(filters("category"))
.with_tags(filters("tag"))
.with_status(filters("status"))
2014-05-14 11:52:06 +00:00
2014-08-07 10:46:24 +00:00
@archives = search_data(@archives,[:title]).page(params[:page]).per(10)
render :partial => "index" if request.xhr?
2014-05-14 11:52:06 +00:00
end
def new
sort_number = ArchiveSortOrder.first.get_default_order
@archive_file = ArchiveFile.new(:sort_number=>sort_number)
@tags = @module_app.tags
@categories = @module_app.categories.enabled
2014-05-14 11:52:06 +00:00
end
def edit
2014-07-31 12:40:44 +00:00
@archive_file = ArchiveFile.find(params[:id])
if can_edit_or_delete?(@archive_file)
@tags = @module_app.tags
2021-12-13 14:42:38 +00:00
@categories = @module_app.categories.enabled
2014-07-31 12:40:44 +00:00
else
render_401
end
2014-05-14 11:52:06 +00:00
end
def categories_order
categories = @module_app.categories
categories.each do |category|
archive_cat = ArchiveCategory.all.select{ |value| value.category_id.to_s == category.id.to_s}
if archive_cat.length == 0
ArchiveCategory.create(category_id: category.id.to_s)
end
end
cats_with_nil = ArchiveCategory.all.in(sort_number: nil)
cats_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
@cats = cats_no_nil.concat(cats_with_nil).collect do |cat|
[categories.where(:_id => cat.category_id).first,cat.sort_number.to_s]
end
render :partial => "categories_order" if request.xhr?
end
2014-08-07 10:46:24 +00:00
def create
2021-09-23 03:35:44 +00:00
params["archive_file"]["archive_file_multiples_attributes"].each do |k,v|
if v["file"].class == String
file_content_info = JSON.parse(v["file"])
content_type = file_content_info["type"]
filename = file_content_info["name"]
head = "Content-Disposition: form-data; name=\"archive_file[archive_file_multiples_attributes][#{k}][file]\"; filename=\"#{filename}\" Content-Type: #{content_type}"
tempfile = Tempfile.new(filename,binmode: true)
tempfile.write(file_content_info["content"].unpack('U*').map{|i| i.chr }.join)
v["file"] = ActionDispatch::Http::UploadedFile.new({:filename=>filename,:type=>content_type,:head=>head,:tempfile=>tempfile})
end
end rescue nil
2014-05-14 11:52:06 +00:00
@archive_file = ArchiveFile.new(archive_vars)
@archive_file.create_user_id = current_user.id
@archive_file.update_user_id = current_user.id
respond_to do |format|
if @archive_file.save
Thread.new do
@archive_file.notify_feed("create")
end
format.html { redirect_to(admin_archive_files_path) }
2014-05-14 11:52:06 +00:00
format.xml { render :xml => @archive_file, :status => :created, :location => @archive_file }
else
@tags = @module_app.tags
format.html { render :action => "new" }
format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity }
end
end
end
# PUT /archive_files/1
# PUT /archive_files/1.xml
def update
2021-06-16 03:52:20 +00:00
params["archive_file"]["archive_file_multiples_attributes"].each do |k,v|
if v["file"].class == String
file_content_info = JSON.parse(v["file"])
content_type = file_content_info["type"]
filename = file_content_info["name"]
head = "Content-Disposition: form-data; name=\"archive_file[archive_file_multiples_attributes][#{k}][file]\"; filename=\"#{filename}\" Content-Type: #{content_type}"
tempfile = Tempfile.new(filename,binmode: true)
tempfile.write(file_content_info["content"].unpack('U*').map{|i| i.chr }.join)
v["file"] = ActionDispatch::Http::UploadedFile.new({:filename=>filename,:type=>content_type,:head=>head,:tempfile=>tempfile})
end
2021-07-12 08:28:38 +00:00
end rescue nil
2014-05-14 11:52:06 +00:00
@archive_file = ArchiveFile.find(params[:id])
2014-08-07 10:46:24 +00:00
2014-05-14 11:52:06 +00:00
@archive_file.update_user_id = current_user.id
respond_to do |format|
if @archive_file.update_attributes(archive_vars)
Thread.new do
@archive_file.notify_feed("update")
end
format.html { redirect_to(admin_archive_files_path) }
2014-05-14 11:52:06 +00:00
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity }
end
end
end
2014-06-03 10:56:02 +00:00
def destroy
archive_file = ArchiveFile.find(params[:id])
archive_file.destroy
Thread.new do
archive_file.notify_feed("destroy")
end
2014-06-03 10:56:02 +00:00
redirect_to admin_archive_files_path
end
def delete
if params[:ids]
ArchiveFile.any_in(:uid => params[:ids]).destroy_all
Thread.new do
ArchiveFile.notify_feed_delete(params[:ids])
end
end
if request.xhr?
render :nothing => true, :status => 204
else
redirect_to admin_archive_files_path
end
end
def feed
@table_feed_fields = ["archive.feed_name",:tags , :category , "archive.rssfeed", "archive.jsonfeed"]
@feeds = ArchiveFileFeed.all.asc(:created_at)
end
def feedform
if params[:type] == "new"
@archive_feed = ArchiveFileFeed.new(id: nil)
render :partial => "feed_form"
else params[:type] == "edit"
@archive_feed = ArchiveFileFeed.find(params[:id])
render :partial => "edit_feed_form"
end
end
2014-06-03 10:56:02 +00:00
def createfeed
archive_feed = ArchiveFileFeed.new(feed_params)
archive_feed.save
#ArchiveFileFeed.create_feed_cache(nil,archive_feed)
feeds = ArchiveFileFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
def updatefeed
archive_feed = ArchiveFileFeed.find(params[:id])
archive_feed.update_attributes(feed_params)
archive_feed.save
#ArchiveFileFeed.create_feed_cache(nil,archive_feed)
feeds = ArchiveFileFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
2014-05-14 11:52:06 +00:00
def deletefeed
archive_feed = ArchiveFileFeed.find(params[:id])
archive_feed.destroy
feeds = ArchiveFileFeed.all.asc(:created_at)
render :partial => "feed", :collection => feeds
end
private
def feed_params
feed_params = params.require(:archive_file_feed).permit!
if feed_params[:tag_ids].nil?
feed_params[:tag_ids] = []
end
if feed_params[:category_ids].nil?
feed_params[:category_ids] = []
end
feed_params
end
2014-05-14 11:52:06 +00:00
def archive_vars
2014-08-07 10:46:24 +00:00
params[:archive_file][:tags] ||=[]
params.require(:archive_file).permit!
2014-05-14 11:52:06 +00:00
end
def setup_vars
2014-08-07 10:46:24 +00:00
@module_app = ModuleApp.where(:key => "archive").first
2014-05-14 11:52:06 +00:00
end
2016-03-14 07:47:38 +00:00
# def sort
# unless params[:sort].blank?
# case params[:sort]
# when "downloaded_times"
# else
# @sort = sort
# end
# else
# @sort = sort
# end
# @sort
# end
2014-05-08 06:03:33 +00:00
end