foodsoft/plugins/wiki/app/controllers/pages_controller.rb

144 lines
3.6 KiB
Ruby
Raw Normal View History

# encoding: utf-8
class PagesController < ApplicationController
before_filter -> { require_plugin_enabled FoodsoftWiki }
2014-01-04 20:12:01 +01:00
skip_before_filter :authenticate, :only => :all
before_filter :only => :all do
authenticate_or_token(['wiki', 'all'])
end
before_filter do
content_for :head, view_context.rss_meta_tag
end
def index
@page = Page.find_by_permalink "Home"
if @page
render :action => 'show'
else
redirect_to all_pages_path
end
end
def show
2009-09-28 15:31:20 +02:00
if params[:permalink]
@page = Page.find_by_permalink(params[:permalink])
elsif params[:id]
page = Page.find_by_id(params[:id])
if page.nil?
2013-04-12 00:58:38 +02:00
flash[:error] = I18n.t('pages.cshow.error_noexist')
2009-09-28 15:31:20 +02:00
redirect_to all_pages_path and return
else
redirect_to wiki_page_path(page.permalink) and return
end
end
if @page.nil?
redirect_to new_page_path(:title => params[:permalink])
elsif @page.redirect?
page = Page.find_by_id(@page.redirect)
unless page.nil?
2013-04-12 00:58:38 +02:00
flash[:notice] = I18n.t('pages.cshow.redirect_notice', :page => @page.title)
redirect_to wiki_page_path(page.permalink)
end
end
end
def new
@page = Page.new
@page.title = params[:title].gsub("_", " ") if params[:title]
@page.parent = Page.find_by_permalink(params[:parent]) if params[:parent]
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @page }
end
end
def edit
@page = Page.find(params[:id])
end
def create
2013-11-25 16:33:12 +01:00
@page = Page.new(params[:page].merge({:user => current_user}))
if params[:preview]
render :action => 'new'
else
if @page.save
2013-04-12 00:58:38 +02:00
flash[:notice] = I18n.t('pages.create.notice')
redirect_to(wiki_page_path(@page.permalink))
else
render :action => "new"
end
end
end
def update
@page = Page.find(params[:id])
@page.attributes = params[:page].merge({:user => current_user})
if params[:preview]
@page.attributes = params[:page]
render :action => 'edit'
else
if @page.save
@page.parent_id = parent_id if (!params[:parent_id].blank? \
&& params[:parent_id] != @page_id)
2013-04-12 00:58:38 +02:00
flash[:notice] = I18n.t('pages.update.notice')
redirect_to wiki_page_path(@page.permalink)
else
render :action => "edit"
end
end
rescue ActiveRecord::StaleObjectError
2013-04-12 00:58:38 +02:00
flash[:error] = I18n.t('pages.error_stale_object')
redirect_to wiki_page_path(@page.permalink)
end
def destroy
@page = Page.find(params[:id])
@page.destroy
2013-04-12 00:58:38 +02:00
flash[:notice] = I18n.t('pages.destroy.notice', :page => @page.title)
redirect_to wiki_path
end
def all
2012-10-09 02:31:10 +02:00
@pages = Page.non_redirected
@partial = params[:view] || 'recent_changes'
if params[:name]
@pages = @pages.where("title LIKE ?", "%#{params[:name]}%").limit(20).order('updated_at DESC')
@partial = 'title_list'
else
order = case @partial
when 'recent_changes' then
'updated_at DESC'
when 'site_map' then
'created_at DESC'
when 'title_list' then
'title DESC'
end
@pages.order(order)
end
2014-01-04 20:12:01 +01:00
respond_to do |format|
format.html
format.rss { render :layout => false }
end
end
2009-05-15 17:32:45 +02:00
def version
@page = Page.find(params[:id])
@version = Page::Version.find_by_page_id_and_lock_version params[:id], params[:version]
2009-05-15 17:32:45 +02:00
end
def revert
@page = Page.find(params[:id])
@page.revert_to!(params[:version].to_i)
2009-05-15 17:32:45 +02:00
redirect_to wiki_page_path(@page.permalink)
2009-05-15 17:32:45 +02:00
end
end