]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
edf8f1121cbf9b297eaf7ab10d9997961153bec6
[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   def index
12     if current_user.moderator?
13       @issue_types = @moderator_issues
14       @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
15     else
16       @issue_types = @admin_issues
17       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
18     end
19
20     @issues = Issue.where(:issue_type => @user_role)
21
22     # If search
23     if params[:search_by_user] && params[:search_by_user].present?
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     if params[:status] && params[:status][0].present?
33       @issues = @issues.where(:status => params[:status][0].to_i)
34     end
35
36     if params[:issue_type] && params[:issue_type][0].present?
37       @issues = @issues.where(:reportable_type => params[:issue_type][0])
38     end
39
40     # If last_updated_by
41     if params[:last_updated_by] && params[:last_updated_by][0].present?
42       last_updated_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
43       @issues = @issues.where(:updated_by => last_updated_by)
44     end
45
46     if params[:last_reported_by] && params[:last_reported_by][0].present?
47       last_reported_by = params[:last_reported_by][0].to_s == "nil" ? nil : params[:last_reported_by][0].to_i
48       @issues = @issues.where(:updated_by => last_reported_by)
49     end
50
51     redirect_to issues_path, :notice => notice if notice
52   end
53
54   def show
55     @read_reports = @issue.read_reports
56     @unread_reports = @issue.unread_reports
57     @comments = @issue.comments
58     @related_issues = @issue.reported_user.issues.where(:issue_type => @user_role)
59     @new_comment = IssueComment.new(:issue => @issue)
60   end
61
62   def update
63     @issue = Issue.find_by(issue_params)
64     # Check if details provided are sufficient
65     if check_report_params
66       @report = @issue.reports.where(:reporter_user_id => current_user.id).first
67
68       if @report.nil?
69         @report = @issue.reports.build(report_params)
70         @report.reporter_user_id = current_user.id
71         notice = t("issues.update.new_report")
72       end
73
74       details = report_details
75       @report.details = details
76
77       # Checking if instance has been updated since last report
78       @last_report = @issue.reports.order(:updated_at => :desc).last
79       if check_if_updated
80         @issue.reopen
81         @issue.save!
82       end
83
84       notice = t("issues.update.successful_update") if notice.nil?
85
86       if @report.save!
87         @issue.report_count = @issue.reports.count
88         @issue.save!
89         redirect_back :fallback_location => "/", :notice => notice
90       end
91     else
92       redirect_to new_issue_path(:reportable_type => @issue.reportable_type, :reportable_id => @issue.reportable_id), :notice => t("issues.update.provide_details")
93     end
94   end
95
96   # Status Transistions
97   def resolve
98     if @issue.resolve
99       @issue.save!
100       redirect_to @issue, :notice => t("issues.resolved")
101     else
102       render :show
103     end
104   end
105
106   def ignore
107     if @issue.ignore
108       @issue.updated_by = current_user.id
109       @issue.save!
110       redirect_to @issue, :notice => t("issues.ignored")
111     else
112       render :show
113     end
114   end
115
116   def reopen
117     if @issue.reopen
118       @issue.updated_by = current_user.id
119       @issue.save!
120       redirect_to @issue, :notice => t("issues.reopened")
121     else
122       render :show
123     end
124   end
125
126   # Reassign Issues between Administrators and Moderators
127   def reassign_issue
128     @issue.issue_type = upgrade_issue(@issue.issue_type)
129     @issue.save!
130   end
131
132   private
133
134   def upgrade_issue(type)
135     if type == "moderator"
136       "administrator"
137     else
138       "moderator"
139     end
140   end
141
142   def set_issues
143     @admin_issues = %w[DiaryEntry DiaryComment User]
144     @moderator_issues = %w[Changeset Note]
145   end
146
147   def setup_user_role
148     # Get user role
149     @user_role = current_user.administrator? ? "administrator" : "moderator"
150   end
151
152   def check_if_updated
153     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
154       true
155     else
156       false
157     end
158   end
159
160   def report_details
161     params[:report][:details] + "--||--" + params[:report_type].to_s + "--||--"
162   end
163
164   def check_report_params
165     params[:report] && params[:report][:details] && params[:report_type]
166   end
167
168   def find_issue
169     @issue = Issue.find(params[:id])
170   end
171
172   def check_permission
173     unless current_user.administrator? || current_user.moderator?
174       flash[:error] = t("application.require_admin.not_an_admin")
175       redirect_to root_path
176     end
177   end
178
179   def issue_params
180     params[:issue].permit(:reportable_id, :reportable_type)
181   end
182
183   def report_params
184     params[:report].permit(:details)
185   end
186
187   def issue_comment_params
188     params.require(:issue_comment).permit(:body)
189   end
190
191   def sort_column
192     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
193   end
194
195   def sort_direction
196     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
197   end
198 end