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