##// END OF EJS Templates
Prevents NoMethodError when requesting /time_entries/edit without an id (#6904)....
Jean-Philippe Lang -
r4296:3ba3c540fbb2
parent child
Show More
@@ -1,220 +1,226
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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, :authorize, :only => [:new, :create, :edit, :update, :destroy]
20 before_filter :find_project, :only => [:new, :create]
21 before_filter :find_time_entry, :only => [:edit, :update, :destroy]
22 before_filter :authorize, :except => [:index]
21 before_filter :find_optional_project, :only => [:index]
23 before_filter :find_optional_project, :only => [:index]
22
24
23 helper :sort
25 helper :sort
24 include SortHelper
26 include SortHelper
25 helper :issues
27 helper :issues
26 include TimelogHelper
28 include TimelogHelper
27 helper :custom_fields
29 helper :custom_fields
28 include CustomFieldsHelper
30 include CustomFieldsHelper
29
31
30 def index
32 def index
31 sort_init 'spent_on', 'desc'
33 sort_init 'spent_on', 'desc'
32 sort_update 'spent_on' => 'spent_on',
34 sort_update 'spent_on' => 'spent_on',
33 'user' => 'user_id',
35 'user' => 'user_id',
34 'activity' => 'activity_id',
36 'activity' => 'activity_id',
35 'project' => "#{Project.table_name}.name",
37 'project' => "#{Project.table_name}.name",
36 'issue' => 'issue_id',
38 'issue' => 'issue_id',
37 'hours' => 'hours'
39 'hours' => 'hours'
38
40
39 cond = ARCondition.new
41 cond = ARCondition.new
40 if @project.nil?
42 if @project.nil?
41 cond << Project.allowed_to_condition(User.current, :view_time_entries)
43 cond << Project.allowed_to_condition(User.current, :view_time_entries)
42 elsif @issue.nil?
44 elsif @issue.nil?
43 cond << @project.project_condition(Setting.display_subprojects_issues?)
45 cond << @project.project_condition(Setting.display_subprojects_issues?)
44 else
46 else
45 cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
47 cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}"
46 end
48 end
47
49
48 retrieve_date_range
50 retrieve_date_range
49 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
51 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
50
52
51 TimeEntry.visible_by(User.current) do
53 TimeEntry.visible_by(User.current) do
52 respond_to do |format|
54 respond_to do |format|
53 format.html {
55 format.html {
54 # Paginate results
56 # Paginate results
55 @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
57 @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
56 @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']
57 @entries = TimeEntry.find(:all,
59 @entries = TimeEntry.find(:all,
58 :include => [:project, :activity, :user, {:issue => :tracker}],
60 :include => [:project, :activity, :user, {:issue => :tracker}],
59 :conditions => cond.conditions,
61 :conditions => cond.conditions,
60 :order => sort_clause,
62 :order => sort_clause,
61 :limit => @entry_pages.items_per_page,
63 :limit => @entry_pages.items_per_page,
62 :offset => @entry_pages.current.offset)
64 :offset => @entry_pages.current.offset)
63 @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
65 @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
64
66
65 render :layout => !request.xhr?
67 render :layout => !request.xhr?
66 }
68 }
67 format.atom {
69 format.atom {
68 entries = TimeEntry.find(:all,
70 entries = TimeEntry.find(:all,
69 :include => [:project, :activity, :user, {:issue => :tracker}],
71 :include => [:project, :activity, :user, {:issue => :tracker}],
70 :conditions => cond.conditions,
72 :conditions => cond.conditions,
71 :order => "#{TimeEntry.table_name}.created_on DESC",
73 :order => "#{TimeEntry.table_name}.created_on DESC",
72 :limit => Setting.feeds_limit.to_i)
74 :limit => Setting.feeds_limit.to_i)
73 render_feed(entries, :title => l(:label_spent_time))
75 render_feed(entries, :title => l(:label_spent_time))
74 }
76 }
75 format.csv {
77 format.csv {
76 # Export all entries
78 # Export all entries
77 @entries = TimeEntry.find(:all,
79 @entries = TimeEntry.find(:all,
78 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
80 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
79 :conditions => cond.conditions,
81 :conditions => cond.conditions,
80 :order => sort_clause)
82 :order => sort_clause)
81 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
83 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
82 }
84 }
83 end
85 end
84 end
86 end
85 end
87 end
86
88
87 def new
89 def new
88 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
90 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
89 @time_entry.attributes = params[:time_entry]
91 @time_entry.attributes = params[:time_entry]
90
92
91 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
93 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
92 render :action => 'edit'
94 render :action => 'edit'
93 end
95 end
94
96
95 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
97 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
96 def create
98 def create
97 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
99 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
98 @time_entry.attributes = params[:time_entry]
100 @time_entry.attributes = params[:time_entry]
99
101
100 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
102 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
101
103
102 if @time_entry.save
104 if @time_entry.save
103 flash[:notice] = l(:notice_successful_update)
105 flash[:notice] = l(:notice_successful_update)
104 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
106 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
105 else
107 else
106 render :action => 'edit'
108 render :action => 'edit'
107 end
109 end
108 end
110 end
109
111
110 def edit
112 def edit
111 (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current)
112 @time_entry.attributes = params[:time_entry]
113 @time_entry.attributes = params[:time_entry]
113
114
114 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
115 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
115 end
116 end
116
117
117 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
118 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
118 def update
119 def update
119 (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current)
120 @time_entry.attributes = params[:time_entry]
120 @time_entry.attributes = params[:time_entry]
121
121
122 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
122 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
123
123
124 if @time_entry.save
124 if @time_entry.save
125 flash[:notice] = l(:notice_successful_update)
125 flash[:notice] = l(:notice_successful_update)
126 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
126 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
127 else
127 else
128 render :action => 'edit'
128 render :action => 'edit'
129 end
129 end
130 end
130 end
131
131
132 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
132 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
133 def destroy
133 def destroy
134 (render_404; return) unless @time_entry
135 (render_403; return) unless @time_entry.editable_by?(User.current)
136 if @time_entry.destroy && @time_entry.destroyed?
134 if @time_entry.destroy && @time_entry.destroyed?
137 flash[:notice] = l(:notice_successful_delete)
135 flash[:notice] = l(:notice_successful_delete)
138 else
136 else
139 flash[:error] = l(:notice_unable_delete_time_entry)
137 flash[:error] = l(:notice_unable_delete_time_entry)
140 end
138 end
141 redirect_to :back
139 redirect_to :back
142 rescue ::ActionController::RedirectBackError
140 rescue ::ActionController::RedirectBackError
143 redirect_to :action => 'index', :project_id => @time_entry.project
141 redirect_to :action => 'index', :project_id => @time_entry.project
144 end
142 end
145
143
146 private
144 private
145 def find_time_entry
146 @time_entry = TimeEntry.find(params[:id])
147 unless @time_entry.editable_by?(User.current)
148 render_403
149 return false
150 end
151 @project = @time_entry.project
152 rescue ActiveRecord::RecordNotFound
153 render_404
154 end
155
147 def find_project
156 def find_project
148 if params[:id]
157 if params[:issue_id]
149 @time_entry = TimeEntry.find(params[:id])
150 @project = @time_entry.project
151 elsif params[:issue_id]
152 @issue = Issue.find(params[:issue_id])
158 @issue = Issue.find(params[:issue_id])
153 @project = @issue.project
159 @project = @issue.project
154 elsif params[:project_id]
160 elsif params[:project_id]
155 @project = Project.find(params[:project_id])
161 @project = Project.find(params[:project_id])
156 else
162 else
157 render_404
163 render_404
158 return false
164 return false
159 end
165 end
160 rescue ActiveRecord::RecordNotFound
166 rescue ActiveRecord::RecordNotFound
161 render_404
167 render_404
162 end
168 end
163
169
164 def find_optional_project
170 def find_optional_project
165 if !params[:issue_id].blank?
171 if !params[:issue_id].blank?
166 @issue = Issue.find(params[:issue_id])
172 @issue = Issue.find(params[:issue_id])
167 @project = @issue.project
173 @project = @issue.project
168 elsif !params[:project_id].blank?
174 elsif !params[:project_id].blank?
169 @project = Project.find(params[:project_id])
175 @project = Project.find(params[:project_id])
170 end
176 end
171 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
177 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
172 end
178 end
173
179
174 # Retrieves the date range based on predefined ranges or specific from/to param dates
180 # Retrieves the date range based on predefined ranges or specific from/to param dates
175 def retrieve_date_range
181 def retrieve_date_range
176 @free_period = false
182 @free_period = false
177 @from, @to = nil, nil
183 @from, @to = nil, nil
178
184
179 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
185 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
180 case params[:period].to_s
186 case params[:period].to_s
181 when 'today'
187 when 'today'
182 @from = @to = Date.today
188 @from = @to = Date.today
183 when 'yesterday'
189 when 'yesterday'
184 @from = @to = Date.today - 1
190 @from = @to = Date.today - 1
185 when 'current_week'
191 when 'current_week'
186 @from = Date.today - (Date.today.cwday - 1)%7
192 @from = Date.today - (Date.today.cwday - 1)%7
187 @to = @from + 6
193 @to = @from + 6
188 when 'last_week'
194 when 'last_week'
189 @from = Date.today - 7 - (Date.today.cwday - 1)%7
195 @from = Date.today - 7 - (Date.today.cwday - 1)%7
190 @to = @from + 6
196 @to = @from + 6
191 when '7_days'
197 when '7_days'
192 @from = Date.today - 7
198 @from = Date.today - 7
193 @to = Date.today
199 @to = Date.today
194 when 'current_month'
200 when 'current_month'
195 @from = Date.civil(Date.today.year, Date.today.month, 1)
201 @from = Date.civil(Date.today.year, Date.today.month, 1)
196 @to = (@from >> 1) - 1
202 @to = (@from >> 1) - 1
197 when 'last_month'
203 when 'last_month'
198 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
204 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
199 @to = (@from >> 1) - 1
205 @to = (@from >> 1) - 1
200 when '30_days'
206 when '30_days'
201 @from = Date.today - 30
207 @from = Date.today - 30
202 @to = Date.today
208 @to = Date.today
203 when 'current_year'
209 when 'current_year'
204 @from = Date.civil(Date.today.year, 1, 1)
210 @from = Date.civil(Date.today.year, 1, 1)
205 @to = Date.civil(Date.today.year, 12, 31)
211 @to = Date.civil(Date.today.year, 12, 31)
206 end
212 end
207 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
213 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
208 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
214 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
209 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
215 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
210 @free_period = true
216 @free_period = true
211 else
217 else
212 # default
218 # default
213 end
219 end
214
220
215 @from, @to = @to, @from if @from && @to && @from > @to
221 @from, @to = @to, @from if @from && @to && @from > @to
216 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
222 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
217 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
223 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
218 end
224 end
219
225
220 end
226 end
General Comments 0
You need to be logged in to leave comments. Login now