archive/app/controllers/archives_controller.rb

156 lines
4.8 KiB
Ruby

class ArchivesController < ApplicationController
def index
if OrbitHelper.page_data_count > 0
OrbitHelper.set_page_data_count 0
page = Page.where(:page_id => OrbitHelper.params["page_id"]).first rescue nil
if !page.nil?
page.data_count = 0
page.save
end
end
files_by_cateogry = ArchiveFile.where(is_hidden: false).filter_by_categories.filter_by_tags.group_by(&:category)
categories = files_by_cateogry.keys
cats = categories.collect do |category|
url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : ""
archives = files_by_cateogry[category].collect do |archive|
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=>'desc').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 ""
files << {
"file-name" => title,
"file-type" => extension,
"file-url" => "/xhr/archive/download?file=#{file.id}"
}
end
end
{
"archive-title" => archive.title,
"statuses" => statuses,
"sort_number" => archive.sort_number,
"is_top" => (archive.is_top ? 1 : 0),
"files" => files
}
end
sorted = archives.sort_by{|k,v| k["sort_number"].to_i}
sorted = sorted.sort{|k,v| v["is_top"] <=> k["is_top"]}
{
"category-title" => category.title,
"archives" => sorted,
"url_to_edit" => url_to_edit
}
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 group_by_category
cat_id = OrbitHelper.widget_categories.map {|cat_id| cat_id.to_s}
if OrbitHelper.widget_tags.first == "all"
files_by_cateogry = ArchiveFile.where(:category_id.in => OrbitHelper.widget_categories).with_tags(OrbitHelper.widget_module_app.tags.collect{|tag| tag.id.to_s}).asc(:sort_number).group_by(&:category)
else
files_by_cateogry = ArchiveFile.where(:category_id.in => OrbitHelper.widget_categories).with_tags(OrbitHelper.widget_tags).asc(:sort_number).group_by(&:category)
end
cats = files_by_cateogry.keys.collect do |cat|
files_by_category_tag = files_by_cateogry[cat].group_by(&:tags)
ts = []
archive_counts = 0
files_by_category_tag.keys.each do |t|
archives = []
archive_counts = archive_counts + 1
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?
break if ts[index]["archives"].count >= OrbitHelper.widget_data_count
ts[index]["archives"] << a
else
break if archives.count >= OrbitHelper.widget_data_count
archives << a
end
end
else
break if archives.count >= OrbitHelper.widget_data_count
archives << a
end
end
ts << {
"tag-name" => (t[0].name rescue ""),
"archives" => archives
} if !archives.blank? && archive_counts < OrbitHelper.widget_data_count
end
{
"category-title" => cat.title,
"tags" => ts
}
end
{
"categories" => cats,
"extras" => {"widget-title" => "Archives","more_url"=>OrbitHelper.widget_more_url}
}
end
def widget
categories = OrbitHelper.widget_categories
@categories = []
if categories.first == "all"
module_app = OrbitHelper.widget_module_app
@categories = module_app.categories.collect do |cat|
{
"title" => cat.title,
"id" => cat.id.to_s
}
end
else
categories.each do |cat|
c = Category.find(cat)
@categories << {"title" => c.title, "id" => c.id.to_s}
end
end
cats = @categories.collect do |cat|
archives = ArchiveFile.can_display.where(:category_id => cat["id"]).collect do |archive|
url = archive.archive_file_multiples.count > 1 ? OrbitHelper.widget_more_url : "/xhr/archive/download?file=#{archive.archive_file_multiples.first.id}"
{
"archive-title" => archive.title,
"archive_url" => url
}
end
{
"category-title" => cat["title"],
"category-id" => cat["id"],
"archives" => archives
}
end
{
"categories" => cats,
"extras" => {"widget-title" => "Archives","more_url"=>OrbitHelper.widget_more_url}
}
end
end