]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Fully internationalise the issues and reports interfaces.
[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
8   before_action :find_issue, :only => [:show, :resolve, :reopen, :ignore]
9
10   def index
11     @title = t ".title"
12
13     if current_user.moderator?
14       @issue_types = @moderator_issues
15       @users = User.joins(:roles).where(:user_roles => { :role => "moderator" })
16     else
17       @issue_types = @admin_issues
18       @users = User.joins(:roles).where(:user_roles => { :role => "administrator" })
19     end
20
21     @issues = Issue.where(:assigned_role => current_user.roles.map(&:role))
22
23     # If search
24     if params[:search_by_user] && params[:search_by_user].present?
25       @find_user = User.find_by(:display_name => params[:search_by_user])
26       if @find_user
27         @issues = @issues.where(:reported_user_id => @find_user.id)
28       else
29         notice = t("issues.index.user_not_found")
30       end
31     end
32
33     if params[:status] && params[:status][0].present?
34       @issues = @issues.where(:status => params[:status][0].to_i)
35     end
36
37     if params[:issue_type] && params[:issue_type][0].present?
38       @issues = @issues.where(:reportable_type => params[:issue_type][0])
39     end
40
41     # If last_updated_by
42     if params[:last_updated_by] && params[:last_updated_by][0].present?
43       last_updated_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
44       @issues = @issues.where(:updated_by => last_updated_by)
45     end
46
47     if params[:last_reported_by] && params[:last_reported_by][0].present?
48       last_reported_by = params[:last_reported_by][0].to_s == "nil" ? nil : params[:last_reported_by][0].to_i
49       @issues = @issues.where(:updated_by => last_reported_by)
50     end
51
52     redirect_to issues_path, :notice => notice if notice
53   end
54
55   def show
56     @read_reports = @issue.read_reports
57     @unread_reports = @issue.unread_reports
58     @comments = @issue.comments
59     @related_issues = @issue.reported_user.issues.where(:assigned_role => current_user.roles.map(&:role))
60     @new_comment = IssueComment.new(:issue => @issue)
61   end
62
63   # Status Transistions
64   def resolve
65     if @issue.resolve
66       @issue.save!
67       redirect_to @issue, :notice => t("issues.resolved")
68     else
69       render :show
70     end
71   end
72
73   def ignore
74     if @issue.ignore
75       @issue.updated_by = current_user.id
76       @issue.save!
77       redirect_to @issue, :notice => t("issues.ignored")
78     else
79       render :show
80     end
81   end
82
83   def reopen
84     if @issue.reopen
85       @issue.updated_by = current_user.id
86       @issue.save!
87       redirect_to @issue, :notice => t("issues.reopened")
88     else
89       render :show
90     end
91   end
92
93   private
94
95   def set_issues
96     @admin_issues = %w[DiaryEntry DiaryComment User]
97     @moderator_issues = %w[Changeset Note]
98   end
99
100   def check_if_updated
101     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
102       true
103     else
104       false
105     end
106   end
107
108   def find_issue
109     @issue = Issue.find(params[:id])
110   end
111
112   def check_permission
113     unless current_user.administrator? || current_user.moderator?
114       flash[:error] = t("application.require_admin.not_an_admin")
115       redirect_to root_path
116     end
117   end
118
119   def issue_params
120     params[:issue].permit(:reportable_id, :reportable_type)
121   end
122
123   def report_params
124     params[:report].permit(:details)
125   end
126
127   def issue_comment_params
128     params.require(:issue_comment).permit(:body)
129   end
130
131   def sort_column
132     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
133   end
134
135   def sort_direction
136     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
137   end
138 end