##// END OF EJS Templates
Fixed: DoubleRenderError introduced by #7996....
Jean-Philippe Lang -
r5198:efccc61d79d3
parent child
Show More
@@ -1,321 +1,323
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2010 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 menu_item :issues
20 20 before_filter :find_project, :only => [:new, :create]
21 21 before_filter :find_time_entry, :only => [:show, :edit, :update]
22 22 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
23 23 before_filter :authorize, :except => [:index]
24 24 before_filter :find_optional_project, :only => [:index]
25 25 accept_key_auth :index, :show, :create, :update, :destroy
26 26
27 27 helper :sort
28 28 include SortHelper
29 29 helper :issues
30 30 include TimelogHelper
31 31 helper :custom_fields
32 32 include CustomFieldsHelper
33 33
34 34 def index
35 35 sort_init 'spent_on', 'desc'
36 36 sort_update 'spent_on' => 'spent_on',
37 37 'user' => 'user_id',
38 38 'activity' => 'activity_id',
39 39 'project' => "#{Project.table_name}.name",
40 40 'issue' => 'issue_id',
41 41 'hours' => 'hours'
42 42
43 43 cond = ARCondition.new
44 44 if @issue
45 45 cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
46 46 elsif @project
47 47 cond << @project.project_condition(Setting.display_subprojects_issues?)
48 48 end
49 49
50 50 retrieve_date_range
51 51 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
52 52
53 53 respond_to do |format|
54 54 format.html {
55 55 # Paginate results
56 56 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
57 57 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
58 58 @entries = TimeEntry.visible.find(:all,
59 59 :include => [:project, :activity, :user, {:issue => :tracker}],
60 60 :conditions => cond.conditions,
61 61 :order => sort_clause,
62 62 :limit => @entry_pages.items_per_page,
63 63 :offset => @entry_pages.current.offset)
64 64 @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
65 65
66 66 render :layout => !request.xhr?
67 67 }
68 68 format.api {
69 69 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
70 70 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
71 71 @entries = TimeEntry.visible.find(:all,
72 72 :include => [:project, :activity, :user, {:issue => :tracker}],
73 73 :conditions => cond.conditions,
74 74 :order => sort_clause,
75 75 :limit => @entry_pages.items_per_page,
76 76 :offset => @entry_pages.current.offset)
77 77 }
78 78 format.atom {
79 79 entries = TimeEntry.visible.find(:all,
80 80 :include => [:project, :activity, :user, {:issue => :tracker}],
81 81 :conditions => cond.conditions,
82 82 :order => "#{TimeEntry.table_name}.created_on DESC",
83 83 :limit => Setting.feeds_limit.to_i)
84 84 render_feed(entries, :title => l(:label_spent_time))
85 85 }
86 86 format.csv {
87 87 # Export all entries
88 88 @entries = TimeEntry.visible.find(:all,
89 89 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
90 90 :conditions => cond.conditions,
91 91 :order => sort_clause)
92 92 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
93 93 }
94 94 end
95 95 end
96 96
97 97 def show
98 98 respond_to do |format|
99 99 # TODO: Implement html response
100 100 format.html { render :nothing => true, :status => 406 }
101 101 format.api
102 102 end
103 103 end
104 104
105 105 def new
106 106 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
107 107 @time_entry.attributes = params[:time_entry]
108 108
109 109 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
110 110 render :action => 'edit'
111 111 end
112 112
113 113 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
114 114 def create
115 115 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
116 116 @time_entry.attributes = params[:time_entry]
117 117
118 118 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
119 119
120 120 if @time_entry.save
121 121 respond_to do |format|
122 122 format.html {
123 123 flash[:notice] = l(:notice_successful_update)
124 124 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
125 125 }
126 126 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
127 127 end
128 128 else
129 129 respond_to do |format|
130 130 format.html { render :action => 'edit' }
131 131 format.api { render_validation_errors(@time_entry) }
132 132 end
133 133 end
134 134 end
135 135
136 136 def edit
137 137 @time_entry.attributes = params[:time_entry]
138 138
139 139 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
140 140 end
141 141
142 142 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
143 143 def update
144 144 @time_entry.attributes = params[:time_entry]
145 145
146 146 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
147 147
148 148 if @time_entry.save
149 149 respond_to do |format|
150 150 format.html {
151 151 flash[:notice] = l(:notice_successful_update)
152 152 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
153 153 }
154 154 format.api { head :ok }
155 155 end
156 156 else
157 157 respond_to do |format|
158 158 format.html { render :action => 'edit' }
159 159 format.api { render_validation_errors(@time_entry) }
160 160 end
161 161 end
162 162 end
163 163
164 164 def bulk_edit
165 165 @available_activities = TimeEntryActivity.shared.active
166 166 @custom_fields = TimeEntry.first.available_custom_fields
167 167 end
168 168
169 169 def bulk_update
170 170 attributes = parse_params_for_bulk_time_entry_attributes(params)
171 171
172 172 unsaved_time_entry_ids = []
173 173 @time_entries.each do |time_entry|
174 174 time_entry.reload
175 175 time_entry.attributes = attributes
176 176 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
177 177 unless time_entry.save
178 178 # Keep unsaved time_entry ids to display them in flash error
179 179 unsaved_time_entry_ids << time_entry.id
180 180 end
181 181 end
182 182 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
183 183 redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
184 184 end
185 185
186 186 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
187 187 def destroy
188 188 @time_entries.each do |t|
189 189 begin
190 190 unless t.destroy && t.destroyed?
191 191 respond_to do |format|
192 192 format.html {
193 193 flash[:error] = l(:notice_unable_delete_time_entry)
194 194 redirect_to :back
195 195 }
196 196 format.api { render_validation_errors(t) }
197 197 end
198 return
198 199 end
199 200 rescue ::ActionController::RedirectBackError
200 201 redirect_to :action => 'index', :project_id => @projects.first
202 return
201 203 end
202 204 end
203 205
204 206 respond_to do |format|
205 207 format.html {
206 208 flash[:notice] = l(:notice_successful_delete)
207 209 redirect_back_or_default(:action => 'index', :project_id => @projects.first)
208 210 }
209 211 format.api { head :ok }
210 212 end
211 213 end
212 214
213 215 private
214 216 def find_time_entry
215 217 @time_entry = TimeEntry.find(params[:id])
216 218 unless @time_entry.editable_by?(User.current)
217 219 render_403
218 220 return false
219 221 end
220 222 @project = @time_entry.project
221 223 rescue ActiveRecord::RecordNotFound
222 224 render_404
223 225 end
224 226
225 227 def find_time_entries
226 228 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
227 229 raise ActiveRecord::RecordNotFound if @time_entries.empty?
228 230 @projects = @time_entries.collect(&:project).compact.uniq
229 231 @project = @projects.first if @projects.size == 1
230 232 rescue ActiveRecord::RecordNotFound
231 233 render_404
232 234 end
233 235
234 236 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
235 237 if unsaved_time_entry_ids.empty?
236 238 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
237 239 else
238 240 flash[:error] = l(:notice_failed_to_save_time_entries,
239 241 :count => unsaved_time_entry_ids.size,
240 242 :total => time_entries.size,
241 243 :ids => '#' + unsaved_time_entry_ids.join(', #'))
242 244 end
243 245 end
244 246
245 247 def find_project
246 248 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
247 249 @issue = Issue.find(issue_id)
248 250 @project = @issue.project
249 251 elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
250 252 @project = Project.find(project_id)
251 253 else
252 254 render_404
253 255 return false
254 256 end
255 257 rescue ActiveRecord::RecordNotFound
256 258 render_404
257 259 end
258 260
259 261 def find_optional_project
260 262 if !params[:issue_id].blank?
261 263 @issue = Issue.find(params[:issue_id])
262 264 @project = @issue.project
263 265 elsif !params[:project_id].blank?
264 266 @project = Project.find(params[:project_id])
265 267 end
266 268 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
267 269 end
268 270
269 271 # Retrieves the date range based on predefined ranges or specific from/to param dates
270 272 def retrieve_date_range
271 273 @free_period = false
272 274 @from, @to = nil, nil
273 275
274 276 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
275 277 case params[:period].to_s
276 278 when 'today'
277 279 @from = @to = Date.today
278 280 when 'yesterday'
279 281 @from = @to = Date.today - 1
280 282 when 'current_week'
281 283 @from = Date.today - (Date.today.cwday - 1)%7
282 284 @to = @from + 6
283 285 when 'last_week'
284 286 @from = Date.today - 7 - (Date.today.cwday - 1)%7
285 287 @to = @from + 6
286 288 when '7_days'
287 289 @from = Date.today - 7
288 290 @to = Date.today
289 291 when 'current_month'
290 292 @from = Date.civil(Date.today.year, Date.today.month, 1)
291 293 @to = (@from >> 1) - 1
292 294 when 'last_month'
293 295 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
294 296 @to = (@from >> 1) - 1
295 297 when '30_days'
296 298 @from = Date.today - 30
297 299 @to = Date.today
298 300 when 'current_year'
299 301 @from = Date.civil(Date.today.year, 1, 1)
300 302 @to = Date.civil(Date.today.year, 12, 31)
301 303 end
302 304 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
303 305 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
304 306 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
305 307 @free_period = true
306 308 else
307 309 # default
308 310 end
309 311
310 312 @from, @to = @to, @from if @from && @to && @from > @to
311 313 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
312 314 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
313 315 end
314 316
315 317 def parse_params_for_bulk_time_entry_attributes(params)
316 318 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
317 319 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
318 320 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
319 321 attributes
320 322 end
321 323 end
General Comments 0
You need to be logged in to leave comments. Login now