d31326a7db881196517bebeb7fdbcd6532e69b4b
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :set_issues
7   before_action :check_permission, :only => [:index, :show, :resolve, :open, :ignore, :comment]
8   before_action :find_issue, :only => [:show, :resolve, :reopen, :ignore]
9   before_action :setup_user_role, :only => [:show, :index]
10
11   helper_method :sort_column, :sort_direction
12
13   def index
14     if @user.moderator?
15       @issue_types = @moderator_issues
16       @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
17     else
18       @issue_types = @admin_issues
19       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
20     end
21
22     @issues = Issue.where(:issue_type => @user_role).order(sort_column + " " + sort_direction)
23
24     # If search
25     if params[:search_by_user] && !params[:search_by_user].blank?
26       @find_user = User.find_by_display_name(params[:search_by_user])
27       if @find_user
28         @issues = @issues.where(:reported_user_id => @find_user.id)
29       else
30         notice = t("issues.index.search.user_not_found")
31       end
32     end
33
34     if params[:status] && !params[:status][0].blank?
35       @issues = @issues.where(:status => params[:status][0].to_i)
36     end
37
38     if params[:issue_type] && !params[:issue_type][0].blank?
39       @issues = @issues.where(:reportable_type => params[:issue_type][0])
40     end
41
42     # If last_updated_by
43     if params[:last_updated_by] && !params[:last_updated_by][0].blank?
44       last_updated_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
45       @issues = @issues.where(:updated_by => last_updated_by)
46     end
47
48     notice = t("issues.index.search.issues_not_found") if @issues.first.nil?
49
50     if params[:last_reported_by] && !params[:last_reported_by][0].blank?
51       last_reported_by = params[:last_reported_by][0].to_s == "nil" ? nil : params[:last_reported_by][0].to_i
52       @issues = @issues.where(:updated_by => last_reported_by)
53     end
54
55     redirect_to issues_path, :notice => notice if notice
56   end
57
58   def show
59     @read_reports = @issue.read_reports
60     @unread_reports = @issue.unread_reports
61     @comments = @issue.comments
62     @related_issues = @issue.user.issues.where(:issue_type => @user_role)
63
64     @updated_by_admin = User.find(@issue.updated_by) if @issue.updated_by
65   end
66
67   def new
68     unless create_new_issue_params.blank?
69       @issue = Issue.find_or_initialize_by(create_new_issue_params)
70       path = "issues.report_strings." + @issue.reportable.class.name.to_s
71       @report_strings_yaml = t(path)
72       flash[:referer] = params[:referer]
73     end
74   end
75
76   def create
77     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id], params[:reportable_type])
78     # Check if Issue already exists
79     unless @issue
80       @issue = Issue.find_or_initialize_by(issue_params)
81       @issue.updated_by = nil
82
83       # Reassign to moderators if it is a moderator issue
84       @issue.issue_type = "administrator"
85       reassign_issue if @moderator_issues.include? @issue.reportable.class.name
86     end
87
88     # Check if details provided are sufficient
89     if check_report_params
90       @report = @issue.reports.build(report_params)
91       details = report_details
92       @report.reporter_user_id = @user.id
93       @report.details = details
94       # Checking if instance has been updated since last report
95       @last_report = @issue.reports.order(:updated_at => :desc).last
96       if check_if_updated
97         @issue.save! if @issue.reopen
98       end
99
100       if @issue.save!
101         @issue.report_count = @issue.reports.count
102         @issue.save!
103
104         @admins_or_mods = UserRole.where(:role => @issue.issue_type)
105         @admins_or_mods.each do |user|
106           Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now
107         end
108
109         redirect_to flash[:referer], :notice => t("issues.create.successful_report")
110       end
111     else
112       redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id, :reported_user_id => @issue.reported_user_id), :notice => t("issues.create.provide_details")
113     end
114   end
115
116   def update
117     @issue = Issue.find_by(issue_params)
118     # Check if details provided are sufficient
119     if check_report_params
120       @report = @issue.reports.where(:reporter_user_id => @user.id).first
121
122       if @report.nil?
123         @report = @issue.reports.build(report_params)
124         @report.reporter_user_id = @user.id
125         notice = t("issues.update.new_report")
126       end
127
128       details = report_details
129       @report.details = details
130
131       # Checking if instance has been updated since last report
132       @last_report = @issue.reports.order(:updated_at => :desc).last
133       if check_if_updated
134         @issue.reopen
135         @issue.save!
136       end
137
138       notice = t("issues.update.successful_update") if notice.nil?
139
140       if @report.save!
141         @issue.report_count = @issue.reports.count
142         @issue.save!
143         redirect_to flash[:referer], :notice => notice
144       end
145     else
146       redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id, :reported_user_id => @issue.reported_user_id), :notice => t("issues.update.provide_details")
147     end
148   end
149
150   def comment
151     @issue = Issue.find(params[:id])
152     if issue_comment_params.blank?
153       notice = t("issues.comment.provide_details")
154     else
155       @issue_comment = @issue.comments.build(issue_comment_params)
156       @issue_comment.commenter_user_id = @user.id
157       if params[:reassign]
158         reassign_issue
159         @issue_comment.reassign = true
160       end
161       @issue_comment.save!
162       @issue.updated_by = @user.id
163       @issue.save!
164       notice = t("issues.comment.comment_created")
165     end
166     redirect_to @issue, :notice => notice
167   end
168
169   # Status Transistions
170   def resolve
171     if @issue.resolve
172       @issue.save!
173       redirect_to @issue, :notice => t("issues.resolved")
174     else
175       render :show
176     end
177   end
178
179   def ignore
180     if @issue.ignore
181       @issue.updated_by = @user.id
182       @issue.save!
183       redirect_to @issue, :notice => t("issues.ignored")
184     else
185       render :show
186     end
187   end
188
189   def reopen
190     if @issue.reopen
191       @issue.updated_by = @user.id
192       @issue.save!
193       redirect_to @issue, :notice => t("issues.reopened")
194     else
195       render :show
196     end
197   end
198
199   # Reassign Issues between Administrators and Moderators
200   def reassign_issue
201     @issue.issue_type = upgrade_issue(@issue.issue_type)
202     @issue.save!
203   end
204
205   private
206
207   def upgrade_issue(type)
208     if type == "moderator"
209       "administrator"
210     else
211       "moderator"
212     end
213   end
214
215   def set_issues
216     @admin_issues = %w(DiaryEntry DiaryComment User)
217     @moderator_issues = %w(Changeset Note)
218   end
219
220   def setup_user_role
221     # Get user role
222     @user_role = @user.administrator? ? "administrator" : "moderator"
223   end
224
225   def check_if_updated
226     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
227       return true
228     else
229       return false
230     end
231   end
232
233   def report_details
234     params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
235   end
236
237   def check_report_params
238     params[:report] && params[:report][:details] && params[:report_type]
239   end
240
241   def find_issue
242     @issue = Issue.find(params[:id])
243   end
244
245   def check_permission
246     unless @user.administrator? || @user.moderator?
247       flash[:error] = t("application.require_admin.not_an_admin")
248       redirect_to root_path
249     end
250   end
251
252   def create_new_issue_params
253     params.permit(:reportable_id, :reportable_type, :reported_user_id)
254   end
255
256   def issue_params
257     params[:issue].permit(:reportable_id, :reportable_type, :reported_user_id)
258   end
259
260   def report_params
261     params[:report].permit(:details)
262   end
263
264   def issue_comment_params
265     params.require(:issue_comment).permit(:body)
266   end
267
268   def sort_column
269     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
270   end
271
272   def sort_direction
273     %w(asc desc).include?(params[:direction]) ? params[:direction] : "asc"
274   end
275 end