redmine自定义长文本字段显示及导出pdf排版问题临时解决方案

问题介绍

    因项目组2月底正式引进了redmine2.2.1管理从需求到开发到测试到上线的任务跟踪,近期又想将另一个数据变更系统的数据定时导入redmine来跟踪分发管理,以尽可能的避免丢失变更单的情况。

    直接带来的一个问题就是自定义些许字段来放置数据变更单审批系统的数据,其中就牵涉到几个长文本字段。如:变更原因及内容、备份脚本、回滚脚本、变更脚本等;这些通过自定义类型为长文本的字段后即可使用,问题来了:

  • 在issue现实页,自定义长文本字段混在自定义的字符串字段中,页面不好看;
  • 在issue页导出pdf时,自定义的长文本字段又仅显示一行,自动隐藏溢出的字符;

    为了处理这个问题,经过google和redmine官方网站仔细查询,没能找到合适的插件来处理,倒是碰上了一些人也有这个需求:

第一个是找插件的,第二个也是国人提交的help,我用自己仅有的几个单词配合google翻译回了帖子。

算法:

 

节选自我回复的帖子。follow this:

  • custom a longtxt field with "$" start: "$pm";
  • set longtxt field not display when it's output properties ;
  • set longtxt field display when it's after output description;

 

代码分享

代码:http://t.cn/zQPBMtK

修订清单:

文件:issues_helper.rb  地址:app\helpers\issues_helper.rb
文件:show.html.erb    地址:app\views\issues\show.html.erb
文件:pdf.rb       地址:lib\redmine\export\pdf.rb
 

修订概要:

  • 文件:issues_helper.rb
  • 修订render_custom_fields_rows(issue)函数;判断自定义的长文本字段不显示
  • 新增render_custom_fields_rows2(issue)函数;判断如为自定义的长文本字段则显示;
  • 文件:show.html.erb  新增自定义长文本字段的展现在描述后
  • 文件:pdf.rb 
  • 代码1:修订自定义长文本字段不在属性里显示;
  • 代码2:修订自定义长文本字段在描述后显示; 
  • 详细修订列表及变动清单见附件modifylist.txt;
  • 此修改基于redmine2.3版本(只能下载到这个版本),现项目组使用的版本为2.2.1:
  • 修订版本仅仅为调试通过代码,最终效果还看得过去。

存在以下问题:

 

  • 自定义长文本属性,增加了“$”开头以作为标识,但显示时需删掉此标识,我没能调试成功。
  • 问题单模版和pdf输出仅能调整为勉强能用的效果,请有需要者继续优化;

修改后的效果图如下:

附,modifylist.txt

----------------------

文件:issues_helper.rb 地址:app\helpers\issues_helper.rb
文件:show.html.erb 地址:app\views\issues\show.html.erb
文件:pdf.rb 地址:lib\redmine\export\pdf.rb

 

文件:issues_helper.rb

 

修订render_custom_fields_rows(issue)函数;判断自定义的长文本字段不显示
新增render_custom_fields_rows2(issue)函数;判断如为自定义的长文本字段则显示;

 

原函数render_custom_fields_rows(issue):   

def render_custom_fields_rows(issue)
    return if issue.custom_field_values.empty?
    ordered_values = []
    half = (issue.custom_field_values.size / 2.0).ceil
    half.times do |i|
      ordered_values << issue.custom_field_values[i]
      ordered_values << issue.custom_field_values[i + half]
    end
    s = "<tr>\n"
    n = 0
    ordered_values.compact.each do |value|
      s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
      #s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
      if h(value.custom_field.name).include?"$"
      #s << "\t<th>#{ h(value.custom_field.name) }:</th><td colspan=\"3\">#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
      else
      s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
    end
      n += 1
    end
    s << "</tr>\n"
    s.html_safe
  end


修订为:
  

def render_custom_fields_rows(issue)
    return if issue.custom_field_values.empty?
    ordered_values = []
    half = (issue.custom_field_values.size / 2.0).ceil
    half.times do |i|
      ordered_values << issue.custom_field_values[i]
      ordered_values << issue.custom_field_values[i + half]
    end
    s = "<tr>\n"
    n = 0
    ordered_values.compact.each do |value|
      s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
      #s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
      if h(value.custom_field.name).include?"$"
      #s << "\t<th>#{ h(value.custom_field.name) }:</th><td colspan=\"3\">#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
      else
      s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
    end
      n += 1
    end
    s << "</tr>\n"
    s.html_safe
  end

 

新增render_custom_fields_rows2(issue)函数  

  

 

def render_custom_fields_rows2(issue)
    return if issue.custom_field_values.empty?
    ordered_values = []
    half = (issue.custom_field_values.size / 2.0).ceil
    half.times do |i|
      ordered_values << issue.custom_field_values[i]
      ordered_values << issue.custom_field_values[i + half]
    end
    s = "<tr>\n"
    n = 0
    ordered_values.compact.each do |value|
      s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
#######s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
      if h(value.custom_field.name).include?"$"
      s << "\t<p><strong><th>#{ h(value.custom_field.name) }:</th></p></strong></tr>\n<tr>\n<td colspan=\"3\"><pre>#{ simple_format_without_paragraph(h(show_value(value))) }</pre></td>\n"
      end
#######
      n += 1
    end
    s << "</tr>\n"
    s.html_safe
  end

文件:show.html.erb 新增自定义长文本字段的展现在描述后

变更以下代码

<% if @issue.description? %>
<div class="description">
  <div class="contextual">
  <%= link_to l(:button_quote), quoted_issue_path(@issue), :remote => true, :method => 'post', :class => 'icon icon-comment' if authorize_for('issues', 'edit') %>
  </div>


  <p><strong><%=l(:field_description)%></strong></p>
  <div class="wiki">
  <%= textilizable @issue, :description, :attachments => @issue.attachments %>
  </div>
</div>
<% end %>


为:

 

<% if @issue.description? %>
<div class="description">
  <div class="contextual">
  <%= link_to l(:button_quote), quoted_issue_path(@issue), :remote => true, :method => 'post', :class => 'icon icon-comment' if authorize_for('issues', 'edit') %>
  </div>


  <p><strong><%=l(:field_description)%></strong></p>
  <div class="wiki">
  <%= textilizable @issue, :description, :attachments => @issue.attachments %>
  </div>
</div>
<hr />
<div class="description">
<div class="wiki">
<%= render_custom_fields_rows2(@issue) %>
</div>
</div>
<% end %>

 

文件:pdf.rb 

代码1:修订自定义长文本字段不在属性里显示;
代码2:修订自定义长文本字段在描述后显示;
找到以下代码1:

 

 

half = (issue.custom_field_values.size / 2.0).ceil
        issue.custom_field_values.each_with_index do |custom_value, i|
          (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value)]          
        end


修订为:

 

half = (issue.custom_field_values.size / 2.0).ceil
        issue.custom_field_values.each_with_index do |custom_value, i|
        #############
        if custom_value.custom_field.name.include?"$"
        else
          (i < half ? left : right) << [custom_value.custom_field.name, show_value(custom_value)]
          end
          #############
        end


找到以下代码2:

 

pdf.SetFontStyle('B',9)
        pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
        pdf.SetFontStyle('',9)
        # Set resize image scale
        pdf.SetImageScale(1.6)
        pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
              issue.description.to_s, issue.attachments, "LRB")


修订为:

 

pdf.SetFontStyle('B',9)
        pdf.RDMCell(35+155, 5, l(:field_description), "LRT", 1)
        pdf.SetFontStyle('',9)
        # Set resize image scale
        pdf.SetImageScale(1.6)
        pdf.RDMwriteHTMLCell(35+155, 5, 0, 0,
              issue.description.to_s, issue.attachments, "LRB")
##########
issue.custom_field_values.each_with_index do |custom_value, i|
        if custom_value.custom_field.name.include?"$"         
        itemcusname = custom_value.custom_field.name
        itemcusvalue = show_value(custom_value)
        pdf.SetFontStyle('B',9)
        pdf.RDMCell(35+155, 5, (itemcusname.to_s), "LRT", 1)
        pdf.SetFontStyle('',8)
        pdf.SetImageScale(1.6)
        pdf.RDMMultiCell(35+155, 5, itemcusvalue.to_s)
        else
          
          end
          pdf.Ln
        end


######

 

 

 

----------------------

 

 

2016.11.3Update:

翻到自己三年前写的博客,感慨万千,把不相关的插件推荐给删除了。继续说重点:

那时候在Redmine求助帖子里贴完我写的代码,后来有外国友人 Jürgen Diez 发贴求助时,Redmine的Contributor Toshi MARUYAMA 给出了我的在另一个求助帖里的回复链接。征求了我的同意后,基于我发布的code,Jürgen Diez发布了patch  pdf.rb.patch,不过我没测试这个patch;  

Contributor Toshi MARUYAMA 最后修复了此Defect #13860;我没有后续关注redmine的源码,不知道如何修复的 :)

这个求助帖详见:http://www.redmine.org/issues/13860

我回复的求助帖详见:http://www.redmine.org/boards/2/topics/38434?r=38548#message-38548

当年还不会使用github,对开源软件社区管理提交代码也不了解,否则就不会在帖子中贴源码了,直接也写个path提交上去,没准说出去也是一件脸上有光的事:曾经给Redmine提交过path。

哈哈哈哈哈,后续有的是机会。

开源世界,我来了!

 

转载于:https://my.oschina.net/hexie/blog/142134

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值