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