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