site-menu-widget/app/controllers/site_menu_widgets_controlle...

63 lines
1.6 KiB
Ruby

class SiteMenuWidgetsController < ApplicationController
def widget
params = OrbitHelper.params
page = Page.where(:page_id => params[:page_id]).first
if page.parent_page == Page.root
pages = page.sorted_published_child_pages
else
pages = page.parent_page.sorted_published_child_pages
end
def create_json(pages)
items = []
widget_title = []
pages.each do |page|
item = {}
if page.child_page.size > 0
if page.page_type == "page" or page.page_type == "link"
item["page_name"] = page.name
item["url"] = (page.page_type == "link") ? "/#{locale.to_s}" + page.external_url : "/#{locale.to_s}" + page.url
item["children"] = create_json(page.sorted_published_child_pages)
item["target"] = "_self"
else
item["page_name"] = page.name
item["url"] = "/#{locale.to_s}" + page.url
item["children"] = create_json(page.sorted_published_child_pages)
item["target"] = "_blank"
end
else
if page.page_type == "page"
item["page_name"] = page.name
item["url"] = "/#{locale.to_s}" + page.url
item["target"] = "_self"
item["children"] = []
else
item["page_name"] = page.name
item["url"] = page.external_url
item["target"] = "_blank"
item["children"] = []
end
end
items << item
end
items
end
page_part_id = PagePart.where(:page_id => pages.first.parent_page_id).last.id
widgets = SubPart.where(:page_part_id => page_part_id).collect do |sp|
{
"widget_title" => sp.title
}
end
items = create_json(pages)
{
"pages" => items,
"widget_title" => widgets
}
end
end