Merge branch 'master' into moderation
[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     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   # Status Transistions
62   def resolve
63     if @issue.resolve
64       @issue.save!
65       redirect_to @issue, :notice => t("issues.resolved")
66     else
67       render :show
68     end
69   end
70
71   def ignore
72     if @issue.ignore
73       @issue.updated_by = current_user.id
74       @issue.save!
75       redirect_to @issue, :notice => t("issues.ignored")
76     else
77       render :show
78     end
79   end
80
81   def reopen
82     if @issue.reopen
83       @issue.updated_by = current_user.id
84       @issue.save!
85       redirect_to @issue, :notice => t("issues.reopened")
86     else
87       render :show
88     end
89   end
90
91   private
92
93   def set_issues
94     @admin_issues = %w[DiaryEntry DiaryComment User]
95     @moderator_issues = %w[Changeset Note]
96   end
97
98   def check_if_updated
99     if @issue.reportable && (@issue.ignored? || @issue.resolved?) && @issue.reportable.has_attribute?(:updated_by) && @issue.reportable.updated_at > @last_report.updated_at
100       true
101     else
102       false
103     end
104   end
105
106   def find_issue
107     @issue = Issue.find(params[:id])
108   end
109
110   def check_permission
111     unless current_user.administrator? || current_user.moderator?
112       flash[:error] = t("application.require_admin.not_an_admin")
113       redirect_to root_path
114     end
115   end
116
117   def issue_params
118     params[:issue].permit(:reportable_id, :reportable_type)
119   end
120
121   def report_params
122     params[:report].permit(:details)
123   end
124
125   def issue_comment_params
126     params.require(:issue_comment).permit(:body)
127   end
128
129   def sort_column
130     Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
131   end
132
133   def sort_direction
134     %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
135   end
136 end