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