##// END OF EJS Templates
fixed: unable to attach a file when creating an issue ("attachment: invalid" error)...
Jean-Philippe Lang -
r119:3bfaa20c057f
parent child
Show More
@@ -1,533 +1,535
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 ProjectsController < ApplicationController
19 19 layout 'base'
20 20 before_filter :find_project, :authorize, :except => [ :index, :list, :add ]
21 21 before_filter :require_admin, :only => [ :add, :destroy ]
22 22
23 23 helper :sort
24 24 include SortHelper
25 25 helper :custom_fields
26 26 include CustomFieldsHelper
27 27 helper :ifpdf
28 28 include IfpdfHelper
29 29 helper IssuesHelper
30 30 helper :queries
31 31 include QueriesHelper
32 32
33 33 def index
34 34 list
35 35 render :action => 'list' unless request.xhr?
36 36 end
37 37
38 38 # Lists public projects
39 39 def list
40 40 sort_init 'name', 'asc'
41 41 sort_update
42 42 @project_count = Project.count(["is_public=?", true])
43 43 @project_pages = Paginator.new self, @project_count,
44 44 15,
45 45 @params['page']
46 46 @projects = Project.find :all, :order => sort_clause,
47 47 :conditions => ["is_public=?", true],
48 48 :limit => @project_pages.items_per_page,
49 49 :offset => @project_pages.current.offset
50 50
51 51 render :action => "list", :layout => false if request.xhr?
52 52 end
53 53
54 54 # Add a new project
55 55 def add
56 56 @custom_fields = IssueCustomField.find(:all)
57 57 @root_projects = Project.find(:all, :conditions => "parent_id is null")
58 58 @project = Project.new(params[:project])
59 59 if request.get?
60 60 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
61 61 else
62 62 @project.custom_fields = CustomField.find(@params[:custom_field_ids]) if @params[:custom_field_ids]
63 63 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
64 64 @project.custom_values = @custom_values
65 65 if params[:repository_enabled] && params[:repository_enabled] == "1"
66 66 @project.repository = Repository.new
67 67 @project.repository.attributes = params[:repository]
68 68 end
69 69 if @project.save
70 70 flash[:notice] = l(:notice_successful_create)
71 71 redirect_to :controller => 'admin', :action => 'projects'
72 72 end
73 73 end
74 74 end
75 75
76 76 # Show @project
77 77 def show
78 78 @custom_values = @project.custom_values.find(:all, :include => :custom_field)
79 79 @members = @project.members.find(:all, :include => [:user, :role])
80 80 @subprojects = @project.children if @project.children_count > 0
81 81 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "news.created_on DESC")
82 82 @trackers = Tracker.find(:all)
83 83 end
84 84
85 85 def settings
86 86 @root_projects = Project::find(:all, :conditions => ["parent_id is null and id <> ?", @project.id])
87 87 @custom_fields = IssueCustomField::find_all
88 88 @issue_category ||= IssueCategory.new
89 89 @member ||= @project.members.new
90 90 @roles = Role.find_all
91 91 @users = User.find_all - @project.members.find(:all, :include => :user).collect{|m| m.user }
92 92 @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
93 93 end
94 94
95 95 # Edit @project
96 96 def edit
97 97 if request.post?
98 98 @project.custom_fields = IssueCustomField.find(@params[:custom_field_ids]) if @params[:custom_field_ids]
99 99 if params[:custom_fields]
100 100 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
101 101 @project.custom_values = @custom_values
102 102 end
103 103 if params[:repository_enabled]
104 104 case params[:repository_enabled]
105 105 when "0"
106 106 @project.repository = nil
107 107 when "1"
108 108 @project.repository ||= Repository.new
109 109 @project.repository.attributes = params[:repository]
110 110 end
111 111 end
112 112 @project.attributes = params[:project]
113 113 if @project.save
114 114 flash[:notice] = l(:notice_successful_update)
115 115 redirect_to :action => 'settings', :id => @project
116 116 else
117 117 settings
118 118 render :action => 'settings'
119 119 end
120 120 end
121 121 end
122 122
123 123 # Delete @project
124 124 def destroy
125 125 if request.post? and params[:confirm]
126 126 @project.destroy
127 127 redirect_to :controller => 'admin', :action => 'projects'
128 128 end
129 129 end
130 130
131 131 # Add a new issue category to @project
132 132 def add_issue_category
133 133 if request.post?
134 134 @issue_category = @project.issue_categories.build(params[:issue_category])
135 135 if @issue_category.save
136 136 flash[:notice] = l(:notice_successful_create)
137 137 redirect_to :action => 'settings', :id => @project
138 138 else
139 139 settings
140 140 render :action => 'settings'
141 141 end
142 142 end
143 143 end
144 144
145 145 # Add a new version to @project
146 146 def add_version
147 147 @version = @project.versions.build(params[:version])
148 148 if request.post? and @version.save
149 149 flash[:notice] = l(:notice_successful_create)
150 150 redirect_to :action => 'settings', :id => @project
151 151 end
152 152 end
153 153
154 154 # Add a new member to @project
155 155 def add_member
156 156 @member = @project.members.build(params[:member])
157 157 if request.post?
158 158 if @member.save
159 159 flash[:notice] = l(:notice_successful_create)
160 160 redirect_to :action => 'settings', :id => @project
161 161 else
162 162 settings
163 163 render :action => 'settings'
164 164 end
165 165 end
166 166 end
167 167
168 168 # Show members list of @project
169 169 def list_members
170 170 @members = @project.members
171 171 end
172 172
173 173 # Add a new document to @project
174 174 def add_document
175 175 @categories = Enumeration::get_values('DCAT')
176 176 @document = @project.documents.build(params[:document])
177 177 if request.post?
178 178 # Save the attachment
179 179 if params[:attachment][:file].size > 0
180 180 @attachment = @document.attachments.build(params[:attachment])
181 181 @attachment.author_id = self.logged_in_user.id if self.logged_in_user
182 182 end
183 183 if @document.save
184 184 flash[:notice] = l(:notice_successful_create)
185 185 redirect_to :action => 'list_documents', :id => @project
186 186 end
187 187 end
188 188 end
189 189
190 190 # Show documents list of @project
191 191 def list_documents
192 192 @documents = @project.documents.find :all, :include => :category
193 193 end
194 194
195 195 # Add a new issue to @project
196 196 def add_issue
197 197 @tracker = Tracker.find(params[:tracker_id])
198 198 @priorities = Enumeration::get_values('IPRI')
199 199 @issue = Issue.new(:project => @project, :tracker => @tracker)
200 200 if request.get?
201 201 @issue.start_date = Date.today
202 202 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
203 203 else
204 204 @issue.attributes = params[:issue]
205 205 @issue.author_id = self.logged_in_user.id if self.logged_in_user
206 206 # Multiple file upload
207 @attachments = []
207 208 params[:attachments].each { |a|
208 @attachment = @issue.attachments.build(:file => a, :author => self.logged_in_user) unless a.size == 0
209 @attachments << Attachment.new(:container => @issue, :file => a, :author => logged_in_user) unless a.size == 0
209 210 } if params[:attachments] and params[:attachments].is_a? Array
210 211 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
211 212 @issue.custom_values = @custom_values
212 213 if @issue.save
214 @attachments.each(&:save)
213 215 flash[:notice] = l(:notice_successful_create)
214 216 Mailer.deliver_issue_add(@issue) if Permission.find_by_controller_and_action(@params[:controller], @params[:action]).mail_enabled?
215 217 redirect_to :action => 'list_issues', :id => @project
216 218 end
217 219 end
218 220 end
219 221
220 222 # Show filtered/sorted issues list of @project
221 223 def list_issues
222 224 sort_init 'issues.id', 'desc'
223 225 sort_update
224 226
225 227 retrieve_query
226 228
227 229 @results_per_page_options = [ 15, 25, 50, 100 ]
228 230 if params[:per_page] and @results_per_page_options.include? params[:per_page].to_i
229 231 @results_per_page = params[:per_page].to_i
230 232 session[:results_per_page] = @results_per_page
231 233 else
232 234 @results_per_page = session[:results_per_page] || 25
233 235 end
234 236
235 237 if @query.valid?
236 238 @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
237 239 @issue_pages = Paginator.new self, @issue_count, @results_per_page, @params['page']
238 240 @issues = Issue.find :all, :order => sort_clause,
239 241 :include => [ :author, :status, :tracker, :project ],
240 242 :conditions => @query.statement,
241 243 :limit => @issue_pages.items_per_page,
242 244 :offset => @issue_pages.current.offset
243 245 end
244 246 render :layout => false if request.xhr?
245 247 end
246 248
247 249 # Export filtered/sorted issues list to CSV
248 250 def export_issues_csv
249 251 sort_init 'issues.id', 'desc'
250 252 sort_update
251 253
252 254 retrieve_query
253 255 render :action => 'list_issues' and return unless @query.valid?
254 256
255 257 @issues = Issue.find :all, :order => sort_clause,
256 258 :include => [ :author, :status, :tracker, :project, :custom_values ],
257 259 :conditions => @query.statement
258 260
259 261 ic = Iconv.new('ISO-8859-1', 'UTF-8')
260 262 export = StringIO.new
261 263 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
262 264 # csv header fields
263 265 headers = [ "#", l(:field_status), l(:field_tracker), l(:field_subject), l(:field_author), l(:field_created_on), l(:field_updated_on) ]
264 266 for custom_field in @project.all_custom_fields
265 267 headers << custom_field.name
266 268 end
267 269 csv << headers.collect {|c| ic.iconv(c) }
268 270 # csv lines
269 271 @issues.each do |issue|
270 272 fields = [issue.id, issue.status.name, issue.tracker.name, issue.subject, issue.author.display_name, l_datetime(issue.created_on), l_datetime(issue.updated_on)]
271 273 for custom_field in @project.all_custom_fields
272 274 fields << (show_value issue.custom_value_for(custom_field))
273 275 end
274 276 csv << fields.collect {|c| ic.iconv(c.to_s) }
275 277 end
276 278 end
277 279 export.rewind
278 280 send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
279 281 end
280 282
281 283 # Export filtered/sorted issues to PDF
282 284 def export_issues_pdf
283 285 sort_init 'issues.id', 'desc'
284 286 sort_update
285 287
286 288 retrieve_query
287 289 render :action => 'list_issues' and return unless @query.valid?
288 290
289 291 @issues = Issue.find :all, :order => sort_clause,
290 292 :include => [ :author, :status, :tracker, :project, :custom_values ],
291 293 :conditions => @query.statement
292 294
293 295 @options_for_rfpdf ||= {}
294 296 @options_for_rfpdf[:file_name] = "export.pdf"
295 297 render :layout => false
296 298 end
297 299
298 300 def move_issues
299 301 @issues = @project.issues.find(params[:issue_ids]) if params[:issue_ids]
300 302 redirect_to :action => 'list_issues', :id => @project and return unless @issues
301 303 @projects = []
302 304 # find projects to which the user is allowed to move the issue
303 305 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role_id)}
304 306 # issue can be moved to any tracker
305 307 @trackers = Tracker.find(:all)
306 308 if request.post? and params[:new_project_id] and params[:new_tracker_id]
307 309 new_project = Project.find(params[:new_project_id])
308 310 new_tracker = Tracker.find(params[:new_tracker_id])
309 311 @issues.each { |i|
310 312 # project dependent properties
311 313 unless i.project_id == new_project.id
312 314 i.category = nil
313 315 i.fixed_version = nil
314 316 end
315 317 # move the issue
316 318 i.project = new_project
317 319 i.tracker = new_tracker
318 320 i.save
319 321 }
320 322 flash[:notice] = l(:notice_successful_update)
321 323 redirect_to :action => 'list_issues', :id => @project
322 324 end
323 325 end
324 326
325 327 def add_query
326 328 @query = Query.new(params[:query])
327 329 @query.project = @project
328 330 @query.user = logged_in_user
329 331
330 332 params[:fields].each do |field|
331 333 @query.add_filter(field, params[:operators][field], params[:values][field])
332 334 end if params[:fields]
333 335
334 336 if request.post? and @query.save
335 337 flash[:notice] = l(:notice_successful_create)
336 338 redirect_to :controller => 'reports', :action => 'issue_report', :id => @project
337 339 end
338 340 render :layout => false if request.xhr?
339 341 end
340 342
341 343 # Add a news to @project
342 344 def add_news
343 345 @news = News.new(:project => @project)
344 346 if request.post?
345 347 @news.attributes = params[:news]
346 348 @news.author_id = self.logged_in_user.id if self.logged_in_user
347 349 if @news.save
348 350 flash[:notice] = l(:notice_successful_create)
349 351 redirect_to :action => 'list_news', :id => @project
350 352 end
351 353 end
352 354 end
353 355
354 356 # Show news list of @project
355 357 def list_news
356 358 @news_pages, @news = paginate :news, :per_page => 10, :conditions => ["project_id=?", @project.id], :include => :author, :order => "news.created_on DESC"
357 359 render :action => "list_news", :layout => false if request.xhr?
358 360 end
359 361
360 362 def add_file
361 363 @attachment = Attachment.new(params[:attachment])
362 364 if request.post? and params[:attachment][:file].size > 0
363 365 @attachment.container = @project.versions.find_by_id(params[:version_id])
364 366 @attachment.author = logged_in_user
365 367 if @attachment.save
366 368 flash[:notice] = l(:notice_successful_create)
367 369 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
368 370 end
369 371 end
370 372 @versions = @project.versions
371 373 end
372 374
373 375 def list_files
374 376 @versions = @project.versions
375 377 end
376 378
377 379 # Show changelog for @project
378 380 def changelog
379 381 @trackers = Tracker.find(:all, :conditions => ["is_in_chlog=?", true])
380 382 if request.get?
381 383 @selected_tracker_ids = @trackers.collect {|t| t.id.to_s }
382 384 else
383 385 @selected_tracker_ids = params[:tracker_ids].collect { |id| id.to_i.to_s } if params[:tracker_ids] and params[:tracker_ids].is_a? Array
384 386 end
385 387 @selected_tracker_ids ||= []
386 388 @fixed_issues = @project.issues.find(:all,
387 389 :include => [ :fixed_version, :status, :tracker ],
388 390 :conditions => [ "issue_statuses.is_closed=? and issues.tracker_id in (#{@selected_tracker_ids.join(',')}) and issues.fixed_version_id is not null", true],
389 391 :order => "versions.effective_date DESC, issues.id DESC"
390 392 ) unless @selected_tracker_ids.empty?
391 393 @fixed_issues ||= []
392 394 end
393 395
394 396 def activity
395 397 if params[:year] and params[:year].to_i > 1900
396 398 @year = params[:year].to_i
397 399 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
398 400 @month = params[:month].to_i
399 401 end
400 402 end
401 403 @year ||= Date.today.year
402 404 @month ||= Date.today.month
403 405
404 406 @date_from = Date.civil(@year, @month, 1)
405 407 @date_to = (@date_from >> 1)-1
406 408
407 409 @events_by_day = {}
408 410
409 411 unless params[:show_issues] == "0"
410 412 @project.issues.find(:all, :include => [:author, :status], :conditions => ["issues.created_on>=? and issues.created_on<=?", @date_from, @date_to] ).each { |i|
411 413 @events_by_day[i.created_on.to_date] ||= []
412 414 @events_by_day[i.created_on.to_date] << i
413 415 }
414 416 @show_issues = 1
415 417 end
416 418
417 419 unless params[:show_news] == "0"
418 420 @project.news.find(:all, :conditions => ["news.created_on>=? and news.created_on<=?", @date_from, @date_to], :include => :author ).each { |i|
419 421 @events_by_day[i.created_on.to_date] ||= []
420 422 @events_by_day[i.created_on.to_date] << i
421 423 }
422 424 @show_news = 1
423 425 end
424 426
425 427 unless params[:show_files] == "0"
426 428 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN versions ON versions.id = attachments.container_id", :conditions => ["attachments.container_type='Version' and versions.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
427 429 @events_by_day[i.created_on.to_date] ||= []
428 430 @events_by_day[i.created_on.to_date] << i
429 431 }
430 432 @show_files = 1
431 433 end
432 434
433 435 unless params[:show_documents] == "0"
434 436 @project.documents.find(:all, :conditions => ["documents.created_on>=? and documents.created_on<=?", @date_from, @date_to] ).each { |i|
435 437 @events_by_day[i.created_on.to_date] ||= []
436 438 @events_by_day[i.created_on.to_date] << i
437 439 }
438 440 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN documents ON documents.id = attachments.container_id", :conditions => ["attachments.container_type='Document' and documents.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
439 441 @events_by_day[i.created_on.to_date] ||= []
440 442 @events_by_day[i.created_on.to_date] << i
441 443 }
442 444 @show_documents = 1
443 445 end
444 446
445 447 render :layout => false if request.xhr?
446 448 end
447 449
448 450 def calendar
449 451 if params[:year] and params[:year].to_i > 1900
450 452 @year = params[:year].to_i
451 453 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
452 454 @month = params[:month].to_i
453 455 end
454 456 end
455 457 @year ||= Date.today.year
456 458 @month ||= Date.today.month
457 459
458 460 @date_from = Date.civil(@year, @month, 1)
459 461 @date_to = (@date_from >> 1)-1
460 462 # start on monday
461 463 @date_from = @date_from - (@date_from.cwday-1)
462 464 # finish on sunday
463 465 @date_to = @date_to + (7-@date_to.cwday)
464 466
465 467 @issues = @project.issues.find(:all, :include => :tracker, :conditions => ["((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?))", @date_from, @date_to, @date_from, @date_to])
466 468 render :layout => false if request.xhr?
467 469 end
468 470
469 471 def gantt
470 472 if params[:year] and params[:year].to_i >0
471 473 @year_from = params[:year].to_i
472 474 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
473 475 @month_from = params[:month].to_i
474 476 else
475 477 @month_from = 1
476 478 end
477 479 else
478 480 @month_from ||= (Date.today << 1).month
479 481 @year_from ||= (Date.today << 1).year
480 482 end
481 483
482 484 @zoom = (params[:zoom].to_i > 0 and params[:zoom].to_i < 5) ? params[:zoom].to_i : 2
483 485 @months = (params[:months].to_i > 0 and params[:months].to_i < 25) ? params[:months].to_i : 6
484 486
485 487 @date_from = Date.civil(@year_from, @month_from, 1)
486 488 @date_to = (@date_from >> @months) - 1
487 489 @issues = @project.issues.find(:all, :order => "start_date, due_date", :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to])
488 490
489 491 if params[:output]=='pdf'
490 492 @options_for_rfpdf ||= {}
491 493 @options_for_rfpdf[:file_name] = "gantt.pdf"
492 494 render :template => "projects/gantt.rfpdf", :layout => false
493 495 else
494 496 render :template => "projects/gantt.rhtml"
495 497 end
496 498 end
497 499
498 500 private
499 501 # Find project of id params[:id]
500 502 # if not found, redirect to project list
501 503 # Used as a before_filter
502 504 def find_project
503 505 @project = Project.find(params[:id])
504 506 @html_title = @project.name
505 507 rescue
506 508 redirect_to :action => 'list'
507 509 end
508 510
509 511 # Retrieve query from session or build a new query
510 512 def retrieve_query
511 513 if params[:query_id]
512 514 @query = @project.queries.find(params[:query_id])
513 515 else
514 516 if params[:set_filter] or !session[:query] or session[:query].project_id != @project.id
515 517 # Give it a name, required to be valid
516 518 @query = Query.new(:name => "_")
517 519 @query.project = @project
518 520 if params[:fields] and params[:fields].is_a? Array
519 521 params[:fields].each do |field|
520 522 @query.add_filter(field, params[:operators][field], params[:values][field])
521 523 end
522 524 else
523 525 @query.available_filters.keys.each do |field|
524 526 @query.add_short_filter(field, params[field]) if params[field]
525 527 end
526 528 end
527 529 session[:query] = @query
528 530 else
529 531 @query = session[:query]
530 532 end
531 533 end
532 534 end
533 535 end
General Comments 0
You need to be logged in to leave comments. Login now