From 7abd0b4e394d5989777405192ebeabf23fa15c4f Mon Sep 17 00:00:00 2001 From: chiu Date: Fri, 20 Sep 2019 22:01:26 +0800 Subject: [PATCH] add the feature that let user can modify the show order of categories for index and widget by themselves --- .../admin/archive_files_controller.rb | 33 +++++++++- app/controllers/archives_controller.rb | 64 +++++++++++++++---- app/models/archive_category.rb | 5 ++ .../archive_files/categories_order.html.erb | 60 +++++++++++++++++ config/locales/en.yml | 7 +- config/locales/zh_tw.yml | 7 +- config/routes.rb | 2 + lib/archive/engine.rb | 5 ++ 8 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 app/models/archive_category.rb create mode 100644 app/views/admin/archive_files/categories_order.html.erb diff --git a/app/controllers/admin/archive_files_controller.rb b/app/controllers/admin/archive_files_controller.rb index e4873d5..647a333 100644 --- a/app/controllers/admin/archive_files_controller.rb +++ b/app/controllers/admin/archive_files_controller.rb @@ -7,7 +7,24 @@ class Admin::ArchiveFilesController < OrbitAdminController redirect_to '/' + I18n.locale.to_s + module_pages[0] + '?title=' + params['title'].to_s end 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 def index + if ArchiveSortOrder.count == 0 ArchiveSortOrder.new('sort_order' => false).save end @@ -48,7 +65,21 @@ class Admin::ArchiveFilesController < OrbitAdminController render_401 end 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 def create @archive_file = ArchiveFile.new(archive_vars) @archive_file.create_user_id = current_user.id diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 79af1a1..2c9f9c9 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -1,5 +1,7 @@ class ArchivesController < ApplicationController 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 @@ -7,8 +9,9 @@ class ArchivesController < ApplicationController 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.each do |category| + categories_sort.each do |category| url_to_edit = "" flag = false archives = [] @@ -66,7 +69,8 @@ class ArchivesController < ApplicationController 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| + 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| @@ -247,24 +251,31 @@ F cats = files_by_cateogry.keys.collect do |cat| end def widget + begin + 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 + categories = OrbitHelper.widget_categories #data are categories' ids or 'all' @categories = [] if categories.first == "all" - module_app = OrbitHelper.widget_module_app - @categories = module_app.categories.collect do |cat| + categories = OrbitHelper.widget_module_app.categories + puts 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.each do |cat| - c = Category.find(cat) - @categories << {"title" => c.title, "id" => c.id.to_s} + 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| @@ -306,13 +317,44 @@ F cats = files_by_cateogry.keys.collect do |cat| "archives" => archives } end + rescue => e + puts e + end { "categories" => cats, "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 + 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 end \ No newline at end of file diff --git a/app/models/archive_category.rb b/app/models/archive_category.rb new file mode 100644 index 0000000..78e7abc --- /dev/null +++ b/app/models/archive_category.rb @@ -0,0 +1,5 @@ +class ArchiveCategory + include Mongoid::Document + field :category_id, type: String + field :sort_number, type: Integer +end \ No newline at end of file diff --git a/app/views/admin/archive_files/categories_order.html.erb b/app/views/admin/archive_files/categories_order.html.erb new file mode 100644 index 0000000..1e2a939 --- /dev/null +++ b/app/views/admin/archive_files/categories_order.html.erb @@ -0,0 +1,60 @@ +<% all_locales = I18n.available_locales %> +<% count = all_locales.length %> + +
+
+ + + + + + + + +<% @cats.each do |cat| %> + + + + +<% end %> + +
<%= t('archive.category') %><%= t('archive.sort_number') %>
+ <% all_locales.each_with_index do |locale,index| %> + <%= "#{cat[0][:title][locale]}" %> + <%= index!=(count-1) ? "/" : ":" %> + <% end %> + + +
+ +
+
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 15f4593..1ec522f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,12 +1,17 @@ en: - + restful_actions: + categories_order: Categories Order archive: + category: Category + sort_number: Sort Order + save: Save no_page: You have to create at least one page for archive module choose_order: Order with ascending "yes": "Yes" "no": "No" archive: Archive all: All + categories_order: Categories Order new: New downloaded_times: Downloaded Times show_lang: Language diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index ddb5d8c..695fd11 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,6 +1,10 @@ zh_tw: - + restful_actions: + categories_order: 類別排序 archive: + category: 類別 + sort_number: 排序號碼 + save: 儲存 no_page: 請至少為檔案室模組新增一頁內容頁 choose_order: 升序排列 "yes": 是 @@ -8,6 +12,7 @@ zh_tw: show_lang: 呈現語系 download: 檔案下載 archive: 檔案室 + categories_order: 類別排序 Title: 標題 downloaded_times: 下載次數 Files: 檔案 diff --git a/config/routes.rb b/config/routes.rb index abdbc94..cfda3ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,8 @@ locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_l scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do + get 'archive_files/categories_order' => 'archive_files#categories_order' + post 'archive_files/categories_order' => 'archive_files#save_categories_order' resources :archive_files end get "/xhr/archive/download" => "archives#download_file" diff --git a/lib/archive/engine.rb b/lib/archive/engine.rb index 52dd15b..225657a 100644 --- a/lib/archive/engine.rb +++ b/lib/archive/engine.rb @@ -43,6 +43,11 @@ module Archive :active_for_action=>{'admin/archive_files'=>'tags'}, :active_for_tag => 'Archive', :available_for => 'managers' + context_link 'archive.categories_order', + :link_path=>"admin_archive_files_categories_order_path" , + :priority=>5, + :active_for_action=>{'admin/archive_files'=>'categories_order'}, + :available_for => 'managers' end end end