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



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/controllers/caboose/posts_controller.rb', line 266

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.blank?
    resp.error = 'A title is required.'      
  else
    post.save        
    post.set_slug_and_uri(post.title)
    bt = BlockType.where(:id => @site.default_layout_id).first
    Block.create(:post_id => post.id, :block_type_id => bt.id, :name => bt.name) if post && bt
    resp.redirect = "/admin/posts/#{post.id}"
    Caboose::ChangeLog.create(:site_id => @site.id, :description => post.title, :user_id => logged_in_user.id, :post_id => post.id, :timestamp => DateTime.now, :action => 'created') if @site.use_change_logs
  end
  render :json => resp
end

#admin_add_to_categoryObject



290
291
292
293
294
295
296
297
298
299
300
# File 'app/controllers/caboose/posts_controller.rb', line 290

def admin_add_to_category
  return if !user_is_allowed('posts', 'edit')
  post = get_edit_post(params[:id], @site.id)
  cat_id = params[:post_category_id]
  cat = PostCategory.where(:id => cat_id).first
  if post && !PostCategoryMembership.exists?(:post_id => post.id, :post_category_id => cat_id)
    PostCategoryMembership.create(:post_id => post.id, :post_category_id => cat_id)
    Caboose::ChangeLog.create(:site_id => @site.id, :description => "Post Category", :user_id => logged_in_user.id, :post_id => post.id, :timestamp => DateTime.now, :action => "added", :new_value => (cat ? cat.name : nil)) if @site.use_change_logs
  end
  render :json => true      
end

#admin_change_logsObject



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

def admin_change_logs
  return unless user_is_allowed('logs', 'view')
  @post = Post.find(params[:id])  
  if @post.site_id != @logged_in_user.site_id && !@logged_in_user.is_super_admin?
    redirect_to '/admin/posts'
  else
    @logs = ChangeLog.where(:post_id => @post.id).where('(is_field is false and (action = ? OR action = ?)) OR (action != ? and action != ?)','added','deleted','added','deleted').order('timestamp desc, id desc').limit(500)
    render :layout => 'caboose/admin'
  end
end

#admin_deleteObject



316
317
318
319
320
321
322
323
# File 'app/controllers/caboose/posts_controller.rb', line 316

def admin_delete
  return if !user_is_allowed('posts', 'edit')
  post = get_edit_post(params[:id], @site.id)
  PostCategoryMembership.where(:post_id => post.id).destroy_all if post
  Caboose::ChangeLog.create(:site_id => @site.id, :description => post.title, :user_id => logged_in_user.id, :post_id => post.id, :timestamp => DateTime.now, :action => 'deleted') if post && @site.use_change_logs
  Post.where(:id => post.id).destroy_all if post
  render :json => { 'redirect' => '/admin/posts' }      
end

#admin_delete_formObject



165
166
167
168
169
# File 'app/controllers/caboose/posts_controller.rb', line 165

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

#admin_edit_categoriesObject



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

def admin_edit_categories
  return if !user_is_allowed('posts', 'edit')    
  @post = get_edit_post(params[:id], @site.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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/caboose/posts_controller.rb', line 119

def admin_edit_content
  return if !user_is_allowed('posts', 'edit')    
  @post = get_edit_post(params[:id], @site.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



173
174
175
176
177
178
# File 'app/controllers/caboose/posts_controller.rb', line 173

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

#admin_edit_layoutObject



158
159
160
161
162
# File 'app/controllers/caboose/posts_controller.rb', line 158

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

#admin_edit_previewObject



70
71
72
73
74
# File 'app/controllers/caboose/posts_controller.rb', line 70

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

#admin_indexObject



35
36
37
38
# File 'app/controllers/caboose/posts_controller.rb', line 35

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

#admin_jsonObject



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

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,
      'additional_where' => [ "(site_id = #{@site.id})" ]
  })
  render :json => {
    :pager => pager,
    :models => pager.items.as_json()        
  }
end

#admin_json_singleObject



63
64
65
66
67
# File 'app/controllers/caboose/posts_controller.rb', line 63

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

#admin_newObject



259
260
261
262
263
# File 'app/controllers/caboose/posts_controller.rb', line 259

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

#admin_preview_postObject



138
139
140
141
142
143
# File 'app/controllers/caboose/posts_controller.rb', line 138

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

#admin_publishObject



89
90
91
92
93
94
95
96
97
# File 'app/controllers/caboose/posts_controller.rb', line 89

def admin_publish
  resp = Caboose::StdClass.new
  resp.success = false
  render :json => resp and return unless user_is_allowed('posts', 'edit')
  post = get_edit_post(params[:id], @site.id)
  resp.success = true
  post.delay(:queue => 'caching', :priority => 5).publish(logged_in_user.id)
  render :json => resp
end

#admin_remove_from_categoryObject



303
304
305
306
307
308
309
310
311
312
313
# File 'app/controllers/caboose/posts_controller.rb', line 303

def admin_remove_from_category
  return if !user_is_allowed('posts', 'edit')
  post = get_edit_post(params[:id], @site.id)
  cat_id = params[:post_category_id]
  cat = PostCategory.where(:id => cat_id).first
  if post && PostCategoryMembership.exists?(:post_id => post.id, :post_category_id => cat_id)
    PostCategoryMembership.where(:post_id => post.id, :post_category_id => cat_id).destroy_all
    Caboose::ChangeLog.create(:site_id => @site.id, :description => "Post Category", :user_id => logged_in_user.id, :post_id => post.id, :timestamp => DateTime.now, :action => "deleted", :old_value => (cat ? cat.name : nil)) if @site.use_change_logs
  end
  render :json => true      
end

#admin_remove_imageObject



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

def admin_remove_image
  return unless user_is_allowed("posts", 'edit')
  resp = Caboose::StdClass.new
  user = logged_in_user
  b = Post.find(params[:id])
  Caboose::ChangeLog.create(:site_id => @site.id, :description => "Post Image", :user_id => user.id, :post_id => b.id, :timestamp => DateTime.now, :action => 'edited', :old_value => b.image_file_name, :new_value => 'None') if @site.use_change_logs
  b.image_file_name = nil
  b.image_file_size = nil
  b.image_content_type = nil
  b.image_updated_at = nil
  resp.success = b.save
  render :json => resp    
end

#admin_revertObject



111
112
113
114
115
116
# File 'app/controllers/caboose/posts_controller.rb', line 111

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

#admin_statusObject



100
101
102
103
104
105
106
107
108
# File 'app/controllers/caboose/posts_controller.rb', line 100

def admin_status
  resp = Caboose::StdClass.new
  resp.status = 'pending'
  post = get_edit_post(params[:id], @site.id)
  if post && post.is_published
    resp.status = 'published'
  end
  render :json => resp
end

#admin_updateObject



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
# File 'app/controllers/caboose/posts_controller.rb', line 194

def admin_update      
  return if !user_is_allowed('posts', 'edit')
  resp = Caboose::StdClass.new({'attributes' => {}})
  post = get_edit_post(params[:id], @site.id)
  save = true
  user = logged_in_user
  params.each do |name, value|
    old_value = post[name]
    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
    if @site.use_change_logs && !['id','action','controller'].include?(name)
      ov = old_value
      nv = value
      ov = ['published'].include?(name) ? (ov ? 'yes' : 'no') : ov
      nv = ['published'].include?(name) ? ( ['1',1,true,'true'].include?(nv) ? 'yes' : 'no') : nv
      cl = Caboose::ChangeLog.create(:site_id => @site.id, :description => "Post #{name.titleize}", :user_id => user.id, :post_id => post.id, :timestamp => DateTime.now, :action => 'edited', :old_value => ov, :new_value => nv)
    end
  end
  resp.success = save && post.save      
  render :json => resp
end

#admin_update_imageObject



229
230
231
232
233
234
235
236
237
238
239
240
# File 'app/controllers/caboose/posts_controller.rb', line 229

def admin_update_image
  return if !user_is_allowed('posts', 'edit')
  user = logged_in_user
  resp = Caboose::StdClass.new
  post = get_edit_post(params[:id], @site.id)
  ov = post.image_file_name
  post.image = params[:image]            
  resp.success = post.save
  resp.attributes = { 'image' => { 'value' => post.image.url(:thumb) }}
  Caboose::ChangeLog.create(:site_id => @site.id, :description => "Post Image", :user_id => user.id, :post_id => post.id, :timestamp => DateTime.now, :action => 'edited', :old_value => ov, :new_value => post.image_file_name) if @site.use_change_logs
  render :text => resp.to_json
end

#admin_update_layoutObject



181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/caboose/posts_controller.rb', line 181

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

#showObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/caboose/posts_controller.rb', line 8

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?                 

  @editing = false
  @preview = false
  @post = Caboose.plugin_hook('post_content', @post)
end