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