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 %> + +