Class: Caboose::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/posts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_edit, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/caboose/posts_controller.rb', line 219

def admin_add
  return if !user_is_allowed('posts', 'add')
  
  resp = Caboose::StdClass.new({
    'error' => nil,
    'redirect' => nil
  })

  post = Post.new
  post.site_id = @site.id
  post.title = params[:title]                  
  post.published = false
  
  if post.title == nil || post.title.length == 0
    resp.error = 'A title is required.'      
  else
    post.save        
    post.set_slug_and_uri(post.title)                
    resp.redirect = "/admin/posts/#{post.id}"
  end
  
  render :json => resp
end

#admin_add_to_categoryObject



244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/controllers/caboose/posts_controller.rb', line 244

def admin_add_to_category
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  cat_id = params[:post_category_id]
  
  if !PostCategoryMembership.exists?(:post_id => post_id, :post_category_id => cat_id)
    PostCategoryMembership.create(:post_id => post_id, :post_category_id => cat_id)
  end
  
  render :json => true      
end

#admin_deleteObject



272
273
274
275
276
277
278
279
280
# File 'app/controllers/caboose/posts_controller.rb', line 272

def admin_delete
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  PostCategoryMembership.where(:post_id => post_id).destroy_all
  Post.where(:id => post_id).destroy_all
  
  render :json => { 'redirect' => '/admin/posts' }      
end

#admin_delete_formObject



142
143
144
145
146
# File 'app/controllers/caboose/posts_controller.rb', line 142

def admin_delete_form
  return if !user_is_allowed('posts', 'delete')
  @post = Post.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_categoriesObject



123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/caboose/posts_controller.rb', line 123

def admin_edit_categories
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  @categories = PostCategory.where(:site_id => @site.id).reorder(:name).all
  if @categories.nil? || @categories.count == 0
    PostCategory.create(:site_id => @site.id, :name => 'General News')
    @categories = PostCategory.where(:site_id => @site.id).reorder(:name).all
  end
  render :layout => 'caboose/admin'
end

#admin_edit_contentObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/caboose/posts_controller.rb', line 96

def admin_edit_content
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  if @post.body
    @post.preview = @post.body
    @post.body = nil
    @post.save
  end
  if @post.block.nil?      
    redirect_to "/admin/posts/#{@post.id}/layout"
    return
  end
  Caboose::Block.where(:post_id => @post.id, :new_sort_order => nil).update_all('new_sort_order = sort_order') if @post && !@post.id.nil?
  Caboose::Block.where(:post_id => @post.id, :status => nil).update_all(:status => 'published') if @post && !@post.id.nil?
  @editing = true
  @preview = false
end

#admin_edit_generalObject



150
151
152
153
154
155
# File 'app/controllers/caboose/posts_controller.rb', line 150

def admin_edit_general
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  @post.verify_custom_field_values_exist
  render :layout => 'caboose/admin'
end

#admin_edit_layoutObject



135
136
137
138
139
# File 'app/controllers/caboose/posts_controller.rb', line 135

def admin_edit_layout
  return unless user_is_allowed('posts', 'edit')      
  @post = Post.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_previewObject



73
74
75
76
77
# File 'app/controllers/caboose/posts_controller.rb', line 73

def admin_edit_preview
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])      
  render :layout => 'caboose/admin'
end

#admin_indexObject



39
40
41
42
# File 'app/controllers/caboose/posts_controller.rb', line 39

def admin_index
  return if !user_is_allowed('posts', 'view')
  render :layout => 'caboose/admin'           
end

#admin_jsonObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/caboose/posts_controller.rb', line 45

def admin_json
  return if !user_is_allowed('posts', 'view')
    
  pager = PageBarGenerator.new(params, {
      'site_id'     => @site.id,
      'title_like'  => '',
  },{
      'model'       => 'Caboose::Post',
      'sort'        => 'created_at',
      'desc'        => true,
      'base_url'    => '/admin/posts',
      'items_per_page' => 50,
      'use_url_params' => false
  })
  render :json => {
    :pager => pager,
    :models => pager.items.as_json()        
  }
end

#admin_json_singleObject



66
67
68
69
70
# File 'app/controllers/caboose/posts_controller.rb', line 66

def admin_json_single
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  render :json => @post
end

#admin_newObject



212
213
214
215
216
# File 'app/controllers/caboose/posts_controller.rb', line 212

def admin_new
  return if !user_is_allowed('posts', 'new')  
  @new_post = Post.new  
  render :layout => 'caboose/admin'
end

#admin_preview_postObject



115
116
117
118
119
120
# File 'app/controllers/caboose/posts_controller.rb', line 115

def admin_preview_post
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  @editing = true
  @preview = true
end

#admin_publishObject



80
81
82
83
84
85
# File 'app/controllers/caboose/posts_controller.rb', line 80

def admin_publish
  return unless user_is_allowed('posts', 'edit')
  post = Post.find(params[:id])
  post.publish
  redirect_to "/admin/posts/#{post.id}/content"
end

#admin_remove_from_categoryObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/controllers/caboose/posts_controller.rb', line 258

def admin_remove_from_category
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  cat_id = params[:post_category_id]
  
  if PostCategoryMembership.exists?(:post_id => post_id, :post_category_id => cat_id)
    PostCategoryMembership.where(:post_id => post_id, :post_category_id => cat_id).destroy_all
  end
  
  render :json => true      
end

#admin_revertObject



88
89
90
91
92
93
# File 'app/controllers/caboose/posts_controller.rb', line 88

def admin_revert
  return unless user_is_allowed('posts', 'edit')
  post = Post.find(params[:id])
  post.revert
  redirect_to "/admin/posts/#{post.id}/content"
end

#admin_updateObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/caboose/posts_controller.rb', line 170

def admin_update      
  return if !user_is_allowed('posts', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  post = Post.find(params[:id])
  
  save = true
  params.each do |name, value|    
    case name                      
      when 'site_id'    then post.site_id    = value.to_i
      when 'slug'       then post.set_slug_and_uri(value)                        
      when 'title'      then post.title      = value            
      when 'subtitle'   then post.subtitle   = value
      when 'author'     then post.author     = value
      when 'body'       then post.body       = value
      when 'preview'    then post.preview    = value
      when 'hide'       then post.hide       = value
      when 'image_url'  then post.image_url  = value
      when 'published'  then post.published  = value
      when 'created_at' then post.created_at = DateTime.strptime(value,'%m/%d/%Y')
      when 'updated_at' then post.updated_at = DateTime.parse(value)
    end
  end
  resp.success = save && post.save      
  render :json => resp
end

#admin_update_imageObject



198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/caboose/posts_controller.rb', line 198

def admin_update_image
  return if !user_is_allowed('posts', 'edit')
             
  resp = Caboose::StdClass.new
  post = Post.find(params[:id])
  post.image = params[:image]            
  resp.success = post.save
  resp.attributes = { 'image' => { 'value' => post.image.url(:thumb) }}
  
  render :text => resp.to_json
end

#admin_update_layoutObject



158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/caboose/posts_controller.rb', line 158

def admin_update_layout
  return unless user_is_allowed('posts', 'edit')      
  bt = BlockType.find(params[:block_type_id])
  Block.where(:post_id => params[:id]).destroy_all
  Block.create(:post_id => params[:id], :block_type_id => params[:block_type_id], :name => bt.name)
  resp = Caboose::StdClass.new({
    'redirect' => "/admin/posts/#{params[:id]}/content"
  })
  render :json => resp
end

#showObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/caboose/posts_controller.rb', line 13

def show
  
  # Make sure we're not under construction or on a forwarded domain
  return if under_construction_or_forwarding_domain?
  
  if params[:id]
    @post = Post.where(:id => params[:id]).first
 #   render :layout => "caboose/application"
  else      
    # Find the page with an exact URI match        
    uri = "/posts/#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}"
    @post = Post.where(:site_id => @site.id, :uri => uri).first
  #  render :layout => "caboose/application"
  end      
  render :file => "caboose/extras/error404" and return if @post.nil?                 

  @post = Caboose.plugin_hook('post_content', @post)
  @editmode = !params['edit'].nil? && user.is_allowed('posts', 'edit') ? true : false  
end