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