boissier_app/app/controllers/admin/menu_items_controller.rb
Nicolas Bally 6abf7679fd initial
2011-05-14 13:36:30 +02:00

175 lines
3.9 KiB
Ruby

# -*- encoding : utf-8 -*-
class Admin::MenuItemsController < ApplicationController
before_filter :authenticate_admin!
#load_and_authorize_resource
layout "admin"
navigation :menu_items
def index
if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
params[:parent_id] = nil if !params[:parent_id]
@menu_parent = MenuItem.find(params[:parent_id]) if params[:parent_id]
@order = "position ASC"
@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())
if @menu_items.num_pages.to_i < magick_page().to_i
params[:page] = @menu_items.num_pages
@menu_items = @menu.menu_items.where(:parent_id => params[:parent_id]).order(@order).page(magick_page()).per(magick_per_page())
end
else
redirect_to admin_root_path, :alert => "Un menu doit être séléctionné."
end
end
def new
current_navigation :new_menu_items
@menu_item = MenuItem.new(:menu_id => params[:menu_id],:parent_id => params[:parent_id])
params[:menu_item_content_type] = "Page" if !params[:menu_item_content_type]
@content = params[:menu_item_content_type].constantize.new()
@menu_item.menu_content = @content
end
def create
current_navigation :new_menu_items
params_content = params[:menu_item].delete(:menu_content_attributes)
content_type = params[:menu_item][:menu_content_type]
puts params[:menu_item][:menu_content_type]
@menu_item= MenuItem.new(params[:menu_item])
content = content_type.constantize.new(params_content)
@menu_item.menu_content = content
respond_to do |format|
if @menu_item.save
MenuItem.all.each do |mi|
mi.set_permalink
end
flash[:notice] = "Le menu à été ajouté avec succès."
format.html {
redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id))
}
else
format.html { render :action => "new" }
end
end
end
def edit
current_navigation :edit_menu_items
@menu_item= MenuItem.find(params[:id])
end
def update
current_navigation :edit_menu_items
@menu_item= MenuItem.find(params[:id])
@menu = @menu_item.menu
if request.xhr?
@menu_parent = @menu_item.parent if @menu_item.parent
@menu_items = MenuItem.where(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id).order(:position)
end
if params[:menu_item][:parent_id] and params[:menu_item][:parent_id] == "no-menu-selected"
@menu_item.parent_id = nil
@menu_item.save
params[:menu_item].delete(:parent_id)
end
@reorder = true if params[:reorder]
respond_to do |format|
if @menu_item.update_attributes(params[:menu_item])
puts "TEST"
MenuItem.all.each do |mi|
mi.set_permalink
end
flash[:notice] = "Le menu à été modifié avec succès."
format.html { redirect_to(admin_menu_items_path(:parent_id => @menu_item.parent_id, :menu_id => @menu_item.menu_id)) }
if @reorder
format.js {
render :action => "update" }
else
format.js {
render :action => "show" }
end
else
flash[:alert] = "Cet élément de menu n'a pas pu être déplacé. Vérifiez que sont lien permanent n'éxiste pas déjà dans l'élément cible."
if @reorder
format.js { render :action => "update_reorder_failled" }
else
format.html { render :action => "edit" }
format.js { render :action => "edit" }
end
end
end
end
def destroy
@menu_item = MenuItem.find(params[:id])
@menu_item.destroy
flash[:notice] = "L'élément de menu a été supprimé avec succès."
respond_to do |format|
format.html { redirect_to(admin_menu_items_path(:menu_id => @menu_item.menu_id, :parent_id => @menu_item.parent_id)) }
format.js
end
end
def show
@menu_item= MenuItem.find(params[:id])
end
end