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