##// END OF EJS Templates
Fixed: timelog redirects inappropriately when :back_url is blank (#1524)....
Jean-Philippe Lang -
r1575:f79f19f84c3b
parent child
Show More
@@ -1,262 +1,262
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 layout 'base'
19 layout 'base'
20 menu_item :issues
20 menu_item :issues
21 before_filter :find_project, :authorize
21 before_filter :find_project, :authorize
22
22
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :issues
27 helper :issues
28 include TimelogHelper
28 include TimelogHelper
29 helper :custom_fields
29 helper :custom_fields
30 include CustomFieldsHelper
30 include CustomFieldsHelper
31
31
32 def report
32 def report
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
34 :klass => Project,
34 :klass => Project,
35 :label => :label_project},
35 :label => :label_project},
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
37 :klass => Version,
37 :klass => Version,
38 :label => :label_version},
38 :label => :label_version},
39 'category' => {:sql => "#{Issue.table_name}.category_id",
39 'category' => {:sql => "#{Issue.table_name}.category_id",
40 :klass => IssueCategory,
40 :klass => IssueCategory,
41 :label => :field_category},
41 :label => :field_category},
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
43 :klass => User,
43 :klass => User,
44 :label => :label_member},
44 :label => :label_member},
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
46 :klass => Tracker,
46 :klass => Tracker,
47 :label => :label_tracker},
47 :label => :label_tracker},
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
49 :klass => Enumeration,
49 :klass => Enumeration,
50 :label => :label_activity},
50 :label => :label_activity},
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
52 :klass => Issue,
52 :klass => Issue,
53 :label => :label_issue}
53 :label => :label_issue}
54 }
54 }
55
55
56 # Add list and boolean custom fields as available criterias
56 # Add list and boolean custom fields as available criterias
57 @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
57 @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
58 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
58 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
59 :format => cf.field_format,
59 :format => cf.field_format,
60 :label => cf.name}
60 :label => cf.name}
61 end
61 end
62
62
63 @criterias = params[:criterias] || []
63 @criterias = params[:criterias] || []
64 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
64 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
65 @criterias.uniq!
65 @criterias.uniq!
66 @criterias = @criterias[0,3]
66 @criterias = @criterias[0,3]
67
67
68 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
68 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
69
69
70 retrieve_date_range
70 retrieve_date_range
71
71
72 unless @criterias.empty?
72 unless @criterias.empty?
73 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
73 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
74 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
74 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
75
75
76 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
76 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
77 sql << " FROM #{TimeEntry.table_name}"
77 sql << " FROM #{TimeEntry.table_name}"
78 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
78 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
79 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
79 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
80 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
80 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
81 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
81 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
82 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
82 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
83 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
83 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
84
84
85 @hours = ActiveRecord::Base.connection.select_all(sql)
85 @hours = ActiveRecord::Base.connection.select_all(sql)
86
86
87 @hours.each do |row|
87 @hours.each do |row|
88 case @columns
88 case @columns
89 when 'year'
89 when 'year'
90 row['year'] = row['tyear']
90 row['year'] = row['tyear']
91 when 'month'
91 when 'month'
92 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
92 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
93 when 'week'
93 when 'week'
94 row['week'] = "#{row['tyear']}-#{row['tweek']}"
94 row['week'] = "#{row['tyear']}-#{row['tweek']}"
95 when 'day'
95 when 'day'
96 row['day'] = "#{row['spent_on']}"
96 row['day'] = "#{row['spent_on']}"
97 end
97 end
98 end
98 end
99
99
100 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
100 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
101
101
102 @periods = []
102 @periods = []
103 # Date#at_beginning_of_ not supported in Rails 1.2.x
103 # Date#at_beginning_of_ not supported in Rails 1.2.x
104 date_from = @from.to_time
104 date_from = @from.to_time
105 # 100 columns max
105 # 100 columns max
106 while date_from <= @to.to_time && @periods.length < 100
106 while date_from <= @to.to_time && @periods.length < 100
107 case @columns
107 case @columns
108 when 'year'
108 when 'year'
109 @periods << "#{date_from.year}"
109 @periods << "#{date_from.year}"
110 date_from = (date_from + 1.year).at_beginning_of_year
110 date_from = (date_from + 1.year).at_beginning_of_year
111 when 'month'
111 when 'month'
112 @periods << "#{date_from.year}-#{date_from.month}"
112 @periods << "#{date_from.year}-#{date_from.month}"
113 date_from = (date_from + 1.month).at_beginning_of_month
113 date_from = (date_from + 1.month).at_beginning_of_month
114 when 'week'
114 when 'week'
115 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
115 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
116 date_from = (date_from + 7.day).at_beginning_of_week
116 date_from = (date_from + 7.day).at_beginning_of_week
117 when 'day'
117 when 'day'
118 @periods << "#{date_from.to_date}"
118 @periods << "#{date_from.to_date}"
119 date_from = date_from + 1.day
119 date_from = date_from + 1.day
120 end
120 end
121 end
121 end
122 end
122 end
123
123
124 respond_to do |format|
124 respond_to do |format|
125 format.html { render :layout => !request.xhr? }
125 format.html { render :layout => !request.xhr? }
126 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
126 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
127 end
127 end
128 end
128 end
129
129
130 def details
130 def details
131 sort_init 'spent_on', 'desc'
131 sort_init 'spent_on', 'desc'
132 sort_update
132 sort_update
133
133
134 cond = ARCondition.new
134 cond = ARCondition.new
135 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
135 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
136 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
136 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
137
137
138 retrieve_date_range
138 retrieve_date_range
139 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
139 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
140
140
141 TimeEntry.visible_by(User.current) do
141 TimeEntry.visible_by(User.current) do
142 respond_to do |format|
142 respond_to do |format|
143 format.html {
143 format.html {
144 # Paginate results
144 # Paginate results
145 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
145 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
146 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
146 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
147 @entries = TimeEntry.find(:all,
147 @entries = TimeEntry.find(:all,
148 :include => [:project, :activity, :user, {:issue => :tracker}],
148 :include => [:project, :activity, :user, {:issue => :tracker}],
149 :conditions => cond.conditions,
149 :conditions => cond.conditions,
150 :order => sort_clause,
150 :order => sort_clause,
151 :limit => @entry_pages.items_per_page,
151 :limit => @entry_pages.items_per_page,
152 :offset => @entry_pages.current.offset)
152 :offset => @entry_pages.current.offset)
153 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
153 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
154
154
155 render :layout => !request.xhr?
155 render :layout => !request.xhr?
156 }
156 }
157 format.atom {
157 format.atom {
158 entries = TimeEntry.find(:all,
158 entries = TimeEntry.find(:all,
159 :include => [:project, :activity, :user, {:issue => :tracker}],
159 :include => [:project, :activity, :user, {:issue => :tracker}],
160 :conditions => cond.conditions,
160 :conditions => cond.conditions,
161 :order => "#{TimeEntry.table_name}.created_on DESC",
161 :order => "#{TimeEntry.table_name}.created_on DESC",
162 :limit => Setting.feeds_limit.to_i)
162 :limit => Setting.feeds_limit.to_i)
163 render_feed(entries, :title => l(:label_spent_time))
163 render_feed(entries, :title => l(:label_spent_time))
164 }
164 }
165 format.csv {
165 format.csv {
166 # Export all entries
166 # Export all entries
167 @entries = TimeEntry.find(:all,
167 @entries = TimeEntry.find(:all,
168 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
168 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
169 :conditions => cond.conditions,
169 :conditions => cond.conditions,
170 :order => sort_clause)
170 :order => sort_clause)
171 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
171 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
172 }
172 }
173 end
173 end
174 end
174 end
175 end
175 end
176
176
177 def edit
177 def edit
178 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
178 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
179 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
179 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
180 @time_entry.attributes = params[:time_entry]
180 @time_entry.attributes = params[:time_entry]
181 if request.post? and @time_entry.save
181 if request.post? and @time_entry.save
182 flash[:notice] = l(:notice_successful_update)
182 flash[:notice] = l(:notice_successful_update)
183 redirect_to(params[:back_url] || {:action => 'details', :project_id => @time_entry.project})
183 redirect_to(params[:back_url].blank? ? {:action => 'details', :project_id => @time_entry.project} : params[:back_url])
184 return
184 return
185 end
185 end
186 @activities = Enumeration::get_values('ACTI')
186 @activities = Enumeration::get_values('ACTI')
187 end
187 end
188
188
189 def destroy
189 def destroy
190 render_404 and return unless @time_entry
190 render_404 and return unless @time_entry
191 render_403 and return unless @time_entry.editable_by?(User.current)
191 render_403 and return unless @time_entry.editable_by?(User.current)
192 @time_entry.destroy
192 @time_entry.destroy
193 flash[:notice] = l(:notice_successful_delete)
193 flash[:notice] = l(:notice_successful_delete)
194 redirect_to :back
194 redirect_to :back
195 rescue RedirectBackError
195 rescue RedirectBackError
196 redirect_to :action => 'details', :project_id => @time_entry.project
196 redirect_to :action => 'details', :project_id => @time_entry.project
197 end
197 end
198
198
199 private
199 private
200 def find_project
200 def find_project
201 if params[:id]
201 if params[:id]
202 @time_entry = TimeEntry.find(params[:id])
202 @time_entry = TimeEntry.find(params[:id])
203 @project = @time_entry.project
203 @project = @time_entry.project
204 elsif params[:issue_id]
204 elsif params[:issue_id]
205 @issue = Issue.find(params[:issue_id])
205 @issue = Issue.find(params[:issue_id])
206 @project = @issue.project
206 @project = @issue.project
207 elsif params[:project_id]
207 elsif params[:project_id]
208 @project = Project.find(params[:project_id])
208 @project = Project.find(params[:project_id])
209 else
209 else
210 render_404
210 render_404
211 return false
211 return false
212 end
212 end
213 rescue ActiveRecord::RecordNotFound
213 rescue ActiveRecord::RecordNotFound
214 render_404
214 render_404
215 end
215 end
216
216
217 # Retrieves the date range based on predefined ranges or specific from/to param dates
217 # Retrieves the date range based on predefined ranges or specific from/to param dates
218 def retrieve_date_range
218 def retrieve_date_range
219 @free_period = false
219 @free_period = false
220 @from, @to = nil, nil
220 @from, @to = nil, nil
221
221
222 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
222 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
223 case params[:period].to_s
223 case params[:period].to_s
224 when 'today'
224 when 'today'
225 @from = @to = Date.today
225 @from = @to = Date.today
226 when 'yesterday'
226 when 'yesterday'
227 @from = @to = Date.today - 1
227 @from = @to = Date.today - 1
228 when 'current_week'
228 when 'current_week'
229 @from = Date.today - (Date.today.cwday - 1)%7
229 @from = Date.today - (Date.today.cwday - 1)%7
230 @to = @from + 6
230 @to = @from + 6
231 when 'last_week'
231 when 'last_week'
232 @from = Date.today - 7 - (Date.today.cwday - 1)%7
232 @from = Date.today - 7 - (Date.today.cwday - 1)%7
233 @to = @from + 6
233 @to = @from + 6
234 when '7_days'
234 when '7_days'
235 @from = Date.today - 7
235 @from = Date.today - 7
236 @to = Date.today
236 @to = Date.today
237 when 'current_month'
237 when 'current_month'
238 @from = Date.civil(Date.today.year, Date.today.month, 1)
238 @from = Date.civil(Date.today.year, Date.today.month, 1)
239 @to = (@from >> 1) - 1
239 @to = (@from >> 1) - 1
240 when 'last_month'
240 when 'last_month'
241 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
241 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
242 @to = (@from >> 1) - 1
242 @to = (@from >> 1) - 1
243 when '30_days'
243 when '30_days'
244 @from = Date.today - 30
244 @from = Date.today - 30
245 @to = Date.today
245 @to = Date.today
246 when 'current_year'
246 when 'current_year'
247 @from = Date.civil(Date.today.year, 1, 1)
247 @from = Date.civil(Date.today.year, 1, 1)
248 @to = Date.civil(Date.today.year, 12, 31)
248 @to = Date.civil(Date.today.year, 12, 31)
249 end
249 end
250 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
250 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
251 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
251 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
252 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
252 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
253 @free_period = true
253 @free_period = true
254 else
254 else
255 # default
255 # default
256 end
256 end
257
257
258 @from, @to = @to, @from if @from && @to && @from > @to
258 @from, @to = @to, @from if @from && @to && @from > @to
259 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
259 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
260 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
260 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
261 end
261 end
262 end
262 end
General Comments 0
You need to be logged in to leave comments. Login now