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