]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Proper implementation of report strings + cleaning up
[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       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
39       @report_strings_yaml = t( path)
40     end
41   end
42
43   def create
44     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
45     # Check if Issue alrwady exists
46     if !@issue 
47       @issue = Issue.find_or_initialize_by(issue_params)
48       @admins = UserRole.where(role: "administrator")
49       @admins.each do |admin|
50         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
51       end
52     end
53
54     # Check if details provided are sufficient
55     if check_report_params
56       @report = @issue.reports.build(report_params)
57       details =  get_report_details
58       @report.reporter_user_id = @user.id
59       @report.details = details
60
61       # Checking if instance has been updated since last report
62       @last_report = @issue.reports.order(updated_at: :desc).last
63       if check_if_updated
64         if @issue.reopen
65           @issue.save!
66         end
67       end
68
69       if @issue.save!
70         redirect_to root_path, notice: t('issues.create.successful_report')
71       end
72     else
73       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')
74     end
75   end
76
77   def update
78     @issue = Issue.find_by(issue_params)
79     # Check if details provided are sufficient
80     if check_report_params
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 =  get_report_details
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 check_if_updated
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
148   private
149
150     def check_if_updated
151       if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
152         return true
153       else
154         return false
155       end
156     end
157  
158     def get_report_details
159       details = params[:report][:details] + "--||--"
160       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
161       @report_strings_yaml = t( path)
162       @report_strings_yaml.each do |k,v|
163         if params[k.to_sym]
164           details = details + params[k.to_sym] + "--||--"
165         end
166       end
167       return details
168     end
169
170     def check_report_params
171       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
172       @report_strings_yaml = t( path)
173       if params[:report] and params[:report][:details]
174         @report_strings_yaml.each do |k,v|
175           if params[k.to_sym]
176             return true
177           end
178         end
179       end
180       return false
181     end
182
183     def find_issue
184       @issue = Issue.find(params[:id])
185     end
186
187     def check_permission
188       unless @user.administrator?
189         flash[:error] = t('application.require_admin.not_an_admin')
190         redirect_to root_path
191       end
192     end
193
194     def create_new_issue_params
195       params.permit(:reportable_id, :reportable_type, :reported_user_id)
196     end
197
198     def issue_params
199       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
200     end
201
202     def report_params
203       params[:report].permit(:details)
204     end
205
206     def issue_comment_params
207       params.require(:issue_comment).permit(:body)
208     end
209
210 end