Merge branch 'master' into 'master'

Master

add the feature that let user can modify the show order of categories for index and widget by themselves

See merge request !5
This commit is contained in:
wmcheng 2019-09-21 01:20:11 +08:00
commit d32de01d1e
8 changed files with 164 additions and 14 deletions

View File

@ -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

View File

@ -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,29 @@ F cats = files_by_cateogry.keys.collect do |cat|
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
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
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|
@ -312,7 +321,35 @@ F cats = files_by_cateogry.keys.collect do |cat|
}
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
end

View File

@ -0,0 +1,5 @@
class ArchiveCategory
include Mongoid::Document
field :category_id, type: String
field :sort_number, type: Integer
end

View File

@ -0,0 +1,60 @@
<% all_locales = I18n.available_locales %>
<% count = all_locales.length %>
<script type='text/javascript'>
$('.category_sort_number_form').ready(function(){
$('form[name="category_sort_number_form"]').submit(function(){
var eles = $('form[name="category_sort_number_form"]').serialize().split('&')
var keys=[],values=[]
for (var i=0;i<eles.length;i++){
var temp = eles[i].split('=')
keys.push(temp[0])
values.push(temp[1])
}
if (navigator.onLine) {
$.ajax({
url : "/admin/archive_files/categories_order",
dataType : "json",
type : "post",
data:{data_keys: keys, data_values: values},
success:function(data){
location.reload();
},
error:function(){
alert('Your server has some problem, please try again later!')
}
})
} else {
alert('Please connect the network and try again later!')
}
return false;
})
})
</script>
<div class='category_sort_number_form'>
<form method="post" action="/admin/archive_files/categories_order?" name="category_sort_number_form" >
<table>
<thead style='line-height: 3;'>
<tr style="text-align: center;">
<td><%= t('archive.category') %></td>
<td><%= t('archive.sort_number') %></td>
</tr>
</thead>
<tbody style='line-height: 4;'>
<% @cats.each do |cat| %>
<tr>
<td style='padding-right: 10px;'>
<% all_locales.each_with_index do |locale,index| %>
<%= "#{cat[0][:title][locale]}" %>
<%= index!=(count-1) ? "/" : ":" %>
<% end %>
</td>
<td>
<input style='margin: 0;' type="number" name="sort_number-<%= cat[0].id %>" value="<%= cat[1].to_s %>">
</td>
</tr>
<% end %>
</tbody>
</table>
<input type="submit" value="<%= t('archive.save') %>">
</form>
</div>

View File

@ -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

View File

@ -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: 檔案

View File

@ -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"

View File

@ -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