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