Skip to content

Commit 6bd4a9d

Browse files
committed
print list of issues
1 parent 213f95a commit 6bd4a9d

File tree

10 files changed

+162
-22
lines changed

10 files changed

+162
-22
lines changed

app/controllers/gtt_print_jobs_controller.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ class GttPrintJobsController < ApplicationController
22
layout 'base'
33

44
before_action :find_optional_project
5-
before_action :find_issue, only: :create
5+
before_action :find_issues, only: :create
66

7-
before_action :authorize, only: :create
7+
before_action :authorize_create, only: :create
88
before_action :authorize_global, except: :create
99

1010
menu_item :issues
1111

1212
def create
13-
if @issue and (layout = params[:gtt_print_layout]).present?
14-
@result = RedmineGttPrint.mapfish.print_issue @issue, layout
15-
render status: (@result.success? ? :created : 422)
13+
if (layout = params[:gtt_print_layout]).present?
14+
if @issue
15+
@result = RedmineGttPrint.mapfish.print_issue @issue, layout
16+
elsif @issues
17+
@result = RedmineGttPrint.mapfish.print_issues @issues, layout
18+
end
19+
render status: (@result&.success? ? :created : 422)
1620
else
1721
render_404
1822
end
@@ -41,9 +45,19 @@ def show
4145

4246
private
4347

44-
def find_issue
48+
def authorize_create
49+
if @project
50+
authorize
51+
else
52+
authorize_global
53+
end
54+
end
55+
56+
def find_issues
4557
if params[:issue_id]
4658
@issue = (@project ? @project.issues : Issue).visible.find params[:issue_id]
59+
elsif params[:issue_ids]
60+
@issues = Issue.visible.where(id: params[:issue_ids].split(','))
4761
end
4862
@project ||= @issue.project if @issue
4963
end

app/views/gtt_print/_settings.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@
1919
RedmineGttPrint.tracker_config(t)) %>
2020
</p>
2121
<% end %>
22+
23+
<p>
24+
<%= content_tag :label, l(:label_gtt_list_config) %>
25+
<%= select_tag "settings[issue_list_config]",
26+
content_tag(:option, l(:label_gtt_no_printing), value: '') +
27+
options_from_collection_for_select(RedmineGttPrint.mapfish.print_configs,
28+
:to_s,
29+
:to_s,
30+
RedmineGttPrint.list_config) %>
31+
</p>

app/views/hooks/_print_issue_form.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@
77
<% end %>
88
<% end %>
99

10+
<% if @issues and
11+
( @project && User.current.allowed_to?(:view_gtt_print, @project) or
12+
User.current.allowed_to?(:view_gtt_print, nil, global: true) ) and
13+
(layouts = RedmineGttPrint.list_layouts).present?
14+
%>
15+
16+
<h3><%= l :label_gtt_print_title %></h3>
17+
<%= form_tag gtt_print_jobs_path, remote: true do |f| %>
18+
<%= hidden_field_tag :issue_ids, @issues.map(&:id).join(',') %>
19+
<%= select_tag :gtt_print_layout, options_from_collection_for_select(layouts, :to_s, :to_s) %>
20+
<button type="submit"><%= l :button_gtt_print_submit %></button>
21+
<% end %>
22+
23+
<% end %>

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
en:
22
field_gtt_print_server: Default print server URL
33
gtt_print_configurations: Print configurations
4+
label_gtt_list_config: Issue lists
45
label_gtt_no_printing: No printing
56
label_gtt_print_title: Template Print
67
label_gtt_print_failed: Printing failed

init.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
settings(
1717
default: {
1818
'default_print_server' => "http://localhost:8080/mfp",
19-
"tracker_config" => {}
19+
"tracker_config" => {},
20+
"issue_list_config" => nil
2021
},
2122
partial: 'gtt_print/settings'
2223
)

lib/redmine_gtt_print.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ def self.settings
88
Setting.plugin_redmine_gtt_print
99
end
1010

11+
def self.list_config
12+
settings['issue_list_config']
13+
end
14+
15+
def self.list_layouts
16+
if cfg = list_config
17+
mapfish.layouts cfg
18+
end
19+
end
20+
1121
def self.tracker_config(tracker)
1222
(settings['tracker_config'] || {})[tracker.id.to_s]
1323
end

lib/redmine_gtt_print/issue_to_json.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ module RedmineGttPrint
33
# Transforms the given issue into JSON ready to be sent to the mapfish print
44
# server.
55
#
6-
# TODO: fetch template capabilities
7-
# (https://print.***REMOVED***/print/DEMO_gtt/capabilities.json?pretty=true)
8-
# and determine issue attributes that have to be included from that.
9-
#
106
class IssueToJson
117
def initialize(issue, layout)
128
@issue = issue
@@ -20,31 +16,33 @@ def self.call(issue, layout)
2016
def call
2117
json = {
2218
layout: @layout,
23-
attributes: {
24-
title: @issue.subject,
25-
}
19+
attributes: self.class.attributes_hash(@issue)
2620
}
2721

2822
if data = @issue.geodata_for_print
29-
json[:attributes][:map] = map_data(data)
23+
json[:attributes][:map] = self.class.map_data(data[:center], [data[:geojson]])
3024
end
3125

3226
json.to_json
3327
end
3428

35-
private
29+
# the following static helpers are used by IssuesToJson as well
30+
31+
def self.attributes_hash(issue)
32+
{
33+
title: issue.subject,
34+
}
35+
end
3636

37-
def map_data(data)
37+
def self.map_data(center, features)
3838
{
39-
center: data[:center],
39+
center: center,
4040
rotation: 0,
4141
longitudeFirst: true,
4242
layers: [
4343
{
4444
geoJson: {
45-
features: [
46-
data[:geojson]
47-
],
45+
features: features,
4846
type: "FeatureCollection",
4947
},
5048
style: {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module RedmineGttPrint
2+
3+
# Transforms the given array of issues into JSON ready to be sent to the
4+
# mapfish print server
5+
#
6+
class IssuesToJson
7+
def initialize(issues, layout)
8+
@issues = issues
9+
@layout = layout
10+
end
11+
12+
def self.call(issues, layout)
13+
new(issues, layout).call
14+
end
15+
16+
def call
17+
hsh = {
18+
layout: @layout,
19+
attributes: {
20+
issues: @issues.map{|i| IssueToJson.attributes_hash(i)}
21+
}
22+
}
23+
24+
if (features = @issues.map(&:geodata_for_print).compact).any?
25+
# TODO determine a proper center for the whole set of features
26+
center = features.first[:center]
27+
hsh[:attributes][:map] = IssueToJson.map_data(
28+
center,
29+
features.map{|f| f[:geojson] }
30+
)
31+
end
32+
33+
hsh.to_json
34+
end
35+
36+
end
37+
end
38+

lib/redmine_gtt_print/mapfish.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,22 @@ def print_issue(issue, layout, format: 'pdf')
5959
end
6060
end
6161

62+
def print_issues(issues, layout, format: 'pdf')
63+
json = IssuesToJson.(issues, layout)
64+
if ref = request_print(json, RedmineGttPrint.list_config, format)
65+
CreateJobResult.new success: true, ref: ref
66+
else
67+
CreateJobResult.new
68+
end
69+
end
70+
6271
private
6372

6473
def request_print(json, print_config, format)
6574
url = "#{@host}/print/#{print_config}/report.#{format}"
66-
#(File.open("/tmp/mapfish.json", "wb") << json).close
75+
if Rails.env.development?
76+
(File.open(Rails.root.join("tmp/mapfish.json"), "wb") << json).close
77+
end
6778
r = HTTParty.post url, body: json, headers: { 'Content-Type' => 'application/json' }
6879
if r.success?
6980
json = JSON.parse r.body

test/unit/issues_to_json_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2+
3+
class IssuesToJsonTest < ActiveSupport::TestCase
4+
include GttTestData
5+
6+
fixtures :projects,
7+
:trackers,
8+
:projects_trackers,
9+
:enabled_modules,
10+
:issue_statuses,
11+
:issues,
12+
:enumerations,
13+
:custom_fields,
14+
:custom_values,
15+
:custom_fields_trackers,
16+
:attachments
17+
18+
setup do
19+
@issues = Issue.find(1, 2)
20+
@issues.first.update_attribute :geojson, test_geojson
21+
end
22+
23+
test 'should build mapfish json' do
24+
assert j = RedmineGttPrint::IssuesToJson.(@issues, 'das layout')
25+
assert h = JSON.parse(j)
26+
assert_equal 'das layout', h['layout']
27+
assert_equal @issues[0].subject, h['attributes']['issues'][0]['title']
28+
assert_equal @issues[1].subject, h['attributes']['issues'][1]['title']
29+
30+
assert map = h['attributes']['map']
31+
assert_equal 2, map['center'].size
32+
assert geo = map['layers'][0]['geoJson']
33+
assert_equal 'FeatureCollection', geo['type']
34+
assert feature = geo['features'][0]
35+
assert_equal 'Feature', feature['type']
36+
assert geom = feature['geometry']
37+
assert_equal 'Polygon', geom['type']
38+
assert_equal 15052703.2783315, geom['coordinates'].flatten.first
39+
end
40+
41+
end
42+
43+

0 commit comments

Comments
 (0)