From e2fbba4cf692a7751153cacf2d71e68f1ef591c1 Mon Sep 17 00:00:00 2001 From: chiu Date: Thu, 5 Sep 2019 15:15:45 +0800 Subject: [PATCH] 1.fix some error 2.add model so that user can choice the sort order 3.add the feature that can devide the data to multiple page by the user setting on the widget --- .../admin/archive_files_controller.rb | 14 +- app/controllers/archives_controller.rb | 150 +++++++++++++++--- app/models/archive_sort_order.rb | 4 + .../archive_files/_sort_order_choose.html.erb | 10 ++ app/views/admin/archive_files/index.html.erb | 1 + app/views/archives/index.html.erb | 3 +- config/locales/en.yml | 3 + config/locales/zh_tw.yml | 3 + 8 files changed, 164 insertions(+), 24 deletions(-) create mode 100644 app/models/archive_sort_order.rb create mode 100644 app/views/admin/archive_files/_sort_order_choose.html.erb diff --git a/app/controllers/admin/archive_files_controller.rb b/app/controllers/admin/archive_files_controller.rb index f07ca3d..623c8c8 100644 --- a/app/controllers/admin/archive_files_controller.rb +++ b/app/controllers/admin/archive_files_controller.rb @@ -1,6 +1,18 @@ class Admin::ArchiveFilesController < OrbitAdminController - def index + if ArchiveSortOrder.count == 0 + ArchiveSortOrder.new('sort_order' => false).save + end + 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 + else + ArchiveSortOrder.first.update_attributes('sort_order' => false) + end + @choose = ArchiveSortOrder.first['sort_order'] @table_fields = [:status, :category, :title, :updated_at,:last_modified,"archive.downloaded_times"] @categories = @module_app.categories @tags = @module_app.tags diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 1f3f266..702b9cd 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,14 +1,19 @@ 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.order_by(:created_at => "desc").group_by(&:category) + if ArchiveSortOrder.count == 0 + ArchiveSortOrder.new('sort_order' => false).save + end + params = OrbitHelper.params + 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 cats = categories.collect do |category| url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : "" @@ -20,7 +25,7 @@ class ArchivesController < ApplicationController } end files = [] - archive.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| + archive.archive_file_multiples.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 "" @@ -33,6 +38,7 @@ class ArchivesController < ApplicationController 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, @@ -41,7 +47,8 @@ class ArchivesController < ApplicationController "files" => files } end - sorted = archives.sort_by{|k,v| k["sort_number"].to_i} + 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, @@ -49,8 +56,75 @@ class ArchivesController < ApplicationController "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 { - "categories" => cats + "categories" => cats_last } end @@ -72,7 +146,7 @@ class ArchivesController < ApplicationController 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| +F cats = files_by_cateogry.keys.collect do |cat| files_by_category_tag = files_by_cateogry[cat].group_by(&:tags) ts = [] @@ -116,11 +190,15 @@ class ArchivesController < ApplicationController end { "categories" => cats, - "extras" => {"widget-title" => "Archives","more_url"=>OrbitHelper.widget_more_url} + "extras" => {"widget-title" => "Archives","more_url" => OrbitHelper.widget_more_url} } end def widget + if ArchiveSortOrder.count == 0 + ArchiveSortOrder.new('sort_order' => false).save + end + page_data_count = OrbitHelper.widget_data_count categories = OrbitHelper.widget_categories @categories = [] if categories.first == "all" @@ -138,13 +216,37 @@ class ArchivesController < ApplicationController 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" => archive.url, - "archive_url" => url - } + 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 : "/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 + '?data_count=' + page_data_count.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"], @@ -154,7 +256,11 @@ class ArchivesController < ApplicationController end { "categories" => cats, - "extras" => {"widget-title" => "Archives","more_url"=>OrbitHelper.widget_more_url} + "extras" => {"widget-title" => "Archives","more_url" => (OrbitHelper.widget_more_url + '?data_count=' + page_data_count.to_s)} } end + private + def self.get_total_pages + @@total_pages + end end \ No newline at end of file diff --git a/app/models/archive_sort_order.rb b/app/models/archive_sort_order.rb new file mode 100644 index 0000000..a6563bb --- /dev/null +++ b/app/models/archive_sort_order.rb @@ -0,0 +1,4 @@ +class ArchiveSortOrder + include Mongoid::Document + field :sort_order, :type => Boolean # true => desc , false => asc +end \ No newline at end of file diff --git a/app/views/admin/archive_files/_sort_order_choose.html.erb b/app/views/admin/archive_files/_sort_order_choose.html.erb new file mode 100644 index 0000000..9c49971 --- /dev/null +++ b/app/views/admin/archive_files/_sort_order_choose.html.erb @@ -0,0 +1,10 @@ +
+ +<%= t('archive.choose_order')+':' %> +<% if @choose %> +<%= t('archive.yes') %> +<% else %> +<%= t('archive.no') %> +<% end %> + +
\ No newline at end of file diff --git a/app/views/admin/archive_files/index.html.erb b/app/views/admin/archive_files/index.html.erb index eb4b023..3c989f7 100644 --- a/app/views/admin/archive_files/index.html.erb +++ b/app/views/admin/archive_files/index.html.erb @@ -1,4 +1,5 @@ <%= render_filter @filter_fields, "index_table" %> +<%= render 'sort_order_choose' %>
<%= render 'index'%>
\ No newline at end of file diff --git a/app/views/archives/index.html.erb b/app/views/archives/index.html.erb index 648b75c..d9e27a1 100644 --- a/app/views/archives/index.html.erb +++ b/app/views/archives/index.html.erb @@ -1 +1,2 @@ -<%= render_view %> \ No newline at end of file +<%= render_view %> +<%= create_pagination(ArchivesController.get_total_pages).html_safe %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index ea548e0..859691a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,6 +1,9 @@ en: archive: + choose_order: Order with ascending + "yes": "Yes" + "no": "No" archive: Archive all: All new: New diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 3a409c5..862c629 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,6 +1,9 @@ zh_tw: archive: + choose_order: 升序排列 + "yes": 是 + "no": 否 show_lang: 呈現語系 download: 檔案下載 archive: 檔案室