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