diff --git a/app/assets/javascripts/bulletin_form.js.erb b/app/assets/javascripts/bulletin_form.js.erb index 01685c3dc..cf2dfd69c 100644 --- a/app/assets/javascripts/bulletin_form.js.erb +++ b/app/assets/javascripts/bulletin_form.js.erb @@ -11,17 +11,9 @@ $(document).on('click', '.action a.remove_existing_record', function(){ $("tr #" + $(this).prev().attr('value')).hide(); }); -$('.quick_edit_cancel').live('click', function(){ +$(document).on('click', '.quick_edit_cancel', function(){ tr = $(this).attr('rel'); $('#' + tr).hide(); $("tr#bulletin_file_" + $(this).prev().attr('value')).hide(); $("tr#bulletin_link_" + $(this).prev().attr('value')).hide(); -}); - -$(document).on('click', '.list-remove', function(){ - $('#delete_bulletins').submit(); -}); - -$(document).on('click', '#check_all_bulletins', function(){ - $('.checkbox_in_list').attr("checked", this.checked); }); \ No newline at end of file diff --git a/app/assets/javascripts/sort_header.js b/app/assets/javascripts/sort_header.js new file mode 100644 index 000000000..25a163449 --- /dev/null +++ b/app/assets/javascripts/sort_header.js @@ -0,0 +1,7 @@ +$(document).on('click', '.list-remove', function(){ + $('#delete_all').submit(); +}); + +$(document).on('click', '#check_all', function(){ + $('.checkbox_in_list').attr("checked", this.checked); +}); \ No newline at end of file diff --git a/app/controllers/orbit_backend_controller.rb b/app/controllers/orbit_backend_controller.rb index de2f4fd37..7e9fa32a8 100644 --- a/app/controllers/orbit_backend_controller.rb +++ b/app/controllers/orbit_backend_controller.rb @@ -12,7 +12,7 @@ class OrbitBackendController< ApplicationController @app_title = request.fullpath.split('/')[2] @module_app = ModuleApp.first(conditions: {:key => @app_title} ) end - + private def force_order @@ -26,5 +26,135 @@ class OrbitBackendController< ApplicationController render :text => '403 Forbidden' end end + + def get_sorted_and_filtered(object_class) + object_class = object_class.classify.constantize + objects = object_class.all + + if !params[:sort].blank? + options = params[:sort_options] + options = [options] if !options.class.eql?(Array) + options.each do |option| + if object_class.fields.include?(option) + case object_class.fields[option].type.to_s + when 'BigDecimal', 'Boolean', 'Date', 'DateTime', 'Float', 'Integer', 'String', 'Symbol', 'Time' + (objects = objects.order_by(option, params[:direction])) rescue nil + when 'Object' + objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option) + end + elsif object_class.relations.include?(option) + case object_class.relations[option].macro + when :references_one + a = Array.new + objects.each { |object| a << [get_string_value_from_object(object), object] } + sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse! + objects = sorted.collect {|x| x[1] } + when :references_many, :references_and_referenced_in_many + objects = get_objects_from_self(object_class, objects, option) + when :referenced_in + objects = get_objects_from_referenced_objects(object_class.relations[option].class_name.constantize, objects, "#{option}_id") + end + end + end + end + if @filter + @filter.each do |key, value| + case key + when 'status' + a = Array.new + objects.each do |bulletin| + value.each do |v| + case v + when 'pending' + a << bulletin if bulletin.is_checked.nil? + when 'rejected' + a << bulletin if bulletin.is_checked.eql?(false) + else + a << bulletin if bulletin[v] + end + end + end + objects = a.uniq + when 'categories' + a = Array.new + objects.each do |bulletin| + a << bulletin if value.include?(bulletin.bulletin_category.id.to_s) + end + objects = a.uniq + when 'tags' + a = Array.new + objects.each do |bulletin| + bulletin.tags.each do |tag| + a << bulletin if value.include?(tag.id.to_s) + end + end + objects = a.uniq + end if value.size > 0 + end + end + Kaminari.paginate_array(objects).page(params[:page]).per(10) + end + + def get_string_value_from_object(object) + s = object[I18n.locale] rescue nil + s = object.i18n_variable unless s rescue nil + s = object.name unless s rescue nil + s = object.title unless s rescue nil + if s + case s.class.to_s + when "String" + s.downcase + when "I18nVariable" + s[I18n.locale].downcase + else + nil + end + end + end + + def get_objects_from_referenced_objects(object_class, objects, option) + referer_ids = objects.distinct(option) + referenced_objects = object_class.find(referer_ids) rescue nil + if referenced_objects + a = Array.new + referenced_objects.each { |referer| a << [get_string_value_from_object(referer), referer.id] } + sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse! + sorted_objects = sorted.collect {|x| objects.where(option => x[1]).entries } + sorted_objects.flatten! + sorted_objects.uniq! + get_with_nil(objects, option, sorted_objects) + else + objects + end + end + + def get_objects_from_self(object_class, objects, option) + referenced_class = object_class.relations[option].class_name.constantize + referenced_objects = referenced_class.all rescue nil + if referenced_objects + reverse_relation = nil + referenced_class.relations.each { |relation| reverse_relation = relation[1].name.to_s if relation[1].class_name.eql?(object_class.to_s) } + a = Array.new + referenced_objects.each { |referenced_object| a << [get_string_value_from_object(referenced_object), referenced_object] } + a.compact! + sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse! + sorted_objects = Array.new + sorted.each {|x| sorted_objects << x[1].send(reverse_relation) } + sorted_objects.flatten! + sorted_objects.uniq! + get_with_nil(objects, option, sorted_objects) + else + objects + end + end + + def get_with_nil(objects, option, sorted_objects) + tmp = Array.new + objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).count == 0) } + sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse! + sorted_tmp = sorted.collect {|a| a[1] } + a = params[:direction].eql?('asc') ? (sorted_tmp + sorted_objects) : (sorted_objects + sorted_tmp) + a.flatten + end end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 258000be9..682799eb7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -87,26 +87,6 @@ module ApplicationHelper ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil end - def sortable(column) - direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc" - {:sort => column, :direction => direction} - end - - def is_sort_active?(name) - res = '' - res << ' select' if params[:sort].eql?(name) - res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc') - res - end - - def is_sort?(name) - ' web-symbol' if params[:sort].eql?(name) - end - - def is_filter_active?(type, id) - ' active' if (@filter[type].include?(id.to_s) rescue nil) - end - def process_page(page, id) parse_page_noko(page, id) end diff --git a/app/helpers/orbit_backend_helper.rb b/app/helpers/orbit_backend_helper.rb new file mode 100644 index 000000000..d1b83fd22 --- /dev/null +++ b/app/helpers/orbit_backend_helper.rb @@ -0,0 +1,45 @@ +module OrbitBackendHelper + + def sortable(column) + direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc" + {:sort => column, :direction => direction} + end + + def is_sort_active?(name) + res = '' + res << ' select' if params[:sort].eql?(name) + res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc') + res + end + + def is_sort?(name) + ' web-symbol' if params[:sort].eql?(name) + end + + def is_filter_active?(type, id) + ' active' if (@filter[type].include?(id.to_s) rescue nil) + end + + def render_sort_bar(url, delete_all, *titles) + content_tag :table, :class => "table main-list" do + content_tag :thead do + content_tag :tr, :class => "sort-header" do + concat (content_tag :th, :class => "span1 strong" do + check_box_tag :check_all + link_to content_tag(:i, nil, :class => "icon-trash"), '#', :class => "list-remove" + end) if delete_all + titles.each do |title| + concat render_title(url, title[0], title[1], title[2], title[3]) + end + end + end + end + end + + def render_title(url, title, fields, span, translation) + content_tag :th, :class => "sort #{span} #{is_sort_active?(title)}" do + link_to (t(translation) + content_tag(:b, nil, :class => is_sort?(title))).html_safe, url_for({:filter => @filter}.merge(sortable(title).merge(:sort_options => fields))), :class => 'js_history' + end + end + +end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb index f77e70b71..62de400d1 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb @@ -30,7 +30,10 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController # @bulletins = Bulletin.search(params[:search], params[:category_id]) # @bulletins = Bulletin.all.order_by([params[:sort], params[:direction]]) - @bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered_bulletins : Bulletin.all.page(params[:page]).per(10) + + + # @bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered_bulletins : Bulletin.all.page(params[:page]).per(10) + @bulletins = (params[:sort] || @filter) ? get_sorted_and_filtered("bulletin") : Bulletin.all.page(params[:page]).per(10) @bulletin_categories = BulletinCategory.all @bulletin_link = BulletinLink.new @@ -300,7 +303,7 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController if params[:to_delete] bulletins = Bulletin.any_in(:_id => params[:to_delete]).delete_all end - redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort]) + redirect_to panel_announcement_back_end_bulletins_url(:filter => params[:filter], :direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options]) end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 55d1ec84a..44882df36 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -14,7 +14,7 @@ class Bulletin field :deadline , :type => Date # field :url field :create_user_id - field :update_user_id + field :update_user_id, :class_name => "User" field :is_top, :type => Boolean, :default => false field :is_hot, :type => Boolean, :default => false diff --git a/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb b/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb new file mode 100644 index 000000000..9cb29038b --- /dev/null +++ b/vendor/built_in_modules/announcement/app/models/bulletin_tag.rb @@ -0,0 +1,11 @@ +class AnnouncementTag < Tag + + has_and_belongs_to_many :bulletins + + + def get_visible_bulletins(sort = :name) + date_now = Time.now + self.bulletins.where(:is_hidden => false).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc(:is_top, sort) + end + +end \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_filter.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_filter.html.erb index 36b12c19e..4d140880d 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_filter.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_filter.html.erb @@ -30,4 +30,8 @@ <%= render 'sort_headers' %> - \ No newline at end of file + + +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "sort_header" %> +<% end %> \ No newline at end of file diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb index df86f2211..360cead24 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb @@ -292,7 +292,6 @@ <% content_for :page_specific_javascript do %> - <%= javascript_include_tag "bulletin_form" %> <%= javascript_include_tag "inc/jquery.imagesloaded.js" %>