@@ -1,274 +1,274 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2010 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, :only => [:new, :create] |
|
21 | 21 | before_filter :find_time_entry, :only => [:show, :edit, :update, :destroy] |
|
22 | 22 | before_filter :authorize, :except => [:index] |
|
23 | 23 | before_filter :find_optional_project, :only => [:index] |
|
24 | 24 | accept_key_auth :index, :show, :create, :update, :destroy |
|
25 | 25 | |
|
26 | 26 | helper :sort |
|
27 | 27 | include SortHelper |
|
28 | 28 | helper :issues |
|
29 | 29 | include TimelogHelper |
|
30 | 30 | helper :custom_fields |
|
31 | 31 | include CustomFieldsHelper |
|
32 | 32 | |
|
33 | 33 | def index |
|
34 | 34 | sort_init 'spent_on', 'desc' |
|
35 | 35 | sort_update 'spent_on' => 'spent_on', |
|
36 | 36 | 'user' => 'user_id', |
|
37 | 37 | 'activity' => 'activity_id', |
|
38 | 38 | 'project' => "#{Project.table_name}.name", |
|
39 | 39 | 'issue' => 'issue_id', |
|
40 | 40 | 'hours' => 'hours' |
|
41 | 41 | |
|
42 | 42 | cond = ARCondition.new |
|
43 | 43 | if @project.nil? |
|
44 | 44 | cond << Project.allowed_to_condition(User.current, :view_time_entries) |
|
45 | 45 | elsif @issue.nil? |
|
46 | 46 | cond << @project.project_condition(Setting.display_subprojects_issues?) |
|
47 | 47 | else |
|
48 | 48 | cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}" |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | retrieve_date_range |
|
52 | 52 | cond << ['spent_on BETWEEN ? AND ?', @from, @to] |
|
53 | 53 | |
|
54 | 54 | TimeEntry.visible_by(User.current) do |
|
55 | 55 | respond_to do |format| |
|
56 | 56 | format.html { |
|
57 | 57 | # Paginate results |
|
58 | 58 | @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions) |
|
59 | 59 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
|
60 | 60 | @entries = TimeEntry.find(:all, |
|
61 | 61 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
62 | 62 | :conditions => cond.conditions, |
|
63 | 63 | :order => sort_clause, |
|
64 | 64 | :limit => @entry_pages.items_per_page, |
|
65 | 65 | :offset => @entry_pages.current.offset) |
|
66 | 66 | @total_hours = TimeEntry.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f |
|
67 | 67 | |
|
68 | 68 | render :layout => !request.xhr? |
|
69 | 69 | } |
|
70 | 70 | format.api { |
|
71 | 71 | @entry_count = TimeEntry.count(:include => [:project, :issue], :conditions => cond.conditions) |
|
72 | 72 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
|
73 | 73 | @entries = TimeEntry.find(:all, |
|
74 | 74 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
75 | 75 | :conditions => cond.conditions, |
|
76 | 76 | :order => sort_clause, |
|
77 | 77 | :limit => @entry_pages.items_per_page, |
|
78 | 78 | :offset => @entry_pages.current.offset) |
|
79 | 79 | |
|
80 | 80 | render :template => 'timelog/index.apit' |
|
81 | 81 | } |
|
82 | 82 | format.atom { |
|
83 | 83 | entries = TimeEntry.find(:all, |
|
84 | 84 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
85 | 85 | :conditions => cond.conditions, |
|
86 | 86 | :order => "#{TimeEntry.table_name}.created_on DESC", |
|
87 | 87 | :limit => Setting.feeds_limit.to_i) |
|
88 | 88 | render_feed(entries, :title => l(:label_spent_time)) |
|
89 | 89 | } |
|
90 | 90 | format.csv { |
|
91 | 91 | # Export all entries |
|
92 | 92 | @entries = TimeEntry.find(:all, |
|
93 | 93 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
|
94 | 94 | :conditions => cond.conditions, |
|
95 | 95 | :order => sort_clause) |
|
96 | 96 | send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
97 | 97 | } |
|
98 | 98 | end |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def show |
|
103 | 103 | respond_to do |format| |
|
104 | 104 | # TODO: Implement html response |
|
105 | 105 | format.html { render :nothing => true, :status => 406 } |
|
106 | 106 | format.api { render :template => 'timelog/show.apit' } |
|
107 | 107 | end |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def new |
|
111 | 111 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
112 | 112 | @time_entry.attributes = params[:time_entry] |
|
113 | 113 | |
|
114 | 114 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
115 | 115 | render :action => 'edit' |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
119 | 119 | def create |
|
120 | 120 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) |
|
121 | 121 | @time_entry.attributes = params[:time_entry] |
|
122 | 122 | |
|
123 | 123 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
124 | 124 | |
|
125 | 125 | if @time_entry.save |
|
126 | 126 | respond_to do |format| |
|
127 | 127 | format.html { |
|
128 | 128 | flash[:notice] = l(:notice_successful_update) |
|
129 | 129 | redirect_back_or_default :action => 'index', :project_id => @time_entry.project |
|
130 | 130 | } |
|
131 | 131 | format.api { render :template => 'timelog/show.apit', :status => :created, :location => time_entry_url(@time_entry) } |
|
132 | 132 | end |
|
133 | 133 | else |
|
134 | 134 | respond_to do |format| |
|
135 | 135 | format.html { render :action => 'edit' } |
|
136 | 136 | format.api { render_validation_errors(@time_entry) } |
|
137 | 137 | end |
|
138 | 138 | end |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def edit |
|
142 | 142 | @time_entry.attributes = params[:time_entry] |
|
143 | 143 | |
|
144 | 144 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
148 | 148 | def update |
|
149 | 149 | @time_entry.attributes = params[:time_entry] |
|
150 | 150 | |
|
151 | 151 | call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) |
|
152 | 152 | |
|
153 | 153 | if @time_entry.save |
|
154 | 154 | respond_to do |format| |
|
155 | 155 | format.html { |
|
156 | 156 | flash[:notice] = l(:notice_successful_update) |
|
157 | 157 | redirect_back_or_default :action => 'index', :project_id => @time_entry.project |
|
158 | 158 | } |
|
159 | 159 | format.api { head :ok } |
|
160 | 160 | end |
|
161 | 161 | else |
|
162 | 162 | respond_to do |format| |
|
163 | 163 | format.html { render :action => 'edit' } |
|
164 | 164 | format.api { render_validation_errors(@time_entry) } |
|
165 | 165 | end |
|
166 | 166 | end |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } |
|
170 | 170 | def destroy |
|
171 | 171 | if @time_entry.destroy && @time_entry.destroyed? |
|
172 | 172 | respond_to do |format| |
|
173 | 173 | format.html { |
|
174 | 174 | flash[:notice] = l(:notice_successful_delete) |
|
175 | 175 | redirect_to :back |
|
176 | 176 | } |
|
177 | 177 | format.api { head :ok } |
|
178 | 178 | end |
|
179 | 179 | else |
|
180 | 180 | respond_to do |format| |
|
181 | 181 | format.html { |
|
182 |
flash[: |
|
|
182 | flash[:error] = l(:notice_unable_delete_time_entry) | |
|
183 | 183 | redirect_to :back |
|
184 | 184 | } |
|
185 | 185 | format.api { render_validation_errors(@time_entry) } |
|
186 | 186 | end |
|
187 | 187 | end |
|
188 | 188 | rescue ::ActionController::RedirectBackError |
|
189 | 189 | redirect_to :action => 'index', :project_id => @time_entry.project |
|
190 | 190 | end |
|
191 | 191 | |
|
192 | 192 | private |
|
193 | 193 | def find_time_entry |
|
194 | 194 | @time_entry = TimeEntry.find(params[:id]) |
|
195 | 195 | unless @time_entry.editable_by?(User.current) |
|
196 | 196 | render_403 |
|
197 | 197 | return false |
|
198 | 198 | end |
|
199 | 199 | @project = @time_entry.project |
|
200 | 200 | rescue ActiveRecord::RecordNotFound |
|
201 | 201 | render_404 |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | def find_project |
|
205 | 205 | if issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id]) |
|
206 | 206 | @issue = Issue.find(issue_id) |
|
207 | 207 | @project = @issue.project |
|
208 | 208 | elsif project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id]) |
|
209 | 209 | @project = Project.find(project_id) |
|
210 | 210 | else |
|
211 | 211 | render_404 |
|
212 | 212 | return false |
|
213 | 213 | end |
|
214 | 214 | rescue ActiveRecord::RecordNotFound |
|
215 | 215 | render_404 |
|
216 | 216 | end |
|
217 | 217 | |
|
218 | 218 | def find_optional_project |
|
219 | 219 | if !params[:issue_id].blank? |
|
220 | 220 | @issue = Issue.find(params[:issue_id]) |
|
221 | 221 | @project = @issue.project |
|
222 | 222 | elsif !params[:project_id].blank? |
|
223 | 223 | @project = Project.find(params[:project_id]) |
|
224 | 224 | end |
|
225 | 225 | deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | # Retrieves the date range based on predefined ranges or specific from/to param dates |
|
229 | 229 | def retrieve_date_range |
|
230 | 230 | @free_period = false |
|
231 | 231 | @from, @to = nil, nil |
|
232 | 232 | |
|
233 | 233 | if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) |
|
234 | 234 | case params[:period].to_s |
|
235 | 235 | when 'today' |
|
236 | 236 | @from = @to = Date.today |
|
237 | 237 | when 'yesterday' |
|
238 | 238 | @from = @to = Date.today - 1 |
|
239 | 239 | when 'current_week' |
|
240 | 240 | @from = Date.today - (Date.today.cwday - 1)%7 |
|
241 | 241 | @to = @from + 6 |
|
242 | 242 | when 'last_week' |
|
243 | 243 | @from = Date.today - 7 - (Date.today.cwday - 1)%7 |
|
244 | 244 | @to = @from + 6 |
|
245 | 245 | when '7_days' |
|
246 | 246 | @from = Date.today - 7 |
|
247 | 247 | @to = Date.today |
|
248 | 248 | when 'current_month' |
|
249 | 249 | @from = Date.civil(Date.today.year, Date.today.month, 1) |
|
250 | 250 | @to = (@from >> 1) - 1 |
|
251 | 251 | when 'last_month' |
|
252 | 252 | @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 |
|
253 | 253 | @to = (@from >> 1) - 1 |
|
254 | 254 | when '30_days' |
|
255 | 255 | @from = Date.today - 30 |
|
256 | 256 | @to = Date.today |
|
257 | 257 | when 'current_year' |
|
258 | 258 | @from = Date.civil(Date.today.year, 1, 1) |
|
259 | 259 | @to = Date.civil(Date.today.year, 12, 31) |
|
260 | 260 | end |
|
261 | 261 | elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) |
|
262 | 262 | begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end |
|
263 | 263 | begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end |
|
264 | 264 | @free_period = true |
|
265 | 265 | else |
|
266 | 266 | # default |
|
267 | 267 | end |
|
268 | 268 | |
|
269 | 269 | @from, @to = @to, @from if @from && @to && @from > @to |
|
270 | 270 | @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) |
|
271 | 271 | @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) |
|
272 | 272 | end |
|
273 | 273 | |
|
274 | 274 | end |
General Comments 0
You need to be logged in to leave comments.
Login now