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