Class: Sections::Controller::Sections

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

Overview

Sections are data containers with a specific purpose. For example, you might have a "Blog" or "Pages" section each with it's own entries, categories, custom fields and so on. A section and it's entries glue all the other data types (such as those mentioned earlier) together to form the content displayed on your website.

Sections can be managed by going to /admin. This page will show an overview of all existing sections as well as a few buttons and links that allow you to edit, create or delete sections as well as managing the entries for each existing section.

Sections

Creating/Editing Sections

Creating a new section can be done by clicking the button "Add section" while editing a section can be done by clicking the name of a section. In both cases you'll end up with a form that looks like the one in the images below.

General Comments Groups

In this form you can specify the following fields:

  • Name (required): the name of the section.
  • Slug: a URL friendly version of the section name. If no slug is specified one will be generated automatically.
  • Description: a description of the section to help clarify it's purpose.
  • Allow comments (required): whether or not users can submit comments for entries assigned to the section.
  • Comments require an account (required): when set to "Yes" a user has to be logged in in order to post a comment.
  • Moderate comments (required): when enabled a comment first has to be approved before it's displayed. This option is disabled by default.
  • Comment format (required): the format comments are posted in such as Markdown or plain text.
  • Custom field groups: all the custom field groups to assign to the section. These groups can then be used by all the entries in the section.
  • Category groups: all the category groups that should be available to the section entries of this section.

Note that the name and the slug of a section can not be longer than 255 characters.

Used Permissions

  • show_section
  • new_section
  • edit_section
  • delete_section

Events

All events in this controller receive an instance of Model::Section. The event after_delete_section receives an instance that has already been removed, thus you can't make any changes to it and save those in the database.

Example of creating a dummy section entry:

Zen::Event.listen(:new_section) do |section|
  section.add_section_entry(:title   => 'My Entry', :user_id => user.id)
end

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete

Deletes a number of sections and all the related data. These sections should be specified in the POST array "section_ids[]".

Since:

  • 0.1



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 267

def delete
  authorize_user!(:delete_section)

  if !request.params['section_ids'] \
  or request.params['section_ids'].empty?
    message(:error, lang('sections.errors.no_delete'))
    redirect_referrer
  end

  request.params['section_ids'].each do |id|
    section = ::Sections::Model::Section[id]

    next if section.nil?
    Zen::Event.call(:before_delete_section, section)

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

      redirect_referrer
    end

    Zen::Event.call(:after_delete_section, section)
  end

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

- (Object) edit(id)

Show a form that lets the user edit an existing section.

Parameters:

  • id (Fixnum)

    The ID of the section to edit.

Since:

  • 0.1



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 151

def edit(id)
  authorize_user!(:edit_section)

  set_breadcrumbs(
    Sections.a(lang('sections.titles.index'), :index),
    @page_title
  )

  @section = flash[:form_data] || validate_section(id)

  render_view(:form)
end

- (Object) index

Show an overview of all existing sections. Using this overview a user can manage an existing section, delete it or create a new one.

Since:

  • 0.1



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 131

def index
  authorize_user!(:show_section)

  set_breadcrumbs(lang('sections.titles.index'))

  @sections = search do |query|
    ::Sections::Model::Section.search(query).order(:id.asc)
  end

  @sections ||= ::Sections::Model::Section.order(:id.asc)
  @sections   = paginate(@sections)
end

- (Object) new

Show a form that lets the user create a new section.

Since:

  • 0.1



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 170

def new
  authorize_user!(:new_section)

  set_breadcrumbs(
    Sections.a(lang('sections.titles.index'), :index),
    @page_title
  )

  @section = flash[:form_data] || ::Sections::Model::Section.new

  render_view(:form)
end

- (Object) save

Saves any changes made to an existing section or creates a new one.

Since:

  • 0.1



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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/zen/package/sections/lib/sections/controller/sections.rb', line 194

def save
  post = request.subset(
    :id,
    :name,
    :slug,
    :description,
    :comment_allow,
    :comment_require_account,
    :comment_moderate,
    :comment_format,
    :custom_field_group_pks,
    :category_group_pks
  )

  if post['id'] and !post['id'].empty?
    authorize_user!(:edit_section)

    section      = validate_section(post['id'])
    save_action  = :save
    before_event = :before_edit_section
    after_event  = :after_edit_section
  else
    authorize_user!(:new_section)

    section      = ::Sections::Model::Section.new
    save_action  = :new
    before_event = :before_new_section
    after_event  = :after_new_section
  end

  success = lang("sections.success.#{save_action}")
  error   = lang("sections.errors.#{save_action}")

  post['custom_field_group_pks'] ||= []
  post['category_group_pks']     ||= []

  post.delete('id')

  begin
    post.each { |k, v| section.send("#{k}=", v) }
    Zen::Event.call(before_event, section)

    section.save

    if save_action == :new
      section.custom_field_group_pks = post['custom_field_group_pks']
      section.category_group_pks     = post['category_group_pks']
    end
  rescue => e
    Ramaze::Log.error(e.inspect)
    message(:error, error)

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

    redirect_referrer
  end

  Zen::Event.call(after_event, section)

  message(:success, success)
  redirect(Sections.r(:edit, section.id))
end