]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Added search for issues
[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 :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
7   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
8
9   def index
10     if params[:search_by_user].present?
11       @user = User.find_by_display_name(params[:search_by_user])
12       if @user.present?
13         @issues = Issue.where(reported_user_id: @user.id).order(:status)
14       else 
15         @issues = Issue.all.order(:status)
16         redirect_to issues_path, notice: t('issues.index.search.user_not_found') 
17       end
18       
19       if @user.present? and not @issues.present?
20         @issues = Issue.all.order(:status)
21         redirect_to issues_path, notice: t('issues.index.search.issues_not_found')
22       end
23     else
24       @issues = Issue.all.order(:status)
25     end
26   end
27
28   def show
29     @read_reports = @issue.read_reports
30     @unread_reports = @issue.unread_reports
31     @comments = @issue.comments
32     @related_issues = @issue.user.issues
33   end
34
35   def new
36     unless create_new_issue_params.blank?
37       @issue = Issue.find_or_initialize_by(create_new_issue_params)
38     end
39   end
40
41   def create
42     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
43
44     # Check if Issue alrwady exists
45     if !@issue 
46       @issue = Issue.find_or_initialize_by(issue_params)
47       @admins = UserRole.where(role: "administrator")
48       @admins.each do |admin|
49         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
50       end
51     end
52
53     # Check if details provided are sufficient
54     if params[:report][:details] and (params[:spam] or params[:offensive] or params[:threat] or params[:vandal] or params[:other])
55       @report = @issue.reports.build(report_params)
56       details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
57       @report.reporter_user_id = @user.id
58       @report.details = details
59
60       # Checking if instance has been updated since last report
61       @last_report = @issue.reports.order(updated_at: :desc).last
62       if @issue.reportable.updated_at.present? and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
63         if @issue.reopen
64           @issue.save!
65         end
66       end
67
68       if @issue.save!
69         redirect_to root_path, notice: t('issues.create.successful_report')
70       end
71     else
72       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.create.provide_details')
73     end
74   end
75
76   def update
77     @issue = Issue.find_by(issue_params)
78
79     # Check if details provided are sufficient
80     if params[:report][:details] and (params[:spam] or params[:offensive] or params[:threat] or params[:vandal] or params[:other])
81       @report = @issue.reports.where(reporter_user_id: @user.id).first
82       
83       if @report == nil
84         @report = @issue.reports.build(report_params)
85         @report.reporter_user_id = @user.id
86         notice = t('issues.update.new_report')
87       end
88
89       details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
90       @report.details = details    
91
92     # Checking if instance has been updated since last report
93       @last_report = @issue.reports.order(updated_at: :desc).last
94       if @issue.reportable.updated_at.present? and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
95         @issue.reopen
96         @issue.save!
97       end
98
99       if notice == nil
100         notice = t('issues.update.successful_update')
101       end
102
103       if @report.save!
104         redirect_to root_path, notice: notice
105       end
106     else
107       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.update.provide_details')
108     end  
109   end
110
111   def comment
112     @issue = Issue.find(params[:id])
113     @issue_comment = @issue.comments.build(issue_comment_params)
114     @issue_comment.commenter_user_id = @user.id
115     @issue_comment.save!
116     redirect_to @issue
117   end
118
119   # Status Transistions
120   def resolve
121     if @issue.resolve
122       @issue.save!
123       redirect_to @issue, notice: t('issues.resolved')
124     else
125       render :show
126     end
127   end
128
129   def ignore
130     if @issue.ignore
131       @issue.save!
132       redirect_to @issue, notice: t('issues.ignored')
133     else
134       render :show
135     end
136   end
137
138   def reopen
139     if @issue.reopen
140       @issue.save!
141       redirect_to @issue, notice: t('issues.reopened')
142     else
143       render :show
144     end
145   end
146
147   private
148
149     def find_issue
150       @issue = Issue.find(params[:id])
151     end
152
153     def check_permission
154       unless @user.administrator?
155         flash[:error] = t("application.require_admin.not_an_admin")
156         redirect_to root_path
157       end
158     end
159
160     def create_new_issue_params
161       params.permit(:reportable_id, :reportable_type, :reported_user_id)
162     end
163
164     def issue_params
165       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
166     end
167
168     def report_params
169       params[:report].permit(:details)
170     end
171
172     def issue_comment_params
173       params.require(:issue_comment).permit(:body)
174     end
175 end