From c2331695cd75ff5f3974ae2d5c40e380e10d6538 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 22 Apr 2013 16:23:39 +0800 Subject: [PATCH] Set order of page context to match the order of structure Add some params to keep the sorting --- app/models/item.rb | 12 ++++++ .../back_end/page_contexts_controller.rb | 42 ++++++++++--------- .../back_end/page_contexts/_form.html.erb | 3 ++ .../page_contexts/_page_context.html.erb | 2 +- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index fe5691412..07da2c8f9 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -64,6 +64,10 @@ class Item true end end + + def self.structure_ordered_items + self.get_children(Item.root, []) + end protected @@ -87,5 +91,13 @@ class Item def validates_presence_of_parent_id? true end + + def self.get_children(item, tree) + tree << item + item.children.each do |child| + self.get_children(child, tree) + end + tree + end end diff --git a/vendor/built_in_modules/page_content/app/controllers/panel/page_content/back_end/page_contexts_controller.rb b/vendor/built_in_modules/page_content/app/controllers/panel/page_content/back_end/page_contexts_controller.rb index 538227825..9b67fff07 100644 --- a/vendor/built_in_modules/page_content/app/controllers/panel/page_content/back_end/page_contexts_controller.rb +++ b/vendor/built_in_modules/page_content/app/controllers/panel/page_content/back_end/page_contexts_controller.rb @@ -8,7 +8,7 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll #before_filter :is_admin? def index - @page_contexts = (params.has_key?:sort)? get_sorted_and_filtered("page_contexts", {:archived => false}) : PageContext.where(:archived => false).page(params[:page]).per(10) + @page_contexts = (params.has_key?:sort)? get_sorted_and_filtered("page_contexts", {:archived => false}) : get_sorted_page_from_structure respond_to do |format| format.html # index.html.erb @@ -65,7 +65,8 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll respond_to do |format| if @page_context.save - format.html { redirect_to(panel_page_content_back_end_page_contexts_url, :notice => t('page_content.create_page_content_success')) } + # format.html { redirect_to(panel_page_content_back_end_page_contexts_url, :notice => t('page_content.create_page_content_success')) } + format.html { redirect_to(panel_page_content_back_end_page_contexts_url) } format.xml { render :xml => @page_context, :status => :created, :location => @page_context } else format.html { render :action => "new" } @@ -79,29 +80,24 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll def update @page_context = PageContext.find(params[:id]) - if ( @page_context.version > 0 ) - @create_page_context = PageContext.new(:version => @page_context.version) - @create_page_context = @page_context.clone - @create_page_context.context = @page_context.context.clone - @create_page_context.archived = true - end + if ( @page_context.version > 0 ) + @create_page_context = PageContext.new(:version => @page_context.version) + @create_page_context = @page_context.clone + @create_page_context.context = @page_context.context.clone + @create_page_context.archived = true + end @page_context.update_user_id = current_user.id @page_context.version = @page_context.version + 1 respond_to do |format| if @page_context.update_attributes(params[:page_context]) - - if ( @page_context.version > 1 ) - @create_page_context.save - end - - if !params[:page].blank? - format.html { redirect_to(panel_page_content_back_end_page_contexts_url(:page =>params[:page]), :notice => t('page_content.update_page_content_success')) } - else - format.html { redirect_to(panel_page_content_back_end_page_contexts_url, :notice => t('page_content.update_page_content_success')) } - end - format.js { render 'toggle_enable' } + if ( @page_context.version > 1 ) + @create_page_context.save + end + # format.html { redirect_to(panel_page_content_back_end_page_contexts_url(:page =>params[:page]), :notice => t('page_content.update_page_content_success')) } + format.html { redirect_to(panel_page_content_back_end_page_contexts_url(page: params[:page], sort: params[:sort], direction: params[:direction], sort_options: params[:sort_options])) } + format.js { render 'toggle_enable' } format.xml { head :ok } else format.html { render :action => "edit" } @@ -122,5 +118,13 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll format.js end end + + def get_sorted_page_from_structure + page_contexts = Item.structure_ordered_items.inject([]){ |pages, page| + pages << page.page_contexts.where(archived: false).limit(1)[0] if page.is_a?(Page) && !page.page_contexts.blank? + pages + } + Kaminari.paginate_array(page_contexts).page(params[:page]).per(10) + end end diff --git a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_form.html.erb b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_form.html.erb index 726c34e49..caef96329 100644 --- a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_form.html.erb +++ b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_form.html.erb @@ -38,6 +38,9 @@
<%= hidden_field_tag 'page', params[:page] if !params[:page].blank? %> + <%= hidden_field_tag 'sort', params[:sort] if !params[:sort].blank? %> + <%= hidden_field_tag 'direction', params[:direction] if !params[:direction].blank? %> + <%= hidden_field_tag 'sort_options', params[:sort_options] if !params[:sort_options].blank? %> <%= f.submit t('submit'), :class=>'btn btn-primary' %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
diff --git a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb index 1b4c3d903..21c740340 100644 --- a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb +++ b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb @@ -5,7 +5,7 @@