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