##// END OF EJS Templates
Fixed Iconv::IllegalSequence errors in csv exports...
Jean-Philippe Lang -
r567:600018d5adfd
parent child
Show More
@@ -1,667 +1,667
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 require 'csv'
19 19
20 20 class ProjectsController < ApplicationController
21 21 layout 'base'
22 22 before_filter :find_project, :except => [ :index, :list, :add ]
23 23 before_filter :authorize, :except => [ :index, :list, :add, :archive, :unarchive, :destroy ]
24 24 before_filter :require_admin, :only => [ :add, :archive, :unarchive, :destroy ]
25 25
26 26 cache_sweeper :project_sweeper, :only => [ :add, :edit, :archive, :unarchive, :destroy ]
27 27 cache_sweeper :issue_sweeper, :only => [ :add_issue ]
28 28
29 29 helper :sort
30 30 include SortHelper
31 31 helper :custom_fields
32 32 include CustomFieldsHelper
33 33 helper :ifpdf
34 34 include IfpdfHelper
35 35 helper IssuesHelper
36 36 helper :queries
37 37 include QueriesHelper
38 38 helper :repositories
39 39 include RepositoriesHelper
40 40
41 41 def index
42 42 list
43 43 render :action => 'list' unless request.xhr?
44 44 end
45 45
46 46 # Lists public projects
47 47 def list
48 48 sort_init "#{Project.table_name}.name", "asc"
49 49 sort_update
50 50 @project_count = Project.count(:all, :conditions => Project.visible_by(logged_in_user))
51 51 @project_pages = Paginator.new self, @project_count,
52 52 15,
53 53 params['page']
54 54 @projects = Project.find :all, :order => sort_clause,
55 55 :conditions => Project.visible_by(logged_in_user),
56 56 :include => :parent,
57 57 :limit => @project_pages.items_per_page,
58 58 :offset => @project_pages.current.offset
59 59
60 60 render :action => "list", :layout => false if request.xhr?
61 61 end
62 62
63 63 # Add a new project
64 64 def add
65 65 @custom_fields = IssueCustomField.find(:all)
66 66 @root_projects = Project.find(:all, :conditions => "parent_id is null")
67 67 @project = Project.new(params[:project])
68 68 if request.get?
69 69 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
70 70 else
71 71 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
72 72 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
73 73 @project.custom_values = @custom_values
74 74 if params[:repository_enabled] && params[:repository_enabled] == "1"
75 75 @project.repository = Repository.factory(params[:repository_scm])
76 76 @project.repository.attributes = params[:repository]
77 77 end
78 78 if "1" == params[:wiki_enabled]
79 79 @project.wiki = Wiki.new
80 80 @project.wiki.attributes = params[:wiki]
81 81 end
82 82 if @project.save
83 83 flash[:notice] = l(:notice_successful_create)
84 84 redirect_to :controller => 'admin', :action => 'projects'
85 85 end
86 86 end
87 87 end
88 88
89 89 # Show @project
90 90 def show
91 91 @custom_values = @project.custom_values.find(:all, :include => :custom_field)
92 92 @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role}
93 93 @subprojects = @project.active_children
94 94 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
95 95 @trackers = Tracker.find(:all, :order => 'position')
96 96 @open_issues_by_tracker = Issue.count(:group => :tracker, :joins => "INNER JOIN #{IssueStatus.table_name} ON #{IssueStatus.table_name}.id = #{Issue.table_name}.status_id", :conditions => ["project_id=? and #{IssueStatus.table_name}.is_closed=?", @project.id, false])
97 97 @total_issues_by_tracker = Issue.count(:group => :tracker, :conditions => ["project_id=?", @project.id])
98 98 end
99 99
100 100 def settings
101 101 @root_projects = Project::find(:all, :conditions => ["parent_id is null and id <> ?", @project.id])
102 102 @custom_fields = IssueCustomField.find(:all)
103 103 @issue_category ||= IssueCategory.new
104 104 @member ||= @project.members.new
105 105 @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
106 106 end
107 107
108 108 # Edit @project
109 109 def edit
110 110 if request.post?
111 111 @project.custom_fields = IssueCustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
112 112 if params[:custom_fields]
113 113 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
114 114 @project.custom_values = @custom_values
115 115 end
116 116 if params[:repository_enabled]
117 117 case params[:repository_enabled]
118 118 when "0"
119 119 @project.repository = nil
120 120 when "1"
121 121 @project.repository ||= Repository.factory(params[:repository_scm])
122 122 @project.repository.update_attributes params[:repository] if @project.repository
123 123 end
124 124 end
125 125 if params[:wiki_enabled]
126 126 case params[:wiki_enabled]
127 127 when "0"
128 128 @project.wiki.destroy if @project.wiki
129 129 when "1"
130 130 @project.wiki ||= Wiki.new
131 131 @project.wiki.update_attributes params[:wiki]
132 132 end
133 133 end
134 134 @project.attributes = params[:project]
135 135 if @project.save
136 136 flash[:notice] = l(:notice_successful_update)
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 def archive
146 146 @project.archive if request.post? && @project.active?
147 147 redirect_to :controller => 'admin', :action => 'projects'
148 148 end
149 149
150 150 def unarchive
151 151 @project.unarchive if request.post? && !@project.active?
152 152 redirect_to :controller => 'admin', :action => 'projects'
153 153 end
154 154
155 155 # Delete @project
156 156 def destroy
157 157 @project_to_destroy = @project
158 158 if request.post? and params[:confirm]
159 159 @project_to_destroy.destroy
160 160 redirect_to :controller => 'admin', :action => 'projects'
161 161 end
162 162 # hide project in layout
163 163 @project = nil
164 164 end
165 165
166 166 # Add a new issue category to @project
167 167 def add_issue_category
168 168 if request.post?
169 169 @issue_category = @project.issue_categories.build(params[:issue_category])
170 170 if @issue_category.save
171 171 flash[:notice] = l(:notice_successful_create)
172 172 redirect_to :action => 'settings', :tab => 'categories', :id => @project
173 173 else
174 174 settings
175 175 render :action => 'settings'
176 176 end
177 177 end
178 178 end
179 179
180 180 # Add a new version to @project
181 181 def add_version
182 182 @version = @project.versions.build(params[:version])
183 183 if request.post? and @version.save
184 184 flash[:notice] = l(:notice_successful_create)
185 185 redirect_to :action => 'settings', :tab => 'versions', :id => @project
186 186 end
187 187 end
188 188
189 189 # Add a new member to @project
190 190 def add_member
191 191 @member = @project.members.build(params[:member])
192 192 if request.post? && @member.save
193 193 respond_to do |format|
194 194 format.html { redirect_to :action => 'settings', :tab => 'members', :id => @project }
195 195 format.js { render(:update) {|page| page.replace_html "tab-content-members", :partial => 'members'} }
196 196 end
197 197 else
198 198 settings
199 199 render :action => 'settings'
200 200 end
201 201 end
202 202
203 203 # Show members list of @project
204 204 def list_members
205 205 @members = @project.members.find(:all)
206 206 end
207 207
208 208 # Add a new document to @project
209 209 def add_document
210 210 @categories = Enumeration::get_values('DCAT')
211 211 @document = @project.documents.build(params[:document])
212 212 if request.post? and @document.save
213 213 # Save the attachments
214 214 params[:attachments].each { |a|
215 215 Attachment.create(:container => @document, :file => a, :author => logged_in_user) unless a.size == 0
216 216 } if params[:attachments] and params[:attachments].is_a? Array
217 217 flash[:notice] = l(:notice_successful_create)
218 218 Mailer.deliver_document_add(@document) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
219 219 redirect_to :action => 'list_documents', :id => @project
220 220 end
221 221 end
222 222
223 223 # Show documents list of @project
224 224 def list_documents
225 225 @documents = @project.documents.find :all, :include => :category
226 226 end
227 227
228 228 # Add a new issue to @project
229 229 def add_issue
230 230 @tracker = Tracker.find(params[:tracker_id])
231 231 @priorities = Enumeration::get_values('IPRI')
232 232
233 233 default_status = IssueStatus.default
234 234 unless default_status
235 235 flash.now[:notice] = 'No default issue status defined. Please check your configuration.'
236 236 render :nothing => true, :layout => true
237 237 return
238 238 end
239 239 @issue = Issue.new(:project => @project, :tracker => @tracker)
240 240 @issue.status = default_status
241 241 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker))if logged_in_user
242 242 if request.get?
243 243 @issue.start_date = Date.today
244 244 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
245 245 else
246 246 @issue.attributes = params[:issue]
247 247
248 248 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
249 249 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
250 250
251 251 @issue.author_id = self.logged_in_user.id if self.logged_in_user
252 252 # Multiple file upload
253 253 @attachments = []
254 254 params[:attachments].each { |a|
255 255 @attachments << Attachment.new(:container => @issue, :file => a, :author => logged_in_user) unless a.size == 0
256 256 } if params[:attachments] and params[:attachments].is_a? Array
257 257 @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]) }
258 258 @issue.custom_values = @custom_values
259 259 if @issue.save
260 260 @attachments.each(&:save)
261 261 flash[:notice] = l(:notice_successful_create)
262 262 Mailer.deliver_issue_add(@issue) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
263 263 redirect_to :action => 'list_issues', :id => @project
264 264 end
265 265 end
266 266 end
267 267
268 268 # Show filtered/sorted issues list of @project
269 269 def list_issues
270 270 sort_init "#{Issue.table_name}.id", "desc"
271 271 sort_update
272 272
273 273 retrieve_query
274 274
275 275 @results_per_page_options = [ 15, 25, 50, 100 ]
276 276 if params[:per_page] and @results_per_page_options.include? params[:per_page].to_i
277 277 @results_per_page = params[:per_page].to_i
278 278 session[:results_per_page] = @results_per_page
279 279 else
280 280 @results_per_page = session[:results_per_page] || 25
281 281 end
282 282
283 283 if @query.valid?
284 284 @issue_count = Issue.count(:include => [:status, :project, :custom_values], :conditions => @query.statement)
285 285 @issue_pages = Paginator.new self, @issue_count, @results_per_page, params['page']
286 286 @issues = Issue.find :all, :order => sort_clause,
287 287 :include => [ :assigned_to, :status, :tracker, :project, :priority, :custom_values ],
288 288 :conditions => @query.statement,
289 289 :limit => @issue_pages.items_per_page,
290 290 :offset => @issue_pages.current.offset
291 291 end
292 292 render :layout => false if request.xhr?
293 293 end
294 294
295 295 # Export filtered/sorted issues list to CSV
296 296 def export_issues_csv
297 297 sort_init "#{Issue.table_name}.id", "desc"
298 298 sort_update
299 299
300 300 retrieve_query
301 301 render :action => 'list_issues' and return unless @query.valid?
302 302
303 303 @issues = Issue.find :all, :order => sort_clause,
304 304 :include => [ :assigned_to, :author, :status, :tracker, :priority, :project, {:custom_values => :custom_field} ],
305 305 :conditions => @query.statement,
306 306 :limit => Setting.issues_export_limit.to_i
307 307
308 308 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
309 309 export = StringIO.new
310 310 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
311 311 # csv header fields
312 312 headers = [ "#", l(:field_status),
313 313 l(:field_project),
314 314 l(:field_tracker),
315 315 l(:field_priority),
316 316 l(:field_subject),
317 317 l(:field_assigned_to),
318 318 l(:field_author),
319 319 l(:field_start_date),
320 320 l(:field_due_date),
321 321 l(:field_done_ratio),
322 322 l(:field_created_on),
323 323 l(:field_updated_on)
324 324 ]
325 325 for custom_field in @project.all_custom_fields
326 326 headers << custom_field.name
327 327 end
328 csv << headers.collect {|c| ic.iconv(c) }
328 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
329 329 # csv lines
330 330 @issues.each do |issue|
331 331 fields = [issue.id, issue.status.name,
332 332 issue.project.name,
333 333 issue.tracker.name,
334 334 issue.priority.name,
335 335 issue.subject,
336 336 (issue.assigned_to ? issue.assigned_to.name : ""),
337 337 issue.author.name,
338 338 issue.start_date ? l_date(issue.start_date) : nil,
339 339 issue.due_date ? l_date(issue.due_date) : nil,
340 340 issue.done_ratio,
341 341 l_datetime(issue.created_on),
342 342 l_datetime(issue.updated_on)
343 343 ]
344 344 for custom_field in @project.all_custom_fields
345 345 fields << (show_value issue.custom_value_for(custom_field))
346 346 end
347 csv << fields.collect {|c| ic.iconv(c.to_s) }
347 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
348 348 end
349 349 end
350 350 export.rewind
351 351 send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
352 352 end
353 353
354 354 # Export filtered/sorted issues to PDF
355 355 def export_issues_pdf
356 356 sort_init "#{Issue.table_name}.id", "desc"
357 357 sort_update
358 358
359 359 retrieve_query
360 360 render :action => 'list_issues' and return unless @query.valid?
361 361
362 362 @issues = Issue.find :all, :order => sort_clause,
363 363 :include => [ :author, :status, :tracker, :priority, :project, :custom_values ],
364 364 :conditions => @query.statement,
365 365 :limit => Setting.issues_export_limit.to_i
366 366
367 367 @options_for_rfpdf ||= {}
368 368 @options_for_rfpdf[:file_name] = "export.pdf"
369 369 render :layout => false
370 370 end
371 371
372 372 def move_issues
373 373 @issues = @project.issues.find(params[:issue_ids]) if params[:issue_ids]
374 374 redirect_to :action => 'list_issues', :id => @project and return unless @issues
375 375 @projects = []
376 376 # find projects to which the user is allowed to move the issue
377 377 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role)}
378 378 # issue can be moved to any tracker
379 379 @trackers = Tracker.find(:all)
380 380 if request.post? and params[:new_project_id] and params[:new_tracker_id]
381 381 new_project = Project.find(params[:new_project_id])
382 382 new_tracker = Tracker.find(params[:new_tracker_id])
383 383 @issues.each { |i|
384 384 # project dependent properties
385 385 unless i.project_id == new_project.id
386 386 i.category = nil
387 387 i.fixed_version = nil
388 388 # delete issue relations
389 389 i.relations_from.clear
390 390 i.relations_to.clear
391 391 end
392 392 # move the issue
393 393 i.project = new_project
394 394 i.tracker = new_tracker
395 395 i.save
396 396 }
397 397 flash[:notice] = l(:notice_successful_update)
398 398 redirect_to :action => 'list_issues', :id => @project
399 399 end
400 400 end
401 401
402 402 # Add a news to @project
403 403 def add_news
404 404 @news = News.new(:project => @project)
405 405 if request.post?
406 406 @news.attributes = params[:news]
407 407 @news.author_id = self.logged_in_user.id if self.logged_in_user
408 408 if @news.save
409 409 flash[:notice] = l(:notice_successful_create)
410 410 redirect_to :action => 'list_news', :id => @project
411 411 end
412 412 end
413 413 end
414 414
415 415 # Show news list of @project
416 416 def list_news
417 417 @news_pages, @news = paginate :news, :per_page => 10, :conditions => ["project_id=?", @project.id], :include => :author, :order => "#{News.table_name}.created_on DESC"
418 418 render :action => "list_news", :layout => false if request.xhr?
419 419 end
420 420
421 421 def add_file
422 422 if request.post?
423 423 @version = @project.versions.find_by_id(params[:version_id])
424 424 # Save the attachments
425 425 @attachments = []
426 426 params[:attachments].each { |file|
427 427 next unless file.size > 0
428 428 a = Attachment.create(:container => @version, :file => file, :author => logged_in_user)
429 429 @attachments << a unless a.new_record?
430 430 } if params[:attachments] and params[:attachments].is_a? Array
431 431 Mailer.deliver_attachments_add(@attachments) if !@attachments.empty? and Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
432 432 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
433 433 end
434 434 @versions = @project.versions.sort
435 435 end
436 436
437 437 def list_files
438 438 @versions = @project.versions.sort
439 439 end
440 440
441 441 # Show changelog for @project
442 442 def changelog
443 443 @trackers = Tracker.find(:all, :conditions => ["is_in_chlog=?", true], :order => 'position')
444 444 retrieve_selected_tracker_ids(@trackers)
445 445 @versions = @project.versions.sort
446 446 end
447 447
448 448 def roadmap
449 449 @trackers = Tracker.find(:all, :conditions => ["is_in_roadmap=?", true], :order => 'position')
450 450 retrieve_selected_tracker_ids(@trackers)
451 451 conditions = ("1" == params[:completed] ? nil : [ "#{Version.table_name}.effective_date > ? OR #{Version.table_name}.effective_date IS NULL", Date.today])
452 452 @versions = @project.versions.find(:all, :conditions => conditions).sort
453 453 end
454 454
455 455 def activity
456 456 if params[:year] and params[:year].to_i > 1900
457 457 @year = params[:year].to_i
458 458 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
459 459 @month = params[:month].to_i
460 460 end
461 461 end
462 462 @year ||= Date.today.year
463 463 @month ||= Date.today.month
464 464
465 465 @date_from = Date.civil(@year, @month, 1)
466 466 @date_to = @date_from >> 1
467 467
468 468 @events_by_day = {}
469 469
470 470 unless params[:show_issues] == "0"
471 471 @project.issues.find(:all, :include => [:author], :conditions => ["#{Issue.table_name}.created_on>=? and #{Issue.table_name}.created_on<=?", @date_from, @date_to] ).each { |i|
472 472 @events_by_day[i.created_on.to_date] ||= []
473 473 @events_by_day[i.created_on.to_date] << i
474 474 }
475 475 @show_issues = 1
476 476 end
477 477
478 478 unless params[:show_news] == "0"
479 479 @project.news.find(:all, :conditions => ["#{News.table_name}.created_on>=? and #{News.table_name}.created_on<=?", @date_from, @date_to], :include => :author ).each { |i|
480 480 @events_by_day[i.created_on.to_date] ||= []
481 481 @events_by_day[i.created_on.to_date] << i
482 482 }
483 483 @show_news = 1
484 484 end
485 485
486 486 unless params[:show_files] == "0"
487 487 Attachment.find(:all, :select => "#{Attachment.table_name}.*", :joins => "LEFT JOIN #{Version.table_name} ON #{Version.table_name}.id = #{Attachment.table_name}.container_id", :conditions => ["#{Attachment.table_name}.container_type='Version' and #{Version.table_name}.project_id=? and #{Attachment.table_name}.created_on>=? and #{Attachment.table_name}.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
488 488 @events_by_day[i.created_on.to_date] ||= []
489 489 @events_by_day[i.created_on.to_date] << i
490 490 }
491 491 @show_files = 1
492 492 end
493 493
494 494 unless params[:show_documents] == "0"
495 495 @project.documents.find(:all, :conditions => ["#{Document.table_name}.created_on>=? and #{Document.table_name}.created_on<=?", @date_from, @date_to] ).each { |i|
496 496 @events_by_day[i.created_on.to_date] ||= []
497 497 @events_by_day[i.created_on.to_date] << i
498 498 }
499 499 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN #{Document.table_name} ON #{Document.table_name}.id = #{Attachment.table_name}.container_id", :conditions => ["#{Attachment.table_name}.container_type='Document' and #{Document.table_name}.project_id=? and #{Attachment.table_name}.created_on>=? and #{Attachment.table_name}.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
500 500 @events_by_day[i.created_on.to_date] ||= []
501 501 @events_by_day[i.created_on.to_date] << i
502 502 }
503 503 @show_documents = 1
504 504 end
505 505
506 506 unless @project.wiki.nil? || params[:show_wiki_edits] == "0"
507 507 select = "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
508 508 "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title"
509 509 joins = "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
510 510 "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id "
511 511 conditions = ["#{Wiki.table_name}.project_id = ? AND #{WikiContent.versioned_table_name}.updated_on BETWEEN ? AND ?",
512 512 @project.id, @date_from, @date_to]
513 513
514 514 WikiContent.versioned_class.find(:all, :select => select, :joins => joins, :conditions => conditions).each { |i|
515 515 # We provide this alias so all events can be treated in the same manner
516 516 def i.created_on
517 517 self.updated_on
518 518 end
519 519
520 520 @events_by_day[i.created_on.to_date] ||= []
521 521 @events_by_day[i.created_on.to_date] << i
522 522 }
523 523 @show_wiki_edits = 1
524 524 end
525 525
526 526 unless @project.repository.nil? || params[:show_changesets] == "0"
527 527 @project.repository.changesets.find(:all, :conditions => ["#{Changeset.table_name}.committed_on BETWEEN ? AND ?", @date_from, @date_to]).each { |i|
528 528 def i.created_on
529 529 self.committed_on
530 530 end
531 531 @events_by_day[i.created_on.to_date] ||= []
532 532 @events_by_day[i.created_on.to_date] << i
533 533 }
534 534 @show_changesets = 1
535 535 end
536 536
537 537 render :layout => false if request.xhr?
538 538 end
539 539
540 540 def calendar
541 541 @trackers = Tracker.find(:all, :order => 'position')
542 542 retrieve_selected_tracker_ids(@trackers)
543 543
544 544 if params[:year] and params[:year].to_i > 1900
545 545 @year = params[:year].to_i
546 546 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
547 547 @month = params[:month].to_i
548 548 end
549 549 end
550 550 @year ||= Date.today.year
551 551 @month ||= Date.today.month
552 552
553 553 @date_from = Date.civil(@year, @month, 1)
554 554 @date_to = (@date_from >> 1)-1
555 555 # start on monday
556 556 @date_from = @date_from - (@date_from.cwday-1)
557 557 # finish on sunday
558 558 @date_to = @date_to + (7-@date_to.cwday)
559 559
560 560 @events = []
561 561 @project.issues_with_subprojects(params[:with_subprojects]) do
562 562 @events += Issue.find(:all,
563 563 :include => [:tracker, :status, :assigned_to, :priority, :project],
564 564 :conditions => ["((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?)) and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')})", @date_from, @date_to, @date_from, @date_to]
565 565 ) unless @selected_tracker_ids.empty?
566 566 end
567 567 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
568 568
569 569 @ending_events_by_days = @events.group_by {|event| event.due_date}
570 570 @starting_events_by_days = @events.group_by {|event| event.start_date}
571 571
572 572 render :layout => false if request.xhr?
573 573 end
574 574
575 575 def gantt
576 576 @trackers = Tracker.find(:all, :order => 'position')
577 577 retrieve_selected_tracker_ids(@trackers)
578 578
579 579 if params[:year] and params[:year].to_i >0
580 580 @year_from = params[:year].to_i
581 581 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
582 582 @month_from = params[:month].to_i
583 583 else
584 584 @month_from = 1
585 585 end
586 586 else
587 587 @month_from ||= (Date.today << 1).month
588 588 @year_from ||= (Date.today << 1).year
589 589 end
590 590
591 591 @zoom = (params[:zoom].to_i > 0 and params[:zoom].to_i < 5) ? params[:zoom].to_i : 2
592 592 @months = (params[:months].to_i > 0 and params[:months].to_i < 25) ? params[:months].to_i : 6
593 593
594 594 @date_from = Date.civil(@year_from, @month_from, 1)
595 595 @date_to = (@date_from >> @months) - 1
596 596
597 597 @events = []
598 598 @project.issues_with_subprojects(params[:with_subprojects]) do
599 599 @events += Issue.find(:all,
600 600 :order => "start_date, due_date",
601 601 :include => [:tracker, :status, :assigned_to, :priority, :project],
602 602 :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 and #{Issue.table_name}.tracker_id in (#{@selected_tracker_ids.join(',')}))", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to]
603 603 ) unless @selected_tracker_ids.empty?
604 604 end
605 605 @events += @project.versions.find(:all, :conditions => ["effective_date BETWEEN ? AND ?", @date_from, @date_to])
606 606 @events.sort! {|x,y| x.start_date <=> y.start_date }
607 607
608 608 if params[:output]=='pdf'
609 609 @options_for_rfpdf ||= {}
610 610 @options_for_rfpdf[:file_name] = "gantt.pdf"
611 611 render :template => "projects/gantt.rfpdf", :layout => false
612 612 else
613 613 render :template => "projects/gantt.rhtml"
614 614 end
615 615 end
616 616
617 617 def feeds
618 618 @queries = @project.queries.find :all, :conditions => ["is_public=? or user_id=?", true, (logged_in_user ? logged_in_user.id : 0)]
619 619 @key = logged_in_user.get_or_create_rss_key.value if logged_in_user
620 620 end
621 621
622 622 private
623 623 # Find project of id params[:id]
624 624 # if not found, redirect to project list
625 625 # Used as a before_filter
626 626 def find_project
627 627 @project = Project.find(params[:id])
628 628 @html_title = @project.name
629 629 rescue ActiveRecord::RecordNotFound
630 630 render_404
631 631 end
632 632
633 633 def retrieve_selected_tracker_ids(selectable_trackers)
634 634 if ids = params[:tracker_ids]
635 635 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
636 636 else
637 637 @selected_tracker_ids = selectable_trackers.collect {|t| t.id.to_s }
638 638 end
639 639 end
640 640
641 641 # Retrieve query from session or build a new query
642 642 def retrieve_query
643 643 if params[:query_id]
644 644 @query = @project.queries.find(params[:query_id])
645 645 @query.executed_by = logged_in_user
646 646 session[:query] = @query
647 647 else
648 648 if params[:set_filter] or !session[:query] or session[:query].project_id != @project.id
649 649 # Give it a name, required to be valid
650 650 @query = Query.new(:name => "_", :executed_by => logged_in_user)
651 651 @query.project = @project
652 652 if params[:fields] and params[:fields].is_a? Array
653 653 params[:fields].each do |field|
654 654 @query.add_filter(field, params[:operators][field], params[:values][field])
655 655 end
656 656 else
657 657 @query.available_filters.keys.each do |field|
658 658 @query.add_short_filter(field, params[field]) if params[field]
659 659 end
660 660 end
661 661 session[:query] = @query
662 662 else
663 663 @query = session[:query]
664 664 end
665 665 end
666 666 end
667 667 end
@@ -1,80 +1,80
1 1 class TimelogController < ApplicationController
2 2 layout 'base'
3 3
4 4 before_filter :find_project
5 5 before_filter :authorize, :only => :edit
6 6 before_filter :check_project_privacy, :only => :details
7 7
8 8 helper :sort
9 9 include SortHelper
10 10
11 11 def details
12 12 sort_init 'spent_on', 'desc'
13 13 sort_update
14 14
15 15 @entries = (@issue ? @issue : @project).time_entries.find(:all, :include => [:activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], :order => sort_clause)
16 16
17 17 @total_hours = @entries.inject(0) { |sum,entry| sum + entry.hours }
18 18 @owner_id = logged_in_user ? logged_in_user.id : 0
19 19
20 20 send_csv and return if 'csv' == params[:export]
21 21 render :action => 'details', :layout => false if request.xhr?
22 22 end
23 23
24 24 def edit
25 25 render_404 and return if @time_entry && @time_entry.user != logged_in_user
26 26 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => logged_in_user, :spent_on => Date.today)
27 27 @time_entry.attributes = params[:time_entry]
28 28 if request.post? and @time_entry.save
29 29 flash[:notice] = l(:notice_successful_update)
30 30 redirect_to :action => 'details', :project_id => @time_entry.project, :issue_id => @time_entry.issue
31 31 return
32 32 end
33 33 @activities = Enumeration::get_values('ACTI')
34 34 end
35 35
36 36 private
37 37 def find_project
38 38 if params[:id]
39 39 @time_entry = TimeEntry.find(params[:id])
40 40 @project = @time_entry.project
41 41 elsif params[:issue_id]
42 42 @issue = Issue.find(params[:issue_id])
43 43 @project = @issue.project
44 44 elsif params[:project_id]
45 45 @project = Project.find(params[:project_id])
46 46 else
47 47 render_404
48 48 return false
49 49 end
50 50 end
51 51
52 52 def send_csv
53 53 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
54 54 export = StringIO.new
55 55 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
56 56 # csv header fields
57 57 headers = [l(:field_spent_on),
58 58 l(:field_user),
59 59 l(:field_activity),
60 60 l(:field_issue),
61 61 l(:field_hours),
62 62 l(:field_comments)
63 63 ]
64 csv << headers.collect {|c| ic.iconv(c) }
64 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
65 65 # csv lines
66 66 @entries.each do |entry|
67 67 fields = [l_date(entry.spent_on),
68 68 entry.user.name,
69 69 entry.activity.name,
70 70 (entry.issue ? entry.issue.id : nil),
71 71 entry.hours,
72 72 entry.comments
73 73 ]
74 csv << fields.collect {|c| ic.iconv(c.to_s) }
74 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
75 75 end
76 76 end
77 77 export.rewind
78 78 send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
79 79 end
80 80 end
General Comments 0
You need to be logged in to leave comments. Login now