diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 36716c5f8d..d25a603fd6 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -57,9 +57,9 @@ def info def new @page ||= Page.new(layoutpage: params[:layoutpage] == 'true', parent_id: params[:parent_id]) - @page_layouts = PageLayout.layouts_for_select(@current_language.id, @page.layoutpage?) + @page_layouts = PageLayout.layouts_for_select(@current_language.id, @page.layoutpage?, @page.parent.page_layout) @clipboard = get_clipboard('pages') - @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, @page.layoutpage?) + @clipboard_items = Page.all_from_clipboard_for_select(@clipboard, @current_language.id, @page.layoutpage?, @page.parent.page_layout) end def create diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index f4d6b051d9..b4495f8ca7 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -254,11 +254,11 @@ def all_from_clipboard(clipboard) where(id: clipboard.collect { |p| p['id'] }) end - def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false) + def all_from_clipboard_for_select(clipboard, language_id, layoutpage = false, page_layout_name = nil) return [] if clipboard.blank? clipboard_pages = all_from_clipboard(clipboard) - allowed_page_layouts = Alchemy::PageLayout.selectable_layouts(language_id, layoutpage) + allowed_page_layouts = Alchemy::PageLayout.selectable_layouts(language_id, layoutpage, page_layout_name) allowed_page_layout_names = allowed_page_layouts.collect { |p| p['name'] } clipboard_pages.select { |cp| allowed_page_layout_names.include?(cp.page_layout) } end diff --git a/app/serializers/alchemy/page_tree_serializer.rb b/app/serializers/alchemy/page_tree_serializer.rb index f6a36b6d29..d3a95ad378 100644 --- a/app/serializers/alchemy/page_tree_serializer.rb +++ b/app/serializers/alchemy/page_tree_serializer.rb @@ -56,6 +56,7 @@ def page_hash(page, has_children, level, folded) visible: page.visible?, restricted: page.restricted?, page_layout: page.page_layout, + allowing_sub_pages: page.definition['sub_pages'] != false, slug: page.slug, urlname: page.urlname, level: level, diff --git a/app/views/alchemy/admin/pages/_new_page_form.html.erb b/app/views/alchemy/admin/pages/_new_page_form.html.erb index b44f5ed276..aac0b0fc49 100644 --- a/app/views/alchemy/admin/pages/_new_page_form.html.erb +++ b/app/views/alchemy/admin/pages/_new_page_form.html.erb @@ -4,8 +4,9 @@ <%= f.input :page_layout, collection: @page_layouts, label: Alchemy.t(:page_type), - include_blank: Alchemy.t('Please choose'), + include_blank: @page_layouts.length == 1 ? nil : Alchemy.t('Please choose'), required: true, + selected: @page_layouts.length == 1 ? @page_layouts.first : nil, input_html: {class: 'alchemy_selectbox'} %> <%= f.input :name %> <%= f.submit Alchemy.t(:create) %> diff --git a/app/views/alchemy/admin/pages/_page.html.erb b/app/views/alchemy/admin/pages/_page.html.erb index 9ddda23f77..12ee441d63 100644 --- a/app/views/alchemy/admin/pages/_page.html.erb +++ b/app/views/alchemy/admin/pages/_page.html.erb @@ -124,17 +124,21 @@ {{/if}} {{#if permissions.create}} -
- <%= link_to_dialog( - render_icon(:plus), - alchemy.new_admin_page_path(parent_id: '__ID__').gsub('__ID__', '{{id}}'), - { - title: Alchemy.t(:create_page), - size: '340x165', - overflow: true - } - ) -%> - + {{#if allowing_sub_pages}} +
+ <%= link_to_dialog( + render_icon(:plus), + alchemy.new_admin_page_path(parent_id: '__ID__').gsub('__ID__', '{{id}}'), + { + title: Alchemy.t(:create_page), + size: '340x165', + overflow: true + } + ) -%> + + {{else}} +
+ {{/if}} {{else}}
<%= render_icon(:plus) %> diff --git a/lib/alchemy/page_layout.rb b/lib/alchemy/page_layout.rb index fec48f918c..c5b21f19f3 100644 --- a/lib/alchemy/page_layout.rb +++ b/lib/alchemy/page_layout.rb @@ -58,15 +58,15 @@ def get_all_by_attributes(attributes) # Returns page layouts ready for Rails' select form helper. # - def layouts_for_select(language_id, only_layoutpages = false) + def layouts_for_select(language_id, only_layoutpages = false, page_layout_name = nil) @map_array = [] - mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages)) + mapped_layouts_for_select(selectable_layouts(language_id, only_layoutpages, page_layout_name)) end # Returns page layouts including given layout ready for Rails' select form helper. # def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages = false) - layouts = selectable_layouts(language_id, only_layoutpages) + layouts = selectable_layouts(language_id, only_layoutpages, page_layout_name) if layouts.detect { |l| l['name'] == page_layout_name }.nil? @map_array = [[human_layout_name(page_layout_name), page_layout_name]] else @@ -83,9 +83,14 @@ def layouts_with_own_for_select(page_layout_name, language_id, only_layoutpages # language_id of current used Language. # @param [Boolean] (false) # Pass true to only select layouts for global/layout pages. + # @param [String] + # name of current/parent page layout # - def selectable_layouts(language_id, only_layoutpages = false) + def selectable_layouts(language_id, only_layoutpages = false, page_layout_name = nil) @language_id = language_id + @page_layout = page_layout_name.present? && all.detect{ |layout| layout['name'] == page_layout_name } + return [] if @page_layout && @page_layout['sub_pages'] == false + all.select do |layout| if only_layoutpages layout['layoutpage'] && layout_available?(layout) @@ -127,7 +132,13 @@ def human_layout_name(layout) # Returns true if the given layout is unique and not already taken or it should be hidden. # def layout_available?(layout) - !layout['hide'] && !already_taken?(layout) && available_on_site?(layout) + allowed_in_definition?(layout) && !already_taken?(layout) && available_on_site?(layout) + end + + def allowed_in_definition?(layout) + !layout['hide'] && layout['parent_pages'] != false && @page_layout['sub_pages'] != false && + (@page_layout['sub_pages'].blank? || @page_layout['sub_pages'].include?(layout['name'])) && + (layout['parent_pages'].blank? || layout['parent_pages'].include?(@page_layout['name'])) end # Returns true if this layout is unique and already taken by another page.