diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 30a8a3ac..fcb1052a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,8 +3,8 @@ class ApplicationController < ActionController::Base include ParserFrontEnd, ParserBackEnd, ApplicationHelper include OrbitApp::ErrorHandlers::PageErrorHandler - include OrbitApp::ErrorHandlers::ObjectAuthErrorHandler - include OrbitApp::ErrorHandlers::ModuleAppErrorHandler + include OrbitApp::ErrorHandlers::ObjectAuthErrorHandler + include OrbitApp::ErrorHandlers::ModuleAppErrorHandler rescue_from ObjectAuthError, :with => :render_object_auth_error rescue_from ModuleAppError, :with => :render_module_app_error @@ -34,11 +34,11 @@ class ApplicationController < ActionController::Base def front_end_available(module_app_title='') app_controller = ModuleApp.first(conditions: {:key => module_app_title} ) - unless app_controller.enable_frontend? + unless app_controller.enable_frontend? render :nothing => true end end - + def get_all_app_engines ary = ["vender/plugins/new_blog"] app_engines = ary.collect{|t| @@ -51,23 +51,23 @@ class ApplicationController < ActionController::Base def flaten_controller ary=[] - Find.find(File.join(Rails.root , 'vendor/plugins/')) { |name| + Find.find(File.join(Rails.root , 'vendor/plugins/')) { |name| require_dependency(name) if /_controller\.rb$/ =~ name ary << name } ary end - + # Find the parent for the given item def find_parent_item @parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil end - + def auth_failed_in_backend - #redirect_to admin_dashboards_url + #redirect_to admin_dashboards_url redirect_to root_path end - + def for_admin_only if is_admin? true @@ -76,7 +76,7 @@ class ApplicationController < ActionController::Base auth_failed_in_backend end end - + def for_app_manager if is_manager? true @@ -85,7 +85,7 @@ class ApplicationController < ActionController::Base auth_failed_in_backend end end - + def for_app_sub_manager if (@module_app.sub_managing_users.include?(current_or_guest_user) || is_manager?) true @@ -108,7 +108,7 @@ class ApplicationController < ActionController::Base flash[:error] = t("access.denied.object") auth_failed_in_backend unless (obj.get_object_auth_by_title(title).auth_users.include?(current_or_guest_user) || is_manager? || is_admin? ) end - + # Render the page def render_page(args={}) if @item @@ -132,9 +132,9 @@ class ApplicationController < ActionController::Base @orig_url = "http://#{request.host_with_port}/#{@item.path}?id=#{@object.id}" render 'shared/render_share', :layout => false end - + protected - + # Set I18n.locale def set_locale # update session if passed @@ -148,11 +148,11 @@ class ApplicationController < ActionController::Base else browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue nil condition = @site_in_use_locales.include?(browser_locale) - end + end session[:locale] = condition ? (browser_locale || session[:locale]) : I18n.default_locale.to_s I18n.locale = session[:locale].to_sym end - + # Set the site variables def set_site # set site if exist or create site @@ -161,11 +161,11 @@ class ApplicationController < ActionController::Base @site_in_use_locales = site_locales_default_head(@site.in_use_locales) @site_valid_locales = site_locales_default_head(@site.valid_locales) end - + def set_current_item session[:current_page] = params[:id] || @item.id rescue nil end - + def decrypt_data(encrypted_data, encrypted_key, encrypted_iv) site = Site.find(session[:site]) if encrypted_data @@ -181,7 +181,7 @@ class ApplicationController < ActionController::Base '' end end - + def get_homepage Page.root end @@ -229,6 +229,174 @@ class ApplicationController < ActionController::Base redirect_to ret end + def get_sorted_and_filtered(object_class, query = nil, objects = nil) + objects ||= get_objects(object_class, query) + object_class = object_class.classify.constantize + 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) + if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil? + objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option) + else + (objects = objects.order_by(option, params[:direction])) rescue nil + 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 + elsif option.eql?('tags') + tag_array = @module_app.tags.inject([]){ |result, value| + result << [value.name, value] + } + params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse! + sorted_objects = Array.new + tag_array.each do |x| + taggings = x[1].taggings + taggings.each {|tagging| sorted_objects << tagging.taggable } + end + # debugger + sorted_objects.flatten! + sorted_objects.uniq! + objects = get_with_nil(objects, option, sorted_objects) + end + end + end + if @filter + @filter.each do |key, value| + case key + when 'status' + a = Array.new + objects.each do |object| + value.each do |v| + a << object if object[v] + end + end + objects = a.uniq + when 'categories' + a = Array.new + objects.each do |object| + a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil) + end + objects = a.uniq + when 'tags' + a = Array.new + objects.each do |object| + object.tags.each do |tag| + a << object if value.include?(tag.id.to_s) + end + end + objects = a.uniq + end if value.size > 0 + end + end + Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10) + end + + def get_viewable(object_class, query=nil) + objects = get_objects(object_class,query).order_by(:created_at, :desc) + Kaminari.paginate_array(objects).page(params[:page]).per(10) + end + + def get_objects(object_class, query=nil) + object_class = object_class.classify.constantize + # debugger + # a=1 + if query + if object_class.include?(Mongoid::Sortable) + objects = object_class.default_sort(params[:sort]).where(query) + else + objects = object_class.where(query) + end + else + if object_class.include?(Mongoid::Sortable) + objects = object_class.default_sort(params[:sort]).all + else + objects = object_class.all + end + end + objects + 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.to_a.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_string_value_from_object(object) + s = object.name_translations[I18n.locale.to_s] unless s rescue nil + s = object.title_translations[I18n.locale.to_s] unless s rescue nil + s = object.name unless s rescue nil + s = object.title unless s rescue nil + s.downcase rescue '' + 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).blank? || (object.send(option).size == 0 rescue nil)) } + 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 + + + def filter_authorized_objects(objects) + if(!is_admin? || !is_manager?) + objects.delete_if{ |object| + if object.is_pending == true + if check_permission(:manager) + object.create_user_id != current_user.id + else + !object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false + end + else + false + end + } + end + objects + end private diff --git a/app/controllers/orbit_backend_controller.rb b/app/controllers/orbit_backend_controller.rb index a92d54e7..cbf963df 100644 --- a/app/controllers/orbit_backend_controller.rb +++ b/app/controllers/orbit_backend_controller.rb @@ -4,9 +4,9 @@ class OrbitBackendController < ApplicationController include OrbitTag::Tagging include AdminHelper include ApplicationHelper - + layout 'new_admin' - + def setup_vars @app_title ||= controller_path.split('/')[1].singularize @module_app ||= ModuleApp.first(conditions: {:key => @app_title} ) @@ -24,177 +24,15 @@ class OrbitBackendController < ApplicationController def force_order_for_user setup_vars - set_current_user + set_current_user authenticate_user! check_user_can_use end - def check_user_can_use + def check_user_can_use unless check_permission #redirect_to polymorphic_path(['panel',@app_title,'back_end','public']) redirect_to root_url end end - - def get_sorted_and_filtered(object_class, query=nil) - objects = get_objects(object_class, query) - object_class = object_class.classify.constantize - 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) - if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil? - objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option) - else - (objects = objects.order_by(option, params[:direction])) rescue nil - 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 - elsif option.eql?('tags') - tag_array = @module_app.tags.inject([]){ |result, value| - result << [value.name, value] - } - params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse! - sorted_objects = Array.new - tag_array.each do |x| - taggings = x[1].taggings - taggings.each {|tagging| sorted_objects << tagging.taggable } - end - # debugger - sorted_objects.flatten! - sorted_objects.uniq! - objects = get_with_nil(objects, option, sorted_objects) - end - end - end - if @filter - @filter.each do |key, value| - case key - when 'status' - a = Array.new - objects.each do |object| - value.each do |v| - a << object if object[v] - end - end - objects = a.uniq - when 'categories' - a = Array.new - objects.each do |object| - a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil) - end - objects = a.uniq - when 'tags' - a = Array.new - objects.each do |object| - object.tags.each do |tag| - a << object if value.include?(tag.id.to_s) - end - end - objects = a.uniq - end if value.size > 0 - end - end - Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10) - end - - def get_string_value_from_object(object) - s = object.name_translations[I18n.locale.to_s] unless s rescue nil - s = object.title_translations[I18n.locale.to_s] unless s rescue nil - s = object.name unless s rescue nil - s = object.title unless s rescue nil - s.downcase rescue '' - 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.to_a.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).blank? || (object.send(option).size == 0 rescue nil)) } - 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 - - - def get_viewable(object_class, query=nil) - objects = get_objects(object_class,query).order_by(:created_at, :desc) - Kaminari.paginate_array(objects).page(params[:page]).per(10) - end - - def get_objects(object_class, query=nil) - object_class = object_class.classify.constantize - # debugger - # a=1 - if query - objects = object_class.where(query) - else - objects = object_class.all - end - objects - end - - def filter_authorized_objects(objects) - if(!is_admin? || !is_manager?) - objects.delete_if{ |object| - if object.is_pending == true - if check_permission(:manager) - object.create_user_id != current_user.id - else - !object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false - end - else - false - end - } - end - objects - end - -end \ No newline at end of file +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index afd43d63..2c538cd4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -14,7 +14,7 @@ module ApplicationHelper def check_user_role_enable(attribute_fields) @user.attribute_values.collect{|t| attribute_fields.include?(t.attribute_field) }.include?(true) rescue false end - + def show_attribute_value(value) if value.kind_of? Hash result = [] @@ -29,7 +29,7 @@ module ApplicationHelper u = nil User.without_callback(:create, :before, :initialize_desktop) do u = User.create(:name => "guest", :email => "guest_#{Time.now.to_i}#{rand(99)}@example.com") - end + end u.admin = false u.save(:validate => false) u @@ -57,24 +57,24 @@ module ApplicationHelper def flash_messages return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)} - formatted_messages = messages.map do |type| + formatted_messages = messages.map do |type| content_tag :div, :class => type.to_s do message_for_item(flash[type], flash["#{type}_item".to_sym]) end end raw(formatted_messages.join) end - + def link_back(custom_class=nil) case custom_class when nil - link_to t('back'), get_go_back, :class => 'nav' + link_to t('back'), get_go_back, :class => 'nav' else - link_to t('back'), get_go_back, :class => custom_class + link_to t('back'), get_go_back, :class => custom_class end - + end - + # Clean the link back def get_go_back begin @@ -115,28 +115,28 @@ module ApplicationHelper message % item end end - + def add_attribute(partial, f, attribute) new_object = f.object.send(attribute).build fields = f.fields_for(attribute, new_object, :child_index => "new_#{attribute}") do |f| render :partial => partial, :object => new_object, :locals => {:f => f} end end - + def active_for_ob_auths_object(object_class,field = :object_auth_id) unless active_for_action("object_auths_new_interface","setting").nil? ob_auth = ObjectAuth.find params[field] ob_auth.obj_authable_type == object_class.to_s ? 'active' : nil end end - + def active_for_ob_auth(ob_auth_title,field = :object_auth_id) unless active_for_action("module_apps_new_interface","setting").nil? oa_auth = ObjectAuth.find params[field] oa_auth.title == ob_auth_title ? 'active' : nil end end - + def active_for_app_auth(app_title ='', opt={:controller_name => 'module_apps_new_interface',:action_name=>'setting',:field => :module_app_id}) unless active_for_action(opt[:controller_name],opt[:action_name]).nil? app = ModuleApp.find params[opt[:field]] @@ -145,7 +145,7 @@ module ApplicationHelper nil end end - + def active_for_controllers(*controller_names) (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? 'active' : nil end @@ -153,11 +153,11 @@ module ApplicationHelper def visible_for_controllers(*controller_names) (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? '' : 'hide' end - + def active_for_action(controller_name, action_name) ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil end - + def page_metas(page) tmp_meta = {} metas = '' @@ -186,11 +186,11 @@ module ApplicationHelper res << page_title end res << "\n" - end + end def page_stylesheets(page, edit=nil) stylesheets = '' - unless edit + unless edit stylesheets << "\n" stylesheets << "\n" stylesheets << "\n" @@ -199,9 +199,9 @@ module ApplicationHelper stylesheets << "\n" stylesheets << "\n" stylesheets << "\n" - stylesheets << "\n" - stylesheets << "\n" - stylesheets << "\n" + stylesheets << "\n" + stylesheets << "\n" + stylesheets << "\n" stylesheets << "\n" if page.design.css_default theme = page.design.themes.detect{ |d| d.id == page.theme_id } stylesheets << "\n" if theme @@ -235,9 +235,9 @@ module ApplicationHelper end def at_least_module_manager - is_manager? || is_admin? + is_manager? || is_admin? end - + def dislpay_view_count(object) "#{t(:view_count)}: #{object.view_count}" end @@ -303,4 +303,12 @@ module ApplicationHelper "http://#{request.host}:2#{site_number}00" end + def sortable(column, title = nil, options = {}) + options[:remote] ||= true + title ||= column.titleize + css_class = column.to_s == params[:sort] ? "current #{params[:direction]}" : 'asc' + direction = column.to_s == params[:sort] && params[:direction] == 'asc' ? 'desc' : 'asc' + link_to title, params.merge({direction: direction, sort: column, sort_options: column}), {:class => "#{css_class} sortable", :remote => options[:remote]} + end + end diff --git a/app/models/mongoid/sortable.rb b/app/models/mongoid/sortable.rb new file mode 100644 index 00000000..1e7302b8 --- /dev/null +++ b/app/models/mongoid/sortable.rb @@ -0,0 +1,34 @@ +module Mongoid + module Sortable + extend ActiveSupport::Concern + + included do + field :sort_number, type: Integer + + after_initialize :set_sort_number + end + + module ClassMethods + def default_sort(sorted = true) + if !sorted + asc(:sort_number) + else + asc + end + end + end + + private + + def set_sort_number + if self.sort_number.nil? + last_record = self.class.desc(:sort_number).first + if last_record + self.sort_number = (last_record.sort_number + 10) / 10 * 10 + else + self.sort_number = 10 + end + end + end + end +end diff --git a/config/locales/mobile.zh_tw.yml b/config/locales/mobile.zh_tw.yml index 76f96140..84c32558 100644 --- a/config/locales/mobile.zh_tw.yml +++ b/config/locales/mobile.zh_tw.yml @@ -10,4 +10,4 @@ zh_tw: language: 語言 location: 地理位置 location_description: '
@@ -20,4 +21,4 @@ |
---|
<%= t("archive.Title")%> | +<%= sortable(:title, t("archive.Title"))%> | <%= t("archive.Files")%> | -<%= t("archive.Category")%> | +<%= sortable(:archive_file_category, t("archive.Category"))%> |
---|---|---|---|---|
<%= post.title %> |
@@ -22,7 +21,7 @@