2009-03-25 19:54:04 +01:00
|
|
|
class PagesController < ApplicationController
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action -> { require_plugin_enabled FoodsoftWiki }
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :catch_special_pages, only: %i[show new]
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
skip_before_action :authenticate, only: :all
|
|
|
|
before_action only: :all do
|
|
|
|
authenticate_or_token(%w[wiki all])
|
2014-01-04 20:12:01 +01:00
|
|
|
end
|
2019-10-28 21:11:35 +01:00
|
|
|
before_action do
|
2014-01-04 20:12:01 +01:00
|
|
|
content_for :head, view_context.rss_meta_tag
|
|
|
|
end
|
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
def index
|
2023-05-12 13:01:12 +02:00
|
|
|
@page = Page.find_by_permalink 'Home'
|
2009-03-25 19:54:04 +01:00
|
|
|
|
|
|
|
if @page
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'show'
|
2009-03-25 19:54:04 +01:00
|
|
|
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
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-03-25 19:54:04 +01:00
|
|
|
if @page.nil?
|
2023-05-12 13:01:12 +02:00
|
|
|
redirect_to new_page_path(title: params[:permalink])
|
2009-06-11 23:51:26 +02:00
|
|
|
elsif @page.redirect?
|
2009-09-28 17:18:09 +02:00
|
|
|
page = Page.find_by_id(@page.redirect)
|
|
|
|
unless page.nil?
|
2023-05-12 13:01:12 +02:00
|
|
|
flash[:notice] = I18n.t('pages.cshow.redirect_notice', page: @page.title)
|
2009-09-28 17:18:09 +02:00
|
|
|
redirect_to wiki_page_path(page.permalink)
|
|
|
|
end
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2009-05-20 15:26:53 +02:00
|
|
|
@page = Page.new
|
2023-05-12 13:01:12 +02:00
|
|
|
@page.title = params[:title].gsub('_', ' ') if params[:title]
|
2009-08-12 18:41:25 +02:00
|
|
|
@page.parent = Page.find_by_permalink(params[:parent]) if params[:parent]
|
2009-03-25 19:54:04 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html # new.html.erb
|
2023-05-12 13:01:12 +02:00
|
|
|
format.xml { render xml: @page }
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@page = Page.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2023-05-12 13:01:12 +02:00
|
|
|
@page = Page.new(params[:page].merge({ user: current_user }))
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2009-05-20 12:42:01 +02:00
|
|
|
if params[:preview]
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'new'
|
|
|
|
elsif @page.save
|
|
|
|
flash[:notice] = I18n.t('pages.create.notice')
|
|
|
|
redirect_to(wiki_page_path(@page.permalink))
|
2009-05-20 12:42:01 +02:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'new'
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@page = Page.find(params[:id])
|
2023-05-12 13:01:12 +02:00
|
|
|
@page.attributes = params[:page].merge({ user: current_user })
|
2009-03-25 19:54:04 +01:00
|
|
|
|
2009-05-20 12:42:01 +02:00
|
|
|
if params[:preview]
|
|
|
|
@page.attributes = params[:page]
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'edit'
|
|
|
|
elsif @page.save
|
|
|
|
@page.parent_id = parent_id if params[:parent_id].present? \
|
|
|
|
&& params[:parent_id] != @page_id
|
|
|
|
flash[:notice] = I18n.t('pages.update.notice')
|
|
|
|
redirect_to wiki_page_path(@page.permalink)
|
2009-05-15 13:54:42 +02:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'edit'
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
2009-05-15 13:54:42 +02:00
|
|
|
rescue ActiveRecord::StaleObjectError
|
2013-04-12 00:58:38 +02:00
|
|
|
flash[:error] = I18n.t('pages.error_stale_object')
|
2009-05-15 13:54:42 +02:00
|
|
|
redirect_to wiki_page_path(@page.permalink)
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@page = Page.find(params[:id])
|
|
|
|
@page.destroy
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
flash[:notice] = I18n.t('pages.destroy.notice', page: @page.title)
|
2009-09-28 16:26:53 +02:00
|
|
|
redirect_to wiki_path
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def all
|
2012-10-09 02:31:10 +02:00
|
|
|
@pages = Page.non_redirected
|
2018-06-11 15:34:58 +02:00
|
|
|
@partial = params[:view] || 'site_map'
|
2012-10-09 02:31:10 +02:00
|
|
|
|
|
|
|
if params[:name]
|
2023-05-12 13:01:12 +02:00
|
|
|
@pages = @pages.where('title LIKE ?', "%#{params[:name]}%").limit(20)
|
2012-10-09 02:31:10 +02:00
|
|
|
@partial = 'title_list'
|
2016-03-06 18:32:46 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
sort = if params[:sort]
|
|
|
|
case params[:sort]
|
|
|
|
when 'title' then 'title'
|
|
|
|
when 'title_reverse' then 'title DESC'
|
|
|
|
when 'last_updated' then 'updated_at DESC'
|
|
|
|
when 'last_updated_reverse' then 'updated_at'
|
2021-03-01 15:27:26 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
else
|
|
|
|
'title'
|
|
|
|
end
|
2016-03-06 18:32:46 +01:00
|
|
|
@pages = @pages.order(sort)
|
2014-01-04 20:12:01 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2023-05-12 13:01:12 +02:00
|
|
|
format.rss { render layout: false }
|
2014-01-04 20:12:01 +01:00
|
|
|
end
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|
2009-05-15 17:32:45 +02:00
|
|
|
|
2016-03-06 21:05:06 +01:00
|
|
|
def diff
|
|
|
|
@page = Page.find(params[:id])
|
|
|
|
@old_version = Page::Version.find_by_page_id_and_lock_version params[:id], params[:old]
|
|
|
|
@new_version = Page::Version.find_by_page_id_and_lock_version params[:id], params[:new]
|
|
|
|
@diff = Diffy::Diff.new(@old_version.body, @new_version.body).to_s(:html)
|
|
|
|
end
|
|
|
|
|
2009-05-15 17:32:45 +02:00
|
|
|
def version
|
|
|
|
@page = Page.find(params[:id])
|
2009-05-20 15:26:53 +02:00
|
|
|
@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])
|
2009-05-20 15:26:53 +02:00
|
|
|
@page.revert_to!(params[:version].to_i)
|
2009-05-15 17:32:45 +02:00
|
|
|
|
2009-05-20 15:26:53 +02:00
|
|
|
redirect_to wiki_page_path(@page.permalink)
|
2009-05-15 17:32:45 +02:00
|
|
|
end
|
2015-03-20 16:32:58 +01:00
|
|
|
|
|
|
|
def variables
|
|
|
|
keys = Foodsoft::ExpansionVariables.variables.keys
|
2023-05-12 13:01:12 +02:00
|
|
|
@variables = keys.index_with { |k| Foodsoft::ExpansionVariables.get(k) }
|
2015-03-20 16:32:58 +01:00
|
|
|
render 'variables'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def catch_special_pages
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless params[:id] == 'Help:Foodsoft_variables'
|
|
|
|
|
|
|
|
variables
|
2015-03-20 16:32:58 +01:00
|
|
|
end
|
2009-03-25 19:54:04 +01:00
|
|
|
end
|