This repository has been archived by the owner on Apr 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
content_pages_controller.rb
179 lines (160 loc) · 6.16 KB
/
content_pages_controller.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class ContentPagesController < ApplicationController
before_filter :require_admin_user, :except => [:home, :index, :show, :search]
before_filter :expire_content_page_caches, :only => [:update, :destroy, :delete_asset]
def home
@content_page = ContentPage.get_front_page
@page_layout_file = File.join(Rails.root, "/themes/page_layouts/#{@content_page.page_layout || 'default'}")
unless @content_page.layout.blank?
@special_layout_file = File.join(Rails.root, "/themes/layouts/#{@content_page.layout}.html.erb")
end
render :action => :show
end
# GET /content_pages
# GET /content_pages.xml
def index
@content_pages = ContentPage.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @content_pages }
end
end
# GET /content_pages/1
# GET /content_pages/1.xml
def show
if !fragment_exist?({}) or (current_user and current_user.is_admin?)
@content_page = ContentPage.find(params[:id])
@page_layout_file = File.join(Rails.root, "/themes/page_layouts/#{@content_page.page_layout || 'default'}")
else
@content_page = ContentPage.select('layout, name, is_preview_only, publish_on').find(params[:id])
end
unless @content_page.layout.blank?
@special_layout_file = File.join(Rails.root, "/themes/layouts/#{@content_page.layout}.html.erb")
end
if @content_page.ready_for_publishing? or (current_user and current_user.is_admin?)
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @content_page }
end
else
flash[:warning] = "That page is currently unavailable."
redirect_to content_pages_path
end
end
# GET /content_pages/new
# GET /content_pages/new.xml
def new
@content_page = ContentPage.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @content_page }
end
end
# GET /content_pages/1/edit
def edit
@content_page = ContentPage.find(params[:id])
unless @content_page.editing_user
@content_page.update_attributes :editing_user => current_user, :started_editing_at => Time.now
end
@rel_dir = File.join "content_page_assets", "content_page_#{@content_page.id}"
@assets = Dir[File.join(Rails.root, 'public', @rel_dir, '*')].map { |f| File.basename(f) }
end
# POST /content_pages
# POST /content_pages.xml
def create
@content_page = ContentPage.new(params[:content_page])
respond_to do |format|
if @content_page.save
if params[:new_category] and !params[:new_category].blank?
cat = Category.find_or_create_by_name params[:new_category]
@content_page.categories << cat
@content_page.save
end
flash[:notice] = "Page <em>#{@content_page.name}</em> was created."
format.html { redirect_to(edit_content_page_path(@content_page)) }
format.xml { render :xml => @content_page, :status => :created, :location => @content_page }
else
format.html { render :action => "new" }
format.xml { render :xml => @content_page.errors, :status => :unprocessable_entity }
end
end
end
# PUT /content_pages/1
# PUT /content_pages/1.xml
def update
@content_page = ContentPage.find(params[:id])
respond_to do |format|
if @content_page.update_attributes(params[:content_page])
@content_page.update_attributes :editing_user => nil, :started_editing_at => nil
if params[:content_page] and params[:content_page][:category_ids].blank?
@content_page.categories = []
end
if params[:new_category] and !params[:new_category].blank?
cat = Category.find_or_create_by_name params[:new_category]
@content_page.categories << cat
@content_page.save
end
flash[:notice] = "Page <em>#{@content_page.name}</em> was updated."
format.html { redirect_to(@content_page) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @content_page.errors, :status => :unprocessable_entity }
end
end
end
# called when someone willingly navigates away
def un_edit
@content_page = ContentPage.find params[:id]
if @content_page.editing_user == current_user
@content_page.update_attributes :editing_user => nil, :started_editing_at => nil
end
respond_to do |wants|
wants.html do
flash[:notice] = "Page <em>#{@content_page.title}</em> was not changed."
redirect_to @content_page
end
wants.js { render :nothing => true }
end
end
# DELETE /content_pages/1
# DELETE /content_pages/1.xml
def destroy
@content_page = ContentPage.find(params[:id])
@content_page.destroy
respond_to do |format|
format.html { redirect_to(content_pages_url) }
format.xml { head :ok }
end
end
def upload_handler
@content_page = ContentPage.find params[:id]
file_name = params[:upload].original_filename
# break it up into file and extension
# we need this to check the types and to build new names if necessary
rel_dir = File.join "content_page_assets", "content_page_#{@content_page.id}"
actual_dir = File.join(Rails.root, 'public', rel_dir)
FileUtils.mkdir_p actual_dir
File.open(File.join(actual_dir, file_name), 'wb') do |f|
f.write(params[:upload].read)
end
render :text => "<html><body><script type=\"text/javascript\">" +
"parent.CKEDITOR.tools.callFunction( #{params[:CKEditorFuncNum]}, '/#{rel_dir}/#{file_name}' )" +
"</script></body></html>"
end
def delete_asset
@content_page = ContentPage.find params[:id]
file = File.join Rails.root, 'public', "content_page_assets", "content_page_#{@content_page.id}", params[:asset]
if File.exists?(file) and File.delete(file)
flash[:notice] = "#{File.basename(file)} deleted."
else
flash[:error] = "Unable to delete #{File.basename(file)}."
end
redirect_to edit_content_page_path(@content_page)
end
def search
@search_phrase = params[:q]
@content_pages = ContentPage.search @search_phrase
@categories = Category.search @search_phrase
render :action => :index
end
end