archive/app/controllers/archives_controller.rb

355 lines
14 KiB
Ruby
Raw Normal View History

2014-05-14 11:52:06 +00:00
class ArchivesController < ApplicationController
2015-09-18 08:45:42 +00:00
def index
#avoid the categories to be not in the ArchiveCategory
check_cat_sort_data()
if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save
end
params = OrbitHelper.params
if !params['title'].nil?
files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category)
categories = files_by_cateogry.keys
categories_sort = get_sorted_cat_with_filter(categories,'orm')
cats_last = []
categories_sort.each do |category|
url_to_edit = ""
flag = false
archives = []
files_by_cateogry[category].each do |archive|
if archive.title == params['title']
flag = true
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
archives << {
"archive-title" => archive.title,
"created_at" => archive.created_at.strftime('%Y%m%d').to_i,
"archive-url" => archive.url,
"url" => archive.url,
"statuses" => statuses,
"sort_number" => archive.sort_number,
"is_top" => (archive.is_top ? 1 : 0),
"files" => files
}
end
end
if flag
cats_last << {
"category-title" => category.title,
"archives" => archives,
"url_to_edit" => url_to_edit
}
end
end
@@total_pages = 1
else
if OrbitHelper.page_data_count > 0
OrbitHelper.set_page_data_count 0
page = Page.where(:page_id => 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)
each_data_count = []
categories = files_by_cateogry.keys
categories_sort = get_sorted_cat_with_filter(categories,'orm')
cats = categories_sort.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 = []
2019-11-18 12:36:24 +00:00
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,
"created_at" => archive.created_at.strftime('%Y%m%d').to_i,
"archive-url" => archive.url,
"url" => archive.url,
"statuses" => statuses,
"sort_number" => archive.sort_number,
"is_top" => (archive.is_top ? 1 : 0),
"files" => files
}
end
each_data_count.push(archives.length)
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
if ArchiveSortOrder.first['sort_order'] #Order with ascending
cats = cats.collect do |cat|
Hash[cat.map{|k,v| [k,k=='archives' ? (v.sort_by{|tp| [(tp['sort_number'].nil? ? Float::INFINITY : tp['sort_number'].to_i),-tp['created_at']]}) : v] }]
end
else
cats = cats.collect do |cat|
Hash[cat.map{|k,v| [k,k=='archives' ? (v.sort_by{|tp| [(tp['sort_number'].nil? ? Float::INFINITY : -tp['sort_number'].to_i),-tp['created_at']]}) : v] }]
end
end
if params[:data_count].to_i <=0
page_data_count = 0
else
page_data_count = params[:data_count].to_i
end
if params[:page_no].nil?
page_no = 1
else
page_no = params[:page_no].to_i
end
if page_data_count != 0
all_data_count = 0
data_start = -1
data_end = -1
for i in 0...each_data_count.length
all_data_count_before = all_data_count
all_data_count += each_data_count[i]
if ( data_start == -1 && (all_data_count > (page_no - 1) * page_data_count) )
data_start = i
data_start_first = (page_no - 1) * page_data_count - all_data_count_before
end
if ( data_end == -1 && (all_data_count > (page_no * page_data_count - 1)) )
data_end = i
data_end_last = (page_no * page_data_count - 1) - all_data_count_before
end
end
if ( data_end == -1 && data_start != -1)
data_end = i
data_end_last = -1
end
if data_start!=-1 && page_no>=1
cats_last = []
for i in data_start..data_end
if data_start != data_end
if i==data_start
cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[data_start_first..-1] : v)]}]
elsif i==data_end
cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[0..data_end_last] : v)]}]
else
cats_last << cats[i]
end
else
cats_last << Hash[cats[i].map{|k,v| [k,(k=='archives' ? v[data_start_first..data_end_last] : v)]}]
end
end
else
cats_last = [Hash[cats[0].map{|k,v| [k,'']}]]
end
@@total_pages = (all_data_count.to_f / page_data_count).ceil
else
if page_no!=1
cats_last = [Hash[cats[0].map{|k,v| [k,'']}]]
@@total_pages = 0
else
cats_last = cats
@@total_pages = 1
end
end
end
2014-05-14 11:52:06 +00:00
{
"categories" => cats_last
2014-05-14 11:52:06 +00:00
}
end
2015-03-18 09:59:03 +00:00
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 :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found
2015-03-18 09:59:03 +00:00
end
def group_by_category
cat_id = OrbitHelper.widget_categories.map {|cat_id| cat_id.to_s}
2015-07-17 07:29:41 +00:00
if OrbitHelper.widget_tags.first == "all"
2015-11-06 10:17:53 +00:00
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)
2015-07-17 07:29:41 +00:00
else
2015-11-06 10:17:53 +00:00
files_by_cateogry = ArchiveFile.where(:category_id.in => OrbitHelper.widget_categories).with_tags(OrbitHelper.widget_tags).asc(:sort_number).group_by(&:category)
2015-07-17 07:29:41 +00:00
end
F cats = files_by_cateogry.keys.collect do |cat|
files_by_category_tag = files_by_cateogry[cat].group_by(&:tags)
2015-07-17 07:29:41 +00:00
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,
2019-03-06 10:31:13 +00:00
"archive-url" => archive.url,
2015-08-18 10:00:52 +00:00
"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
check_cat_sort_data()
if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save
end
page_data_count = OrbitHelper.widget_data_count
categories = OrbitHelper.widget_categories #data are categories' ids or 'all'
@categories = []
if categories.first == "all"
categories = OrbitHelper.widget_module_app.categories
categories_sort = get_sorted_cat_with_filter(categories,'orm')
@categories = categories_sort.collect do |cat|
{
"title" => cat.title,
"id" => cat.id.to_s
}
end
else
categories_sort = get_sorted_cat_with_filter(categories,'id')
@categories = categories_sort.collect do |cat|
{
"title" => cat.title,
"id" => cat.id.to_s
}
end
end
cats = @categories.collect do |cat|
if ArchiveSortOrder.first['sort_order'] #Order with ascending
archives_all = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(OrbitHelper.widget_tags)
temp_with_nil = archives_all.in(sort_number: nil).order_by(created_at: 'desc')
temp_no_nil = archives_all.not_in(sort_number: nil).order_by(sort_number: 'asc',created_at: 'desc')
archives_temp = temp_no_nil.concat(temp_with_nil)
archives = archives_temp.collect do |archive|
if archive.archive_file_multiples.count==0
url = (archive[:url][OrbitHelper.get_site_locale].to_s.empty? ? 'javascript:void' : archive[:url][OrbitHelper.get_site_locale])
else
url = archive.archive_file_multiples.count > 1 ? (OrbitHelper.widget_more_url + '?title=' + archive.title.to_s) : "/xhr/archive/download?file=#{archive.archive_file_multiples.first.id}"
end
{
"archive-title" => archive.title,
"archive-url" => archive.url,
"archive_url" => url
}
end
else
archives_temp = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(OrbitHelper.widget_tags).order_by(sort_number: 'desc',created_at: 'desc')
archives = archives_temp.collect do |archive|
if archive.archive_file_multiples.count==0
url = (archive[:url][OrbitHelper.get_site_locale].to_s.empty? ? 'javascript:void' : archive[:url][OrbitHelper.get_site_locale])
else
url = archive.archive_file_multiples.count > 1 ? (OrbitHelper.widget_more_url + '?title=' + archive.title.to_s) : "/xhr/archive/download?file=#{archive.archive_file_multiples.first.id}"
end
{
"archive-title" => archive.title,
"archive-url" => archive.url,
"archive_url" => url
}
end
end
{
"category-title" => cat["title"],
2017-09-25 07:01:00 +00:00
"category-id" => cat["id"],
"archives" => archives
}
end
{
"categories" => cats,
"extras" => {"widget-title" => "Archives","more_url" => (OrbitHelper.widget_more_url + '?data_count=' + page_data_count.to_s)}
}
end
private
def get_sorted_cat_with_filter(categories,cat_type)
all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil)
all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat|
cat.category_id.to_s
end
if cat_type=='id'
categories_temp = ModuleApp.where(:key => "archive").first.categories
categories = categories_temp.select{|cat| categories.include? cat.id.to_s}
end
categories_sort = []
all_categories_id.each do |cat_id|
category_selected = categories.select{|category| cat_id == category.id.to_s}
if category_selected.length!=0
categories_sort << category_selected[0]
end
end
categories_sort
end
def self.get_total_pages
@@total_pages
end
def check_cat_sort_data
categories_temp = ModuleApp.where(:key => "archive").first.categories
categories_temp.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
end
2014-05-14 11:52:06 +00:00
end