public
Description: OneBody is free, open-source, web-based social networking and online directory software for churches.
Homepage: http://beonebody.com
Clone URL: git://github.com/seven1m/onebody.git
seven1m (author)
Tue Aug 05 06:14:00 -0700 2008
commit  83b27eba03cce8f001d7b871292e47bb9ac1a512
tree    f60e47c0849aa9e637359e511aca85182391eee4
parent  774e4c1209c23c99a044f020e90f18929e69f787
onebody / app / controllers / pages_controller.rb
100644 147 lines (130 sloc) 4.262 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class PagesController < ApplicationController
  skip_before_filter :authenticate_user, :only => %w(show_for_public)
  skip_before_filter :feature_enabled?
  before_filter :get_path
  before_filter :get_page, :get_user, :only => %w(show_for_public)
  before_filter :feature_enabled? # must follow get_page
  
  #caches_action :show_for_public, :for => 1.day,
  # :cache_path => Proc.new { |c| "pages/#{c.instance_eval('@page.path')}" rescue '' },
  # :if => Proc.new { |c| !(l = c.instance_eval('@logged_in')) or !l.admin?(:edit_pages) }
  #cache_sweeper :page_sweeper, :only => %w(create update destroy)
  
  def index
    @pages = Page.find_all_by_parent_id(params[:parent_id], :order => 'title')
    @parent = Page.find_by_id(params[:parent_id])
  end
  
  def show_for_public
    if @theme_name == 'page:template'
      if @page.published?
        render_with_template(@page)
      else
        render_with_template('Page not found.', 404)
      end
    else
      if @page.published?
        render :action => 'show'
      else
        render :text => 'Page not found.', :status => 404
      end
    end
  end
  
  def show
    @page = Page.find(params[:id])
    unless @logged_in.admin?(:edit_pages)
      redirect_to page_for_public_path(:path => @page.path)
    end
  end
  
  def new
    if @logged_in.admin?(:edit_pages)
      @page = Page.new(:parent_id => params[:parent_id])
      @page_paths_and_ids = Page.paths_and_ids
    else
      render :text => 'You are not authorized to create a page.', :layout => true, :status => 401
    end
  end
  
  def create
    if @logged_in.admin?(:edit_pages)
      @page = Page.create(params[:page])
      unless @page.errors.any?
        flash[:notice] = 'Page saved.'
        redirect_to params[:commit] =~ /continue editing/i ? edit_page_path(@page) : @page
      else
        @page_paths_and_ids = Page.paths_and_ids
        render :action => 'new'
      end
    else
      render :text => 'You are not authorized to create a page.', :layout => true, :status => 401
    end
  end
  
  def edit
    @page = Page.find(params[:id])
    if @logged_in.can_edit?(@page)
      @page_paths_and_ids = Page.paths_and_ids
    else
      render :text => 'You are not authorized to edit this page.', :layout => true, :status => 401
    end
  end
  
  def update
    @page = Page.find(params[:id])
    if @logged_in.can_edit?(@page)
      if @page.update_attributes(params[:page])
        flash[:notice] = 'Page saved.'
        redirect_to params[:commit] =~ /continue editing/i ? edit_page_path(@page) : @page
      else
        @page_paths_and_ids = Page.paths_and_ids
        render :action => 'edit'
      end
    else
      render :text => 'You are not authorized to edit this page.', :layout => true, :status => 401
    end
  end
  
  def destroy
    @page = Page.find(params[:id])
    if @logged_in.can_edit?(@page)
      @page.destroy
      if @page.errors.any?
        add_errors_to_flash(@page)
      else
        flash[:notice] = 'Page deleted.'
      end
      redirect_to pages_path
    else
      render :text => 'You are not authorized to delete this page.', :layout => true, :status => 401
    end
  end
  
  private
  
    def render_with_template(page, status=200)
      content = page.is_a?(String) ? page : page.body
      if template = Page.find_by_path('template')
        render :text => template.body.sub(/\[\[content\]\]/, content), :status => status
      else
        render :text => 'Template not found.', :layout => true, :status => 500
      end
    end
  
    def get_path
      @path = params[:path].to_a.join('/')
      if @path.sub!(%r{/edit$}, '')
        redirect_to edit_page_path(Page.find(@path))
        return false
      end
    end
    
    def get_page
      @page = Page.find(@path)
    end
    
    def get_theme_name
      if params[:action] == 'show_for_public'
        if (@theme_name = Setting.get(:appearance, :public_theme)) == 'page:template'
          'aqueouslight'
        else
          @theme_name
        end
      else
        super
      end
    end
    
    def feature_enabled?
      unless (@page and @page.system? and !@page.home?) or Setting.get(:features, :content_management_system)
        redirect_to people_path
        false
      end
    end
 
end