Set order of page context to match the order of structure

Add some params to keep the sorting
This commit is contained in:
chris 2013-04-22 16:23:39 +08:00 committed by Matt K. Fu
parent 127ab3845c
commit c2331695cd
4 changed files with 39 additions and 20 deletions

View File

@ -65,6 +65,10 @@ class Item
end end
end end
def self.structure_ordered_items
self.get_children(Item.root, [])
end
protected protected
def rebuild_path def rebuild_path
@ -88,4 +92,12 @@ class Item
true true
end end
def self.get_children(item, tree)
tree << item
item.children.each do |child|
self.get_children(child, tree)
end
tree
end
end end

View File

@ -8,7 +8,7 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll
#before_filter :is_admin? #before_filter :is_admin?
def index 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| respond_to do |format|
format.html # index.html.erb format.html # index.html.erb
@ -65,7 +65,8 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll
respond_to do |format| respond_to do |format|
if @page_context.save 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 } format.xml { render :xml => @page_context, :status => :created, :location => @page_context }
else else
format.html { render :action => "new" } format.html { render :action => "new" }
@ -79,29 +80,24 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll
def update def update
@page_context = PageContext.find(params[:id]) @page_context = PageContext.find(params[:id])
if ( @page_context.version > 0 ) if ( @page_context.version > 0 )
@create_page_context = PageContext.new(:version => @page_context.version) @create_page_context = PageContext.new(:version => @page_context.version)
@create_page_context = @page_context.clone @create_page_context = @page_context.clone
@create_page_context.context = @page_context.context.clone @create_page_context.context = @page_context.context.clone
@create_page_context.archived = true @create_page_context.archived = true
end end
@page_context.update_user_id = current_user.id @page_context.update_user_id = current_user.id
@page_context.version = @page_context.version + 1 @page_context.version = @page_context.version + 1
respond_to do |format| respond_to do |format|
if @page_context.update_attributes(params[:page_context]) if @page_context.update_attributes(params[:page_context])
if ( @page_context.version > 1 )
if ( @page_context.version > 1 ) @create_page_context.save
@create_page_context.save end
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])) }
if !params[:page].blank? format.js { render 'toggle_enable' }
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' }
format.xml { head :ok } format.xml { head :ok }
else else
format.html { render :action => "edit" } format.html { render :action => "edit" }
@ -123,4 +119,12 @@ class Panel::PageContent::BackEnd::PageContextsController < OrbitBackendControll
end end
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 end

View File

@ -38,6 +38,9 @@
<div class="form-actions"> <div class="form-actions">
<%= hidden_field_tag 'page', params[:page] if !params[:page].blank? %> <%= 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' %> <%= f.submit t('submit'), :class=>'btn btn-primary' %>
<%= link_to t('cancel'), get_go_back, :class=>"btn" %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
</div> </div>

View File

@ -5,7 +5,7 @@
<div class="quick-edit"> <div class="quick-edit">
<ul class="nav nav-pills hide"> <ul class="nav nav-pills hide">
<%if is_manager? || is_admin? || page_context.authed_users(:edit).include?(current_user)%> <%if is_manager? || is_admin? || page_context.authed_users(:edit).include?(current_user)%>
<li><%= link_to t(:edit), edit_panel_page_content_back_end_page_context_path(page_context, :page => params[:page]) %></li> <li><%= link_to t(:edit), edit_panel_page_content_back_end_page_context_path(page_context, page: params[:page], sort: params[:sort], direction: params[:direction], sort_options: params[:sort_options]) %></li>
<%if (is_manager? || is_admin?) %> <%if (is_manager? || is_admin?) %>
<li><%=show_page_context_edit_auth_link page_context%></li> <li><%=show_page_context_edit_auth_link page_context%></li>
<% end%> <% end%>