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