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