Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / app / controllers / admin / .svn / text-base / page_controller.rb.svn-base
blob1f5c454f3dbe7a08018390e5b059e625b284bfe3
1 class Admin::PageController < Admin::AbstractModelController
2   model_class Page
3   before_filter :initialize_meta_rows_and_buttons, :only => [:new, :edit]
4   attr_accessor :cache
6   def initialize
7     super
8     @cache = ResponseCache.instance
9   end
10   
11   def index
12     @homepage = Page.find_by_parent_id(nil)
13   end
14   
15   def new
16     @page = request.get? ? Page.new_with_defaults(config) : Page.new
17     @page.slug = params[:slug]
18     @page.breadcrumb = params[:breadcrumb]
19     @page.parent = Page.find_by_id(params[:parent_id])
20     render :action => :edit if handle_new_or_edit_post
21   end
23   def edit
24     @page = Page.find(params[:id])
25     @old_page_url = @page.url
26     handle_new_or_edit_post
27   end
29   def remove
30     @page = Page.find(params[:id])
31     if request.post?
32       announce_pages_removed(@page.children.count + 1)
33       @page.destroy
34       redirect_to page_index_url
35     end
36   end
38   def clear_cache
39     if request.post?
40       @cache.clear
41       announce_cache_cleared
42       redirect_to page_index_url
43     else
44       render :text => 'Do not access this URL directly.'
45     end
46   end
48   def add_part
49     part = PagePart.new(params[:part])
50     @index = params[:index].to_i if params[:index]
51     render(:partial => 'part', :object => part, :layout => false)
52   end
54   def children
55     @parent = Page.find(params[:id])
56     @level = params[:level].to_i
57     response.headers['Content-Type'] = 'text/html;charset=utf-8'
58     render(:layout => false)
59   end
61   def tag_reference
62     @class_name = params[:class_name]
63     @display_name = @class_name.constantize.display_name
64   end
65   
66   def filter_reference
67     @filter_name = params[:filter_name]
68     @display_name = (@filter_name + "Filter").constantize.filter_name rescue "&lt;none&gt;"
69   end
70   
71   private
72   
73     def announce_saved(message = nil)
74       flash[:notice] = message || "Your page has been saved below."
75     end
76     
77     def announce_pages_removed(count)
78       flash[:notice] = if count > 1
79         "The pages were successfully removed from the site."
80       else
81         "The page was successfully removed from the site."
82       end
83     end
84     
85     def announce_cache_cleared
86       flash[:notice] = "The page cache was successfully cleared."
87     end
88     
89     def initialize_meta_rows_and_buttons
90       @buttons_partials ||= []
91       @meta ||= []
92       @meta << {:field => "slug", :type => "text_field", :args => [{:class => 'textbox', :maxlength => 100}]}
93       @meta << {:field => "breadcrumb", :type => "text_field", :args => [{:class => 'textbox', :maxlength => 160}]}
94     end
95     
96     def save
97       parts = @page.parts
98       parts_to_update = {}
99       (params[:part]||{}).each {|k,v| parts_to_update[v[:name]] = v }
100       
101       parts_to_remove = []
102       @page.parts.each do |part|
103         if(attrs = parts_to_update.delete(part.name))
104           part.attributes = part.attributes.merge(attrs)
105         else
106           parts_to_remove << part
107         end
108       end
109       parts_to_update.values.each do |attrs|
110         @page.parts.build(attrs)
111       end
112       if result = @page.save
113         new_parts = @page.parts - parts_to_remove
114         new_parts.each { |part| part.save }
115         @page.parts = new_parts
116       end
117       result
118     end
119     
120     def clear_model_cache
121       @cache.expire_response(@old_page_url || @page.url)      
122     end