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