Mercurial > hg > soundsoftware-site
comparison app/controllers/timelog_controller.rb @ 37:94944d00e43c
* Update to SVN trunk rev 4411
author | Chris Cannam <chris.cannam@soundsoftware.ac.uk> |
---|---|
date | Fri, 19 Nov 2010 13:24:41 +0000 |
parents | 40f7cfd4df19 |
children | af80e5618e9b |
comparison
equal
deleted
inserted
replaced
22:40f7cfd4df19 | 37:94944d00e43c |
---|---|
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 menu_item :issues | 19 menu_item :issues |
20 before_filter :find_project, :authorize, :only => [:edit, :destroy] | 20 before_filter :find_project, :only => [:new, :create] |
21 before_filter :find_optional_project, :only => [:report, :details] | 21 before_filter :find_time_entry, :only => [:edit, :update, :destroy] |
22 before_filter :load_available_criterias, :only => [:report] | 22 before_filter :authorize, :except => [:index] |
23 | 23 before_filter :find_optional_project, :only => [:index] |
24 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details } | 24 |
25 | |
26 helper :sort | 25 helper :sort |
27 include SortHelper | 26 include SortHelper |
28 helper :issues | 27 helper :issues |
29 include TimelogHelper | 28 include TimelogHelper |
30 helper :custom_fields | 29 helper :custom_fields |
31 include CustomFieldsHelper | 30 include CustomFieldsHelper |
32 | 31 |
33 def report | 32 def index |
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 << time_report_joins | |
59 sql << " WHERE" | |
60 sql << " (%s) AND" % sql_condition | |
61 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from), ActiveRecord::Base.connection.quoted_date(@to)] | |
62 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on" | |
63 | |
64 @hours = ActiveRecord::Base.connection.select_all(sql) | |
65 | |
66 @hours.each do |row| | |
67 case @columns | |
68 when 'year' | |
69 row['year'] = row['tyear'] | |
70 when 'month' | |
71 row['month'] = "#{row['tyear']}-#{row['tmonth']}" | |
72 when 'week' | |
73 row['week'] = "#{row['tyear']}-#{row['tweek']}" | |
74 when 'day' | |
75 row['day'] = "#{row['spent_on']}" | |
76 end | |
77 end | |
78 | |
79 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} | |
80 | |
81 @periods = [] | |
82 # Date#at_beginning_of_ not supported in Rails 1.2.x | |
83 date_from = @from.to_time | |
84 # 100 columns max | |
85 while date_from <= @to.to_time && @periods.length < 100 | |
86 case @columns | |
87 when 'year' | |
88 @periods << "#{date_from.year}" | |
89 date_from = (date_from + 1.year).at_beginning_of_year | |
90 when 'month' | |
91 @periods << "#{date_from.year}-#{date_from.month}" | |
92 date_from = (date_from + 1.month).at_beginning_of_month | |
93 when 'week' | |
94 @periods << "#{date_from.year}-#{date_from.to_date.cweek}" | |
95 date_from = (date_from + 7.day).at_beginning_of_week | |
96 when 'day' | |
97 @periods << "#{date_from.to_date}" | |
98 date_from = date_from + 1.day | |
99 end | |
100 end | |
101 end | |
102 | |
103 respond_to do |format| | |
104 format.html { render :layout => !request.xhr? } | |
105 format.csv { send_data(report_to_csv(@criterias, @periods, @hours), :type => 'text/csv; header=present', :filename => 'timelog.csv') } | |
106 end | |
107 end | |
108 | |
109 def details | |
110 sort_init 'spent_on', 'desc' | 33 sort_init 'spent_on', 'desc' |
111 sort_update 'spent_on' => 'spent_on', | 34 sort_update 'spent_on' => 'spent_on', |
112 'user' => 'user_id', | 35 'user' => 'user_id', |
113 'activity' => 'activity_id', | 36 'activity' => 'activity_id', |
114 'project' => "#{Project.table_name}.name", | 37 'project' => "#{Project.table_name}.name", |
160 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') | 83 send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
161 } | 84 } |
162 end | 85 end |
163 end | 86 end |
164 end | 87 end |
88 | |
89 def new | |
90 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) | |
91 @time_entry.attributes = params[:time_entry] | |
92 | |
93 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |
94 render :action => 'edit' | |
95 end | |
96 | |
97 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } | |
98 def create | |
99 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) | |
100 @time_entry.attributes = params[:time_entry] | |
101 | |
102 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |
103 | |
104 if @time_entry.save | |
105 flash[:notice] = l(:notice_successful_update) | |
106 redirect_back_or_default :action => 'index', :project_id => @time_entry.project | |
107 else | |
108 render :action => 'edit' | |
109 end | |
110 end | |
165 | 111 |
166 def edit | 112 def edit |
167 (render_403; return) if @time_entry && !@time_entry.editable_by?(User.current) | 113 @time_entry.attributes = params[:time_entry] |
168 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) | 114 |
169 @time_entry.attributes = params[:time_entry] | 115 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
170 | 116 end |
171 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | 117 |
172 | 118 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
173 if request.post? and @time_entry.save | 119 def update |
120 @time_entry.attributes = params[:time_entry] | |
121 | |
122 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) | |
123 | |
124 if @time_entry.save | |
174 flash[:notice] = l(:notice_successful_update) | 125 flash[:notice] = l(:notice_successful_update) |
175 redirect_back_or_default :action => 'details', :project_id => @time_entry.project | 126 redirect_back_or_default :action => 'index', :project_id => @time_entry.project |
176 return | 127 else |
128 render :action => 'edit' | |
177 end | 129 end |
178 end | 130 end |
179 | 131 |
132 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } | |
180 def destroy | 133 def destroy |
181 (render_404; return) unless @time_entry | |
182 (render_403; return) unless @time_entry.editable_by?(User.current) | |
183 if @time_entry.destroy && @time_entry.destroyed? | 134 if @time_entry.destroy && @time_entry.destroyed? |
184 flash[:notice] = l(:notice_successful_delete) | 135 flash[:notice] = l(:notice_successful_delete) |
185 else | 136 else |
186 flash[:error] = l(:notice_unable_delete_time_entry) | 137 flash[:error] = l(:notice_unable_delete_time_entry) |
187 end | 138 end |
188 redirect_to :back | 139 redirect_to :back |
189 rescue ::ActionController::RedirectBackError | 140 rescue ::ActionController::RedirectBackError |
190 redirect_to :action => 'details', :project_id => @time_entry.project | 141 redirect_to :action => 'index', :project_id => @time_entry.project |
191 end | 142 end |
192 | 143 |
193 private | 144 private |
145 def find_time_entry | |
146 @time_entry = TimeEntry.find(params[:id]) | |
147 unless @time_entry.editable_by?(User.current) | |
148 render_403 | |
149 return false | |
150 end | |
151 @project = @time_entry.project | |
152 rescue ActiveRecord::RecordNotFound | |
153 render_404 | |
154 end | |
155 | |
194 def find_project | 156 def find_project |
195 if params[:id] | 157 if params[:issue_id] |
196 @time_entry = TimeEntry.find(params[:id]) | |
197 @project = @time_entry.project | |
198 elsif params[:issue_id] | |
199 @issue = Issue.find(params[:issue_id]) | 158 @issue = Issue.find(params[:issue_id]) |
200 @project = @issue.project | 159 @project = @issue.project |
201 elsif params[:project_id] | 160 elsif params[:project_id] |
202 @project = Project.find(params[:project_id]) | 161 @project = Project.find(params[:project_id]) |
203 else | 162 else |
262 @from, @to = @to, @from if @from && @to && @from > @to | 221 @from, @to = @to, @from if @from && @to && @from > @to |
263 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) | 222 @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) |
264 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) | 223 @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) |
265 end | 224 end |
266 | 225 |
267 def load_available_criterias | |
268 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", | |
269 :klass => Project, | |
270 :label => :label_project}, | |
271 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", | |
272 :klass => Version, | |
273 :label => :label_version}, | |
274 'category' => {:sql => "#{Issue.table_name}.category_id", | |
275 :klass => IssueCategory, | |
276 :label => :field_category}, | |
277 'member' => {:sql => "#{TimeEntry.table_name}.user_id", | |
278 :klass => User, | |
279 :label => :label_member}, | |
280 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", | |
281 :klass => Tracker, | |
282 :label => :label_tracker}, | |
283 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", | |
284 :klass => TimeEntryActivity, | |
285 :label => :label_activity}, | |
286 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", | |
287 :klass => Issue, | |
288 :label => :label_issue} | |
289 } | |
290 | |
291 # Add list and boolean custom fields as available criterias | |
292 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields) | |
293 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
294 @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)", | |
295 :format => cf.field_format, | |
296 :label => cf.name} | |
297 end if @project | |
298 | |
299 # Add list and boolean time entry custom fields | |
300 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
301 @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)", | |
302 :format => cf.field_format, | |
303 :label => cf.name} | |
304 end | |
305 | |
306 # Add list and boolean time entry activity custom fields | |
307 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf| | |
308 @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)", | |
309 :format => cf.field_format, | |
310 :label => cf.name} | |
311 end | |
312 | |
313 call_hook(:controller_timelog_available_criterias, { :available_criterias => @available_criterias, :project => @project }) | |
314 @available_criterias | |
315 end | |
316 | |
317 def time_report_joins | |
318 sql = '' | |
319 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id" | |
320 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id" | |
321 call_hook(:controller_timelog_time_report_joins, {:sql => sql} ) | |
322 sql | |
323 end | |
324 end | 226 end |