]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Refactor creating a new report to use a ReportsController
[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 update
66     @issue = Issue.find_by(issue_params)
67     # Check if details provided are sufficient
68     if check_report_params
69       @report = @issue.reports.where(:reporter_user_id => current_user.id).first
70
71       if @report.nil?
72         @report = @issue.reports.build(report_params)
73         @report.reporter_user_id = current_user.id
74         notice = t("issues.update.new_report")
75       end
76
77       details = report_details
78       @report.details = details
79
80       # Checking if instance has been updated since last report
81       @last_report = @issue.reports.order(:updated_at => :desc).last
82       if check_if_updated
83         @issue.reopen
84         @issue.save!
85       end
86
87       notice = t("issues.update.successful_update") if notice.nil?
88
89       if @report.save!
90         @issue.report_count = @issue.reports.count
91         @issue.save!
92         redirect_back :fallback_location => "/", :notice => notice
93       end
94     else
95       redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id), :notice => t("issues.update.provide_details")
96     end
97   end
98
99   def comment
100     @issue = Issue.find(params[:id])
101     if issue_comment_params.blank?
102       notice = t("issues.comment.provide_details")
103     else
104       @issue_comment = @issue.comments.build(issue_comment_params)
105       @issue_comment.commenter_user_id = current_user.id
106       if params[:reassign]
107         reassign_issue
108         @issue_comment.reassign = true
109       end
110       @issue_comment.save!
111       @issue.updated_by = current_user.id
112       @issue.save!
113       notice = t("issues.comment.comment_created")
114     end
115     redirect_to @issue, :notice => notice
116   end
117
118   # Status Transistions
119   def resolve
120     if @issue.resolve
121       @issue.save!
122       redirect_to @issue, :notice => t("issues.resolved")
123     else
124       render :show
125     end
126   end
127
128   def ignore
129     if @issue.ignore
130       @issue.updated_by = current_user.id
131       @issue.save!
132       redirect_to @issue, :notice => t("issues.ignored")
133     else
134       render :show
135     end
136   end
137
138   def reopen
139     if @issue.reopen
140       @issue.updated_by = current_user.id
141       @issue.save!
142       redirect_to @issue, :notice => t("issues.reopened")
143     else
144       render :show
145     end
146   end
147
148   # Reassign Issues between Administrators and Moderators
149   def reassign_issue
150     @issue.issue_type = upgrade_issue(@issue.issue_type)
151     @issue.save!
152   end
153
154   private
155
156   def upgrade_issue(type)
157     if type == "moderator"
158       "administrator"
159     else
160       "moderator"
161     end
162   end
163
164   def set_issues
165     @admin_issues = %w[DiaryEntry DiaryComment User]
166     @moderator_issues = %w[Changeset Note]
167   end
168
169   def setup_user_role
170     # Get user role
171     @user_role = current_user.administrator? ? "administrator" : "moderator"
172   end
173
174   def check_if_updated
175     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
176       true
177     else
178       false
179     end
180   end
181
182   def report_details
183     params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
184   end
185
186   def check_report_params
187     params[:report] && params[:report][:details] && params[:report_type]
188   end
189
190   def find_issue
191     @issue = Issue.find(params[:id])
192   end
193
194   def check_permission
195     unless current_user.administrator? || current_user.moderator?
196       flash[:error] = t("application.require_admin.not_an_admin")
197       redirect_to root_path
198     end
199   end
200
201   def issue_params
202     params[:issue].permit(:reportable_id, :reportable_type)
203   end
204
205   def report_params
206     params[:report].permit(:details)
207   end
208
209   def issue_comment_params
210     params.require(:issue_comment).permit(:body)
211   end
212
213   def sort_column
214     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
215   end
216
217   def sort_direction
218     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
219   end
220 end