Class: CamaleonCms::Admin::PostsController

Inherits:
CamaleonCms::AdminController
  • Object
show all
Defined in:
app/controllers/camaleon_cms/admin/posts_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

#ajaxObject

ajax options


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 152

def ajax
  json = {error: 'Not Found'}
  case params[:method]
    when 'exist_slug'
      slug_orig = params[:slug].to_s
      slug = slug_orig
      post_id = params[:post_id].present? ? params[:post_id] : 0
      i = 0
      while _exist_slug?(slug, post_id)  do
        i +=1
        slug = "#{slug_orig}-#{i}"
      end
      json = {slug: slug, index: i}
  end
  render json: json
end

#createObject


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 70

def create
  authorize! :create_post, @post_type
  post_data = get_post_data(true)
  CamaleonCms::Post.drafts.find(post_data[:draft_id]).destroy rescue nil
  @post = @post_type.posts.create(post_data)
  r = {post: @post, post_type: @post_type}; hooks_run("create_post", r)
  @post = r[:post]
  if @post.valid?
    @post.set_meta_from_form(params[:meta])
    @post.set_field_values(params[:field_options])
    @post.set_option("keywords", post_data[:keywords])
    flash[:notice] = t('camaleon_cms.admin.post.message.created', post_type: @post_type.decorate.the_title)
    r = {post: @post, post_type: @post_type}; hooks_run("created_post", r)
    redirect_to action: :edit, id: @post.id
  else
    # render 'form'
    new
  end
end

#destroyObject


137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 137

def destroy
  authorize! :destroy, @post
  r = {post: @post, post_type: @post_type, flag: true}
  hooks_run("destroy_post", r)
  if r[:flag]
    @post.destroy
    hooks_run("destroy_post", {post: @post, post_type: @post_type})
    flash[:notice] = t('camaleon_cms.admin.post.message.deleted', post_type: @post_type.decorate.the_title)
  else
    # flash[:error] = t('camaleon_cms.admin.post.message.deleted')
  end
  redirect_to action: :index, s: params[:s]
end

#editObject


90
91
92
93
94
95
96
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 90

def edit
  add_breadcrumb I18n.t("camaleon_cms.admin.button.edit")
  authorize! :update, @post
  @post_form_extra_settings = []
  r = {post: @post, post_type: @post_type, extra_settings: @post_form_extra_settings, render: "form"}; hooks_run("edit_post", r)
  render r[:render]
end

#indexObject


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 16

def index
  authorize! :posts, @post_type
  posts_all = @post_type.posts.eager_load(:parent, :post_type)
  if params[:taxonomy].present? && params[:taxonomy_id].present?
    if params[:taxonomy] == "category"
      cat_owner = current_site.full_categories.find(params[:taxonomy_id]).decorate
      posts_all = cat_owner.posts
      add_breadcrumb t("camaleon_cms.admin.post_type.category"), @post_type.the_admin_url("category")
      add_breadcrumb cat_owner.the_title, cat_owner.the_edit_url
    end

    if params[:taxonomy] == "post_tag"
      tag_owner = current_site..find(params[:taxonomy_id]).decorate
      posts_all = tag_owner.posts
      add_breadcrumb t("camaleon_cms.admin.post_type.tags"), @post_type.the_admin_url("tag")
      add_breadcrumb tag_owner.the_title, tag_owner.the_edit_url
    end
  end

  if params[:q].present?
    posts_all = posts_all.where(params[:q].split(" ").map{|text| "#{CamaleonCms::Post.table_name}.title LIKE '%#{text}%'" }.join(" OR "))
  end

  @posts = posts_all
  params[:s] = 'published' unless params[:s].present?
  @lists_tab = params[:s]
  add_breadcrumb I18n.t("camaleon_cms.admin.post_type.#{params[:s]}") if params[:s].present?
  case params[:s]
    when "published", "pending", "draft", "trash"
      @posts = @posts.where(status:  params[:s])

    when "all"
      @posts = @posts.no_trash
  end

  @btns = {published: "#{t('camaleon_cms.admin.post_type.published')} (#{posts_all.where(status: "published").size})", all: "#{t('camaleon_cms.admin.post_type.all')} (#{posts_all.no_trash.size})", pending: "#{t('camaleon_cms.admin.post_type.pending')} (#{posts_all.where(status: "pending").size})", draft: "#{t('camaleon_cms.admin.post_type.draft')} (#{posts_all.where(status: "draft").size})", trash: "#{t('camaleon_cms.admin.post_type.trash')} (#{posts_all.where(status: "trash").size})"}
  r = {posts: @posts, post_type: @post_type, btns: @btns, all_posts: posts_all, render: 'index' }
  hooks_run("list_post", r)
  @posts = r[:posts].paginate(:page => params[:page], :per_page => current_site.admin_per_page)
  render r[:render]
end

#newObject


61
62
63
64
65
66
67
68
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 61

def new
  add_breadcrumb I18n.t("camaleon_cms.admin.button.new")
  authorize! :create_post, @post_type
  @post_form_extra_settings = []
  @post ||= @post_type.posts.new
  r = {post: @post, post_type: @post_type, extra_settings: @post_form_extra_settings, render: "form"}; hooks_run("new_post", r)
  render r[:render]
end

#restoreObject


128
129
130
131
132
133
134
135
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 128

def restore
  @post = @post_type.posts.find(params[:post_id])
  authorize! :update, @post
  @post.update_column('status', @post.options[:status_default] || 'pending')
  @post.update_extra_data
  flash[:notice] = t('camaleon_cms.admin.post.message.restore', post_type: @post_type.decorate.the_title)
  redirect_to action: :index, s: params[:s]
end

#showObject


58
59
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 58

def show
end

#trashObject


117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 117

def trash
  @post = @post_type.posts.find(params[:post_id])
  authorize! :destroy, @post
  @post.set_option('status_default', @post.status)
  @post.children.destroy_all unless @post.draft?
  @post.update_column('status', 'trash')
  @post.update_extra_data
  flash[:notice] = t('camaleon_cms.admin.post.message.trash', post_type: @post_type.decorate.the_title)
  redirect_to action: :index, s: params[:s]
end

#updateObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/camaleon_cms/admin/posts_controller.rb', line 98

def update
  @post = @post.parent if @post.draft? && @post.parent.present?
  authorize! :update, @post
  @post.drafts.destroy_all
  post_data = get_post_data
  r = {post: @post, post_type: @post_type}; hooks_run("update_post", r)
  @post = r[:post]
  if @post.update(post_data)
    @post.set_meta_from_form(params[:meta])
    @post.set_field_values(params[:field_options])
    @post.set_option("keywords", post_data[:keywords])
    hooks_run("updated_post", {post: @post, post_type: @post_type})
    flash[:notice] = t('camaleon_cms.admin.post.message.updated', post_type: @post_type.decorate.the_title)
    redirect_to action: :edit, id: @post.id
  else
    edit
  end
end