@@ -1,311 +1,316 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | before_filter :find_project, :authorize, :only => [:edit, :destroy] |
|
21 | 21 | before_filter :find_optional_project, :only => [:report, :details] |
|
22 | before_filter :load_available_criterias, :only => [:report] | |
|
22 | 23 | |
|
23 | 24 | verify :method => :post, :only => :destroy, :redirect_to => { :action => :details } |
|
24 | 25 | |
|
25 | 26 | helper :sort |
|
26 | 27 | include SortHelper |
|
27 | 28 | helper :issues |
|
28 | 29 | include TimelogHelper |
|
29 | 30 | helper :custom_fields |
|
30 | 31 | include CustomFieldsHelper |
|
31 | 32 | |
|
32 | 33 | def report |
|
33 | @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", | |
|
34 | :klass => Project, | |
|
35 | :label => :label_project}, | |
|
36 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", | |
|
37 | :klass => Version, | |
|
38 | :label => :label_version}, | |
|
39 | 'category' => {:sql => "#{Issue.table_name}.category_id", | |
|
40 | :klass => IssueCategory, | |
|
41 | :label => :field_category}, | |
|
42 | 'member' => {:sql => "#{TimeEntry.table_name}.user_id", | |
|
43 | :klass => User, | |
|
44 | :label => :label_member}, | |
|
45 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", | |
|
46 | :klass => Tracker, | |
|
47 | :label => :label_tracker}, | |
|
48 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", | |
|
49 | :klass => TimeEntryActivity, | |
|
50 | :label => :label_activity}, | |
|
51 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", | |
|
52 | :klass => Issue, | |
|
53 | :label => :label_issue} | |
|
54 | } | |
|
55 | ||
|
56 | # Add list and boolean custom fields as available criterias | |
|
57 | custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) | |
|
58 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
59 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)", | |
|
60 | :format => cf.field_format, | |
|
61 | :label => cf.name} | |
|
62 | end if @project | |
|
63 | ||
|
64 | # Add list and boolean time entry custom fields | |
|
65 | TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
66 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)", | |
|
67 | :format => cf.field_format, | |
|
68 | :label => cf.name} | |
|
69 | end | |
|
70 | ||
|
71 | # Add list and boolean time entry activity custom fields | |
|
72 | TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
73 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id)", | |
|
74 | :format => cf.field_format, | |
|
75 | :label => cf.name} | |
|
76 | end | |
|
77 | ||
|
78 | 34 | @criterias = params[:criterias] || [] |
|
79 | 35 | @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria} |
|
80 | 36 | @criterias.uniq! |
|
81 | 37 | @criterias = @criterias[0,3] |
|
82 | 38 | |
|
83 | 39 | @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month' |
|
84 | 40 | |
|
85 | 41 | retrieve_date_range |
|
86 | 42 | |
|
87 | 43 | unless @criterias.empty? |
|
88 | 44 | sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ') |
|
89 | 45 | sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ') |
|
90 | 46 | sql_condition = '' |
|
91 | 47 | |
|
92 | 48 | if @project.nil? |
|
93 | 49 | sql_condition = Project.allowed_to_condition(User.current, :view_time_entries) |
|
94 | 50 | elsif @issue.nil? |
|
95 | 51 | sql_condition = @project.project_condition(Setting.display_subprojects_issues?) |
|
96 | 52 | else |
|
97 | 53 | sql_condition = "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}" |
|
98 | 54 | end |
|
99 | 55 | |
|
100 | 56 | sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours" |
|
101 | 57 | sql << " FROM #{TimeEntry.table_name}" |
|
102 | 58 | sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id" |
|
103 | 59 | sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id" |
|
104 | 60 | sql << " WHERE" |
|
105 | 61 | sql << " (%s) AND" % sql_condition |
|
106 | 62 | sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)] |
|
107 | 63 | sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on" |
|
108 | 64 | |
|
109 | 65 | @hours = ActiveRecord::Base.connection.select_all(sql) |
|
110 | 66 | |
|
111 | 67 | @hours.each do |row| |
|
112 | 68 | case @columns |
|
113 | 69 | when 'year' |
|
114 | 70 | row['year'] = row['tyear'] |
|
115 | 71 | when 'month' |
|
116 | 72 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" |
|
117 | 73 | when 'week' |
|
118 | 74 | row['week'] = "#{row['tyear']}-#{row['tweek']}" |
|
119 | 75 | when 'day' |
|
120 | 76 | row['day'] = "#{row['spent_on']}" |
|
121 | 77 | end |
|
122 | 78 | end |
|
123 | 79 | |
|
124 | 80 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} |
|
125 | 81 | |
|
126 | 82 | @periods = [] |
|
127 | 83 | # Date#at_beginning_of_ not supported in Rails 1.2.x |
|
128 | 84 | date_from = @from.to_time |
|
129 | 85 | # 100 columns max |
|
130 | 86 | while date_from <= @to.to_time && @periods.length < 100 |
|
131 | 87 | case @columns |
|
132 | 88 | when 'year' |
|
133 | 89 | @periods << "#{date_from.year}" |
|
134 | 90 | date_from = (date_from + 1.year).at_beginning_of_year |
|
135 | 91 | when 'month' |
|
136 | 92 | @periods << "#{date_from.year}-#{date_from.month}" |
|
137 | 93 | date_from = (date_from + 1.month).at_beginning_of_month |
|
138 | 94 | when 'week' |
|
139 | 95 | @periods << "#{date_from.year}-#{date_from.to_date.cweek}" |
|
140 | 96 | date_from = (date_from + 7.day).at_beginning_of_week |
|
141 | 97 | when 'day' |
|
142 | 98 | @periods << "#{date_from.to_date}" |
|
143 | 99 | date_from = date_from + 1.day |
|
144 | 100 | end |
|
145 | 101 | end |
|
146 | 102 | end |
|
147 | 103 | |
|
148 | 104 | respond_to do |format| |
|
149 | 105 | format.html { render :layout => !request.xhr? } |
|
150 | 106 | format.csv { send_data(report_to_csv(@criterias, @periods, @hours), :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
151 | 107 | end |
|
152 | 108 | end |
|
153 | 109 | |
|
154 | 110 | def details |
|
155 | 111 | sort_init 'spent_on', 'desc' |
|
156 | 112 | sort_update 'spent_on' => 'spent_on', |
|
157 | 113 | 'user' => 'user_id', |
|
158 | 114 | 'activity' => 'activity_id', |
|
159 | 115 | 'project' => "#{Project.table_name}.name", |
|
160 | 116 | 'issue' => 'issue_id', |
|
161 | 117 | 'hours' => 'hours' |
|
162 | 118 | |
|
163 | 119 | cond = ARCondition.new |
|
164 | 120 | if @project.nil? |
|
165 | 121 | cond << Project.allowed_to_condition(User.current, :view_time_entries) |
|
166 | 122 | elsif @issue.nil? |
|
167 | 123 | cond << @project.project_condition(Setting.display_subprojects_issues?) |
|
168 | 124 | else |
|
169 | 125 | cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}" |
|
170 | 126 | end |
|
171 | 127 | |
|
172 | 128 | retrieve_date_range |
|
173 | 129 | cond << ['spent_on BETWEEN ? AND ?', @from, @to] |
|
174 | 130 | |
|
175 | 131 | TimeEntry.visible_by(User.current) do |
|
176 | 132 | respond_to do |format| |
|
177 | 133 | format.html { |
|
178 | 134 | # Paginate results |
|
179 | 135 | @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions) |
|
180 | 136 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
|
181 | 137 | @entries = TimeEntry.find(:all, |
|
182 | 138 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
183 | 139 | :conditions => cond.conditions, |
|
184 | 140 | :order => sort_clause, |
|
185 | 141 | :limit => @entry_pages.items_per_page, |
|
186 | 142 | :offset => @entry_pages.current.offset) |
|
187 | 143 | @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f |
|
188 | 144 | |
|
189 | 145 | render :layout => !request.xhr? |
|
190 | 146 | } |
|
191 | 147 | format.atom { |
|
192 | 148 | entries = TimeEntry.find(:all, |
|
193 | 149 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
194 | 150 | :conditions => cond.conditions, |
|
195 | 151 | :order => "#{TimeEntry.table_name}.created_on DESC", |
|
196 | 152 | :limit => Setting.feeds_limit.to_i) |
|
197 | 153 | render_feed(entries, :title => l(:label_spent_time)) |
|
198 | 154 | } |
|
199 | 155 | format.csv { |
|
200 | 156 | # Export all entries |
|
201 | 157 | @entries = TimeEntry.find(:all, |
|
202 | 158 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
|
203 | 159 | :conditions => cond.conditions, |
|
204 | 160 | :order => sort_clause) |
|
205 | 161 | send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
206 | 162 | } |
|
207 | 163 | end |
|
208 | 164 | end |
|
209 | 165 | end |
|
210 | 166 | |
|
211 | 167 | def edit |
|
212 | 168 | (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current) |
|
213 | 169 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
214 | 170 | @time_entry.attributes = params[:time_entry] |
|
215 | 171 | |
|
216 | 172 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
217 | 173 | |
|
218 | 174 | if request.post? and @time_entry.save |
|
219 | 175 | flash[:notice] = l(:notice_successful_update) |
|
220 | 176 | redirect_back_or_default :action => 'details', :project_id => @time_entry.project |
|
221 | 177 | return |
|
222 | 178 | end |
|
223 | 179 | end |
|
224 | 180 | |
|
225 | 181 | def destroy |
|
226 | 182 | (render_404; return) unless @time_entry |
|
227 | 183 | (render_403; return) unless @time_entry.editable_by?(User.current) |
|
228 | 184 | if @time_entry.destroy && @time_entry.destroyed? |
|
229 | 185 | flash[:notice] = l(:notice_successful_delete) |
|
230 | 186 | else |
|
231 | 187 | flash[:error] = l(:notice_unable_delete_time_entry) |
|
232 | 188 | end |
|
233 | 189 | redirect_to :back |
|
234 | 190 | rescue ::ActionController::RedirectBackError |
|
235 | 191 | redirect_to :action => 'details', :project_id => @time_entry.project |
|
236 | 192 | end |
|
237 | 193 | |
|
238 | 194 | private |
|
239 | 195 | def find_project |
|
240 | 196 | if params[:id] |
|
241 | 197 | @time_entry = TimeEntry.find(params[:id]) |
|
242 | 198 | @project = @time_entry.project |
|
243 | 199 | elsif params[:issue_id] |
|
244 | 200 | @issue = Issue.find(params[:issue_id]) |
|
245 | 201 | @project = @issue.project |
|
246 | 202 | elsif params[:project_id] |
|
247 | 203 | @project = Project.find(params[:project_id]) |
|
248 | 204 | else |
|
249 | 205 | render_404 |
|
250 | 206 | return false |
|
251 | 207 | end |
|
252 | 208 | rescue ActiveRecord::RecordNotFound |
|
253 | 209 | render_404 |
|
254 | 210 | end |
|
255 | 211 | |
|
256 | 212 | def find_optional_project |
|
257 | 213 | if !params[:issue_id].blank? |
|
258 | 214 | @issue = Issue.find(params[:issue_id]) |
|
259 | 215 | @project = @issue.project |
|
260 | 216 | elsif !params[:project_id].blank? |
|
261 | 217 | @project = Project.find(params[:project_id]) |
|
262 | 218 | end |
|
263 | 219 | deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) |
|
264 | 220 | end |
|
265 | 221 | |
|
266 | 222 | # Retrieves the date range based on predefined ranges or specific from/to param dates |
|
267 | 223 | def retrieve_date_range |
|
268 | 224 | @free_period = false |
|
269 | 225 | @from, @to = nil, nil |
|
270 | 226 | |
|
271 | 227 | if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) |
|
272 | 228 | case params[:period].to_s |
|
273 | 229 | when 'today' |
|
274 | 230 | @from = @to = Date.today |
|
275 | 231 | when 'yesterday' |
|
276 | 232 | @from = @to = Date.today - 1 |
|
277 | 233 | when 'current_week' |
|
278 | 234 | @from = Date.today - (Date.today.cwday - 1)%7 |
|
279 | 235 | @to = @from + 6 |
|
280 | 236 | when 'last_week' |
|
281 | 237 | @from = Date.today - 7 - (Date.today.cwday - 1)%7 |
|
282 | 238 | @to = @from + 6 |
|
283 | 239 | when '7_days' |
|
284 | 240 | @from = Date.today - 7 |
|
285 | 241 | @to = Date.today |
|
286 | 242 | when 'current_month' |
|
287 | 243 | @from = Date.civil(Date.today.year, Date.today.month, 1) |
|
288 | 244 | @to = (@from >> 1) - 1 |
|
289 | 245 | when 'last_month' |
|
290 | 246 | @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 |
|
291 | 247 | @to = (@from >> 1) - 1 |
|
292 | 248 | when '30_days' |
|
293 | 249 | @from = Date.today - 30 |
|
294 | 250 | @to = Date.today |
|
295 | 251 | when 'current_year' |
|
296 | 252 | @from = Date.civil(Date.today.year, 1, 1) |
|
297 | 253 | @to = Date.civil(Date.today.year, 12, 31) |
|
298 | 254 | end |
|
299 | 255 | elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) |
|
300 | 256 | begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end |
|
301 | 257 | begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end |
|
302 | 258 | @free_period = true |
|
303 | 259 | else |
|
304 | 260 | # default |
|
305 | 261 | end |
|
306 | 262 | |
|
307 | 263 | @from, @to = @to, @from if @from && @to && @from > @to |
|
308 | 264 | @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1 |
|
309 | 265 | @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) |
|
310 | 266 | end |
|
267 | ||
|
268 | def load_available_criterias | |
|
269 | @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", | |
|
270 | :klass => Project, | |
|
271 | :label => :label_project}, | |
|
272 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", | |
|
273 | :klass => Version, | |
|
274 | :label => :label_version}, | |
|
275 | 'category' => {:sql => "#{Issue.table_name}.category_id", | |
|
276 | :klass => IssueCategory, | |
|
277 | :label => :field_category}, | |
|
278 | 'member' => {:sql => "#{TimeEntry.table_name}.user_id", | |
|
279 | :klass => User, | |
|
280 | :label => :label_member}, | |
|
281 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", | |
|
282 | :klass => Tracker, | |
|
283 | :label => :label_tracker}, | |
|
284 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", | |
|
285 | :klass => TimeEntryActivity, | |
|
286 | :label => :label_activity}, | |
|
287 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", | |
|
288 | :klass => Issue, | |
|
289 | :label => :label_issue} | |
|
290 | } | |
|
291 | ||
|
292 | # Add list and boolean custom fields as available criterias | |
|
293 | custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) | |
|
294 | custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
295 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)", | |
|
296 | :format => cf.field_format, | |
|
297 | :label => cf.name} | |
|
298 | end if @project | |
|
299 | ||
|
300 | # Add list and boolean time entry custom fields | |
|
301 | TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
302 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)", | |
|
303 | :format => cf.field_format, | |
|
304 | :label => cf.name} | |
|
305 | end | |
|
306 | ||
|
307 | # Add list and boolean time entry activity custom fields | |
|
308 | TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
|
309 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id)", | |
|
310 | :format => cf.field_format, | |
|
311 | :label => cf.name} | |
|
312 | end | |
|
313 | ||
|
314 | @available_criterias | |
|
315 | end | |
|
311 | 316 | end |
General Comments 0
You need to be logged in to leave comments.
Login now