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