forked from publify/publify
/
feedback_controller.rb
202 lines (172 loc) · 5.83 KB
/
feedback_controller.rb
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
class Admin::FeedbackController < Admin::BaseController
cache_sweeper :blog_sweeper
def index
conditions = ['1 = 1', {}]
if params[:search]
conditions.first << ' and (url like :pattern or author like :pattern or title like :pattern or ip like :pattern or email like :pattern)'
conditions.last.merge!(:pattern => "%#{params[:search]}%")
end
if params[:published] == 'f'
conditions.first << ' and (published = :published)'
conditions.last.merge!(:published => false)
end
if params[:confirmed] == 'f'
conditions.first << ' AND (status_confirmed = :status_confirmed)'
conditions.last.merge!(:status_confirmed => false)
end
if params[:ham] == 'f'
conditions.first << ' AND state = :state '
conditions.last.merge!(:state => 'ham')
end
if params[:spam] == 'f'
conditions.first << ' AND state = :state '
conditions.last.merge!(:state => 'spam')
end
if params[:presumed_ham] == 'f'
conditions.first << ' AND state = :state '
conditions.last.merge!(:state => 'presumed_ham')
end
if params[:presumed_spam] == 'f'
conditions.first << ' AND state = :state '
conditions.last.merge!(:state => 'presumed_spam')
end
# no need params[:page] if empty of == 0, there are a crash otherwise
if params[:page].blank? || params[:page] == "0"
params.delete(:page)
end
@feedback = Feedback.where(conditions).order('feedback.created_at desc').page(params[:page]).per(this_blog.admin_display_elements)
end
def destroy
@record = Feedback.find params[:id]
unless @record.article.user_id == current_user.id
unless current_user.admin?
return redirect_to :controller => 'admin/feedback', :action => :index
end
end
return(render 'admin/shared/destroy') unless request.post?
begin
@record.destroy
flash[:notice] = _("Deleted")
rescue ActiveRecord::RecordNotFound
flash[:notice] = _("Not found")
end
redirect_to :action => 'article', :id => @record.article.id
end
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
if request.post? and @comment.save
# We should probably wave a spam filter over this, but for now, just mark it as published.
@comment.mark_as_ham
@comment.save!
flash[:notice] = _('Comment was successfully created.')
end
redirect_to :action => 'article', :id => @article.id
end
def edit
@comment = Comment.find(params[:id])
@article = @comment.article
unless @article.access_by? current_user
redirect_to :action => 'index'
return
end
end
def update
comment = Comment.find(params[:id])
unless comment.article.access_by? current_user
redirect_to :action => 'index'
return
end
comment.attributes = params[:comment]
if request.post? and comment.save
flash[:notice] = _('Comment was successfully updated.')
redirect_to action: 'article', id: comment.article.id
else
redirect_to action: 'edit', id: comment.id
end
end
def article
@article = Article.find(params[:id])
if params[:ham] && params[:spam].blank?
@feedback = @article.comments.ham
end
if params[:spam] && params[:ham].blank?
@feedback = @article.comments.spam
end
@feedback ||= @article.comments
end
def change_state
return unless request.xhr?
feedback = Feedback.find(params[:id])
template = feedback.change_state!
render(:update) do |page|
if params[:context] != 'listing'
page.visual_effect :fade, "feedback_#{feedback.id}"
else
if template == "ham"
page.visual_effect :appear, "feedback_#{feedback.id}"
page.visual_effect :fade, "placeholder_#{feedback.id}"
else
page.visual_effect :appear, "placeholder_#{feedback.id}"
page.visual_effect :fade, "feedback_#{feedback.id}"
end
end
end
end
def bulkops
ids = (params[:feedback_check]||{}).keys.map(&:to_i)
items = Feedback.find(ids)
@unexpired = true
bulkop = params[:bulkop_top].empty? ? params[:bulkop_bottom] : params[:bulkop_top]
case bulkop
when 'Delete Checked Items'
count = 0
ids.each do |id|
count += Feedback.delete(id) ## XXX Should this be #destroy?
end
flash[:notice] = _("Deleted %d item(s)",count)
items.each do |i|
i.invalidates_cache? or next
flush_cache
return
end
when 'Mark Checked Items as Ham'
update_feedback(items, :mark_as_ham!)
flash[:notice]= _("Marked %d item(s) as Ham",ids.size)
when 'Mark Checked Items as Spam'
update_feedback(items, :mark_as_spam!)
flash[:notice]= _("Marked %d item(s) as Spam",ids.size)
when 'Confirm Classification of Checked Items'
update_feedback(items, :confirm_classification!)
flash[:notice] = _("Confirmed classification of %s item(s)",ids.size)
when 'Delete all spam'
delete_all_spam
else
flash[:notice] = _("Not implemented")
end
if params[:article_id]
redirect_to :action => 'article', :id => params[:article_id], :confirmed => params[:confirmed], :published => params[:published]
else
redirect_to :action => 'index', :page => params[:page], :search => params[:search], :confirmed => params[:confirmed], :published => params[:published]
end
end
protected
def delete_all_spam
if request.post?
Feedback.delete_all(['state = ?', 'spam'])
flash[:notice] = _("All spam have been deleted")
end
end
def update_feedback(items, method)
items.each do |value|
value.send(method)
@unexpired && value.invalidates_cache? or next
flush_cache
end
end
def flush_cache
@unexpired = false
PageCache.sweep_all
end
end