forked from publify/publify
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcontent_controller.rb
More file actions
251 lines (205 loc) · 7.22 KB
/
content_controller.rb
File metadata and controls
251 lines (205 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
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
require 'base64'
require 'pry'
module Admin; end
class Admin::ContentController < Admin::BaseController
layout "administration", :except => [:show, :autosave]
cache_sweeper :blog_sweeper
def auto_complete_for_article_keywords
@items = Tag.find_with_char params[:article][:keywords].strip
render :inline => "<%= raw auto_complete_result @items, 'name' %>"
end
def index
@search = params[:search] ? params[:search] : {}
@articles = Article.search_with_pagination(@search, {:page => params[:page], :per_page => this_blog.admin_display_elements})
if request.xhr?
render :partial => 'article_list', :locals => { :articles => @articles }
else
@article = Article.new(params[:article])
end
end
def new
new_or_edit
end
def edit
@article = Article.find(params[:id])
unless @article.access_by? current_user
redirect_to :action => 'index'
flash[:error] = _("Error, you are not allowed to perform this action")
return
end
new_or_edit
end
def destroy
@record = Article.find(params[:id])
unless @record.access_by?(current_user)
flash[:error] = _("Error, you are not allowed to perform this action")
return(redirect_to :action => 'index')
end
return(render 'admin/shared/destroy') unless request.post?
@record.destroy
flash[:notice] = _("This article was deleted successfully")
redirect_to :action => 'index'
end
def insert_editor
editor = 'visual'
editor = 'simple' if params[:editor].to_s == 'simple'
current_user.editor = editor
current_user.save!
render :partial => "#{editor}_editor"
end
def category_add; do_add_or_remove_fu; end
alias_method :resource_add, :category_add
alias_method :resource_remove, :category_add
def attachment_box_add
render :update do |page|
page["attachment_add_#{params[:id]}"].remove
page.insert_html :bottom, 'attachments',
:partial => 'admin/content/attachment',
:locals => { :attachment_num => params[:id], :hidden => true }
page.visual_effect(:toggle_appear, "attachment_#{params[:id]}")
end
end
def attachment_save(attachment)
begin
Resource.create(:filename => attachment.original_filename, :mime => attachment.content_type.chomp,
:created_at => Time.now).write_to_disk(attachment)
rescue => e
logger.info(e.message)
nil
end
end
def autosave
id = params[:id]
id = params[:article][:id] if params[:article] && params[:article][:id]
@article = Article.get_or_build_article(id)
@article.text_filter = current_user.text_filter if current_user.simple_editor?
get_fresh_or_existing_draft_for_article
@article.attributes = params[:article]
@article.published = false
set_article_author
save_attachments
set_article_title_for_autosave
@article.state = "draft" unless @article.state == "withdrawn"
if @article.save
render(:update) do |page|
page.replace_html('autosave', hidden_field_tag('article[id]', @article.id))
page.replace_html('preview_link', link_to(_("Preview"), {:controller => '/articles', :action => 'preview', :id => @article.id}, {:target => 'new', :class => 'btn info'}))
page.replace_html('destroy_link', link_to_destroy_draft(@article))
end
return true
end
render :text => nil
end
def merge
@article = Article.find(params["id"])
@article.merge_with(params[:merge_with])
redirect_to :action => 'edit', id: @article.id
end
protected
def get_fresh_or_existing_draft_for_article
if @article.published and @article.id
parent_id = @article.id
@article = Article.drafts.child_of(parent_id).first || Article.new
@article.allow_comments = this_blog.default_allow_comments
@article.allow_pings = this_blog.default_allow_pings
@article.parent_id = parent_id
end
end
attr_accessor :resources, :categories, :resource, :category
def do_add_or_remove_fu
attrib, action = params[:action].split('_')
@article = Article.find(params[:id])
self.send("#{attrib}=", self.class.const_get(attrib.classify).find(params["#{attrib}_id"]))
send("setup_#{attrib.pluralize}")
@article.send(attrib.pluralize).send(real_action_for(action), send(attrib))
@article.save
render :partial => "show_#{attrib.pluralize}"
end
def real_action_for(action); { 'add' => :<<, 'remove' => :delete}[action]; end
def new_or_edit
@articles = Article.all if current_user.admin? && params[:action] == "edit"
id = params[:id]
id = params[:article][:id] if params[:article] && params[:article][:id]
@article = Article.get_or_build_article(id)
@article.text_filter = current_user.text_filter if current_user.simple_editor?
@post_types = PostType.find(:all)
if request.post?
if params[:article][:draft]
get_fresh_or_existing_draft_for_article
else
if not @article.parent_id.nil?
@article = Article.find(@article.parent_id)
end
end
end
@article.keywords = Tag.collection_to_string @article.tags
@article.attributes = params[:article]
# TODO: Consider refactoring, because double rescue looks... weird.
@article.published_at = DateTime.strptime(params[:article][:published_at], "%B %e, %Y %I:%M %p GMT%z").utc rescue Time.parse(params[:article][:published_at]).utc rescue nil
if request.post?
set_article_author
save_attachments
@article.state = "draft" if @article.draft
if @article.save
destroy_the_draft unless @article.draft
set_article_categories
set_the_flash
redirect_to :action => 'index'
return
end
end
@images = Resource.images_by_created_at.page(params[:page]).per(10)
@resources = Resource.without_images_by_filename
@macros = TextFilter.macro_filters
render 'new'
end
def set_the_flash
case params[:action]
when 'new'
flash[:notice] = _('Article was successfully created')
when 'edit'
flash[:notice] = _('Article was successfully updated.')
else
raise "I don't know how to tidy up action: #{params[:action]}"
end
end
def destroy_the_draft
Article.all(:conditions => { :parent_id => @article.id }).map(&:destroy)
end
def set_article_author
return if @article.author
@article.author = current_user.login
@article.user = current_user
end
def set_article_title_for_autosave
if @article.title.blank?
lastid = Article.find(:first, :order => 'id DESC').id
@article.title = "Draft article " + lastid.to_s
end
end
def save_attachments
return if params[:attachments].nil?
params[:attachments].each do |k,v|
a = attachment_save(v)
@article.resources << a unless a.nil?
end
end
def set_article_categories
@article.categorizations.clear
if params[:categories]
Category.find(params[:categories]).each do |cat|
@article.categories << cat
end
end
end
def def_build_body
if @article.body =~ /<!--more-->/
body = @article.body.split('<!--more-->')
@article.body = body[0]
@article.extended = body[1]
end
end
def setup_resources
@resources = Resource.by_created_at
end
end