comparison .svn/pristine/e5/e5c904dde1892f8ff602788f810c1a9ce2872e82.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class TimelogController < ApplicationController
19 menu_item :issues
20
21 before_filter :find_project_for_new_time_entry, :only => [:create]
22 before_filter :find_time_entry, :only => [:show, :edit, :update]
23 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
24 before_filter :authorize, :except => [:new, :index, :report]
25
26 before_filter :find_optional_project, :only => [:index, :report]
27 before_filter :find_optional_project_for_new_time_entry, :only => [:new]
28 before_filter :authorize_global, :only => [:new, :index, :report]
29
30 accept_rss_auth :index
31 accept_api_auth :index, :show, :create, :update, :destroy
32
33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34
35 helper :sort
36 include SortHelper
37 helper :issues
38 include TimelogHelper
39 helper :custom_fields
40 include CustomFieldsHelper
41 helper :queries
42 include QueriesHelper
43
44 def index
45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
46 scope = time_entry_scope
47
48 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
49 sort_update(@query.sortable_columns)
50
51 respond_to do |format|
52 format.html {
53 # Paginate results
54 @entry_count = scope.count
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 @entries = scope.all(
57 :include => [:project, :activity, :user, {:issue => :tracker}],
58 :order => sort_clause,
59 :limit => @entry_pages.per_page,
60 :offset => @entry_pages.offset
61 )
62 @total_hours = scope.sum(:hours).to_f
63
64 render :layout => !request.xhr?
65 }
66 format.api {
67 @entry_count = scope.count
68 @offset, @limit = api_offset_and_limit
69 @entries = scope.all(
70 :include => [:project, :activity, :user, {:issue => :tracker}],
71 :order => sort_clause,
72 :limit => @limit,
73 :offset => @offset
74 )
75 }
76 format.atom {
77 entries = scope.all(
78 :include => [:project, :activity, :user, {:issue => :tracker}],
79 :order => "#{TimeEntry.table_name}.created_on DESC",
80 :limit => Setting.feeds_limit.to_i
81 )
82 render_feed(entries, :title => l(:label_spent_time))
83 }
84 format.csv {
85 # Export all entries
86 @entries = scope.all(
87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
88 :order => sort_clause
89 )
90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
91 }
92 end
93 end
94
95 def report
96 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
97 scope = time_entry_scope
98
99 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
100
101 respond_to do |format|
102 format.html { render :layout => !request.xhr? }
103 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
104 end
105 end
106
107 def show
108 respond_to do |format|
109 # TODO: Implement html response
110 format.html { render :nothing => true, :status => 406 }
111 format.api
112 end
113 end
114
115 def new
116 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
117 @time_entry.safe_attributes = params[:time_entry]
118 end
119
120 def create
121 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
122 @time_entry.safe_attributes = params[:time_entry]
123
124 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
125
126 if @time_entry.save
127 respond_to do |format|
128 format.html {
129 flash[:notice] = l(:notice_successful_create)
130 if params[:continue]
131 if params[:project_id]
132 options = {
133 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
134 :back_url => params[:back_url]
135 }
136 if @time_entry.issue
137 redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
138 else
139 redirect_to new_project_time_entry_path(@time_entry.project, options)
140 end
141 else
142 options = {
143 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
144 :back_url => params[:back_url]
145 }
146 redirect_to new_time_entry_path(options)
147 end
148 else
149 redirect_back_or_default project_time_entries_path(@time_entry.project)
150 end
151 }
152 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
153 end
154 else
155 respond_to do |format|
156 format.html { render :action => 'new' }
157 format.api { render_validation_errors(@time_entry) }
158 end
159 end
160 end
161
162 def edit
163 @time_entry.safe_attributes = params[:time_entry]
164 end
165
166 def update
167 @time_entry.safe_attributes = params[:time_entry]
168
169 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
170
171 if @time_entry.save
172 respond_to do |format|
173 format.html {
174 flash[:notice] = l(:notice_successful_update)
175 redirect_back_or_default project_time_entries_path(@time_entry.project)
176 }
177 format.api { render_api_ok }
178 end
179 else
180 respond_to do |format|
181 format.html { render :action => 'edit' }
182 format.api { render_validation_errors(@time_entry) }
183 end
184 end
185 end
186
187 def bulk_edit
188 @available_activities = TimeEntryActivity.shared.active
189 @custom_fields = TimeEntry.first.available_custom_fields
190 end
191
192 def bulk_update
193 attributes = parse_params_for_bulk_time_entry_attributes(params)
194
195 unsaved_time_entry_ids = []
196 @time_entries.each do |time_entry|
197 time_entry.reload
198 time_entry.safe_attributes = attributes
199 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
200 unless time_entry.save
201 # Keep unsaved time_entry ids to display them in flash error
202 unsaved_time_entry_ids << time_entry.id
203 end
204 end
205 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
206 redirect_back_or_default project_time_entries_path(@projects.first)
207 end
208
209 def destroy
210 destroyed = TimeEntry.transaction do
211 @time_entries.each do |t|
212 unless t.destroy && t.destroyed?
213 raise ActiveRecord::Rollback
214 end
215 end
216 end
217
218 respond_to do |format|
219 format.html {
220 if destroyed
221 flash[:notice] = l(:notice_successful_delete)
222 else
223 flash[:error] = l(:notice_unable_delete_time_entry)
224 end
225 redirect_back_or_default project_time_entries_path(@projects.first)
226 }
227 format.api {
228 if destroyed
229 render_api_ok
230 else
231 render_validation_errors(@time_entries)
232 end
233 }
234 end
235 end
236
237 private
238 def find_time_entry
239 @time_entry = TimeEntry.find(params[:id])
240 unless @time_entry.editable_by?(User.current)
241 render_403
242 return false
243 end
244 @project = @time_entry.project
245 rescue ActiveRecord::RecordNotFound
246 render_404
247 end
248
249 def find_time_entries
250 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
251 raise ActiveRecord::RecordNotFound if @time_entries.empty?
252 @projects = @time_entries.collect(&:project).compact.uniq
253 @project = @projects.first if @projects.size == 1
254 rescue ActiveRecord::RecordNotFound
255 render_404
256 end
257
258 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
259 if unsaved_time_entry_ids.empty?
260 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
261 else
262 flash[:error] = l(:notice_failed_to_save_time_entries,
263 :count => unsaved_time_entry_ids.size,
264 :total => time_entries.size,
265 :ids => '#' + unsaved_time_entry_ids.join(', #'))
266 end
267 end
268
269 def find_optional_project_for_new_time_entry
270 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
271 @project = Project.find(project_id)
272 end
273 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
274 @issue = Issue.find(issue_id)
275 @project ||= @issue.project
276 end
277 rescue ActiveRecord::RecordNotFound
278 render_404
279 end
280
281 def find_project_for_new_time_entry
282 find_optional_project_for_new_time_entry
283 if @project.nil?
284 render_404
285 end
286 end
287
288 def find_optional_project
289 if !params[:issue_id].blank?
290 @issue = Issue.find(params[:issue_id])
291 @project = @issue.project
292 elsif !params[:project_id].blank?
293 @project = Project.find(params[:project_id])
294 end
295 end
296
297 # Returns the TimeEntry scope for index and report actions
298 def time_entry_scope
299 scope = TimeEntry.visible.where(@query.statement)
300 if @issue
301 scope = scope.on_issue(@issue)
302 elsif @project
303 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
304 end
305 scope
306 end
307
308 def parse_params_for_bulk_time_entry_attributes(params)
309 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
310 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
311 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
312 attributes
313 end
314 end