tb_cms 0.9.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.markdown +110 -0
  4. data/Rakefile +28 -0
  5. data/app/assets/images/spud/admin/menus_thumb.png +0 -0
  6. data/app/assets/images/spud/admin/menus_thumb@2x.png +0 -0
  7. data/app/assets/images/spud/admin/pages_thumb.png +0 -0
  8. data/app/assets/images/spud/admin/snippets_thumb.png +0 -0
  9. data/app/assets/images/spud/admin/snippets_thumb@2x.png +0 -0
  10. data/app/assets/javascripts/spud/admin/cms/application.js +25 -0
  11. data/app/assets/javascripts/spud/admin/cms/menu_items.js +35 -0
  12. data/app/assets/javascripts/spud/admin/cms/pages.js +29 -0
  13. data/app/assets/javascripts/spud/admin/snippets.js +2 -0
  14. data/app/assets/stylesheets/spud/admin/cms/application.css +28 -0
  15. data/app/assets/stylesheets/spud/admin/cms.css +4 -0
  16. data/app/assets/stylesheets/spud/admin/snippets.css +4 -0
  17. data/app/controllers/pages_controller.rb +90 -0
  18. data/app/controllers/spud/admin/cms_controller.rb +3 -0
  19. data/app/controllers/spud/admin/menu_items_controller.rb +115 -0
  20. data/app/controllers/spud/admin/menus_controller.rb +55 -0
  21. data/app/controllers/spud/admin/pages_controller.rb +182 -0
  22. data/app/controllers/spud/admin/snippets_controller.rb +61 -0
  23. data/app/controllers/spud/cms/sitemaps_controller.rb +13 -0
  24. data/app/helpers/spud/cms/application_helper.rb +283 -0
  25. data/app/models/spud_menu.rb +9 -0
  26. data/app/models/spud_menu_item.rb +72 -0
  27. data/app/models/spud_page.rb +131 -0
  28. data/app/models/spud_page_liquid_tag.rb +4 -0
  29. data/app/models/spud_page_partial.rb +67 -0
  30. data/app/models/spud_page_partial_revision.rb +5 -0
  31. data/app/models/spud_snippet.rb +42 -0
  32. data/app/observers/page_sweeper.rb +48 -0
  33. data/app/observers/snippet_sweeper.rb +41 -0
  34. data/app/views/pages/not_found.html.erb +2 -0
  35. data/app/views/pages/show.html.erb +24 -0
  36. data/app/views/spud/admin/menu_items/_form.html.erb +51 -0
  37. data/app/views/spud/admin/menu_items/_menu_item_row.html.erb +22 -0
  38. data/app/views/spud/admin/menu_items/edit.html.erb +10 -0
  39. data/app/views/spud/admin/menu_items/index.html.erb +17 -0
  40. data/app/views/spud/admin/menu_items/new.html.erb +12 -0
  41. data/app/views/spud/admin/menus/_form.html.erb +11 -0
  42. data/app/views/spud/admin/menus/edit.html.erb +13 -0
  43. data/app/views/spud/admin/menus/index.html.erb +24 -0
  44. data/app/views/spud/admin/menus/new.html.erb +7 -0
  45. data/app/views/spud/admin/pages/_form.html.erb +95 -0
  46. data/app/views/spud/admin/pages/_page_partials_form.html.erb +18 -0
  47. data/app/views/spud/admin/pages/_page_row.html.erb +18 -0
  48. data/app/views/spud/admin/pages/edit.html.erb +32 -0
  49. data/app/views/spud/admin/pages/index.html.erb +13 -0
  50. data/app/views/spud/admin/pages/new.html.erb +34 -0
  51. data/app/views/spud/admin/pages/show.html.erb +20 -0
  52. data/app/views/spud/admin/snippets/_form.html.erb +8 -0
  53. data/app/views/spud/admin/snippets/edit.html.erb +7 -0
  54. data/app/views/spud/admin/snippets/index.html.erb +24 -0
  55. data/app/views/spud/admin/snippets/new.html.erb +7 -0
  56. data/app/views/spud/admin/templates/_form.html.erb +43 -0
  57. data/app/views/spud/admin/templates/edit.html.erb +9 -0
  58. data/app/views/spud/admin/templates/index.html.erb +17 -0
  59. data/app/views/spud/admin/templates/new.html.erb +11 -0
  60. data/app/views/spud/cms/sitemaps/show.xml.builder +16 -0
  61. data/config/boot.rb +6 -0
  62. data/config/routes.rb +29 -0
  63. data/db/migrate/20120101192412_create_spud_pages.rb +19 -0
  64. data/db/migrate/20120101193138_create_spud_menus.rb +10 -0
  65. data/db/migrate/20120101193255_create_spud_menu_items.rb +17 -0
  66. data/db/migrate/20120101194124_create_spud_templates.rb +11 -0
  67. data/db/migrate/20120103034659_create_spud_page_partials.rb +13 -0
  68. data/db/migrate/20120104194032_add_visibility_to_spud_pages.rb +6 -0
  69. data/db/migrate/20120107181337_add_menu_name_to_spud_menu_items.rb +5 -0
  70. data/db/migrate/20120111134754_add_use_custom_url_name_to_spud_pages.rb +5 -0
  71. data/db/migrate/20120118141852_add_notes_to_spud_pages.rb +5 -0
  72. data/db/migrate/20120126232428_add_menu_id_to_spud_menu_items.rb +7 -0
  73. data/db/migrate/20120128163601_add_classes_to_spud_menu_items.rb +6 -0
  74. data/db/migrate/20120329132314_add_site_id_to_spud_pages.rb +6 -0
  75. data/db/migrate/20120329132322_add_site_id_to_spud_templates.rb +6 -0
  76. data/db/migrate/20120329132330_add_site_id_to_spud_menus.rb +6 -0
  77. data/db/migrate/20120510195151_create_spud_page_partial_revisions.rb +13 -0
  78. data/db/migrate/20120911190030_add_symbol_name_to_spud_page_partials.rb +5 -0
  79. data/db/migrate/20120912121313_modify_site_id_for_spud_pages.rb +15 -0
  80. data/db/migrate/20121016233715_add_content_processed_to_spud_page_partials.rb +5 -0
  81. data/db/migrate/20121112151110_add_layout_to_spud_pages.rb +8 -0
  82. data/db/migrate/20121112212113_create_spud_page_liquid_tags.rb +11 -0
  83. data/db/migrate/20121119025608_create_spud_snippets.rb +17 -0
  84. data/db/migrate/20121119030136_change_liquid_tags_to_polymorphic.rb +17 -0
  85. data/lib/generators/spud/cms/template_generator.rb +17 -0
  86. data/lib/generators/spud/cms/templates/template.html.erb +28 -0
  87. data/lib/spud_cms/configuration.rb +27 -0
  88. data/lib/spud_cms/engine.rb +69 -0
  89. data/lib/spud_cms/liquid_snippet.rb +29 -0
  90. data/lib/spud_cms/page_route.rb +5 -0
  91. data/lib/spud_cms/template_parser.rb +126 -0
  92. data/lib/spud_cms/test_files.rb +25 -0
  93. data/lib/spud_cms/version.rb +5 -0
  94. data/lib/spud_cms.rb +10 -0
  95. data/lib/tasks/spud_cms_tasks.rake +4 -0
  96. data/spec/controllers/pages_controller_spec.rb +108 -0
  97. data/spec/controllers/spud/admin/cms_controller_spec.rb +11 -0
  98. data/spec/controllers/spud/admin/menu_items_controller_spec.rb +148 -0
  99. data/spec/controllers/spud/admin/menus_controller_spec.rb +121 -0
  100. data/spec/controllers/spud/admin/pages_controller_spec.rb +111 -0
  101. data/spec/controllers/spud/admin/snippets_controller_spec.rb +5 -0
  102. data/spec/controllers/spud/cms/sitemaps_controller_spec.rb +48 -0
  103. data/spec/dummy/README.rdoc +261 -0
  104. data/spec/dummy/Rakefile +7 -0
  105. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  106. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  107. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  108. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  109. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  110. data/spec/dummy/app/views/layouts/content_left.html.erb +0 -0
  111. data/spec/dummy/config/application.rb +59 -0
  112. data/spec/dummy/config/boot.rb +10 -0
  113. data/spec/dummy/config/database.yml +15 -0
  114. data/spec/dummy/config/environment.rb +5 -0
  115. data/spec/dummy/config/environments/development.rb +37 -0
  116. data/spec/dummy/config/environments/production.rb +67 -0
  117. data/spec/dummy/config/environments/test.rb +37 -0
  118. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  119. data/spec/dummy/config/initializers/inflections.rb +15 -0
  120. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  121. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  122. data/spec/dummy/config/initializers/session_store.rb +8 -0
  123. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  124. data/spec/dummy/config/locales/en.yml +5 -0
  125. data/spec/dummy/config/routes.rb +3 -0
  126. data/spec/dummy/config.ru +4 -0
  127. data/spec/dummy/db/migrate/20120307002859_create_spud_admin_permissions.spud_core.rb +12 -0
  128. data/spec/dummy/db/migrate/20120307002860_create_spud_users.spud_core.rb +30 -0
  129. data/spec/dummy/db/migrate/20120307003559_create_spud_permalinks.spud_permalinks.rb +12 -0
  130. data/spec/dummy/db/migrate/20120610123555_add_time_zone_to_spud_user.spud_core.rb +7 -0
  131. data/spec/dummy/db/migrate/20120610123556_add_scope_to_spud_admin_permissions.spud_core.rb +7 -0
  132. data/spec/dummy/db/migrate/20120610123557_create_spud_user_settings.spud_core.rb +12 -0
  133. data/spec/dummy/db/migrate/20120610123615_add_site_id_to_spud_permalinks.spud_permalinks.rb +7 -0
  134. data/spec/dummy/db/schema.rb +171 -0
  135. data/spec/dummy/log/test.log +1874 -0
  136. data/spec/dummy/public/404.html +26 -0
  137. data/spec/dummy/public/422.html +26 -0
  138. data/spec/dummy/public/500.html +25 -0
  139. data/spec/dummy/public/favicon.ico +0 -0
  140. data/spec/dummy/script/rails +6 -0
  141. data/spec/helpers/spud/cms/application_helper_spec.rb +160 -0
  142. data/spec/models/spud_menu_item_spec.rb +31 -0
  143. data/spec/models/spud_page_liquid_tag_spec.rb +5 -0
  144. data/spec/models/spud_page_partial_revision_spec.rb +5 -0
  145. data/spec/models/spud_page_partial_spec.rb +44 -0
  146. data/spec/models/spud_page_spec.rb +163 -0
  147. data/spec/models/spud_snippet_spec.rb +5 -0
  148. data/spec/spec_helper.rb +40 -0
  149. data/spec/support/authlogic_helper.rb +2 -0
  150. metadata +427 -0
@@ -0,0 +1,182 @@
1
+ class Spud::Admin::PagesController < Spud::Admin::CmsController
2
+ layout 'spud/admin/detail'
3
+ add_breadcrumb "Pages", :spud_admin_pages_path
4
+ belongs_to_spud_app :pages
5
+ before_filter :load_page,:only => [:edit,:update,:show,:destroy]
6
+
7
+ def index
8
+
9
+ @pages = SpudPage.site(session[:admin_site]).where(:spud_page_id => nil).order(:page_order).includes(:spud_pages).paginate :page => params[:page]
10
+
11
+ home_page = SpudPage.where(:url_name => Spud::Cms.root_page_name).first
12
+ if home_page.blank?
13
+ flash.now[:warning] = "You have not setup your default CMS page. This page will be your homepage. To do so, create a page with the name '#{Spud::Cms.root_page_name.titlecase}'"
14
+ end
15
+
16
+ respond_with @pages
17
+ end
18
+
19
+ def show
20
+ layout = @page.layout || Spud::Cms.default_page_layout
21
+
22
+
23
+ render :layout => layout
24
+ end
25
+
26
+ def new
27
+ add_breadcrumb "New", :new_spud_admin_page_path
28
+
29
+ layouts = Spud::Cms::Engine.template_parser.layouts(Spud::Core.site_config_for_id(session[:admin_site] || 0)[:short_name])
30
+ layout, layout_info = layouts.select{|k,v| v[:default]}.flatten
31
+ @page = SpudPage.new(:layout => layout)
32
+ parts = layout_info[:partials]
33
+
34
+ parts.each do |part|
35
+ @page.spud_page_partials.new(:name => part.strip)
36
+ end
37
+ respond_with @page
38
+ end
39
+
40
+ def create
41
+
42
+ @page = SpudPage.new(params[:spud_page])
43
+ @page.site_id = session[:admin_site]
44
+ if params[:preview] && params[:preview].to_i == 1
45
+ preview
46
+ return
47
+ end
48
+ flash[:notice] = "Page Saved successfully" if @page.save
49
+ respond_with @page,:location => spud_admin_pages_url
50
+ end
51
+
52
+ def edit
53
+ add_breadcrumb "#{@page.name}", :spud_admin_page_path
54
+ add_breadcrumb "Edit", :edit_spud_admin_page_path
55
+
56
+ layouts = Spud::Cms::Engine.template_parser.layouts(Spud::Core.site_config_for_id(session[:admin_site] || 0)[:short_name])
57
+ layout, layout_info = layouts.select{|k,v| k == @page.layout}.flatten if @page.layout.blank? == false
58
+ if layout.blank?
59
+ layout, layout_info = layouts.select{|k,v| v[:default]}.flatten
60
+ end
61
+ @page.layout = layout
62
+
63
+ layout_info[:partials].each do |part|
64
+ partial = @page.spud_page_partials.select{|p| p.name == part.strip}
65
+ if partial.blank?
66
+ @page.spud_page_partials.new(:name => part.strip)
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ def update
73
+
74
+ @page.attributes = params[:spud_page]
75
+ if params[:preview] && params[:preview].to_i == 1
76
+ preview
77
+ return
78
+ end
79
+ if @page.save
80
+ flash[:notice] = "Page updated successfully!"
81
+ redirect_to spud_admin_pages_url() and return
82
+ else
83
+ flash[:error] = "There was an error saving this page"
84
+ render :action => "edit"
85
+ end
86
+
87
+ end
88
+
89
+ def preview
90
+ layout = @page.layout || Spud::Cms.default_page_layout
91
+ render :action => :show,:layout => layout
92
+ end
93
+
94
+ def destroy
95
+ status = 500
96
+
97
+ if @page.destroy
98
+ flash[:notice] = "Page removed successfully!"
99
+ status = 200
100
+ else
101
+ flash[:error] = "Error removing page"
102
+ end
103
+ respond_to do |format|
104
+ format.js {render :status => status}
105
+ format.html { redirect_to spud_admin_pages_url()}
106
+ end
107
+ end
108
+
109
+ def page_parts
110
+ layouts = Spud::Cms::Engine.template_parser.layouts(Spud::Core.site_config_for_id(session[:admin_site] || 0)[:short_name])
111
+ template = params[:template] && !params[:template].blank? ? layouts[params[:template]] : nil
112
+ page = SpudPage.where(:id => params[:page]).includes(:spud_page_partials).first
113
+ page = SpudPage.new if page.blank?
114
+ old_page_partials = Array.new(page.spud_page_partials)
115
+ new_page_partials = []
116
+ if !template.blank?
117
+ template[:partials].each do |page_part|
118
+ new_page_partials << page.spud_page_partials.build(:name => page_part.strip)
119
+ end
120
+ else
121
+ layout, layout_info = layouts.select{|k,v| v[:default]}.flatten
122
+ page.layout = layout
123
+ layout_info[:partials].each do |part|
124
+ new_page_partials << page.spud_page_partials.build(:name => part)
125
+ end
126
+ end
127
+ new_page_partials.each do |partial|
128
+ old_partial = old_page_partials.select {|pp| partial.name.strip.downcase == pp.name.strip.downcase }
129
+ logger.debug "testing content swap"
130
+ partial.content = old_partial[0].content if !old_partial.blank?
131
+ logger.debug partial.content
132
+ end
133
+
134
+ respond_to do |format|
135
+ format.js {
136
+ if response.status == 200
137
+ render(:partial => 'page_partials_form', :locals => {:spud_page_partials => new_page_partials, :remove_page_partials => old_page_partials})
138
+ else
139
+ render(:text => message)
140
+ end
141
+ }
142
+ end
143
+ end
144
+
145
+ def clear
146
+ Rails.cache.clear
147
+ SpudPage.site(session[:admin_site]).published_pages.all.each do |record|
148
+ if Spud::Cms.cache_mode == :full_page
149
+ if record.url_name == Spud::Cms.root_page_name
150
+ expire_page root_path
151
+ else
152
+ expire_page page_path(:id => record.url_name)
153
+ end
154
+ elsif Spud::Cms.cache_mode == :action
155
+ if record.url_name == Spud::Cms.root_page_name
156
+ expire_action root_path
157
+ else
158
+ expire_action page_path(:id => record.url_name)
159
+ end
160
+ end
161
+ end
162
+ redirect_to spud_admin_pages_url
163
+ end
164
+
165
+
166
+
167
+ private
168
+ def load_page
169
+ @page = SpudPage.where(:id => params[:id]).includes(:spud_page_partials).first
170
+ if @page.blank?
171
+ flash[:error] = "Page not found!"
172
+ redirect_to spud_admin_pages_url() and return false
173
+ elsif Spud::Core.multisite_mode_enabled && @page.site_id != session[:admin_site]
174
+ flash[:warning] = "Site Context Changed. The page you were viewing is not associated with the current site. Redirected back to page selections."
175
+ redirect_to spud_admin_pages_url() and return false
176
+ end
177
+ return true
178
+ end
179
+
180
+
181
+
182
+ end
@@ -0,0 +1,61 @@
1
+ class Spud::Admin::SnippetsController < Spud::Admin::ApplicationController
2
+ belongs_to_spud_app :snippets
3
+ layout '/spud/admin/detail'
4
+ add_breadcrumb "Snippets", :spud_admin_snippets_url
5
+
6
+ before_filter :load_snippet, :only => [:show, :edit, :update, :destroy]
7
+
8
+ cache_sweeper :snippet_sweeper, :only => [:update,:destroy,:create]
9
+
10
+ def index
11
+ @snippets = SpudSnippet.site(session[:admin_site]).order(:name).paginate :page => params[:page]
12
+
13
+ respond_with @snippets
14
+ end
15
+
16
+ def new
17
+ add_breadcrumb "New", :new_spud_admin_snippet_url
18
+ @snippet = SpudSnippet.new
19
+ respond_with @snippet
20
+ end
21
+
22
+ def create
23
+ add_breadcrumb "New", :new_spud_admin_snippet_url
24
+ @snippet = SpudSnippet.new(params[:spud_snippet])
25
+ @snippet.site_id = session[:admin_site]
26
+
27
+ @snippet.save
28
+
29
+ respond_with @snippet, :location => spud_admin_snippets_url
30
+ end
31
+
32
+ def edit
33
+ add_breadcrumb "Edit", :edit_spud_admin_snippet_url
34
+
35
+ respond_with @snippet
36
+ end
37
+
38
+ def update
39
+ add_breadcrumb "Edit", :edit_spud_admin_snippet_url
40
+ flash[:notice] = "Snippet saved successfully!" if @snippet.update_attributes(params[:spud_snippet])
41
+ respond_with @snippet, :location => spud_admin_snippets_url
42
+ end
43
+
44
+ def destroy
45
+ flash[:notice] = "Snippet removed!" if @snippet.destroy
46
+ respond_with @snippet,:location => spud_admin_snippets_url
47
+ end
48
+
49
+
50
+ private
51
+ def load_snippet
52
+ @snippet = SpudSnippet.where(:id => params[:id]).first
53
+ if @snippet.blank?
54
+ flash[:error] = "Snippet does not exist!"
55
+ redirect_to spud_admin_snippets_url and return false
56
+ elsif Spud::Core.multisite_mode_enabled && @snippet.site_id != session[:admin_site]
57
+ flash[:warning] = "This snippet is for a different site"
58
+ redirect_to spud_admin_snippets_url and return false
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,13 @@
1
+ class Spud::Cms::SitemapsController < Spud::ApplicationController
2
+ respond_to :xml
3
+ caches_page :show,:expires_in => 1.day
4
+ def show
5
+ @pages = SpudPage.published_pages.public.order(:spud_page_id)
6
+ if Spud::Core.multisite_mode_enabled
7
+ site_config = Spud::Core.site_config_for_host(request.host_with_port)
8
+ @pages = @pages.site(!site_config.blank? ? site_config[:site_id] : 0)
9
+ end
10
+ @pages = @pages.all
11
+ respond_with @pages
12
+ end
13
+ end
@@ -0,0 +1,283 @@
1
+ module Spud::Cms::ApplicationHelper
2
+ MENU_INDEX = {}
3
+
4
+ def sp_snippet(name,snippets=nil)
5
+ if name.blank?
6
+ return ''
7
+ end
8
+
9
+ if !snippets.blank?
10
+ snippet = snippets.select {|s| s.name == name}
11
+ else
12
+ snippet = SpudSnippet.where(:name => name).first
13
+ end
14
+
15
+ if !snippet.blank?
16
+ return snippet.content_processed.html_safe
17
+ else
18
+ return nil
19
+ end
20
+
21
+ end
22
+
23
+ def sp_list_pages(options = {})
24
+
25
+ pages = SpudPage.public.published_pages
26
+
27
+ if Spud::Core.multisite_mode_enabled
28
+ site_config = Spud::Core.site_config_for_host(request.host_with_port)
29
+ pages = pages.site(site_config[:site_id]) if !site_config.blank?
30
+ end
31
+ start_page = nil
32
+ max_depth = 0
33
+ active_class = "menu-active"
34
+ if !options.blank?
35
+ if options.has_key?(:exclude)
36
+
37
+ pages = pages.where(["name NOT IN (?)",options[:exclude]])
38
+ end
39
+ if options.has_key?(:start_page_id)
40
+ start_page = options[:start_page_id]
41
+ end
42
+ if options.has_key?(:id)
43
+ content = "<ul id='#{options[:id]}' #{"class='#{options[:class]}'" if options.has_key?(:class)}>"
44
+ else
45
+ content = "<ul #{"class='#{options[:class]}'" if options.has_key?(:class)}>"
46
+ end
47
+ if options.has_key?(:active_class)
48
+ active_class = options[:acive_class]
49
+ end
50
+ if options.has_key?(:max_depth)
51
+ max_depth = options[:max_depth]
52
+ end
53
+
54
+ else
55
+ content = "<ul>"
56
+ end
57
+
58
+ pages = pages.all.group_by(&:spud_page_id)
59
+ if pages[start_page].blank?
60
+
61
+ return ""
62
+ end
63
+ pages[start_page].sort_by{|p| p.page_order}.each do |page|
64
+ active = false
65
+ if !page.url_name.blank?
66
+ if current_page?(page_path(:id => page.url_name))
67
+ active = true
68
+ elsif page.url_name == Spud::Cms.root_page_name && current_page?(root_path)
69
+ active = true
70
+ end
71
+ end
72
+ content += "<li class='#{active_class if active}'><a href='#{page_path(:id => page.url_name)}'>#{page.name}</a>"
73
+ if max_depth == 0 || max_depth > 1
74
+ content += sp_list_page(page,pages,2,max_depth,options)
75
+ end
76
+ content += "</li>"
77
+ end
78
+ content += "</ul>"
79
+
80
+ return content.html_safe
81
+ end
82
+
83
+
84
+ def sp_list_menu(options = {})
85
+
86
+ max_depth = 0
87
+ menu = SpudMenu
88
+ menu_id = nil
89
+ menu_key = ""
90
+ link_options = {}
91
+ start_menu_item = nil
92
+ if Spud::Core.multisite_mode_enabled
93
+ site_config = Spud::Core.site_config_for_host(request.host_with_port)
94
+ menu = menu.site(site_config[:site_id]) if !site_config.blank?
95
+ menu_key += "#{site_config[:site_id]}_"
96
+ end
97
+ if !options.blank?
98
+
99
+ menu_id = options[:menu_id] if options.has_key?(:menu_id)
100
+ link_options = options[:link_options] if options.has_key?(:link_options)
101
+ if options.has_key?(:name)
102
+ menu_key += options[:name]
103
+ menu = menu.where(:name => options[:name])
104
+ menu_id = MENU_INDEX[menu_key]
105
+ if menu_id.blank?
106
+ menu = menu.first
107
+ if(!menu.blank?)
108
+ menu_id = menu.id
109
+ MENU_INDEX[menu_key] = menu.id
110
+ end
111
+ end
112
+ end
113
+ start_menu_item = options[:start_menu_item_id] if options.has_key?(:start_menu_item_id)
114
+ if options.has_key?(:id)
115
+ content = "<ul id='#{options[:id]}' #{"class='#{options[:class]}'" if options.has_key?(:class)}>"
116
+ else
117
+ content = "<ul #{"class='#{options[:class]}'" if options.has_key?(:class)}>"
118
+ end
119
+ max_depth = options[:max_depth] if options.has_key?(:max_depth)
120
+ else
121
+ content = "<ul>"
122
+ end
123
+
124
+ if menu_id.blank?
125
+ return ""
126
+ end
127
+ menu_items = SpudMenuItem.where(:spud_menu_id => menu_id).select("
128
+ #{SpudMenuItem.table_name}.id as id,
129
+ #{SpudMenuItem.table_name}.url as url,
130
+ #{SpudMenuItem.table_name}.classes as classes,
131
+ #{SpudMenuItem.table_name}.parent_type as parent_type,
132
+ #{SpudMenuItem.table_name}.menu_order as menu_order,
133
+ #{SpudMenuItem.table_name}.parent_id as parent_id,
134
+ #{SpudMenuItem.table_name}.name as name,
135
+ #{SpudPage.table_name}.url_name as url_name").order(:parent_type,:parent_id).joins("LEFT JOIN #{SpudPage.table_name} ON (#{SpudPage.table_name}.id = #{SpudMenuItem.table_name}.spud_page_id)").all
136
+
137
+
138
+ grouped_items = menu_items.group_by(&:parent_type)
139
+
140
+ if grouped_items["SpudMenu"].blank?
141
+
142
+ return ""
143
+ end
144
+ child_items = grouped_items["SpudMenuItem"].blank? ? [] : grouped_items["SpudMenuItem"].group_by(&:parent_id)
145
+
146
+ parent_items = grouped_items["SpudMenu"]
147
+ if start_menu_item != nil
148
+ parent_items = child_items[start_menu_item]
149
+ end
150
+
151
+ parent_items.sort_by{|p| p.menu_order}.each do |item|
152
+ active = false
153
+ if !item.url_name.blank?
154
+ if current_page?(page_path(:id => item.url_name))
155
+ active = true
156
+ elsif item.url_name == Spud::Cms.root_page_name && current_page?(root_path)
157
+ active = true
158
+ end
159
+ elsif current_page?(item.url)
160
+ active = true
161
+ end
162
+ link_tag = link_to item.name, !item.url_name.blank? ? (item.url_name == Spud::Cms.root_page_name ? root_path() : page_path(:id => item.url_name)) : item.url, {:class => "#{'menu-active' if active} #{item.classes if !item.classes.blank?}"}.merge(link_options)
163
+ content += "<li class='#{"menu-active" if active} #{item.classes if !item.classes.blank?}'>#{link_tag}"
164
+ if max_depth == 0 || max_depth > 1
165
+ content += sp_list_menu_item(child_items,item.id,2,max_depth,options)
166
+ end
167
+ content += "</li>"
168
+ end
169
+
170
+ content += "</ul>"
171
+
172
+ return content.html_safe
173
+ end
174
+
175
+ def sp_menu_with_seperator(options={})
176
+
177
+ seperator = "&nbsp;|&nbsp;".html_safe
178
+ if(options.has_key?(:seperator))
179
+ seperator = options[:seperator]
180
+ end
181
+
182
+ menu = SpudMenu.where(:name => options[:name])
183
+ if Spud::Core.multisite_mode_enabled
184
+ site_config = Spud::Core.site_config_for_host(request.host_with_port)
185
+ menu = menu.site(site_config[:site_id]) if !site_config.blank?
186
+ end
187
+ menu = menu.first
188
+ if(menu.blank?)
189
+ return ""
190
+ end
191
+ menu_items = menu.spud_menu_items_combined.select("
192
+ #{SpudMenuItem.table_name}.id as id,
193
+ #{SpudMenuItem.table_name}.url as url,
194
+ #{SpudMenuItem.table_name}.classes as classes,
195
+ #{SpudMenuItem.table_name}.parent_type as parent_type,
196
+ #{SpudMenuItem.table_name}.menu_order as menu_order,
197
+ #{SpudMenuItem.table_name}.parent_id as parent_id,
198
+ #{SpudMenuItem.table_name}.name as name,
199
+ #{SpudPage.table_name}.url_name as url_name").order(:parent_type,:parent_id).joins("LEFT JOIN #{SpudPage.table_name} ON (#{SpudPage.table_name}.id = #{SpudMenuItem.table_name}.spud_page_id)").all
200
+
201
+ menu_tags = []
202
+ menu_items.sort_by{|p| p.menu_order}.each do |item|
203
+ menu_tags += ["<a #{"class='#{item.classes}' " if !item.classes.blank?}href='#{!item.url_name.blank? ? (item.url_name == Spud::Cms.root_page_name ? root_path() : page_path(:id => item.url_name)) : item.url}'>#{item.name}</a>"]
204
+ end
205
+
206
+ return menu_tags.join(seperator).html_safe
207
+ end
208
+
209
+ private
210
+ def sp_list_menu_item(items,item_id,depth,max_depth, options = {})
211
+ link_options = options.has_key?(:link_options) ? options[:link_options] : {}
212
+ spud_menu_items = items[item_id]
213
+ if spud_menu_items == nil
214
+ return ""
215
+ end
216
+ content = "<ul>"
217
+
218
+ spud_menu_items.sort_by{|p| p.menu_order}.each do |item|
219
+ active = false
220
+ if !item.url_name.blank?
221
+ if current_page?(page_path(:id => item.url_name))
222
+ active = true
223
+ elsif item.url_name == Spud::Cms.root_page_name && current_page?(root_path)
224
+ active = true
225
+ end
226
+ elsif current_page?(item.url)
227
+ active = true
228
+ end
229
+ link_tag = link_to item.name, !item.url_name.blank? ? (item.url_name == Spud::Cms.root_page_name ? root_path() : page_path(:id => item.url_name)) : item.url, {:class => "#{'menu-active' if active} #{item.classes if !item.classes.blank?}"}.merge(link_options)
230
+ content += "<li class='#{"menu-active" if active} #{item.classes if !item.classes.blank?}'>#{link_tag}"
231
+ if max_depth == 0 || max_depth > depth
232
+ content += sp_list_menu_item(items,item.id,depth+1,max_depth)
233
+ end
234
+ content += "</li>"
235
+ end
236
+ content += "</ul>"
237
+ return content.html_safe
238
+ end
239
+
240
+ def sp_list_page(page,collection,depth,max_depth,options = {})
241
+ active_class = 'menu-active'
242
+ if options.has_key?(:active_class)
243
+ active_class = options[:active_class]
244
+ end
245
+ if collection[page.id].blank?
246
+ return ""
247
+ end
248
+ content = "<ul>"
249
+ collection[page.id].sort_by{|p| p.page_order}.each do |page|
250
+ active = false
251
+ if !page.url_name.blank?
252
+ if current_page?(page_path(:id => page.url_name))
253
+ active = true
254
+ elsif page.url_name == Spud::Cms.root_page_name && current_page?(root_path)
255
+ active = true
256
+ end
257
+ end
258
+
259
+ content += "<li class='#{active_class if active}'><a href='#{page_path(:id => page.url_name)}'>#{page.name}</a>"
260
+ if max_depth == 0 || max_depth > depth
261
+ content += sp_list_page(page,collection,depth+1,max_depth,options)
262
+ end
263
+ content += "</li>"
264
+ end
265
+ content += "</ul>"
266
+ return content.html_safe
267
+ end
268
+
269
+
270
+ def layout_options
271
+ layouts = Spud::Cms::Engine.template_parser.layouts(Spud::Core.site_config_for_id(session[:admin_site] || 0)[:short_name])
272
+
273
+ layouts = Spud::Cms::Engine.template_parser.layouts
274
+ layout_options = []
275
+ layouts.each_pair do |key,value|
276
+ layout_options << [value[:template_name],key]
277
+ end
278
+ return layout_options
279
+ end
280
+
281
+
282
+
283
+ end
@@ -0,0 +1,9 @@
1
+ class SpudMenu < ActiveRecord::Base
2
+ validates :name,:presence => true
3
+ validates_uniqueness_of :name, :scope => [:site_id]
4
+ has_many :spud_menu_items,:as => :parent,:dependent => :destroy
5
+ has_many :spud_menu_items_combined,:class_name => "SpudMenuItem",:foreign_key => :spud_menu_id,:dependent => :destroy
6
+
7
+ attr_protected :site_id
8
+ scope :site, lambda {|sid| where(:site_id => sid)}
9
+ end
@@ -0,0 +1,72 @@
1
+ class SpudMenuItem < ActiveRecord::Base
2
+ belongs_to :parent, :polymorphic=>true
3
+ belongs_to :spud_menu
4
+ belongs_to :spud_page
5
+ has_many :spud_menu_items,:as => :parent,:dependent => :destroy
6
+
7
+ validates :name,:presence => true
8
+ validates :spud_menu_id,:presence => true
9
+ validates :parent_type,:presence => true
10
+ validates :parent_id,:presence => true
11
+
12
+
13
+ attr_accessible :name,:parent_type,:parent_id,:item_type,:spud_page_id,:menu_order,:url,:classes
14
+
15
+ def get_url
16
+ if !self.spud_page.blank?
17
+ return self.spud_page.url_name
18
+ else
19
+ return url
20
+ end
21
+ end
22
+
23
+ def options_tree(options,depth,current = nil)
24
+ sub_items = self.spud_menu_items
25
+ sub_items = sub_items.where(["id != ?",current.id]) if !current.blank? && !current.id.blank?
26
+ if(sub_items.blank?)
27
+ return options
28
+ end
29
+ sub_items.each do |item|
30
+ options << ["#{'-'*depth} #{item.name}",item.id]
31
+ options = item.options_tree(options,depth+1,current)
32
+ end
33
+ return options
34
+ end
35
+ def self.grouped(menu)
36
+ return menu.spud_menu_items_combined.group_by(&:parent_type)
37
+ end
38
+
39
+ # Returns an array of pages in order of heirarchy
40
+ # :fitler Filters out a page by ID, and all of its children
41
+ # :value Pick an attribute to be used in the value field, defaults to ID
42
+ def self.options_tree_for_item(menu,config={})
43
+ collection = config[:collection] || self.grouped(menu)
44
+ level = config[:level] || 0
45
+ parent_id = config[:parent_id] || nil
46
+ parent_type = config[:parent_type] || 'SpudMenu'
47
+ filter = config[:filter] || nil
48
+ value = config[:value] || :id
49
+ list = []
50
+ if parent_type == 'SpudMenu' && collection[parent_type]
51
+ item_collection = collection['SpudMenuItem'].group_by(&:parent_id) if collection['SpudMenuItem']
52
+ collection[parent_type].each do |c|
53
+ if filter.blank? || c.id != filter.id
54
+ list << [level.times.collect{ '- ' }.join('') + c.name, c[value]]
55
+ list += self.options_tree_for_item(menu,{:collection => item_collection, :parent_id => c.id, :level => level+1, :filter => filter,:parent_type => "SpudMenuItem"})
56
+ end
57
+ end
58
+ else
59
+ if collection[parent_id]
60
+ collection[parent_id].each do |c|
61
+ if filter.blank? || c.id != filter.id
62
+ list << [level.times.collect{ '- ' }.join('') + c.name, c[value]]
63
+ list += self.options_tree_for_item(menu,{:collection => collection, :parent_id => c.id, :level => level+1, :filter => filter,:parent_type => "SpudMenuItem"})
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ return list
70
+ end
71
+
72
+ end