Class: Menus::Controller::MenuItems

Inherits:
Zen::Controller::AdminController show all
Defined in:
lib/zen/package/menus/lib/menus/controller/menu_items.rb

Overview

The MenuItems controller allows users to manage menu items of a menu group. In order to manage menu items you must first navigate to a menu group and click the link "Manage menu items" (see Menus for more information). Once you've reached this page you'll see an overview that looks like the image below.

Menu Items

Editing or creating a menu item can be done by either clicking the name of a menu item or by clicking the "Add menu item" button. In both cases you'll end up with a form looking like the one in the following image:

Edit Menu Item

In this form you can specify the following fields:

  • Name: the name of the menu item.
  • URL: the URL of the menu item.
  • Order: a number that indicates the sort order when the menu is built.
  • Parent: a parent menu item. This allows you to easily create sub menus.
  • HTML class: a space separated list of classes to apply to the HTML element.
  • HTML ID: a single ID to apply to the HTML element.

Note that the name, URL, HTML class and HTML ID fields have a maximum length of 255 characters.

Used Permissions

  • show_menu_item
  • new_menu_item
  • edit_menu_item
  • delete_menu_item

Events

All events in this controller receive an instance of Model::MenuItem. Just like other packages the event delete_menu_item receives an instance that has already been destroyed.

Examples:

Automatically prefix URLs with http

Zen::Event.listen(:new_menu_item) do |item|
  unless item.url =~ /^http/
    item.url = 'http://' + item.url
    item.save
  end
end

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete

Delete all specified menu items based on the values in the POST array "menu_item_ids".

Since:

  • 0.2a



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 244

def delete
  authorize_user!(:delete_menu_item)

  post = request.subset(:menu_item_ids)

  if !post['menu_item_ids'] or post['menu_item_ids'].empty?
    message(:error, lang('menu_items.errors.no_delete'))
    redirect_referrer
  end

  post['menu_item_ids'].each do |id|
    menu = ::Menus::Model::MenuItem[id]

    next if menu.nil?
    Zen::Event.call(:before_delete_menu_item, menu)

    begin
      menu.destroy
    rescue => e
      Ramaze::Log.error(e.inspect)
      message(:error, lang('menu_items.errors.delete') % id)

      redirect_referrer
    end

    Zen::Event.call(:after_delete_menu_item, menu)
  end

  message(:success, lang('menu_items.success.delete'))
  redirect_referrer
end

- (Object) edit(menu_id, id)

Allow the user to edit an existing navigation item.

Parameters:

  • menu_id (Fixnum)

    The ID of the current navigation menu.

  • id (Fixnum)

    The ID of the menu item to edit.

Since:

  • 0.2a



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 139

def edit(menu_id, id)
  authorize_user!(:edit_menu_item)

  validate_menu(menu_id)

  set_breadcrumbs(
    Menus.a(lang('menus.titles.index'), :index),
    MenuItems.a(lang('menu_items.titles.index'), :index, menu_id),
    lang('menu_items.titles.edit')
  )

  @menu_id = menu_id

  if flash[:form_data]
    @menu_item = flash[:form_data]
  else
    @menu_item = validate_menu_item(id, menu_id)
  end

  render_view(:form)
end

- (Object) index(menu_id)

Shows an overview of all the menu items for a menu group.

Parameters:

  • menu_id (Fixnum)

    The ID of the current navigation menu.

Since:

  • 0.2a



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 78

def index(menu_id)
  authorize_user!(:show_menu_item)

  menu = validate_menu(menu_id)

  set_breadcrumbs(
    Menus.a(lang('menus.titles.index'), :index),
    lang('menu_items.titles.index')
  )

  @menu_id    = menu_id
  @menu_items = search do |query|
    ::Menus::Model::MenuItem.search(query) \
      .filter(:menu_id => menu_id) \
      .order(:id.asc)
  end

  @menu_items ||= ::Menus::Model::MenuItem \
    .filter(:menu_id => menu_id) \
    .order(:id.asc)

  @menu_items = paginate(@menu_items)
end

- (Object) new(menu_id)

Allow the user to create a new menu item for the current menu.

Parameters:

  • menu_id (Fixnum)

    The ID of the current navigation menu.

Since:

  • 0.2a



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 109

def new(menu_id)
  authorize_user!(:new_menu_item)

  validate_menu(menu_id)

  set_breadcrumbs(
    Menus.a(lang('menus.titles.index'), :index),
    MenuItems.a(lang('menu_items.titles.index'), :index, menu_id),
    lang('menu_items.titles.new')
  )

  @menu_id = menu_id

  if flash[:form_data]
    @menu_item = flash[:form_data]
  else
    @menu_item = ::Menus::Model::MenuItem.new
  end

  render_view(:form)
end

- (Object) save

Saves an existing menu iten or creates a new one using the supplied POST data.

Since:

  • 0.2a



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 173

def save
  post = request.subset(
    :id,
    :parent_id,
    :name,
    :url,
    :sort_order,
    :html_class,
    :html_id,
    :menu_id
  )

  if post['parent_id'].empty? or post['parent_id'] == post['id']
    post['parent_id'] = nil
  end

  # Determine if we're saving changes made to an existing menu item or
  # if we're going to create a new one.
  if post.key?('id') and !post['id'].empty?
    authorize_user!(:edit_menu_item)

    menu_item    = validate_menu_item(post['id'], post['menu_id'])
    save_action  = :save
    before_event = :before_edit_menu_item
    after_event  = :after_edit_menu_item
  else
    authorize_user!(:new_menu_item)

    menu_item    = ::Menus::Model::MenuItem.new
    save_action  = :new
    before_event = :before_new_menu_item
    after_event  = :after_new_menu_item
  end

  post.delete('id')

  # Set our notifications
  success = lang("menu_items.success.#{save_action}")
  error   = lang("menu_items.errors.#{save_action}")

  # Time to save the data
  begin
    post.each { |k, v| menu_item.send("#{k}=", v) }
    Zen::Event.call(before_event, menu_item)

    menu_item.save
  rescue => e
    Ramaze::Log.error(e.inspect)
    message(:error, error)

    flash[:form_data]   = menu_item
    flash[:form_errors] = menu_item.errors

    redirect_referrer
  end

  Zen::Event.call(after_event, menu_item)

  message(:success, success)
  redirect(MenuItems.r(:edit, menu_item.menu_id, menu_item.id))
end