Commit 81a0f40349ee3c5db432059df4abf66e7cb93a3c
1 parent
796ffe2e
Exists in
master
and in
29 other branches
ActionItem114: copying code from comatose to cms_controller
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@886 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
1 changed file
with
348 additions
and
2 deletions
Show diff stats
app/controllers/profile_admin/cms_controller.rb
1 | -class CmsController < Comatose::AdminController | |
1 | +class CmsController < ProfileAdminController | |
2 | 2 | include PermissionCheck |
3 | 3 | include AuthenticatedSystem |
4 | 4 | |
5 | - | |
6 | 5 | define_option :page_class, Article |
7 | 6 | |
8 | 7 | protect 'post_content', :profile, :only => [:edit, :new, :reorder, :delete] |
... | ... | @@ -16,4 +15,351 @@ class CmsController < Comatose::AdminController |
16 | 15 | def user |
17 | 16 | current_user.person |
18 | 17 | end |
18 | + | |
19 | + ############################################################# | |
20 | + # everything below was copied from comatose | |
21 | + ############################################################# | |
22 | + | |
23 | + define_option :original_template_root, nil | |
24 | + define_option :plugin_layout_path, File.join( '..', '..', '..', 'vendor', 'plugins', 'comatose', 'views', 'layouts' ) | |
25 | + | |
26 | + # which class should the controller use when manipulating pages? | |
27 | + # this option is useful to applications that want to inherit page_class | |
28 | + # into their own models. | |
29 | + define_option :page_class, Comatose::Page | |
30 | + | |
31 | + before_filter :handle_authorization | |
32 | + before_filter :set_content_type | |
33 | + | |
34 | + # Shows the page tree | |
35 | + def index | |
36 | + @root_pages = [fetch_root_page].flatten | |
37 | + end | |
38 | + | |
39 | + # Edit a specfic page (posts back) | |
40 | + def edit | |
41 | + # Clear the page cache for this page... ? | |
42 | + @page = page_class.find params[:id] | |
43 | + @root_pages = [fetch_root_page].flatten | |
44 | + if request.post? | |
45 | + @page.update_attributes(params[:page]) | |
46 | + @page.updated_on = Time.now | |
47 | + @page.author = fetch_author_name | |
48 | + if @page.save | |
49 | + expire_cms_page @page | |
50 | + expire_cms_fragment @page | |
51 | + flash[:notice] = "Saved changes to '#{@page.title}'" | |
52 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
53 | + end | |
54 | + end | |
55 | + end | |
56 | + | |
57 | + # Create a new page (posts back) | |
58 | + def new | |
59 | + @root_pages = [fetch_root_page].flatten | |
60 | + if request.post? | |
61 | + @page = page_class.new params[:page] | |
62 | + @page.author = fetch_author_name | |
63 | + if @page.save | |
64 | + flash[:notice] = "Created page '#{@page.title}'" | |
65 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
66 | + end | |
67 | + else | |
68 | + @page = page_class.new(:parent_id=>(params[:parent] || nil)) | |
69 | + end | |
70 | + end | |
71 | + | |
72 | + # Saves position of child pages | |
73 | + def reorder | |
74 | + # If it's AJAX, do our thing and move on... | |
75 | + if request.xhr? | |
76 | + params["page_list_#{params[:id]}"].each_with_index { |id,idx| page_class.update(id, :position => idx) } | |
77 | + expire_cms_page page_class.find(params[:id]) | |
78 | + render :text=>'Updated sort order', :layout=>false | |
79 | + else | |
80 | + @page = page_class.find params[:id] | |
81 | + if params.has_key? :cmd | |
82 | + @target = page_class.find params[:page] | |
83 | + case params[:cmd] | |
84 | + when 'up' then @target.move_higher | |
85 | + when 'down' then @target.move_lower | |
86 | + end | |
87 | + redirect_to :action=>'reorder', :id=>@page | |
88 | + end | |
89 | + end | |
90 | + end | |
91 | + | |
92 | + # Allows comparing between two versions of a page's content | |
93 | + def versions | |
94 | + @page = page_class.find params[:id] | |
95 | + @version_num = (params[:version] || @page.versions.length).to_i | |
96 | + @version = @page.find_version(@version_num) | |
97 | + end | |
98 | + | |
99 | + # Reverts a page to a specific version... | |
100 | + def set_version | |
101 | + if request.post? | |
102 | + @page = page_class.find params[:id] | |
103 | + @version_num = params[:version] | |
104 | + @page.revert_to!(@version_num) | |
105 | + end | |
106 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
107 | + end | |
108 | + | |
109 | + # Deletes the specified page | |
110 | + def delete | |
111 | + @page = page_class.find params[:id] | |
112 | + if request.post? | |
113 | + expire_cms_pages_from_bottom @page | |
114 | + expire_cms_fragments_from_bottom @page | |
115 | + @page.destroy | |
116 | + flash[:notice] = "Deleted page '#{@page.title}'" | |
117 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
118 | + end | |
119 | + end | |
120 | + | |
121 | + # Returns a preview of the page content... | |
122 | + def preview | |
123 | + begin | |
124 | + page = page_class.new(params[:page]) | |
125 | + page.author = fetch_author_name | |
126 | + if params.has_key? :version | |
127 | + content = page.to_html( {'params'=>params.stringify_keys, 'version'=>params[:version]} ) | |
128 | + else | |
129 | + content = page.to_html( {'params'=>params.stringify_keys} ) | |
130 | + end | |
131 | + rescue SyntaxError | |
132 | + content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '<')}</pre></p>" | |
133 | + rescue | |
134 | + content = "<p>There was an error generating the preview.</p><p><pre>#{$!.to_s.gsub(/\</, '<')}</pre></p>" | |
135 | + end | |
136 | + render :text=>content, :layout => false | |
137 | + end | |
138 | + | |
139 | + # Expires the entire page cache | |
140 | + def expire_page_cache | |
141 | + expire_cms_pages_from_bottom( fetch_root_page ) | |
142 | + expire_cms_fragments_from_bottom( fetch_root_page ) | |
143 | + flash[:notice] = "Page cache has been flushed" | |
144 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
145 | + end | |
146 | + | |
147 | + # Walks the page tree and generates HTML files in your /public | |
148 | + # folder... It will skip pages that have a 'nocache' keyword | |
149 | + # TODO: Make page cache generation work when in :plugin mode | |
150 | + def generate_page_cache | |
151 | + if runtime_mode == :plugin | |
152 | + @errors = ["Page cache cannot be generated in plugin mode"] | |
153 | + else | |
154 | + @errors = generate_all_pages_html(params) | |
155 | + end | |
156 | + if @errors.length == 0 | |
157 | + flash[:notice] = "Pages Cached Successfully" | |
158 | + else | |
159 | + flash[:notice] = "Pages Cache Error(s): #{@errors.join(', ')}" | |
160 | + flash[:cache_errors] = @errors | |
161 | + end | |
162 | + redirect_to :controller=>self.controller_name, :action=>'index' | |
163 | + end | |
164 | + | |
165 | + | |
166 | + otected | |
167 | + | |
168 | + def handle_authorization | |
169 | + if Comatose.config.admin_authorization.is_a? Proc | |
170 | + instance_eval &Comatose.config.admin_authorization | |
171 | + elsif Comatose.config.admin_authorization.is_a? Symbol | |
172 | + send(Comatose.config.admin_authorization) | |
173 | + elsif defined? authorize | |
174 | + authorize | |
175 | + else | |
176 | + true | |
177 | + end | |
178 | + end | |
179 | + | |
180 | + def fetch_author_name | |
181 | + if Comatose.config.admin_get_author.is_a? Proc | |
182 | + instance_eval &Comatose.config.admin_get_author | |
183 | + elsif Comatose.config.admin_get_author.is_a? Symbol | |
184 | + send(Comatose.config.admin_get_author) | |
185 | + elsif defined? get_author | |
186 | + get_author | |
187 | + end | |
188 | + end | |
189 | + | |
190 | + # Can be overridden -- return your root comtase page | |
191 | + def fetch_root_page | |
192 | + if Comatose.config.admin_get_root_page.is_a? Proc | |
193 | + instance_eval &Comatose.config.admin_get_root_page | |
194 | + elsif Comatose.config.admin_get_root_page.is_a? Symbol | |
195 | + send(Comatose.config.admin_get_root_page) | |
196 | + elsif defined? get_root_page | |
197 | + get_root_page | |
198 | + end | |
199 | + end | |
200 | + | |
201 | + # Sets the HTTP content-type header based on what's configured | |
202 | + # in Comatose.config.content_type | |
203 | + def set_content_type | |
204 | + response.headers["Content-Type"] = "text/html; charset=#{Comatose.config.content_type}" unless Comatose.config.content_type.nil? | |
205 | + end | |
206 | + | |
207 | + # Calls generate_page_html for each mount point.. | |
208 | + def generate_all_pages_html(params={}) | |
209 | + @errors = [] | |
210 | + @been_cached = [] | |
211 | + Comatose.mount_points.each do |root_info| | |
212 | + page_class.active_mount_info = root_info | |
213 | + generate_page_html(page_class.find_by_path( root_info[:index] ), root_info, params) | |
214 | + end | |
215 | + @errors | |
216 | + end | |
217 | + | |
218 | + # Accepts a Comatose Page and a root_info object to generate | |
219 | + # the page as a static HTML page -- using the layout that was | |
220 | + # defined on the mount point | |
221 | + def generate_page_html(page, root_info, params={}) | |
222 | + @been_cached ||= [] | |
223 | + unless page.has_keyword? :nocache or @been_cached.include? page.id | |
224 | + uri = page.uri | |
225 | + uri = "#{uri}/index".split('/').flatten.join('/') if page.full_path == root_info[:index] | |
226 | + @page = Comatose::PageWrapper.new(page) | |
227 | + begin | |
228 | + page_layout = get_page_layout(root_info) | |
229 | + #puts "mode = #{runtime_mode}, layout = #{page_layout}, template_root = #{template_root}, original_template_root = #{original_template_root}" | |
230 | + html = render_to_string( :text=>page.to_html({'params'=>params.stringify_keys}), :layout=>page_layout ) | |
231 | + cache_page( html, uri ) | |
232 | + rescue | |
233 | + logger.error "Comatose CMS Page Cache Exception: #{$!}" | |
234 | + @errors << "(#{page}/#{page.slug}) - #{$!}" | |
235 | + end | |
236 | + @been_cached << page.id | |
237 | + # recurse... | |
238 | + page.children.each do |child| | |
239 | + generate_page_html(child, root_info) | |
240 | + end | |
241 | + end | |
242 | + end | |
243 | + | |
244 | + # Calls the class methods of the same name... | |
245 | + def expire_cms_page(page) | |
246 | + self.class.expire_cms_page(page) | |
247 | + end | |
248 | + def expire_cms_pages_from_bottom(page) | |
249 | + self.class.expire_cms_pages_from_bottom(page) | |
250 | + end | |
251 | + | |
252 | + | |
253 | + # expire the page from the fragment cache | |
254 | + def expire_cms_fragment(page) | |
255 | + key = page.full_path.gsub(/\//, '+') | |
256 | + expire_fragment(key) | |
257 | + end | |
258 | + | |
259 | + # expire pages starting at a specific node | |
260 | + def expire_cms_fragments_from_bottom(page) | |
261 | + pages = page.is_a?(Array) ? page : [page] | |
262 | + pages.each do |page| | |
263 | + page.children.each {|c| expire_cms_fragments_from_bottom( c ) } if !page.children.empty? | |
264 | + expire_cms_fragment( page ) | |
265 | + end | |
266 | + end | |
267 | + | |
268 | + # Class Methods... | |
269 | + class << self | |
270 | + | |
271 | + # Walks all the way down, and back up the tree -- the allows the expire_cms_page | |
272 | + # to delete empty directories better | |
273 | + def expire_cms_pages_from_bottom(page) | |
274 | + pages = page.is_a?(Array) ? page : [page] | |
275 | + pages.each do |page| | |
276 | + page.children.each {|c| expire_cms_pages_from_bottom( c ) } if !page.children.empty? | |
277 | + expire_cms_page( page ) | |
278 | + end | |
279 | + end | |
280 | + | |
281 | + # Expire the page from all the mount points... | |
282 | + def expire_cms_page(page) | |
283 | + Comatose.mount_points.each do |path_info| | |
284 | + page_class.active_mount_info = path_info | |
285 | + expire_page(page.uri) | |
286 | + # If the page is the index page for the root, expire it too | |
287 | + if path_info[:root] == page.uri | |
288 | + expire_page("#{path_info[:root]}/index") | |
289 | + end | |
290 | + begin # I'm not sure this matters too much -- but it keeps things clean | |
291 | + dir_path = File.join(RAILS_ROOT, 'public', page.uri[1..-1]) | |
292 | + Dir.delete( dir_path ) if FileTest.directory?( dir_path ) and !page.parent.nil? | |
293 | + rescue | |
294 | + # It probably isn't empty -- just as well we leave it be | |
295 | + #STDERR.puts " - Couldn't delete dir #{dir_path} -> #{$!}" | |
296 | + end | |
297 | + end | |
298 | + end | |
299 | + | |
300 | + # Returns a path to plugin layout, if it's unspecified, otherwise | |
301 | + # a path to an application layout... | |
302 | + def get_page_layout(params) | |
303 | + if params[:layout] == 'comatose_content' | |
304 | + File.join(plugin_layout_path, params[:layout]) | |
305 | + else | |
306 | + params[:layout] | |
307 | + end | |
308 | + end | |
309 | + | |
310 | + def configure_template_root | |
311 | + if self.runtime_mode == :unknown | |
312 | + if FileTest.exist? File.join(RAILS_ROOT, 'public', 'javascripts', 'comatose_admin.js') | |
313 | + self.runtime_mode = :application | |
314 | + else | |
315 | + self.runtime_mode = :plugin | |
316 | + end | |
317 | + end | |
318 | + end | |
319 | + | |
320 | + def runtime_mode | |
321 | + @@runtime_mode ||= :unknown | |
322 | + end | |
323 | + | |
324 | + def runtime_mode=(mode) | |
325 | + case mode | |
326 | + when :plugin | |
327 | + self.original_template_root = self.template_root | |
328 | + self.template_root = File.join( File.dirname(__FILE__), '..', '..', 'views') | |
329 | + when :application | |
330 | + self.template_root = self.original_template_root if self.original_template_root | |
331 | + end | |
332 | + @@runtime_mode = mode | |
333 | + end | |
334 | + | |
335 | + end | |
336 | + | |
337 | + # Check to see if we are in 'embedded' mode, or are being 'customized' | |
338 | + # embedded = runtime_mode of :plugin | |
339 | + # customized = runtime_mode of :application | |
340 | + configure_template_root | |
341 | + | |
342 | + # | |
343 | + # Include any modules... | |
344 | + Comatose.config.admin_includes.each do |mod| | |
345 | + if mod.is_a? String | |
346 | + include mod.constantize | |
347 | + elsif mod.is_a? Symbol | |
348 | + include mod.to_s.classify.constantize | |
349 | + else | |
350 | + include mod | |
351 | + end | |
352 | + end | |
353 | + | |
354 | + # Include any helpers... | |
355 | + Comatose.config.admin_helpers.each do |mod| | |
356 | + if mod.is_a? String | |
357 | + helper mod.constantize | |
358 | + elsif mod.is_a? Symbol | |
359 | + helper mod.to_s.classify.constantize | |
360 | + else | |
361 | + helper mod | |
362 | + end | |
363 | + end | |
364 | + | |
19 | 365 | end | ... | ... |