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