Class: CamaleonCms::Admin::Appearances::NavMenusController

Inherits:
CamaleonCms::AdminController
  • Object
show all
Defined in:
app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

Instance Method Details

#add_itemsObject

add items to specific nav-menu



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 101

def add_items
  items = []
  @nav_menu = current_site.nav_menus.find(params[:nav_menu_id])
  if params[:external].present?
    item = @nav_menu.append_menu_item({label: params[:external][:external_label], link: params[:external][:external_url], type: "external"})
    item.set_options(params[:external][:options]) if params[:external][:options].present?
    items << item
  end

  if params[:custom_items].present? # custom menu items
    params[:custom_items].each do |index, item|
      item = @nav_menu.append_menu_item({label: item['label'], link: item['url'], type: 'external'})
      items << item
    end
  end

  if params[:items].present?
    params[:items].each do |index, item|
      item = @nav_menu.append_menu_item({label: 'auto', link: item['id'], type: item['kind']})
      items << item
    end
  end
  render partial: 'menu_items', locals: {items: items, nav_menu: @nav_menu}
end

#createObject



31
32
33
34
35
36
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 31

def create
  nav_menu = current_site.nav_menus.new(params[:nav_menu])
  nav_menu.save
  flash[:notice] = t('.created_menu', default: 'Created Menu')
  redirect_to action: :index, id: nav_menu.id
end

#custom_settingsObject



56
57
58
59
60
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 56

def custom_settings
  @nav_menu = current_site.nav_menus.find(params[:nav_menu_id])
  @nav_menu_item = current_site.nav_menu_items.find(params[:id])
  render "_custom_fields", layout: "camaleon_cms/admin/_ajax"
end

#delete_menu_itemObject



82
83
84
85
86
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 82

def delete_menu_item
  # @nav_menu = current_site.nav_menus.find(params[:nav_menu_id])
  current_site.nav_menu_items.find(params[:id]).destroy
  render nothing: true
end

#destroyObject



50
51
52
53
54
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 50

def destroy
  current_site.nav_menus.find(params[:id]).destroy
  flash[:notice] = t('.deleted_menu', default: 'Menu destroyed')
  redirect_to action: :index
end

#editObject



45
46
47
48
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 45

def edit
  @nav_menu = current_site.nav_menus.find(params[:id])
  render partial: 'form'
end

#edit_menu_itemObject

render edit external menu item



69
70
71
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 69

def edit_menu_item
  render "_external_menu", layout: false, locals: {nav_menu: current_site.nav_menus.find(params[:nav_menu_id]), menu_item: current_site.nav_menu_items.find(params[:id])}
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 13

def index
  if params[:id].present?
    @nav_menu = current_site.nav_menus.find_by_id(params[:id])
  elsif params[:slug].present?
    @nav_menu = current_site.nav_menus.find_by_slug(params[:slug])
  else
    @nav_menu = current_site.nav_menus.first
  end
  @post_types = current_site.post_types
  add_asset_library("nav_menu")
  render "index"
end

#newObject



26
27
28
29
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 26

def new
  @nav_menu = current_site.nav_menus.new
  render partial: 'form'
end

#reorder_items(items = nil, parent_id = nil, is_root = true) ⇒ Object

update the reorder of items



89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 89

def reorder_items(items = nil, parent_id = nil, is_root = true)
  items = params[:items] if items.nil?
  parent_id = params[:nav_menu_id] if parent_id.nil?
  items.each do |index, _item|
    item = current_site.nav_menu_items.find(_item['id'])
    item.update_columns parent_id: parent_id, term_order: index
    reorder_items(_item['children'], _item['id'], false) if _item['children'].present?
  end
  render(inline: '') if is_root
end

#save_custom_settingsObject



62
63
64
65
66
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 62

def save_custom_settings
  @nav_menu_item = current_site.nav_menu_items.find(params[:id])
  @nav_menu_item.set_field_values(params[:field_options])
  render nothing: true
end

#updateObject



38
39
40
41
42
43
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 38

def update
  nav_menu = current_site.nav_menus.find(params[:id])
  nav_menu.update(params[:nav_menu])
  flash[:notice] = t('.updated_menu', default: 'Menu updated')
  redirect_to action: :index, id: nav_menu.id
end

#update_menu_itemObject

update an external menu item



74
75
76
77
78
79
80
# File 'app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb', line 74

def update_menu_item
  @nav_menu = current_site.nav_menus.find(params[:nav_menu_id])
  item = current_site.nav_menu_items.find(params[:id])
  item.update_menu_item({label: params[:external_label], link: params[:external_url]})
  item.set_options(params[:options]) if params[:options].present?
  render partial: 'menu_items', locals: {items: [item], nav_menu: @nav_menu}
end