##// END OF EJS Templates
Backported r9136 from trunk....
Jean-Philippe Lang -
r9032:5475e0dcc5d3
parent child
Show More
@@ -1,324 +1,324
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]
23 before_filter :authorize, :except => [:index]
24 before_filter :find_optional_project, :only => [:index]
24 before_filter :find_optional_project, :only => [:index]
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 cond = ARCondition.new
44 cond = ARCondition.new
45 if @issue
45 if @issue
46 cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
46 cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
47 elsif @project
47 elsif @project
48 cond << @project.project_condition(Setting.display_subprojects_issues?)
48 cond << @project.project_condition(Setting.display_subprojects_issues?)
49 end
49 end
50
50
51 retrieve_date_range
51 retrieve_date_range
52 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
52 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
53
53
54 respond_to do |format|
54 respond_to do |format|
55 format.html {
55 format.html {
56 # Paginate results
56 # Paginate results
57 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
57 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
58 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
58 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
59 @entries = TimeEntry.visible.find(:all,
59 @entries = TimeEntry.visible.find(:all,
60 :include => [:project, :activity, :user, {:issue => :tracker}],
60 :include => [:project, :activity, :user, {:issue => :tracker}],
61 :conditions => cond.conditions,
61 :conditions => cond.conditions,
62 :order => sort_clause,
62 :order => sort_clause,
63 :limit => @entry_pages.items_per_page,
63 :limit => @entry_pages.items_per_page,
64 :offset => @entry_pages.current.offset)
64 :offset => @entry_pages.current.offset)
65 @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
65 @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
66
66
67 render :layout => !request.xhr?
67 render :layout => !request.xhr?
68 }
68 }
69 format.api {
69 format.api {
70 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
70 @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions)
71 @offset, @limit = api_offset_and_limit
71 @offset, @limit = api_offset_and_limit
72 @entries = TimeEntry.visible.find(:all,
72 @entries = TimeEntry.visible.find(:all,
73 :include => [:project, :activity, :user, {:issue => :tracker}],
73 :include => [:project, :activity, :user, {:issue => :tracker}],
74 :conditions => cond.conditions,
74 :conditions => cond.conditions,
75 :order => sort_clause,
75 :order => sort_clause,
76 :limit => @limit,
76 :limit => @limit,
77 :offset => @offset)
77 :offset => @offset)
78 }
78 }
79 format.atom {
79 format.atom {
80 entries = TimeEntry.visible.find(:all,
80 entries = TimeEntry.visible.find(:all,
81 :include => [:project, :activity, :user, {:issue => :tracker}],
81 :include => [:project, :activity, :user, {:issue => :tracker}],
82 :conditions => cond.conditions,
82 :conditions => cond.conditions,
83 :order => "#{TimeEntry.table_name}.created_on DESC",
83 :order => "#{TimeEntry.table_name}.created_on DESC",
84 :limit => Setting.feeds_limit.to_i)
84 :limit => Setting.feeds_limit.to_i)
85 render_feed(entries, :title => l(:label_spent_time))
85 render_feed(entries, :title => l(:label_spent_time))
86 }
86 }
87 format.csv {
87 format.csv {
88 # Export all entries
88 # Export all entries
89 @entries = TimeEntry.visible.find(:all,
89 @entries = TimeEntry.visible.find(:all,
90 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
90 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
91 :conditions => cond.conditions,
91 :conditions => cond.conditions,
92 :order => sort_clause)
92 :order => sort_clause)
93 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
93 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
94 }
94 }
95 end
95 end
96 end
96 end
97
97
98 def show
98 def show
99 respond_to do |format|
99 respond_to do |format|
100 # TODO: Implement html response
100 # TODO: Implement html response
101 format.html { render :nothing => true, :status => 406 }
101 format.html { render :nothing => true, :status => 406 }
102 format.api
102 format.api
103 end
103 end
104 end
104 end
105
105
106 def new
106 def new
107 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
107 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
108 @time_entry.attributes = params[:time_entry]
108 @time_entry.safe_attributes = params[:time_entry]
109
109
110 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
110 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
111 render :action => 'edit'
111 render :action => 'edit'
112 end
112 end
113
113
114 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
114 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
115 def create
115 def create
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.safe_attributes = params[:time_entry]
118
118
119 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
119 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
120
120
121 if @time_entry.save
121 if @time_entry.save
122 respond_to do |format|
122 respond_to do |format|
123 format.html {
123 format.html {
124 flash[:notice] = l(:notice_successful_update)
124 flash[:notice] = l(:notice_successful_update)
125 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
125 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
126 }
126 }
127 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
127 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
128 end
128 end
129 else
129 else
130 respond_to do |format|
130 respond_to do |format|
131 format.html { render :action => 'edit' }
131 format.html { render :action => 'edit' }
132 format.api { render_validation_errors(@time_entry) }
132 format.api { render_validation_errors(@time_entry) }
133 end
133 end
134 end
134 end
135 end
135 end
136
136
137 def edit
137 def edit
138 @time_entry.attributes = params[:time_entry]
138 @time_entry.safe_attributes = params[:time_entry]
139
139
140 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
140 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
141 end
141 end
142
142
143 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
143 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
144 def update
144 def update
145 @time_entry.attributes = params[:time_entry]
145 @time_entry.safe_attributes = params[:time_entry]
146
146
147 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 })
148
148
149 if @time_entry.save
149 if @time_entry.save
150 respond_to do |format|
150 respond_to do |format|
151 format.html {
151 format.html {
152 flash[:notice] = l(:notice_successful_update)
152 flash[:notice] = l(:notice_successful_update)
153 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
153 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
154 }
154 }
155 format.api { head :ok }
155 format.api { head :ok }
156 end
156 end
157 else
157 else
158 respond_to do |format|
158 respond_to do |format|
159 format.html { render :action => 'edit' }
159 format.html { render :action => 'edit' }
160 format.api { render_validation_errors(@time_entry) }
160 format.api { render_validation_errors(@time_entry) }
161 end
161 end
162 end
162 end
163 end
163 end
164
164
165 def bulk_edit
165 def bulk_edit
166 @available_activities = TimeEntryActivity.shared.active
166 @available_activities = TimeEntryActivity.shared.active
167 @custom_fields = TimeEntry.first.available_custom_fields
167 @custom_fields = TimeEntry.first.available_custom_fields
168 end
168 end
169
169
170 def bulk_update
170 def bulk_update
171 attributes = parse_params_for_bulk_time_entry_attributes(params)
171 attributes = parse_params_for_bulk_time_entry_attributes(params)
172
172
173 unsaved_time_entry_ids = []
173 unsaved_time_entry_ids = []
174 @time_entries.each do |time_entry|
174 @time_entries.each do |time_entry|
175 time_entry.reload
175 time_entry.reload
176 time_entry.attributes = attributes
176 time_entry.safe_attributes = attributes
177 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
177 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
178 unless time_entry.save
178 unless time_entry.save
179 # Keep unsaved time_entry ids to display them in flash error
179 # Keep unsaved time_entry ids to display them in flash error
180 unsaved_time_entry_ids << time_entry.id
180 unsaved_time_entry_ids << time_entry.id
181 end
181 end
182 end
182 end
183 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
183 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
184 redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
184 redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
185 end
185 end
186
186
187 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
187 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
188 def destroy
188 def destroy
189 @time_entries.each do |t|
189 @time_entries.each do |t|
190 begin
190 begin
191 unless t.destroy && t.destroyed?
191 unless t.destroy && t.destroyed?
192 respond_to do |format|
192 respond_to do |format|
193 format.html {
193 format.html {
194 flash[:error] = l(:notice_unable_delete_time_entry)
194 flash[:error] = l(:notice_unable_delete_time_entry)
195 redirect_to :back
195 redirect_to :back
196 }
196 }
197 format.api { render_validation_errors(t) }
197 format.api { render_validation_errors(t) }
198 end
198 end
199 return
199 return
200 end
200 end
201 rescue ::ActionController::RedirectBackError
201 rescue ::ActionController::RedirectBackError
202 redirect_to :action => 'index', :project_id => @projects.first
202 redirect_to :action => 'index', :project_id => @projects.first
203 return
203 return
204 end
204 end
205 end
205 end
206
206
207 respond_to do |format|
207 respond_to do |format|
208 format.html {
208 format.html {
209 flash[:notice] = l(:notice_successful_delete)
209 flash[:notice] = l(:notice_successful_delete)
210 redirect_back_or_default(:action => 'index', :project_id => @projects.first)
210 redirect_back_or_default(:action => 'index', :project_id => @projects.first)
211 }
211 }
212 format.api { head :ok }
212 format.api { head :ok }
213 end
213 end
214 end
214 end
215
215
216 private
216 private
217 def find_time_entry
217 def find_time_entry
218 @time_entry = TimeEntry.find(params[:id])
218 @time_entry = TimeEntry.find(params[:id])
219 unless @time_entry.editable_by?(User.current)
219 unless @time_entry.editable_by?(User.current)
220 render_403
220 render_403
221 return false
221 return false
222 end
222 end
223 @project = @time_entry.project
223 @project = @time_entry.project
224 rescue ActiveRecord::RecordNotFound
224 rescue ActiveRecord::RecordNotFound
225 render_404
225 render_404
226 end
226 end
227
227
228 def find_time_entries
228 def find_time_entries
229 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
229 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
230 raise ActiveRecord::RecordNotFound if @time_entries.empty?
230 raise ActiveRecord::RecordNotFound if @time_entries.empty?
231 @projects = @time_entries.collect(&:project).compact.uniq
231 @projects = @time_entries.collect(&:project).compact.uniq
232 @project = @projects.first if @projects.size == 1
232 @project = @projects.first if @projects.size == 1
233 rescue ActiveRecord::RecordNotFound
233 rescue ActiveRecord::RecordNotFound
234 render_404
234 render_404
235 end
235 end
236
236
237 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
237 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
238 if unsaved_time_entry_ids.empty?
238 if unsaved_time_entry_ids.empty?
239 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
239 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
240 else
240 else
241 flash[:error] = l(:notice_failed_to_save_time_entries,
241 flash[:error] = l(:notice_failed_to_save_time_entries,
242 :count => unsaved_time_entry_ids.size,
242 :count => unsaved_time_entry_ids.size,
243 :total => time_entries.size,
243 :total => time_entries.size,
244 :ids => '#' + unsaved_time_entry_ids.join(', #'))
244 :ids => '#' + unsaved_time_entry_ids.join(', #'))
245 end
245 end
246 end
246 end
247
247
248 def find_project
248 def find_project
249 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
249 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
250 @issue = Issue.find(issue_id)
250 @issue = Issue.find(issue_id)
251 @project = @issue.project
251 @project = @issue.project
252 elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
252 elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
253 @project = Project.find(project_id)
253 @project = Project.find(project_id)
254 else
254 else
255 render_404
255 render_404
256 return false
256 return false
257 end
257 end
258 rescue ActiveRecord::RecordNotFound
258 rescue ActiveRecord::RecordNotFound
259 render_404
259 render_404
260 end
260 end
261
261
262 def find_optional_project
262 def find_optional_project
263 if !params[:issue_id].blank?
263 if !params[:issue_id].blank?
264 @issue = Issue.find(params[:issue_id])
264 @issue = Issue.find(params[:issue_id])
265 @project = @issue.project
265 @project = @issue.project
266 elsif !params[:project_id].blank?
266 elsif !params[:project_id].blank?
267 @project = Project.find(params[:project_id])
267 @project = Project.find(params[:project_id])
268 end
268 end
269 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
269 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
270 end
270 end
271
271
272 # Retrieves the date range based on predefined ranges or specific from/to param dates
272 # Retrieves the date range based on predefined ranges or specific from/to param dates
273 def retrieve_date_range
273 def retrieve_date_range
274 @free_period = false
274 @free_period = false
275 @from, @to = nil, nil
275 @from, @to = nil, nil
276
276
277 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
277 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
278 case params[:period].to_s
278 case params[:period].to_s
279 when 'today'
279 when 'today'
280 @from = @to = Date.today
280 @from = @to = Date.today
281 when 'yesterday'
281 when 'yesterday'
282 @from = @to = Date.today - 1
282 @from = @to = Date.today - 1
283 when 'current_week'
283 when 'current_week'
284 @from = Date.today - (Date.today.cwday - 1)%7
284 @from = Date.today - (Date.today.cwday - 1)%7
285 @to = @from + 6
285 @to = @from + 6
286 when 'last_week'
286 when 'last_week'
287 @from = Date.today - 7 - (Date.today.cwday - 1)%7
287 @from = Date.today - 7 - (Date.today.cwday - 1)%7
288 @to = @from + 6
288 @to = @from + 6
289 when '7_days'
289 when '7_days'
290 @from = Date.today - 7
290 @from = Date.today - 7
291 @to = Date.today
291 @to = Date.today
292 when 'current_month'
292 when 'current_month'
293 @from = Date.civil(Date.today.year, Date.today.month, 1)
293 @from = Date.civil(Date.today.year, Date.today.month, 1)
294 @to = (@from >> 1) - 1
294 @to = (@from >> 1) - 1
295 when 'last_month'
295 when 'last_month'
296 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
296 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
297 @to = (@from >> 1) - 1
297 @to = (@from >> 1) - 1
298 when '30_days'
298 when '30_days'
299 @from = Date.today - 30
299 @from = Date.today - 30
300 @to = Date.today
300 @to = Date.today
301 when 'current_year'
301 when 'current_year'
302 @from = Date.civil(Date.today.year, 1, 1)
302 @from = Date.civil(Date.today.year, 1, 1)
303 @to = Date.civil(Date.today.year, 12, 31)
303 @to = Date.civil(Date.today.year, 12, 31)
304 end
304 end
305 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
305 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
306 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
306 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
307 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
307 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
308 @free_period = true
308 @free_period = true
309 else
309 else
310 # default
310 # default
311 end
311 end
312
312
313 @from, @to = @to, @from if @from && @to && @from > @to
313 @from, @to = @to, @from if @from && @to && @from > @to
314 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
314 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
315 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
315 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
316 end
316 end
317
317
318 def parse_params_for_bulk_time_entry_attributes(params)
318 def parse_params_for_bulk_time_entry_attributes(params)
319 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
319 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
320 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
320 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
321 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
321 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
322 attributes
322 attributes
323 end
323 end
324 end
324 end
@@ -1,104 +1,107
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 TimeEntry < ActiveRecord::Base
18 class TimeEntry < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 # could have used polymorphic association
20 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
21 # project association here allows easy loading of time entries at project level with one database trip
21 belongs_to :project
22 belongs_to :project
22 belongs_to :issue
23 belongs_to :issue
23 belongs_to :user
24 belongs_to :user
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
25 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
25
26
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
28
28 acts_as_customizable
29 acts_as_customizable
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
31 :author => :user,
32 :author => :user,
32 :description => :comments
33 :description => :comments
33
34
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
35 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
35 :author_key => :user_id,
36 :author_key => :user_id,
36 :find_options => {:include => :project}
37 :find_options => {:include => :project}
37
38
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
39 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
40 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
41 validates_length_of :comments, :maximum => 255, :allow_nil => true
41 before_validation :set_project_if_nil
42 before_validation :set_project_if_nil
42 validate :validate_time_entry
43 validate :validate_time_entry
43
44
44 named_scope :visible, lambda {|*args| {
45 named_scope :visible, lambda {|*args| {
45 :include => :project,
46 :include => :project,
46 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
47 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
47 }}
48 }}
48
49
50 safe_attributes 'hours', 'comments', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values'
51
49 def after_initialize
52 def after_initialize
50 if new_record? && self.activity.nil?
53 if new_record? && self.activity.nil?
51 if default_activity = TimeEntryActivity.default
54 if default_activity = TimeEntryActivity.default
52 self.activity_id = default_activity.id
55 self.activity_id = default_activity.id
53 end
56 end
54 self.hours = nil if hours == 0
57 self.hours = nil if hours == 0
55 end
58 end
56 end
59 end
57
60
58 def set_project_if_nil
61 def set_project_if_nil
59 self.project = issue.project if issue && project.nil?
62 self.project = issue.project if issue && project.nil?
60 end
63 end
61
64
62 def validate_time_entry
65 def validate_time_entry
63 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
66 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
64 errors.add :project_id, :invalid if project.nil?
67 errors.add :project_id, :invalid if project.nil?
65 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
68 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
66 end
69 end
67
70
68 def hours=(h)
71 def hours=(h)
69 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
72 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
70 end
73 end
71
74
72 # tyear, tmonth, tweek assigned where setting spent_on attributes
75 # tyear, tmonth, tweek assigned where setting spent_on attributes
73 # these attributes make time aggregations easier
76 # these attributes make time aggregations easier
74 def spent_on=(date)
77 def spent_on=(date)
75 super
78 super
76 if spent_on.is_a?(Time)
79 if spent_on.is_a?(Time)
77 self.spent_on = spent_on.to_date
80 self.spent_on = spent_on.to_date
78 end
81 end
79 self.tyear = spent_on ? spent_on.year : nil
82 self.tyear = spent_on ? spent_on.year : nil
80 self.tmonth = spent_on ? spent_on.month : nil
83 self.tmonth = spent_on ? spent_on.month : nil
81 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
84 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
82 end
85 end
83
86
84 # Returns true if the time entry can be edited by usr, otherwise false
87 # Returns true if the time entry can be edited by usr, otherwise false
85 def editable_by?(usr)
88 def editable_by?(usr)
86 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
89 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
87 end
90 end
88
91
89 def self.earilest_date_for_project(project=nil)
92 def self.earilest_date_for_project(project=nil)
90 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
93 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
91 if project
94 if project
92 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
95 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
93 end
96 end
94 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
97 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
95 end
98 end
96
99
97 def self.latest_date_for_project(project=nil)
100 def self.latest_date_for_project(project=nil)
98 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
101 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
99 if project
102 if project
100 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
103 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
101 end
104 end
102 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
105 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
103 end
106 end
104 end
107 end
General Comments 0
You need to be logged in to leave comments. Login now