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