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