archive1/app/controllers/archives_controller.rb

139 lines
3.6 KiB
Ruby

class ArchivesController < ApplicationController
def index
files_by_cateogry = ArchiveFile.filter_by_categories.filter_by_tags.group_by(&:category)
cats = files_by_cateogry.keys.collect do |cat|
files_by_category_tag = files_by_cateogry[cat].group_by(&:tags)
url_to_edit = OrbitHelper.user_has_cateogry?(cat) ? "/admin/archive_files?filters[category][]=#{cat.id.to_s}" : ""
ts = []
files_by_category_tag.keys.each do |t|
archives = []
files_by_category_tag[t].each_with_index do |archive,index|
statuses = archive.statuses_with_classname.collect do |status|
{
"status" => status["name"] || "",
"status-class" => "status-#{status['classname']}"
}
end
files = []
archive.archive_file_multiples.order_by(:sort_number=>'asc').each do |file|
if file.choose_lang.include?(I18n.locale.to_s)
title = (file.file_title.blank? ? File.basename(file.file.path) : file.file_title) rescue ""
extension = file.file.file.extension.downcase rescue ""
# url = file.file.url rescue ""
files << {
"file-name" => title,
"file-type" => extension,
"file-url" => "/xhr/archive/download?file=#{file.id}"
}
end
end
a = {
"archive-title" => archive.title || "",
"statuses" => statuses,
"files" => files
}
if t.count > 1
t.each do |inner_tag|
index = ts.index{|tot| tot["tag-name"] == inner_tag.name}
if !index.nil?
ts[index]["archives"] << a
else
archives << a
end
end
else
archives << a
end
end
ts << {
"tag-name" => (t[0].name rescue ""),
"archives" => archives
} if !archives.blank?
end
if(files_by_category_tag.empty?)
{
"category-title" => "",
"tags" => ts,
"url_to_edit" => url_to_edit
}
else
{
"category-title" => cat.title || "",
"tags" => ts,
"url_to_edit" => url_to_edit
}
end
end
{
"categories" => cats
}
end
def download_file
file_id = params[:file]
file = ArchiveFileMultiple.find(file_id) rescue nil
if !file.nil?
file.download_count = file.download_count + 1
file.save
redirect_to file.file.url and return
end
render :nothing => true
end
def widget
if OrbitHelper.widget_tags.first == "all"
files_by_cateogry = ArchiveFile.filter_by_widget_categories.with_tags(OrbitHelper.widget_module_app.tags.collect{|tag| tag.id.to_s}).group_by(&:category)
else
files_by_cateogry = ArchiveFile.filter_by_widget_categories.with_tags(OrbitHelper.widget_tags).group_by(&:category)
end
cats = files_by_cateogry.keys.collect do |cat|
files_by_category_tag = files_by_cateogry[cat].group_by(&:tags)
ts = []
files_by_category_tag.keys.each do |t|
archives = []
files_by_category_tag[t].each_with_index do |archive,index|
a = {
"archive-title" => archive.title,
"archive_url" => OrbitHelper.widget_more_url
}
if t.count > 1
t.each do |inner_tag|
index = ts.index{|tot| tot["tag-name"] == inner_tag.name}
if !index.nil?
ts[index]["archives"] << a
else
archives << a
end
end
else
archives << a
end
end
ts << {
"tag-name" => (t[0].name rescue ""),
"archives" => archives
} if !archives.blank?
end
{
"category-title" => cat.title,
"tags" => ts
}
end
{
"categories" => cats,
"extras" => {"widget-title" => "Archives","more_url"=>OrbitHelper.widget_more_url}
}
end
end