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