document_paging_and_export.patch

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

Download (9.69 KB)

View differences:

app/controllers/documents_controller.rb (Arbeitskopie)
24 24
  before_filter :authorize
25 25

  
26 26
  helper :attachments
27
  include Redmine::Export::PDF
27 28

  
28 29
  def index
29
    @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
30
    documents = @project.documents.includes(:attachments, :category).all
31
    case @sort_by
32
    when 'date'
33
      @grouped = documents.group_by {|d| d.updated_on.to_date }
34
    when 'title'
35
      @grouped = documents.group_by {|d| d.title.first.upcase}
36
    when 'author'
37
      @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
30
    
31
    # Paging init
32
	case params[:format]
33
    when 'xml', 'json'
34
      @offset, @limit = 5
38 35
    else
39
      @grouped = documents.group_by(&:category)
36
      @limit = 10
40 37
    end
38

  
39
    
40
    @documents_count = @project.documents.visible.count
41
    @documents_pages = Paginator.new self, @documents_count, @limit, params['page']
42
    @offset ||= @documents_pages.current.offset
43
    @documents = @project.documents.visible.all(:include => [:attachments, :category],
44
                                                  :limit => @limit, 
45
                                                  :offset => @offset, 
46
                                                  :order => "#{Document.table_name}.title")
47

  
48
    
49
    @sort_by = %w(category title date author).include?(params[:sort_by]) ? params[:sort_by] : 'category'
50
    
51
    case @sort_by
52
      when 'date'
53
        @documents = @project.documents.visible.all(:include => [:attachments, :category],
54
                                                  :limit => @limit, 
55
                                                  :offset => @offset, 
56
                                                  :order => "#{Document.table_name}.created_on, #{Document.table_name}.title ")
57
        @grouped = @documents.group_by {|d| d.updated_on.to_date}
58
      when 'title'
59
        @grouped = @documents.group_by {|d| d.title.first.upcase}
60
      when 'author'
61
        @grouped = @documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author}
62
      else
63
        @documents = @project.documents.visible.all(:include => [:attachments, :category],
64
                                                  :limit => @limit, 
65
                                                  :offset => @offset, 
66
                                                  :order => "#{Document.table_name}.category_id, #{Document.table_name}.title")
67
        @grouped = @documents.group_by {|d| d.category.name}
68
    end
41 69
    @document = @project.documents.build
42 70
    render :layout => false if request.xhr?
43 71
  end
44 72

  
73
  # Display document and all attachments
74
  # Handle the export functions
45 75
  def show
46 76
    @attachments = @document.attachments.all
77
	# export of document
78
    if User.current.allowed_to?(:edit_documents, @project)	# Check permission
79
      if params[:format] == 'pdf'
80
        send_data(document_to_pdf(@document, @project), :type => 'application/pdf', :filename => "#{@document.title}.pdf")
81
        return
82
      elsif params[:format] == 'html'
83
        export = render_to_string :action => 'export', :layout => false
84
        send_data(export, :type => 'text/html', :filename => "#{@document.title}.html")
85
        return
86
      elsif params[:format] == 'txt'
87
        send_data(@document.description, :type => 'text/plain', :filename => "#{@document.title}.txt")
88
        return
89
      end
90
    end
91

  
47 92
  end
48

  
49 93
  def new
50 94
    @document = @project.documents.build
51 95
    @document.safe_attributes = params[:document]
app/views/documents/export.html.erb (Arbeitskopie)
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
3
<head>
4
<title><%=h @document.title %></title>
5
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
<style>
7
body { font:80% Verdana,Tahoma,Arial,sans-serif; }
8
h1, h2, h3, h4 {  font-family: "Trebuchet MS",Georgia,"Times New Roman",serif; }
9
ul.toc { padding: 4px; margin-left: 0; }
10
ul.toc li { list-style-type:none; }
11
ul.toc li.heading2 { margin-left: 1em; }
12
ul.toc li.heading3 { margin-left: 2em; }
13
a.wiki-anchor { display: none; margin-left: 6px; text-decoration: none; }
14
a.wiki-anchor:hover { color: #aaa !important; text-decoration: none; }
15
</style>
16
</head>
17
<body>
18
<h2><%=h @document.title %></h2>
19

  
20
<p><em><%=h @document.category.name %><br />
21
<%= format_time @document.created_on %></em></p>
22

  
23
<%= textilizable @document.description, :attachments => @attachements %>
24
</body>
25
</html>
app/views/documents/index.html.erb (Arbeitskopie)
15 15
</div>
16 16

  
17 17
<h2><%=l(:label_document_plural)%></h2>
18
<p class="pagination"><%= pagination_links_full @documents_pages %></p>
18 19

  
19 20
<% if @grouped.empty? %><p class="nodata"><%= l(:label_no_data) %></p><% end %>
20 21

  
......
22 23
    <h3><%= group %></h3>
23 24
    <%= render :partial => 'documents/document', :collection => @grouped[group] %>
24 25
<% end %>
26
<p class="pagination"><%= pagination_links_full @documents_pages %></p>
25 27

  
26 28
<% content_for :sidebar do %>
27 29
  <h3><%= l(:label_sort_by, '') %></h3>
app/views/documents/show.html.erb (Arbeitskopie)
10 10
<h2><%=h @document.title %></h2>
11 11

  
12 12
<p><em><%=h @document.category.name %><br />
13
<%= format_date @document.created_on %></em></p>
13
<%= format_time @document.created_on %></em></p>
14 14
<div class="wiki">
15 15
<%= textilizable @document, :description, :attachments => @document.attachments %>
16 16
</div>
......
29 29
  <% end %>
30 30
<% end %>
31 31

  
32
<% other_formats_links do |f| %>
33
  <%= f.link_to 'PDF', :url => {:id => @document.id} %>
34
  <%= f.link_to 'HTML', :url => {:id => @document.id} %>
35
  <%= f.link_to 'TXT', :url => {:id => @document.id} %>
36
<% end if User.current.allowed_to?(:edit_documents, @project) %>
37

  
32 38
<% html_title @document.title -%>
lib/redmine/export/pdf.rb (Arbeitskopie)
696 696
          end
697 697
        end
698 698

  
699
        if issue.attachments.any?
700
          pdf.SetFontStyle('B',9)
701
          pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
702
          pdf.Ln
703
          for attachment in issue.attachments
704
            pdf.SetFontStyle('',8)
705
            pdf.RDMCell(80,5, attachment.filename)
706
            pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
707
            pdf.RDMCell(25,5, format_date(attachment.created_on),0,0,"R")
708
            pdf.RDMCell(65,5, attachment.author.name,0,0,"R")
709
            pdf.Ln
710
          end
711
        end
699
        common_write_attachments_list(pdf, issue.attachments)
712 700
        pdf.Output
713 701
      end
714 702

  
......
765 753
      def write_wiki_page(pdf, page)
766 754
        pdf.RDMwriteHTMLCell(190,5,0,0,
767 755
              page.content.text.to_s, page.attachments, 0)
768
        if page.attachments.any?
756
        common_write_attachments_list(pdf,page.attachments)
757
      end
758

  
759

  
760
      # Generate a PDF of a single document
761
      def document_to_pdf(document, project)
762
        pdf = ITCPDF.new(current_language)
763
        pdf.SetTitle("#{project} - #{document.title}")
764
        pdf.alias_nb_pages
765
        pdf.footer_date = format_date(Date.today)
766
        pdf.AddPage
767
        pdf.SetFontStyle('B',11)
768
        pdf.RDMMultiCell(190,5,
769
             "#{project} - #{document.title} - #{document.category}")
770
        pdf.Ln
771
        # Repeat title and document info
772
        pdf.SetFontStyle('B',14)          # font size for title
773
        pdf.RDMMultiCell(190,5,
774
              document.title.to_s)        # title
775
        pdf.SetFontStyle('I',9)           # font size for document info 
776
        pdf.RDMMultiCell(190,5,
777
              document.category.to_s)
778
        pdf.RDMMultiCell(190,5,
779
              format_date(document.created_on))
780
        # Set resize image scale
781
        pdf.SetImageScale(1.6)
782
        pdf.SetFontStyle('',9)
783
        # Generate document content as pdf
784
        document_to_pdf_content(pdf, document)
785
        pdf.Output
786
      end
787

  
788
      # Attach document content to pdf-object
789
      def document_to_pdf_content(pdf, document)
790
        pdf.RDMwriteHTMLCell(190,5,0,0,
791
              document.description.to_s, document.attachments, 0)
792
        common_write_attachments_list(pdf, document.attachments)
793
      end
794

  
795
      # Write a list of attachments-properties to pdf-object
796
      def common_write_attachments_list(pdf, attachments)
797
        if attachments.any?
769 798
          pdf.Ln
770 799
          pdf.SetFontStyle('B',9)
771 800
          pdf.RDMCell(190,5, l(:label_attachment_plural), "B")
772 801
          pdf.Ln
773
          for attachment in page.attachments
802
          for attachment in attachments
774 803
            pdf.SetFontStyle('',8)
775 804
            pdf.RDMCell(80,5, attachment.filename)
776 805
            pdf.RDMCell(20,5, number_to_human_size(attachment.filesize),0,0,"R")
......
780 809
          end
781 810
        end
782 811
      end
783

  
812
    
813
    
784 814
      class RDMPdfEncoding
785 815
        def self.rdm_from_utf8(txt, encoding)
786 816
          txt ||= ''