@@ -1,282 +1,286 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 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 :time_entries |
|
20 | 20 | |
|
21 | 21 | before_action :find_time_entry, :only => [:show, :edit, :update] |
|
22 | before_action :check_editability, :only => [:edit, :update] | |
|
22 | 23 | before_action :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] |
|
23 | 24 | before_action :authorize, :only => [:show, :edit, :update, :bulk_edit, :bulk_update, :destroy] |
|
24 | 25 | |
|
25 | 26 | before_action :find_optional_issue, :only => [:new, :create] |
|
26 | 27 | before_action :find_optional_project, :only => [:index, :report] |
|
27 | 28 | before_action :authorize_global, :only => [:new, :create, :index, :report] |
|
28 | 29 | |
|
29 | 30 | accept_rss_auth :index |
|
30 | 31 | accept_api_auth :index, :show, :create, :update, :destroy |
|
31 | 32 | |
|
32 | 33 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
33 | 34 | |
|
34 | 35 | helper :sort |
|
35 | 36 | include SortHelper |
|
36 | 37 | helper :issues |
|
37 | 38 | include TimelogHelper |
|
38 | 39 | helper :custom_fields |
|
39 | 40 | include CustomFieldsHelper |
|
40 | 41 | helper :queries |
|
41 | 42 | include QueriesHelper |
|
42 | 43 | |
|
43 | 44 | def index |
|
44 | 45 | retrieve_time_entry_query |
|
45 | 46 | sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria) |
|
46 | 47 | sort_update(@query.sortable_columns) |
|
47 | 48 | scope = time_entry_scope(:order => sort_clause). |
|
48 | 49 | preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]). |
|
49 | 50 | preload(:project, :user) |
|
50 | 51 | |
|
51 | 52 | respond_to do |format| |
|
52 | 53 | format.html { |
|
53 | 54 | @entry_count = scope.count |
|
54 | 55 | @entry_pages = Paginator.new @entry_count, per_page_option, params['page'] |
|
55 | 56 | @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a |
|
56 | 57 | |
|
57 | 58 | render :layout => !request.xhr? |
|
58 | 59 | } |
|
59 | 60 | format.api { |
|
60 | 61 | @entry_count = scope.count |
|
61 | 62 | @offset, @limit = api_offset_and_limit |
|
62 | 63 | @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a |
|
63 | 64 | } |
|
64 | 65 | format.atom { |
|
65 | 66 | entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a |
|
66 | 67 | render_feed(entries, :title => l(:label_spent_time)) |
|
67 | 68 | } |
|
68 | 69 | format.csv { |
|
69 | 70 | # Export all entries |
|
70 | 71 | @entries = scope.to_a |
|
71 | 72 | send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
72 | 73 | } |
|
73 | 74 | end |
|
74 | 75 | end |
|
75 | 76 | |
|
76 | 77 | def report |
|
77 | 78 | retrieve_time_entry_query |
|
78 | 79 | scope = time_entry_scope |
|
79 | 80 | |
|
80 | 81 | @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope) |
|
81 | 82 | |
|
82 | 83 | respond_to do |format| |
|
83 | 84 | format.html { render :layout => !request.xhr? } |
|
84 | 85 | format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
85 | 86 | end |
|
86 | 87 | end |
|
87 | 88 | |
|
88 | 89 | def show |
|
89 | 90 | respond_to do |format| |
|
90 | 91 | # TODO: Implement html response |
|
91 | 92 | format.html { head 406 } |
|
92 | 93 | format.api |
|
93 | 94 | end |
|
94 | 95 | end |
|
95 | 96 | |
|
96 | 97 | def new |
|
97 | 98 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
98 | 99 | @time_entry.safe_attributes = params[:time_entry] |
|
99 | 100 | end |
|
100 | 101 | |
|
101 | 102 | def create |
|
102 | 103 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
103 | 104 | @time_entry.safe_attributes = params[:time_entry] |
|
104 | 105 | if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project) |
|
105 | 106 | render_403 |
|
106 | 107 | return |
|
107 | 108 | end |
|
108 | 109 | |
|
109 | 110 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
110 | 111 | |
|
111 | 112 | if @time_entry.save |
|
112 | 113 | respond_to do |format| |
|
113 | 114 | format.html { |
|
114 | 115 | flash[:notice] = l(:notice_successful_create) |
|
115 | 116 | if params[:continue] |
|
116 | 117 | options = { |
|
117 | 118 | :time_entry => { |
|
118 | 119 | :project_id => params[:time_entry][:project_id], |
|
119 | 120 | :issue_id => @time_entry.issue_id, |
|
120 | 121 | :activity_id => @time_entry.activity_id |
|
121 | 122 | }, |
|
122 | 123 | :back_url => params[:back_url] |
|
123 | 124 | } |
|
124 | 125 | if params[:project_id] && @time_entry.project |
|
125 | 126 | redirect_to new_project_time_entry_path(@time_entry.project, options) |
|
126 | 127 | elsif params[:issue_id] && @time_entry.issue |
|
127 | 128 | redirect_to new_issue_time_entry_path(@time_entry.issue, options) |
|
128 | 129 | else |
|
129 | 130 | redirect_to new_time_entry_path(options) |
|
130 | 131 | end |
|
131 | 132 | else |
|
132 | 133 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
133 | 134 | end |
|
134 | 135 | } |
|
135 | 136 | format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } |
|
136 | 137 | end |
|
137 | 138 | else |
|
138 | 139 | respond_to do |format| |
|
139 | 140 | format.html { render :action => 'new' } |
|
140 | 141 | format.api { render_validation_errors(@time_entry) } |
|
141 | 142 | end |
|
142 | 143 | end |
|
143 | 144 | end |
|
144 | 145 | |
|
145 | 146 | def edit |
|
146 | 147 | @time_entry.safe_attributes = params[:time_entry] |
|
147 | 148 | end |
|
148 | 149 | |
|
149 | 150 | def update |
|
150 | 151 | @time_entry.safe_attributes = params[:time_entry] |
|
151 | 152 | |
|
152 | 153 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
153 | 154 | |
|
154 | 155 | if @time_entry.save |
|
155 | 156 | respond_to do |format| |
|
156 | 157 | format.html { |
|
157 | 158 | flash[:notice] = l(:notice_successful_update) |
|
158 | 159 | redirect_back_or_default project_time_entries_path(@time_entry.project) |
|
159 | 160 | } |
|
160 | 161 | format.api { render_api_ok } |
|
161 | 162 | end |
|
162 | 163 | else |
|
163 | 164 | respond_to do |format| |
|
164 | 165 | format.html { render :action => 'edit' } |
|
165 | 166 | format.api { render_validation_errors(@time_entry) } |
|
166 | 167 | end |
|
167 | 168 | end |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | def bulk_edit |
|
171 | 172 | @available_activities = @projects.map(&:activities).reduce(:&) |
|
172 | 173 | @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported} |
|
173 | 174 | end |
|
174 | 175 | |
|
175 | 176 | def bulk_update |
|
176 | 177 | attributes = parse_params_for_bulk_update(params[:time_entry]) |
|
177 | 178 | |
|
178 | 179 | unsaved_time_entry_ids = [] |
|
179 | 180 | @time_entries.each do |time_entry| |
|
180 | 181 | time_entry.reload |
|
181 | 182 | time_entry.safe_attributes = attributes |
|
182 | 183 | call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) |
|
183 | 184 | unless time_entry.save |
|
184 | 185 | logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info? |
|
185 | 186 | # Keep unsaved time_entry ids to display them in flash error |
|
186 | 187 | unsaved_time_entry_ids << time_entry.id |
|
187 | 188 | end |
|
188 | 189 | end |
|
189 | 190 | set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) |
|
190 | 191 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
191 | 192 | end |
|
192 | 193 | |
|
193 | 194 | def destroy |
|
194 | 195 | destroyed = TimeEntry.transaction do |
|
195 | 196 | @time_entries.each do |t| |
|
196 | 197 | unless t.destroy && t.destroyed? |
|
197 | 198 | raise ActiveRecord::Rollback |
|
198 | 199 | end |
|
199 | 200 | end |
|
200 | 201 | end |
|
201 | 202 | |
|
202 | 203 | respond_to do |format| |
|
203 | 204 | format.html { |
|
204 | 205 | if destroyed |
|
205 | 206 | flash[:notice] = l(:notice_successful_delete) |
|
206 | 207 | else |
|
207 | 208 | flash[:error] = l(:notice_unable_delete_time_entry) |
|
208 | 209 | end |
|
209 | 210 | redirect_back_or_default project_time_entries_path(@projects.first) |
|
210 | 211 | } |
|
211 | 212 | format.api { |
|
212 | 213 | if destroyed |
|
213 | 214 | render_api_ok |
|
214 | 215 | else |
|
215 | 216 | render_validation_errors(@time_entries) |
|
216 | 217 | end |
|
217 | 218 | } |
|
218 | 219 | end |
|
219 | 220 | end |
|
220 | 221 | |
|
221 | 222 | private |
|
222 | 223 | def find_time_entry |
|
223 | 224 | @time_entry = TimeEntry.find(params[:id]) |
|
225 | @project = @time_entry.project | |
|
226 | rescue ActiveRecord::RecordNotFound | |
|
227 | render_404 | |
|
228 | end | |
|
229 | ||
|
230 | def check_editability | |
|
224 | 231 | unless @time_entry.editable_by?(User.current) |
|
225 | 232 | render_403 |
|
226 | 233 | return false |
|
227 | 234 | end |
|
228 | @project = @time_entry.project | |
|
229 | rescue ActiveRecord::RecordNotFound | |
|
230 | render_404 | |
|
231 | 235 | end |
|
232 | 236 | |
|
233 | 237 | def find_time_entries |
|
234 | 238 | @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]). |
|
235 | 239 | preload(:project => :time_entry_activities). |
|
236 | 240 | preload(:user).to_a |
|
237 | 241 | |
|
238 | 242 | raise ActiveRecord::RecordNotFound if @time_entries.empty? |
|
239 | 243 | raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)} |
|
240 | 244 | @projects = @time_entries.collect(&:project).compact.uniq |
|
241 | 245 | @project = @projects.first if @projects.size == 1 |
|
242 | 246 | rescue ActiveRecord::RecordNotFound |
|
243 | 247 | render_404 |
|
244 | 248 | end |
|
245 | 249 | |
|
246 | 250 | def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) |
|
247 | 251 | if unsaved_time_entry_ids.empty? |
|
248 | 252 | flash[:notice] = l(:notice_successful_update) unless time_entries.empty? |
|
249 | 253 | else |
|
250 | 254 | flash[:error] = l(:notice_failed_to_save_time_entries, |
|
251 | 255 | :count => unsaved_time_entry_ids.size, |
|
252 | 256 | :total => time_entries.size, |
|
253 | 257 | :ids => '#' + unsaved_time_entry_ids.join(', #')) |
|
254 | 258 | end |
|
255 | 259 | end |
|
256 | 260 | |
|
257 | 261 | def find_optional_issue |
|
258 | 262 | if params[:issue_id].present? |
|
259 | 263 | @issue = Issue.find(params[:issue_id]) |
|
260 | 264 | @project = @issue.project |
|
261 | 265 | else |
|
262 | 266 | find_optional_project |
|
263 | 267 | end |
|
264 | 268 | end |
|
265 | 269 | |
|
266 | 270 | def find_optional_project |
|
267 | 271 | if params[:project_id].present? |
|
268 | 272 | @project = Project.find(params[:project_id]) |
|
269 | 273 | end |
|
270 | 274 | rescue ActiveRecord::RecordNotFound |
|
271 | 275 | render_404 |
|
272 | 276 | end |
|
273 | 277 | |
|
274 | 278 | # Returns the TimeEntry scope for index and report actions |
|
275 | 279 | def time_entry_scope(options={}) |
|
276 | 280 | @query.results_scope(options) |
|
277 | 281 | end |
|
278 | 282 | |
|
279 | 283 | def retrieve_time_entry_query |
|
280 | 284 | retrieve_query(TimeEntryQuery, false) |
|
281 | 285 | end |
|
282 | 286 | end |
General Comments 0
You need to be logged in to leave comments.
Login now