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