Index: app/controllers/documents_controller.rb
===================================================================
--- app/controllers/documents_controller.rb (Revision 12119)
+++ app/controllers/documents_controller.rb (Arbeitskopie)
@@ -24,28 +24,72 @@
before_filter :authorize
helper :attachments
+ include Redmine::Export::PDF
def index
- @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
- documents = @project.documents.includes(:attachments, :category).all
- case @sort_by
- when 'date'
- @grouped = documents.group_by {|d| d.updated_on.to_date }
- when 'title'
- @grouped = documents.group_by {|d| d.title.first.upcase}
- when 'author'
- @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
+
+ # Paging init
+ case params[:format]
+ when 'xml', 'json'
+ @offset, @limit = 5
else
- @grouped = documents.group_by(&:category)
+ @limit = 10
end
+
+
+ @documents_count = @project.documents.visible.count
+ @documents_pages = Paginator.new self, @documents_count, @limit, params['page']
+ @offset ||= @documents_pages.current.offset
+ @documents = @project.documents.visible.all(:include => [:attachments, :category],
+ :limit => @limit,
+ :offset => @offset,
+ :order => "#{Document.table_name}.title")
+
+
+ @sort_by = %w(category title date author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
+
+ case @sort_by
+ when 'date'
+ @documents = @project.documents.visible.all(:include => [:attachments, :category],
+ :limit => @limit,
+ :offset => @offset,
+ :order => "#{Document.table_name}.created_on, #{Document.table_name}.title ")
+ @grouped = @documents.group_by {|d| d.updated_on.to_date}
+ when 'title'
+ @grouped = @documents.group_by {|d| d.title.first.upcase}
+ when 'author'
+ @grouped = @documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
+ else
+ @documents = @project.documents.visible.all(:include => [:attachments, :category],
+ :limit => @limit,
+ :offset => @offset,
+ :order => "#{Document.table_name}.category_id, #{Document.table_name}.title")
+ @grouped = @documents.group_by {|d| d.category.name}
+ end
@document = @project.documents.build
render :layout => false if request.xhr?
end
+ # Display document and all attachments
+ # Handle the export functions
def show
@attachments = @document.attachments.all
+ # export of document
+ if User.current.allowed_to?(:edit_documents, @project) # Check permission
+ if params[:format] == 'pdf'
+ send_data(document_to_pdf(@document, @project), :type => 'application/pdf', :filename => "#{@document.title}.pdf")
+ return
+ elsif params[:format] == 'html'
+ export = render_to_string :action => 'export', :layout => false
+ send_data(export, :type => 'text/html', :filename => "#{@document.title}.html")
+ return
+ elsif params[:format] == 'txt'
+ send_data(@document.description, :type => 'text/plain', :filename => "#{@document.title}.txt")
+ return
+ end
+ end
+
end
-
def new
@document = @project.documents.build
@document.safe_attributes = params[:document]
Index: app/views/documents/export.html.erb
===================================================================
--- app/views/documents/export.html.erb (Revision 0)
+++ app/views/documents/export.html.erb (Arbeitskopie)
@@ -0,0 +1,25 @@
+
+
+
+<%=h @document.title %>
+
+
+
+
+<%=h @document.title %>
+
+<%=h @document.category.name %>
+<%= format_time @document.created_on %>
+
+<%= textilizable @document.description, :attachments => @attachements %>
+
+
Index: app/views/documents/index.html.erb
===================================================================
--- app/views/documents/index.html.erb (Revision 12119)
+++ app/views/documents/index.html.erb (Arbeitskopie)
@@ -15,6 +15,7 @@
<%=l(:label_document_plural)%>
+
<% if @grouped.empty? %><%= l(:label_no_data) %>
<% end %>
@@ -22,6 +23,7 @@
<%= group %>
<%= render :partial => 'documents/document', :collection => @grouped[group] %>
<% end %>
+
<% content_for :sidebar do %>
<%= l(:label_sort_by, '') %>
Index: app/views/documents/show.html.erb
===================================================================
--- app/views/documents/show.html.erb (Revision 12119)
+++ app/views/documents/show.html.erb (Arbeitskopie)
@@ -10,7 +10,7 @@
<%=h @document.title %>
<%=h @document.category.name %>
-<%= format_date @document.created_on %>
+<%= format_time @document.created_on %>
<%= textilizable @document, :description, :attachments => @document.attachments %>
@@ -29,4 +29,10 @@
<% end %>
<% end %>
+<% other_formats_links do |f| %>
+ <%= f.link_to 'PDF', :url => {:id => @document.id} %>
+ <%= f.link_to 'HTML', :url => {:id => @document.id} %>
+ <%= f.link_to 'TXT', :url => {:id => @document.id} %>
+<% end if User.current.allowed_to?(:edit_documents, @project) %>
+
<% html_title @document.title -%>
Index: lib/redmine/export/pdf.rb
===================================================================
--- lib/redmine/export/pdf.rb (Revision 12119)
+++ lib/redmine/export/pdf.rb (Arbeitskopie)
@@ -696,19 +696,7 @@
end
end
- if issue.attachments.any?
- pdf.SetFontStyle('B',9)
- pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
- pdf.Ln
- for attachment in issue.attachments
- pdf.SetFontStyle('',8)
- pdf.RDMCell(80,5, attachment.filename)
- pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
- pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
- pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
- pdf.Ln
- end
- end
+ common_write_attachments_list(pdf, issue.attachments)
pdf.Output
end
@@ -765,12 +753,53 @@
def write_wiki_page(pdf, page)
pdf.RDMwriteHTMLCell(190,5,0,0,
page.content.text.to_s, page.attachments, 0)
- if page.attachments.any?
+ common_write_attachments_list(pdf,page.attachments)
+ end
+
+
+ # Generate a PDF of a single document
+ def document_to_pdf(document, project)
+ pdf = ITCPDF.new(current_language)
+ pdf.SetTitle("#{project} - #{document.title}")
+ pdf.alias_nb_pages
+ pdf.footer_date = format_date(Date.today)
+ pdf.AddPage
+ pdf.SetFontStyle('B',11)
+ pdf.RDMMultiCell(190,5,
+ "#{project} - #{document.title} - #{document.category}")
+ pdf.Ln
+ # Repeat title and document info
+ pdf.SetFontStyle('B',14) # font size for title
+ pdf.RDMMultiCell(190,5,
+ document.title.to_s) # title
+ pdf.SetFontStyle('I',9) # font size for document info
+ pdf.RDMMultiCell(190,5,
+ document.category.to_s)
+ pdf.RDMMultiCell(190,5,
+ format_date(document.created_on))
+ # Set resize image scale
+ pdf.SetImageScale(1.6)
+ pdf.SetFontStyle('',9)
+ # Generate document content as pdf
+ document_to_pdf_content(pdf, document)
+ pdf.Output
+ end
+
+ # Attach document content to pdf-object
+ def document_to_pdf_content(pdf, document)
+ pdf.RDMwriteHTMLCell(190,5,0,0,
+ document.description.to_s, document.attachments, 0)
+ common_write_attachments_list(pdf, document.attachments)
+ end
+
+ # Write a list of attachments-properties to pdf-object
+ def common_write_attachments_list(pdf, attachments)
+ if attachments.any?
pdf.Ln
pdf.SetFontStyle('B',9)
pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
pdf.Ln
- for attachment in page.attachments
+ for attachment in attachments
pdf.SetFontStyle('',8)
pdf.RDMCell(80,5, attachment.filename)
pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
@@ -780,7 +809,8 @@
end
end
end
-
+
+
class RDMPdfEncoding
def self.rdm_from_utf8(txt, encoding)
txt ||= ''