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