Project

General

Profile

Patch #14862 ยป document_paging_and_export.patch

Documents get paging and export functions - Hans-Georg von Detten, 2013-09-08 21:13

View differences:

app/controllers/documents_controller.rb (Arbeitskopie)
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]
app/views/documents/export.html.erb (Arbeitskopie)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title><%=h @document.title %></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style>
body { font:80% Verdana,Tahoma,Arial,sans-serif; }
h1, h2, h3, h4 { font-family: "Trebuchet MS",Georgia,"Times New Roman",serif; }
ul.toc { padding: 4px; margin-left: 0; }
ul.toc li { list-style-type:none; }
ul.toc li.heading2 { margin-left: 1em; }
ul.toc li.heading3 { margin-left: 2em; }
a.wiki-anchor { display: none; margin-left: 6px; text-decoration: none; }
a.wiki-anchor:hover { color: #aaa !important; text-decoration: none; }
</style>
</head>
<body>
<h2><%=h @document.title %></h2>
<p><em><%=h @document.category.name %><br />
<%= format_time @document.created_on %></em></p>
<%= textilizable @document.description, :attachments => @attachements %>
</body>
</html>
app/views/documents/index.html.erb (Arbeitskopie)
</div>
<h2><%=l(:label_document_plural)%></h2>
<p class="pagination"><%= pagination_links_full @documents_pages %></p>
<% if @grouped.empty? %><p class="nodata"><%= l(:label_no_data) %></p><% end %>
......
<h3><%= group %></h3>
<%= render :partial => 'documents/document', :collection => @grouped[group] %>
<% end %>
<p class="pagination"><%= pagination_links_full @documents_pages %></p>
<% content_for :sidebar do %>
<h3><%= l(:label_sort_by, '') %></h3>
app/views/documents/show.html.erb (Arbeitskopie)
<h2><%=h @document.title %></h2>
<p><em><%=h @document.category.name %><br />
<%= format_date @document.created_on %></em></p>
<%= format_time @document.created_on %></em></p>
<div class="wiki">
<%= textilizable @document, :description, :attachments => @document.attachments %>
</div>
......
<% 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 -%>
lib/redmine/export/pdf.rb (Arbeitskopie)
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
......
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")
......
end
end
end
class RDMPdfEncoding
def self.rdm_from_utf8(txt, encoding)
txt ||= ''
    (1-1/1)