##// END OF EJS Templates
Do not call _before_save hook without saving....
Jean-Philippe Lang -
r8475:919e686e93ec
parent child
Show More
@@ -1,331 +1,329
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
119 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
120 118 render :action => 'edit'
121 119 end
122 120
123 121 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
124 122 def create
125 123 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
126 124 @time_entry.attributes = params[:time_entry]
127 125
128 126 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
129 127
130 128 if @time_entry.save
131 129 respond_to do |format|
132 130 format.html {
133 131 flash[:notice] = l(:notice_successful_update)
134 132 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
135 133 }
136 134 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
137 135 end
138 136 else
139 137 respond_to do |format|
140 138 format.html { render :action => 'edit' }
141 139 format.api { render_validation_errors(@time_entry) }
142 140 end
143 141 end
144 142 end
145 143
146 144 def edit
147 145 @time_entry.attributes = params[:time_entry]
148 146
149 147 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
150 148 end
151 149
152 150 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
153 151 def update
154 152 @time_entry.attributes = params[:time_entry]
155 153
156 154 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
157 155
158 156 if @time_entry.save
159 157 respond_to do |format|
160 158 format.html {
161 159 flash[:notice] = l(:notice_successful_update)
162 160 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
163 161 }
164 162 format.api { head :ok }
165 163 end
166 164 else
167 165 respond_to do |format|
168 166 format.html { render :action => 'edit' }
169 167 format.api { render_validation_errors(@time_entry) }
170 168 end
171 169 end
172 170 end
173 171
174 172 def bulk_edit
175 173 @available_activities = TimeEntryActivity.shared.active
176 174 @custom_fields = TimeEntry.first.available_custom_fields
177 175 end
178 176
179 177 def bulk_update
180 178 attributes = parse_params_for_bulk_time_entry_attributes(params)
181 179
182 180 unsaved_time_entry_ids = []
183 181 @time_entries.each do |time_entry|
184 182 time_entry.reload
185 183 time_entry.attributes = attributes
186 184 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
187 185 unless time_entry.save
188 186 # Keep unsaved time_entry ids to display them in flash error
189 187 unsaved_time_entry_ids << time_entry.id
190 188 end
191 189 end
192 190 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
193 191 redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
194 192 end
195 193
196 194 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
197 195 def destroy
198 196 @time_entries.each do |t|
199 197 begin
200 198 unless t.destroy && t.destroyed?
201 199 respond_to do |format|
202 200 format.html {
203 201 flash[:error] = l(:notice_unable_delete_time_entry)
204 202 redirect_to :back
205 203 }
206 204 format.api { render_validation_errors(t) }
207 205 end
208 206 return
209 207 end
210 208 rescue ::ActionController::RedirectBackError
211 209 redirect_to :action => 'index', :project_id => @projects.first
212 210 return
213 211 end
214 212 end
215 213
216 214 respond_to do |format|
217 215 format.html {
218 216 flash[:notice] = l(:notice_successful_delete)
219 217 redirect_back_or_default(:action => 'index', :project_id => @projects.first)
220 218 }
221 219 format.api { head :ok }
222 220 end
223 221 end
224 222
225 223 private
226 224 def find_time_entry
227 225 @time_entry = TimeEntry.find(params[:id])
228 226 unless @time_entry.editable_by?(User.current)
229 227 render_403
230 228 return false
231 229 end
232 230 @project = @time_entry.project
233 231 rescue ActiveRecord::RecordNotFound
234 232 render_404
235 233 end
236 234
237 235 def find_time_entries
238 236 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
239 237 raise ActiveRecord::RecordNotFound if @time_entries.empty?
240 238 @projects = @time_entries.collect(&:project).compact.uniq
241 239 @project = @projects.first if @projects.size == 1
242 240 rescue ActiveRecord::RecordNotFound
243 241 render_404
244 242 end
245 243
246 244 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
247 245 if unsaved_time_entry_ids.empty?
248 246 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
249 247 else
250 248 flash[:error] = l(:notice_failed_to_save_time_entries,
251 249 :count => unsaved_time_entry_ids.size,
252 250 :total => time_entries.size,
253 251 :ids => '#' + unsaved_time_entry_ids.join(', #'))
254 252 end
255 253 end
256 254
257 255 def find_project
258 256 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
259 257 @issue = Issue.find(issue_id)
260 258 @project = @issue.project
261 259 elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
262 260 @project = Project.find(project_id)
263 261 else
264 262 render_404
265 263 return false
266 264 end
267 265 rescue ActiveRecord::RecordNotFound
268 266 render_404
269 267 end
270 268
271 269 def find_optional_project
272 270 if !params[:issue_id].blank?
273 271 @issue = Issue.find(params[:issue_id])
274 272 @project = @issue.project
275 273 elsif !params[:project_id].blank?
276 274 @project = Project.find(params[:project_id])
277 275 end
278 276 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
279 277 end
280 278
281 279 # Retrieves the date range based on predefined ranges or specific from/to param dates
282 280 def retrieve_date_range
283 281 @free_period = false
284 282 @from, @to = nil, nil
285 283
286 284 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
287 285 case params[:period].to_s
288 286 when 'today'
289 287 @from = @to = Date.today
290 288 when 'yesterday'
291 289 @from = @to = Date.today - 1
292 290 when 'current_week'
293 291 @from = Date.today - (Date.today.cwday - 1)%7
294 292 @to = @from + 6
295 293 when 'last_week'
296 294 @from = Date.today - 7 - (Date.today.cwday - 1)%7
297 295 @to = @from + 6
298 296 when '7_days'
299 297 @from = Date.today - 7
300 298 @to = Date.today
301 299 when 'current_month'
302 300 @from = Date.civil(Date.today.year, Date.today.month, 1)
303 301 @to = (@from >> 1) - 1
304 302 when 'last_month'
305 303 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
306 304 @to = (@from >> 1) - 1
307 305 when '30_days'
308 306 @from = Date.today - 30
309 307 @to = Date.today
310 308 when 'current_year'
311 309 @from = Date.civil(Date.today.year, 1, 1)
312 310 @to = Date.civil(Date.today.year, 12, 31)
313 311 end
314 312 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
315 313 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
316 314 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
317 315 @free_period = true
318 316 else
319 317 # default
320 318 end
321 319
322 320 @from, @to = @to, @from if @from && @to && @from > @to
323 321 end
324 322
325 323 def parse_params_for_bulk_time_entry_attributes(params)
326 324 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
327 325 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
328 326 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
329 327 attributes
330 328 end
331 329 end
General Comments 0
You need to be logged in to leave comments. Login now