cms_controller.rb
2.94 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
class CmsController < MyProfileController
protect 'post_content', :profile, :except => [:set_home_page]
protect 'edit_profile', :profile, :only => [:set_home_page]
def boxes_holder
profile
end
include CmsHelper
ARTICLE_TYPES = [
Folder,
TinyMceArticle,
TextileArticle,
RssFeed,
UploadedFile,
Event,
]
def view
@article = profile.articles.find(params[:id])
@subitems = @article.children.reject {|item| item.folder? }
@folders = @article.children.select {|item| item.folder? }
end
def index
@article = nil
@subitems = profile.top_level_articles.reject {|item| item.folder? }
@folders = profile.top_level_articles.select {|item| item.folder?}
render :action => 'view'
end
def edit
@article = profile.articles.find(params[:id])
@parent_id = params[:parent_id]
@type = params[:type]
if request.post?
@article.last_changed_by = user
if @article.update_attributes(params[:article])
redirect_back
return
end
end
end
def new
# FIXME this method should share some logic wirh edit !!!
# user must choose an article type first
@type = params[:type]
if @type.blank?
@article_types = []
ARTICLE_TYPES.each do |type|
@article_types.push({
:name => type.name,
:short_description => type.short_description,
:description => type.description
})
end
@parent_id = params[:parent_id]
render :action => 'select_article_type', :layout => false
return
end
raise "Invalid article type #{@type}" unless ARTICLE_TYPES.map {|item| item.name}.include?(@type)
klass = @type.constantize
@article = klass.new(params[:article])
if params[:parent_id]
parent = profile.articles.find(params[:parent_id])
if ! parent.allow_children?
raise ArgumentError.new("cannot create child of article which does not accept children")
end
@article.parent = parent
@parent_id = parent.id
end
@article.profile = profile
@article.last_changed_by = user
if request.post?
if @article.save
redirect_back
return
end
end
render :action => 'edit'
end
post_only :set_home_page
def set_home_page
@article = profile.articles.find(params[:id])
profile.home_page = @article
profile.save!
flash[:notice] = _('Article "%s" configured as home page.') % @article.name
redirect_to :action => 'view', :id => @article.id
end
post_only :destroy
def destroy
@article = profile.articles.find(params[:id])
@article.destroy
redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent
end
def why_categorize
render :action => params[:action], :layout => false
end
protected
def redirect_back
if @article.parent
redirect_to :action => 'view', :id => @article.parent
else
redirect_to :action => 'index'
end
end
end