##// END OF EJS Templates
Adds custom fields on time entries (#772)....
Jean-Philippe Lang -
r1672:898fac293b3d
parent child
Show More
@@ -0,0 +1,23
1 # redMine - project management software
2 # Copyright (C) 2008 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 TimeEntryCustomField < CustomField
19 def type_name
20 :label_spent_time
21 end
22 end
23
@@ -1,87 +1,89
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 CustomFieldsController < ApplicationController
19 19 layout 'base'
20 20 before_filter :require_admin
21 21
22 22 def index
23 23 list
24 24 render :action => 'list' unless request.xhr?
25 25 end
26 26
27 27 def list
28 28 @custom_fields_by_type = CustomField.find(:all).group_by {|f| f.class.name }
29 29 @tab = params[:tab] || 'IssueCustomField'
30 30 render :action => "list", :layout => false if request.xhr?
31 31 end
32 32
33 33 def new
34 34 case params[:type]
35 35 when "IssueCustomField"
36 36 @custom_field = IssueCustomField.new(params[:custom_field])
37 37 @custom_field.trackers = Tracker.find(params[:tracker_ids]) if params[:tracker_ids]
38 38 when "UserCustomField"
39 39 @custom_field = UserCustomField.new(params[:custom_field])
40 40 when "ProjectCustomField"
41 41 @custom_field = ProjectCustomField.new(params[:custom_field])
42 when "TimeEntryCustomField"
43 @custom_field = TimeEntryCustomField.new(params[:custom_field])
42 44 else
43 45 redirect_to :action => 'list'
44 46 return
45 47 end
46 48 if request.post? and @custom_field.save
47 49 flash[:notice] = l(:notice_successful_create)
48 50 redirect_to :action => 'list', :tab => @custom_field.class.name
49 51 end
50 52 @trackers = Tracker.find(:all, :order => 'position')
51 53 end
52 54
53 55 def edit
54 56 @custom_field = CustomField.find(params[:id])
55 57 if request.post? and @custom_field.update_attributes(params[:custom_field])
56 58 if @custom_field.is_a? IssueCustomField
57 59 @custom_field.trackers = params[:tracker_ids] ? Tracker.find(params[:tracker_ids]) : []
58 60 end
59 61 flash[:notice] = l(:notice_successful_update)
60 62 redirect_to :action => 'list', :tab => @custom_field.class.name
61 63 end
62 64 @trackers = Tracker.find(:all, :order => 'position')
63 65 end
64 66
65 67 def move
66 68 @custom_field = CustomField.find(params[:id])
67 69 case params[:position]
68 70 when 'highest'
69 71 @custom_field.move_to_top
70 72 when 'higher'
71 73 @custom_field.move_higher
72 74 when 'lower'
73 75 @custom_field.move_lower
74 76 when 'lowest'
75 77 @custom_field.move_to_bottom
76 78 end if params[:position]
77 79 redirect_to :action => 'list', :tab => @custom_field.class.name
78 80 end
79 81
80 82 def destroy
81 83 @custom_field = CustomField.find(params[:id]).destroy
82 84 redirect_to :action => 'list', :tab => @custom_field.class.name
83 85 rescue
84 86 flash[:error] = "Unable to delete custom field"
85 87 redirect_to :action => 'list'
86 88 end
87 89 end
@@ -1,429 +1,430
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 IssuesController < ApplicationController
19 19 layout 'base'
20 20 menu_item :new_issue, :only => :new
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :reply, :destroy_attachment]
23 23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
24 24 before_filter :find_project, :only => [:new, :update_form, :preview]
25 25 before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu]
26 26 before_filter :find_optional_project, :only => [:index, :changes]
27 27 accept_key_auth :index, :changes
28 28
29 29 helper :journals
30 30 helper :projects
31 31 include ProjectsHelper
32 32 helper :custom_fields
33 33 include CustomFieldsHelper
34 34 helper :ifpdf
35 35 include IfpdfHelper
36 36 helper :issue_relations
37 37 include IssueRelationsHelper
38 38 helper :watchers
39 39 include WatchersHelper
40 40 helper :attachments
41 41 include AttachmentsHelper
42 42 helper :queries
43 43 helper :sort
44 44 include SortHelper
45 45 include IssuesHelper
46 46 helper :timelog
47 47
48 48 def index
49 49 sort_init "#{Issue.table_name}.id", "desc"
50 50 sort_update
51 51 retrieve_query
52 52 if @query.valid?
53 53 limit = per_page_option
54 54 respond_to do |format|
55 55 format.html { }
56 56 format.atom { }
57 57 format.csv { limit = Setting.issues_export_limit.to_i }
58 58 format.pdf { limit = Setting.issues_export_limit.to_i }
59 59 end
60 60 @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
61 61 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
62 62 @issues = Issue.find :all, :order => sort_clause,
63 63 :include => [ :assigned_to, :status, :tracker, :project, :priority, :category, :fixed_version ],
64 64 :conditions => @query.statement,
65 65 :limit => limit,
66 66 :offset => @issue_pages.current.offset
67 67 respond_to do |format|
68 68 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
69 69 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
70 70 format.csv { send_data(issues_to_csv(@issues, @project).read, :type => 'text/csv; header=present', :filename => 'export.csv') }
71 71 format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') }
72 72 end
73 73 else
74 74 # Send html if the query is not valid
75 75 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
76 76 end
77 77 rescue ActiveRecord::RecordNotFound
78 78 render_404
79 79 end
80 80
81 81 def changes
82 82 sort_init "#{Issue.table_name}.id", "desc"
83 83 sort_update
84 84 retrieve_query
85 85 if @query.valid?
86 86 @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ],
87 87 :conditions => @query.statement,
88 88 :limit => 25,
89 89 :order => "#{Journal.table_name}.created_on DESC"
90 90 end
91 91 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
92 92 render :layout => false, :content_type => 'application/atom+xml'
93 93 rescue ActiveRecord::RecordNotFound
94 94 render_404
95 95 end
96 96
97 97 def show
98 98 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
99 99 @journals.each_with_index {|j,i| j.indice = i+1}
100 100 @journals.reverse! if User.current.wants_comments_in_reverse_order?
101 101 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
102 102 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
103 103 @priorities = Enumeration::get_values('IPRI')
104 104 @time_entry = TimeEntry.new
105 105 respond_to do |format|
106 106 format.html { render :template => 'issues/show.rhtml' }
107 107 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
108 108 format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
109 109 end
110 110 end
111 111
112 112 # Add a new issue
113 113 # The new issue will be created from an existing one if copy_from parameter is given
114 114 def new
115 115 @issue = Issue.new
116 116 @issue.copy_from(params[:copy_from]) if params[:copy_from]
117 117 @issue.project = @project
118 118 # Tracker must be set before custom field values
119 119 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
120 120 if @issue.tracker.nil?
121 121 flash.now[:error] = 'No tracker is associated to this project. Please check the Project settings.'
122 122 render :nothing => true, :layout => true
123 123 return
124 124 end
125 125 @issue.attributes = params[:issue]
126 126 @issue.author = User.current
127 127
128 128 default_status = IssueStatus.default
129 129 unless default_status
130 130 flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
131 131 render :nothing => true, :layout => true
132 132 return
133 133 end
134 134 @issue.status = default_status
135 135 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq
136 136
137 137 if request.get? || request.xhr?
138 138 @issue.start_date ||= Date.today
139 139 else
140 140 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
141 141 # Check that the user is allowed to apply the requested status
142 142 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
143 143 if @issue.save
144 144 attach_files(@issue, params[:attachments])
145 145 flash[:notice] = l(:notice_successful_create)
146 146 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
147 147 redirect_to :controller => 'issues', :action => 'show', :id => @issue
148 148 return
149 149 end
150 150 end
151 151 @priorities = Enumeration::get_values('IPRI')
152 152 render :layout => !request.xhr?
153 153 end
154 154
155 155 # Attributes that can be updated on workflow transition (without :edit permission)
156 156 # TODO: make it configurable (at least per role)
157 157 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
158 158
159 159 def edit
160 160 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
161 161 @priorities = Enumeration::get_values('IPRI')
162 162 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
163 @time_entry = TimeEntry.new
163 164
164 165 @notes = params[:notes]
165 166 journal = @issue.init_journal(User.current, @notes)
166 167 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
167 168 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
168 169 attrs = params[:issue].dup
169 170 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
170 171 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
171 172 @issue.attributes = attrs
172 173 end
173 174
174 175 if request.post?
175 176 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
176 177 @time_entry.attributes = params[:time_entry]
177 178 attachments = attach_files(@issue, params[:attachments])
178 179 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
179 180 if (@time_entry.hours.nil? || @time_entry.valid?) && @issue.save
180 181 # Log spend time
181 182 if current_role.allowed_to?(:log_time)
182 183 @time_entry.save
183 184 end
184 185 if !journal.new_record?
185 186 # Only send notification if something was actually changed
186 187 flash[:notice] = l(:notice_successful_update)
187 188 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
188 189 end
189 190 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
190 191 end
191 192 end
192 193 rescue ActiveRecord::StaleObjectError
193 194 # Optimistic locking exception
194 195 flash.now[:error] = l(:notice_locking_conflict)
195 196 end
196 197
197 198 def reply
198 199 journal = Journal.find(params[:journal_id]) if params[:journal_id]
199 200 if journal
200 201 user = journal.user
201 202 text = journal.notes
202 203 else
203 204 user = @issue.author
204 205 text = @issue.description
205 206 end
206 207 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
207 208 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
208 209 render(:update) { |page|
209 210 page.<< "$('notes').value = \"#{content}\";"
210 211 page.show 'update'
211 212 page << "Form.Element.focus('notes');"
212 213 page << "Element.scrollTo('update');"
213 214 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
214 215 }
215 216 end
216 217
217 218 # Bulk edit a set of issues
218 219 def bulk_edit
219 220 if request.post?
220 221 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
221 222 priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
222 223 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
223 224 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
224 225 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
225 226
226 227 unsaved_issue_ids = []
227 228 @issues.each do |issue|
228 229 journal = issue.init_journal(User.current, params[:notes])
229 230 issue.priority = priority if priority
230 231 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
231 232 issue.category = category if category || params[:category_id] == 'none'
232 233 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
233 234 issue.start_date = params[:start_date] unless params[:start_date].blank?
234 235 issue.due_date = params[:due_date] unless params[:due_date].blank?
235 236 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
236 237 # Don't save any change to the issue if the user is not authorized to apply the requested status
237 238 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
238 239 # Send notification for each issue (if changed)
239 240 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
240 241 else
241 242 # Keep unsaved issue ids to display them in flash error
242 243 unsaved_issue_ids << issue.id
243 244 end
244 245 end
245 246 if unsaved_issue_ids.empty?
246 247 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
247 248 else
248 249 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
249 250 end
250 251 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
251 252 return
252 253 end
253 254 # Find potential statuses the user could be allowed to switch issues to
254 255 @available_statuses = Workflow.find(:all, :include => :new_status,
255 256 :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
256 257 end
257 258
258 259 def move
259 260 @allowed_projects = []
260 261 # find projects to which the user is allowed to move the issue
261 262 if User.current.admin?
262 263 # admin is allowed to move issues to any active (visible) project
263 264 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name')
264 265 else
265 266 User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)}
266 267 end
267 268 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
268 269 @target_project ||= @project
269 270 @trackers = @target_project.trackers
270 271 if request.post?
271 272 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
272 273 unsaved_issue_ids = []
273 274 @issues.each do |issue|
274 275 issue.init_journal(User.current)
275 276 unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker)
276 277 end
277 278 if unsaved_issue_ids.empty?
278 279 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
279 280 else
280 281 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
281 282 end
282 283 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
283 284 return
284 285 end
285 286 render :layout => false if request.xhr?
286 287 end
287 288
288 289 def destroy
289 290 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
290 291 if @hours > 0
291 292 case params[:todo]
292 293 when 'destroy'
293 294 # nothing to do
294 295 when 'nullify'
295 296 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
296 297 when 'reassign'
297 298 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
298 299 if reassign_to.nil?
299 300 flash.now[:error] = l(:error_issue_not_found_in_project)
300 301 return
301 302 else
302 303 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
303 304 end
304 305 else
305 306 # display the destroy form
306 307 return
307 308 end
308 309 end
309 310 @issues.each(&:destroy)
310 311 redirect_to :action => 'index', :project_id => @project
311 312 end
312 313
313 314 def destroy_attachment
314 315 a = @issue.attachments.find(params[:attachment_id])
315 316 a.destroy
316 317 journal = @issue.init_journal(User.current)
317 318 journal.details << JournalDetail.new(:property => 'attachment',
318 319 :prop_key => a.id,
319 320 :old_value => a.filename)
320 321 journal.save
321 322 redirect_to :action => 'show', :id => @issue
322 323 end
323 324
324 325 def context_menu
325 326 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
326 327 if (@issues.size == 1)
327 328 @issue = @issues.first
328 329 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
329 330 @assignables = @issue.assignable_users
330 331 @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
331 332 end
332 333 projects = @issues.collect(&:project).compact.uniq
333 334 @project = projects.first if projects.size == 1
334 335
335 336 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
336 337 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
337 338 :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))),
338 339 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
339 340 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
340 341 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
341 342 }
342 343
343 344 @priorities = Enumeration.get_values('IPRI').reverse
344 345 @statuses = IssueStatus.find(:all, :order => 'position')
345 346 @back = request.env['HTTP_REFERER']
346 347
347 348 render :layout => false
348 349 end
349 350
350 351 def update_form
351 352 @issue = Issue.new(params[:issue])
352 353 render :action => :new, :layout => false
353 354 end
354 355
355 356 def preview
356 357 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
357 358 @attachements = @issue.attachments if @issue
358 359 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
359 360 render :partial => 'common/preview'
360 361 end
361 362
362 363 private
363 364 def find_issue
364 365 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
365 366 @project = @issue.project
366 367 rescue ActiveRecord::RecordNotFound
367 368 render_404
368 369 end
369 370
370 371 # Filter for bulk operations
371 372 def find_issues
372 373 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
373 374 raise ActiveRecord::RecordNotFound if @issues.empty?
374 375 projects = @issues.collect(&:project).compact.uniq
375 376 if projects.size == 1
376 377 @project = projects.first
377 378 else
378 379 # TODO: let users bulk edit/move/destroy issues from different projects
379 380 render_error 'Can not bulk edit/move/destroy issues from different projects' and return false
380 381 end
381 382 rescue ActiveRecord::RecordNotFound
382 383 render_404
383 384 end
384 385
385 386 def find_project
386 387 @project = Project.find(params[:project_id])
387 388 rescue ActiveRecord::RecordNotFound
388 389 render_404
389 390 end
390 391
391 392 def find_optional_project
392 393 return true unless params[:project_id]
393 394 @project = Project.find(params[:project_id])
394 395 authorize
395 396 rescue ActiveRecord::RecordNotFound
396 397 render_404
397 398 end
398 399
399 400 # Retrieve query from session or build a new query
400 401 def retrieve_query
401 402 if !params[:query_id].blank?
402 403 cond = "project_id IS NULL"
403 404 cond << " OR project_id = #{@project.id}" if @project
404 405 @query = Query.find(params[:query_id], :conditions => cond)
405 406 @query.project = @project
406 407 session[:query] = {:id => @query.id, :project_id => @query.project_id}
407 408 else
408 409 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
409 410 # Give it a name, required to be valid
410 411 @query = Query.new(:name => "_")
411 412 @query.project = @project
412 413 if params[:fields] and params[:fields].is_a? Array
413 414 params[:fields].each do |field|
414 415 @query.add_filter(field, params[:operators][field], params[:values][field])
415 416 end
416 417 else
417 418 @query.available_filters.keys.each do |field|
418 419 @query.add_short_filter(field, params[field]) if params[field]
419 420 end
420 421 end
421 422 session[:query] = {:project_id => @query.project_id, :filters => @query.filters}
422 423 else
423 424 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
424 425 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters])
425 426 @query.project = @project
426 427 end
427 428 end
428 429 end
429 430 end
@@ -1,87 +1,88
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 module CustomFieldsHelper
19 19
20 20 def custom_fields_tabs
21 21 tabs = [{:name => 'IssueCustomField', :label => :label_issue_plural},
22 {:name => 'TimeEntryCustomField', :label => :label_spent_time},
22 23 {:name => 'ProjectCustomField', :label => :label_project_plural},
23 24 {:name => 'UserCustomField', :label => :label_user_plural}
24 25 ]
25 26 end
26 27
27 28 # Return custom field html tag corresponding to its format
28 29 def custom_field_tag(name, custom_value)
29 30 custom_field = custom_value.custom_field
30 31 field_name = "#{name}[custom_field_values][#{custom_field.id}]"
31 32 field_id = "#{name}_custom_field_values_#{custom_field.id}"
32 33
33 34 case custom_field.field_format
34 35 when "date"
35 36 text_field_tag(field_name, custom_value.value, :id => field_id, :size => 10) +
36 37 calendar_for(field_id)
37 38 when "text"
38 39 text_area_tag(field_name, custom_value.value, :id => field_id, :rows => 3, :style => 'width:90%')
39 40 when "bool"
40 41 check_box_tag(field_name, '1', custom_value.true?, :id => field_id) + hidden_field_tag(field_name, '0')
41 42 when "list"
42 43 blank_option = custom_field.is_required? ?
43 44 (custom_field.default_value.blank? ? "<option value=\"\">--- #{l(:actionview_instancetag_blank_option)} ---</option>" : '') :
44 45 '<option></option>'
45 46 select_tag(field_name, blank_option + options_for_select(custom_field.possible_values, custom_value.value), :id => field_id)
46 47 else
47 48 text_field_tag(field_name, custom_value.value, :id => field_id)
48 49 end
49 50 end
50 51
51 52 # Return custom field label tag
52 53 def custom_field_label_tag(name, custom_value)
53 54 content_tag "label", custom_value.custom_field.name +
54 55 (custom_value.custom_field.is_required? ? " <span class=\"required\">*</span>" : ""),
55 56 :for => "#{name}_custom_field_values_#{custom_value.custom_field.id}",
56 57 :class => (custom_value.errors.empty? ? nil : "error" )
57 58 end
58 59
59 60 # Return custom field tag with its label tag
60 61 def custom_field_tag_with_label(name, custom_value)
61 62 custom_field_label_tag(name, custom_value) + custom_field_tag(name, custom_value)
62 63 end
63 64
64 65 # Return a string used to display a custom value
65 66 def show_value(custom_value)
66 67 return "" unless custom_value
67 68 format_value(custom_value.value, custom_value.custom_field.field_format)
68 69 end
69 70
70 71 # Return a string used to display a custom value
71 72 def format_value(value, field_format)
72 73 return "" unless value && !value.empty?
73 74 case field_format
74 75 when "date"
75 76 begin; format_date(value.to_date); rescue; value end
76 77 when "bool"
77 78 l_YesNo(value == "1")
78 79 else
79 80 value
80 81 end
81 82 end
82 83
83 84 # Return an array of custom field formats which can be used in select_tag
84 85 def custom_field_formats_for_select
85 86 CustomField::FIELD_FORMATS.sort {|a,b| a[1][:order]<=>b[1][:order]}.collect { |k| [ l(k[1][:name]), k[0] ] }
86 87 end
87 88 end
@@ -1,78 +1,79
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2008 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 TimeEntry < ActiveRecord::Base
19 19 # could have used polymorphic association
20 20 # project association here allows easy loading of time entries at project level with one database trip
21 21 belongs_to :project
22 22 belongs_to :issue
23 23 belongs_to :user
24 24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
25 25
26 26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27 27
28 acts_as_customizable
28 29 acts_as_event :title => Proc.new {|o| "#{o.user}: #{lwr(:label_f_hour, o.hours)} (#{(o.issue || o.project).event_title})"},
29 30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'details', :project_id => o.project}},
30 31 :author => :user,
31 32 :description => :comments
32 33
33 34 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
34 35 validates_numericality_of :hours, :allow_nil => true
35 36 validates_length_of :comments, :maximum => 255, :allow_nil => true
36 37
37 38 def after_initialize
38 39 if new_record? && self.activity.nil?
39 40 if default_activity = Enumeration.default('ACTI')
40 41 self.activity_id = default_activity.id
41 42 end
42 43 end
43 44 end
44 45
45 46 def before_validation
46 47 self.project = issue.project if issue && project.nil?
47 48 end
48 49
49 50 def validate
50 51 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
51 52 errors.add :project_id, :activerecord_error_invalid if project.nil?
52 53 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
53 54 end
54 55
55 56 def hours=(h)
56 57 write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
57 58 end
58 59
59 60 # tyear, tmonth, tweek assigned where setting spent_on attributes
60 61 # these attributes make time aggregations easier
61 62 def spent_on=(date)
62 63 super
63 64 self.tyear = spent_on ? spent_on.year : nil
64 65 self.tmonth = spent_on ? spent_on.month : nil
65 66 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
66 67 end
67 68
68 69 # Returns true if the time entry can be edited by usr, otherwise false
69 70 def editable_by?(usr)
70 71 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
71 72 end
72 73
73 74 def self.visible_by(usr)
74 75 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
75 76 yield
76 77 end
77 78 end
78 79 end
@@ -1,110 +1,113
1 1 <%= error_messages_for 'custom_field' %>
2 2
3 3 <script type="text/javascript">
4 4 //<![CDATA[
5 5 function toggle_custom_field_format() {
6 6 format = $("custom_field_field_format");
7 7 p_length = $("custom_field_min_length");
8 8 p_regexp = $("custom_field_regexp");
9 9 p_values = $("custom_field_possible_values");
10 10 p_searchable = $("custom_field_searchable");
11 11 p_default = $("custom_field_default_value");
12 12
13 13 p_default.setAttribute('type','text');
14 14 Element.show(p_default.parentNode);
15 15
16 16 switch (format.value) {
17 17 case "list":
18 18 Element.hide(p_length.parentNode);
19 19 Element.hide(p_regexp.parentNode);
20 20 if (p_searchable) Element.show(p_searchable.parentNode);
21 21 Element.show(p_values);
22 22 break;
23 23 case "bool":
24 24 p_default.setAttribute('type','checkbox');
25 25 Element.hide(p_length.parentNode);
26 26 Element.hide(p_regexp.parentNode);
27 27 if (p_searchable) Element.hide(p_searchable.parentNode);
28 28 Element.hide(p_values);
29 29 break;
30 30 case "date":
31 31 Element.hide(p_length.parentNode);
32 32 Element.hide(p_regexp.parentNode);
33 33 if (p_searchable) Element.hide(p_searchable.parentNode);
34 34 Element.hide(p_values);
35 35 break;
36 36 case "float":
37 37 case "int":
38 38 Element.show(p_length.parentNode);
39 39 Element.show(p_regexp.parentNode);
40 40 if (p_searchable) Element.hide(p_searchable.parentNode);
41 41 Element.hide(p_values);
42 42 break;
43 43 default:
44 44 Element.show(p_length.parentNode);
45 45 Element.show(p_regexp.parentNode);
46 46 if (p_searchable) Element.show(p_searchable.parentNode);
47 47 Element.hide(p_values);
48 48 break;
49 49 }
50 50 }
51 51
52 52 function addValueField() {
53 53 var f = $$('p#custom_field_possible_values span');
54 54 p = document.getElementById("custom_field_possible_values");
55 55 var v = f[0].cloneNode(true);
56 56 v.childNodes[0].value = "";
57 57 p.appendChild(v);
58 58 }
59 59
60 60 function deleteValueField(e) {
61 61 var f = $$('p#custom_field_possible_values span');
62 62 if (f.length == 1) {
63 63 e.parentNode.childNodes[0].value = "";
64 64 } else {
65 65 Element.remove(e.parentNode);
66 66 }
67 67 }
68 68
69 69 //]]>
70 70 </script>
71 71
72 72 <div class="box">
73 73 <p><%= f.text_field :name, :required => true %></p>
74 74 <p><%= f.select :field_format, custom_field_formats_for_select, {}, :onchange => "toggle_custom_field_format();" %></p>
75 75 <p><label for="custom_field_min_length"><%=l(:label_min_max_length)%></label>
76 76 <%= f.text_field :min_length, :size => 5, :no_label => true %> -
77 77 <%= f.text_field :max_length, :size => 5, :no_label => true %><br>(<%=l(:text_min_max_length_info)%>)</p>
78 78 <p><%= f.text_field :regexp, :size => 50 %><br>(<%=l(:text_regexp_info)%>)</p>
79 79 <p id="custom_field_possible_values"><label><%= l(:field_possible_values) %> <%= image_to_function "add.png", "addValueField();return false" %></label>
80 80 <% (@custom_field.possible_values.to_a + [""]).each do |value| %>
81 81 <span><%= text_field_tag 'custom_field[possible_values][]', value, :size => 30 %> <%= image_to_function "delete.png", "deleteValueField(this);return false" %><br /></span>
82 82 <% end %>
83 83 </p>
84 84 <p><%= @custom_field.field_format == 'bool' ? f.check_box(:default_value) : f.text_field(:default_value) %></p>
85 85 </div>
86 86
87 87 <div class="box">
88 88 <% case @custom_field.type.to_s
89 89 when "IssueCustomField" %>
90 90
91 91 <fieldset><legend><%=l(:label_tracker_plural)%></legend>
92 92 <% for tracker in @trackers %>
93 93 <%= check_box_tag "tracker_ids[]", tracker.id, (@custom_field.trackers.include? tracker) %> <%= tracker.name %>
94 94 <% end %>
95 95 </fieldset>
96 96 &nbsp;
97 97 <p><%= f.check_box :is_required %></p>
98 98 <p><%= f.check_box :is_for_all %></p>
99 99 <p><%= f.check_box :is_filter %></p>
100 100 <p><%= f.check_box :searchable %></p>
101 101
102 102 <% when "UserCustomField" %>
103 103 <p><%= f.check_box :is_required %></p>
104 104
105 105 <% when "ProjectCustomField" %>
106 106 <p><%= f.check_box :is_required %></p>
107 107
108 <% when "TimeEntryCustomField" %>
109 <p><%= f.check_box :is_required %></p>
110
108 111 <% end %>
109 112 </div>
110 113 <%= javascript_tag "toggle_custom_field_format();" %>
@@ -1,51 +1,54
1 1 <% labelled_tabular_form_for :issue, @issue,
2 2 :url => {:action => 'edit', :id => @issue},
3 3 :html => {:id => 'issue-form',
4 4 :class => nil,
5 5 :multipart => true} do |f| %>
6 6 <%= error_messages_for 'issue' %>
7 7 <%= error_messages_for 'time_entry' %>
8 8 <div class="box">
9 9 <% if @edit_allowed || !@allowed_statuses.empty? %>
10 10 <fieldset class="tabular"><legend><%= l(:label_change_properties) %>
11 11 <% if !@issue.new_record? && !@issue.errors.any? && @edit_allowed %>
12 12 <small>(<%= link_to l(:label_more), {}, :onclick => 'Effect.toggle("issue_descr_fields", "appear", {duration:0.3}); return false;' %>)</small>
13 13 <% end %>
14 14 </legend>
15 15 <%= render :partial => (@edit_allowed ? 'form' : 'form_update'), :locals => {:f => f} %>
16 16 </fieldset>
17 17 <% end %>
18 18 <% if authorize_for('timelog', 'edit') %>
19 19 <fieldset class="tabular"><legend><%= l(:button_log_time) %></legend>
20 20 <% fields_for :time_entry, @time_entry, { :builder => TabularFormBuilder, :lang => current_language} do |time_entry| %>
21 21 <div class="splitcontentleft">
22 22 <p><%= time_entry.text_field :hours, :size => 6, :label => :label_spent_time %> <%= l(:field_hours) %></p>
23 23 </div>
24 24 <div class="splitcontentright">
25 25 <p><%= time_entry.select :activity_id, activity_collection_for_select_options %></p>
26 26 </div>
27 27 <p><%= time_entry.text_field :comments, :size => 60 %></p>
28 <% @time_entry.custom_field_values.each do |value| %>
29 <p><%= custom_field_tag_with_label :time_entry, value %></p>
30 <% end %>
28 31 <% end %>
29 32 </fieldset>
30 33 <% end %>
31 34
32 35 <fieldset><legend><%= l(:field_notes) %></legend>
33 36 <%= text_area_tag 'notes', @notes, :cols => 60, :rows => 10, :class => 'wiki-edit' %>
34 37 <%= wikitoolbar_for 'notes' %>
35 38
36 39 <p><%=l(:label_attachment_plural)%><br /><%= render :partial => 'attachments/form' %></p>
37 40 </fieldset>
38 41 </div>
39 42
40 43 <%= f.hidden_field :lock_version %>
41 44 <%= submit_tag l(:button_submit) %>
42 45 <%= link_to_remote l(:label_preview),
43 46 { :url => { :controller => 'issues', :action => 'preview', :project_id => @project, :id => @issue },
44 47 :method => 'post',
45 48 :update => 'preview',
46 49 :with => 'Form.serialize("issue-form")',
47 50 :complete => "Element.scrollTo('preview')"
48 51 }, :accesskey => accesskey(:preview) %>
49 52 <% end %>
50 53
51 54 <div id="preview" class="wiki"></div>
@@ -1,17 +1,20
1 1 <h2><%= l(:label_spent_time) %></h2>
2 2
3 3 <% labelled_tabular_form_for :time_entry, @time_entry, :url => {:action => 'edit', :project_id => @time_entry.project} do |f| %>
4 4 <%= error_messages_for 'time_entry' %>
5 5 <%= back_url_hidden_field_tag %>
6 6
7 7 <div class="box">
8 8 <p><%= f.text_field :issue_id, :size => 6 %> <em><%= h("#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}") if @time_entry.issue %></em></p>
9 9 <p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
10 10 <p><%= f.text_field :hours, :size => 6, :required => true %></p>
11 11 <p><%= f.text_field :comments, :size => 100 %></p>
12 12 <p><%= f.select :activity_id, activity_collection_for_select_options, :required => true %></p>
13 <% @time_entry.custom_field_values.each do |value| %>
14 <p><%= custom_field_tag_with_label :time_entry, value %></p>
15 <% end %>
13 16 </div>
14 17
15 18 <%= submit_tag l(:button_save) %>
16 19
17 20 <% end %>
General Comments 0
You need to be logged in to leave comments. Login now