##// 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 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class TimelogController < ApplicationController
19 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 23 before_filter :find_optional_project, :only => [:index]
22 24
23 25 helper :sort
24 26 include SortHelper
25 27 helper :issues
26 28 include TimelogHelper
27 29 helper :custom_fields
28 30 include CustomFieldsHelper
29 31
30 32 def index
31 33 sort_init 'spent_on', 'desc'
32 34 sort_update 'spent_on' => 'spent_on',
33 35 'user' => 'user_id',
34 36 'activity' => 'activity_id',
35 37 'project' => "#{Project.table_name}.name",
36 38 'issue' => 'issue_id',
37 39 'hours' => 'hours'
38 40
39 41 cond = ARCondition.new
40 42 if @project.nil?
41 43 cond << Project.allowed_to_condition(User.current, :view_time_entries)
42 44 elsif @issue.nil?
43 45 cond << @project.project_condition(Setting.display_subprojects_issues?)
44 46 else
45 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 48 end
47 49
48 50 retrieve_date_range
49 51 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
50 52
51 53 TimeEntry.visible_by(User.current) do
52 54 respond_to do |format|
53 55 format.html {
54 56 # Paginate results
55 57 @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions)
56 58 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
57 59 @entries = TimeEntry.find(:all,
58 60 :include => [:project, :activity, :user, {:issue => :tracker}],
59 61 :conditions => cond.conditions,
60 62 :order => sort_clause,
61 63 :limit => @entry_pages.items_per_page,
62 64 :offset => @entry_pages.current.offset)
63 65 @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f
64 66
65 67 render :layout => !request.xhr?
66 68 }
67 69 format.atom {
68 70 entries = TimeEntry.find(:all,
69 71 :include => [:project, :activity, :user, {:issue => :tracker}],
70 72 :conditions => cond.conditions,
71 73 :order => "#{TimeEntry.table_name}.created_on DESC",
72 74 :limit => Setting.feeds_limit.to_i)
73 75 render_feed(entries, :title => l(:label_spent_time))
74 76 }
75 77 format.csv {
76 78 # Export all entries
77 79 @entries = TimeEntry.find(:all,
78 80 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
79 81 :conditions => cond.conditions,
80 82 :order => sort_clause)
81 83 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
82 84 }
83 85 end
84 86 end
85 87 end
86 88
87 89 def new
88 90 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
89 91 @time_entry.attributes = params[:time_entry]
90 92
91 93 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
92 94 render :action => 'edit'
93 95 end
94 96
95 97 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
96 98 def create
97 99 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
98 100 @time_entry.attributes = params[:time_entry]
99 101
100 102 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
101 103
102 104 if @time_entry.save
103 105 flash[:notice] = l(:notice_successful_update)
104 106 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
105 107 else
106 108 render :action => 'edit'
107 109 end
108 110 end
109 111
110 112 def edit
111 (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current)
112 113 @time_entry.attributes = params[:time_entry]
113 114
114 115 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
115 116 end
116 117
117 118 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
118 119 def update
119 (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current)
120 120 @time_entry.attributes = params[:time_entry]
121 121
122 122 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
123 123
124 124 if @time_entry.save
125 125 flash[:notice] = l(:notice_successful_update)
126 126 redirect_back_or_default :action => 'index', :project_id => @time_entry.project
127 127 else
128 128 render :action => 'edit'
129 129 end
130 130 end
131 131
132 132 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
133 133 def destroy
134 (render_404; return) unless @time_entry
135 (render_403; return) unless @time_entry.editable_by?(User.current)
136 134 if @time_entry.destroy && @time_entry.destroyed?
137 135 flash[:notice] = l(:notice_successful_delete)
138 136 else
139 137 flash[:error] = l(:notice_unable_delete_time_entry)
140 138 end
141 139 redirect_to :back
142 140 rescue ::ActionController::RedirectBackError
143 141 redirect_to :action => 'index', :project_id => @time_entry.project
144 142 end
145 143
146 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 156 def find_project
148 if params[:id]
149 @time_entry = TimeEntry.find(params[:id])
150 @project = @time_entry.project
151 elsif params[:issue_id]
157 if params[:issue_id]
152 158 @issue = Issue.find(params[:issue_id])
153 159 @project = @issue.project
154 160 elsif params[:project_id]
155 161 @project = Project.find(params[:project_id])
156 162 else
157 163 render_404
158 164 return false
159 165 end
160 166 rescue ActiveRecord::RecordNotFound
161 167 render_404
162 168 end
163 169
164 170 def find_optional_project
165 171 if !params[:issue_id].blank?
166 172 @issue = Issue.find(params[:issue_id])
167 173 @project = @issue.project
168 174 elsif !params[:project_id].blank?
169 175 @project = Project.find(params[:project_id])
170 176 end
171 177 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
172 178 end
173 179
174 180 # Retrieves the date range based on predefined ranges or specific from/to param dates
175 181 def retrieve_date_range
176 182 @free_period = false
177 183 @from, @to = nil, nil
178 184
179 185 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
180 186 case params[:period].to_s
181 187 when 'today'
182 188 @from = @to = Date.today
183 189 when 'yesterday'
184 190 @from = @to = Date.today - 1
185 191 when 'current_week'
186 192 @from = Date.today - (Date.today.cwday - 1)%7
187 193 @to = @from + 6
188 194 when 'last_week'
189 195 @from = Date.today - 7 - (Date.today.cwday - 1)%7
190 196 @to = @from + 6
191 197 when '7_days'
192 198 @from = Date.today - 7
193 199 @to = Date.today
194 200 when 'current_month'
195 201 @from = Date.civil(Date.today.year, Date.today.month, 1)
196 202 @to = (@from >> 1) - 1
197 203 when 'last_month'
198 204 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
199 205 @to = (@from >> 1) - 1
200 206 when '30_days'
201 207 @from = Date.today - 30
202 208 @to = Date.today
203 209 when 'current_year'
204 210 @from = Date.civil(Date.today.year, 1, 1)
205 211 @to = Date.civil(Date.today.year, 12, 31)
206 212 end
207 213 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
208 214 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
209 215 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
210 216 @free_period = true
211 217 else
212 218 # default
213 219 end
214 220
215 221 @from, @to = @to, @from if @from && @to && @from > @to
216 222 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today)
217 223 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today)
218 224 end
219 225
220 226 end
General Comments 0
You need to be logged in to leave comments. Login now