labimotion 1.3.0.rc2 → 1.3.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,204 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'export_table'
3
- require 'labimotion/version'
4
- require 'sablon'
5
-
6
- module Labimotion
7
- class ExportElement
8
- def initialize(current_user, element, export_format)
9
- @current_user = current_user
10
- @element = element
11
- @properties = element.properties
12
- @options = element.properties_release[Labimotion::Prop::SEL_OPTIONS]
13
- @export_format = export_format
14
- rescue StandardError => e
15
- Labimotion.log_exception(e)
16
- byebug
17
- end
18
-
19
- def build_layers
20
- objs = []
21
- @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by { |key| @properties[Labimotion::Prop::LAYERS][key]['position'] }&.each do |key|
22
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
23
-
24
- ## Build fields html
25
- # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive?
26
- # field_html = Sablon.content(:html, field_objs) if field_objs.present?
27
- field_objs = build_fields(layer)
28
-
29
- layer_info = {
30
- label: layer['label'],
31
- layer: layer['layer'],
32
- cols: layer['cols'],
33
- timeRecord: layer['timeRecord'],
34
- fields: field_objs
35
- }
36
- objs.push(layer_info)
37
- end
38
- objs
39
- rescue StandardError => e
40
- Labimotion.log_exception(e)
41
- byebug
42
- end
43
-
44
- def build_field(layer, field)
45
- field_obj = {}
46
- field_obj[:label] = field['label']
47
- field_obj[:field] = field['field']
48
- field_obj[:type] = field['type']
49
-
50
- field_obj.merge!(field.slice('value', 'value_system'))
51
-
52
- case field['type']
53
- when Labimotion::FieldType::DRAG_ELEMENT
54
- field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
55
- field_obj[:obj] = field['value'] ### change to object
56
- when Labimotion::FieldType::DRAG_SAMPLE
57
- val = field.fetch('value', {})
58
- instance = Sample.find_by(id: val['el_id'])
59
- field_obj[:value] = val['el_label']
60
- field_obj[:has_structure] = true
61
- obj_os = Entities::SampleReportEntity.new(
62
- instance,
63
- current_user: @current_user,
64
- detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels,
65
- ).serializable_hash
66
- field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_os, format: 'png').generate
67
- when Labimotion::FieldType::DRAG_MOLECULE
68
- val = field.fetch('value', {})
69
- obj = Molecule.find_by(id: val['el_id'])
70
- field_obj[:value] = val['el_label']
71
- # field_obj[:has_structure] = true
72
- # field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj, format: 'png').generate
73
- when Labimotion::FieldType::SELECT
74
- field_obj[:value] = @options.fetch(field['option_layers'], nil)&.fetch('options', nil)&.find { |ss| ss['key'] == field['value'] }&.fetch('label', nil) || field['value']
75
- when Labimotion::FieldType::UPLOAD
76
- files = field.fetch('value', nil)&.fetch('files', [])
77
- val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n')
78
- field_obj[:value] = val
79
- when Labimotion::FieldType::TABLE
80
- field_obj[:value] = 'this is a table' ## to be handled
81
- when Labimotion::FieldType::INPUT_GROUP
82
- field_obj[:value] = 'this is a input group' ## to be handled
83
- end
84
- field_obj
85
- rescue StandardError => e
86
- Labimotion.log_exception(e)
87
- byebug
88
- end
89
-
90
- def build_fields(layer)
91
- fields = layer[Labimotion::Prop::FIELDS] || []
92
- field_objs = []
93
- fields.each do |field|
94
- field_obj = build_field(layer, field)
95
- field_objs.push(field_obj)
96
- end
97
- field_objs
98
- rescue StandardError => e
99
- Labimotion.log_exception(e)
100
- byebug
101
- end
102
-
103
- def to_docx
104
- # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
105
- location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
106
- File.exist?(location)
107
- template = Sablon.template(location)
108
- layers = build_layers
109
- context = {
110
- name: @element.name,
111
- short_label: @element.short_label,
112
- date: Time.now.strftime('%d/%m/%Y'),
113
- author: @current_user.name,
114
- layers: layers
115
- }
116
- output = "Labimotion1.docx"
117
- template.render_to_file File.expand_path(output), context
118
- File.read(output)
119
- rescue StandardError => e
120
- Labimotion.log_exception(e)
121
- byebug
122
- end
123
-
124
-
125
- def build_field_html(layer, field, cols)
126
- case field['type']
127
- when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
128
- val = (field['value'] && field['value']['el_label']) || ''
129
- when Labimotion::FieldType::UPLOAD
130
- val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || ''
131
- else
132
- val = field['value']
133
- end
134
- htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>'
135
- "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
136
- rescue StandardError => e
137
- Labimotion.log_exception(e)
138
- byebug
139
- end
140
-
141
- def build_fields_html(layer)
142
- fields = layer[Labimotion::Prop::FIELDS] || []
143
- field_objs = []
144
- cols = layer['cols'] || 0
145
- field_objs.push('<table style="width: 4000dxa"><tr>')
146
- fields.each do |field|
147
- if cols&.zero? || field['hasOwnRow'] == true
148
- field_objs.push('</tr><tr>')
149
- cols = layer['cols']
150
- end
151
- field_obj = build_field_html(layer, field, layer['cols'])
152
- field_objs.push(field_obj)
153
- cols -= 1
154
- cols = 0 if field['hasOwnRow'] == true
155
- end
156
- field_objs.push('</tr></table>')
157
- field_objs&.join("")&.gsub('<tr></tr>', '')
158
- rescue StandardError => e
159
- Labimotion.log_exception(e)
160
- byebug
161
- end
162
-
163
-
164
- def build_layers_html
165
- layer_html = []
166
- first_line = true
167
- @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
168
- layer_html.push('</tr></table>') if first_line == false
169
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
170
- fields = layer[Labimotion::Prop::FIELDS] || []
171
- layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
172
- layer_html.push('<table><tr>')
173
- cols = layer['cols']
174
- fields.each do |field|
175
- if (cols === 0)
176
- layer_html.push('</tr><tr>')
177
- end
178
-
179
- val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
180
- layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
181
- cols -= 1
182
- end
183
- first_line = false
184
- end
185
- layer_html.push('</tr></table>')
186
- byebug
187
- layer_html.join('')
188
- rescue StandardError => e
189
- Labimotion.log_exception(e)
190
- byebug
191
- end
192
-
193
- def html_labimotion
194
- layers_html = build_layers_html
195
- html_body = <<-HTML.strip
196
- #{layers_html}
197
- HTML
198
- html_body
199
- rescue StandardError => e
200
- Labimotion.log_exception(e)
201
- byebug
202
- end
203
- end
204
- end
@@ -1,160 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'export_table'
3
- require 'labimotion/version'
4
- require 'sablon'
5
-
6
- module Labimotion
7
- class ExportElement
8
- def initialize(current_user, element, export_format)
9
- @current_user = current_user
10
- @element = element
11
- @properties = element.properties
12
- @export_format = export_format
13
- rescue StandardError => e
14
- Labimotion.log_exception(e)
15
- byebug
16
- end
17
-
18
- def build_field(layer, field)
19
- field_obj = {}
20
- field_obj[:label] = field['label']
21
- field_obj[:field] = field['field']
22
- field_obj[:type] = field['type']
23
-
24
- case field['type']
25
- when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
26
- field_obj[:value] = field['value']['el_label']
27
- else
28
- field_obj[:value] = field['value']
29
- end
30
- field_obj
31
- rescue StandardError => e
32
- Labimotion.log_exception(e)
33
- byebug
34
- end
35
-
36
- def build_fields(layer, fields)
37
- field_objs = []
38
- fields.each do |field|
39
- field_obj = build_field(layer, field)
40
- field_objs.push(field_obj)
41
- end
42
- field_objs
43
- rescue StandardError => e
44
- Labimotion.log_exception(e)
45
- byebug
46
- end
47
-
48
- def build_layers
49
- objs = []
50
- @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
51
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
52
- field_objs = build_fields_html(layer)
53
- field_html = Sablon.content(:html, field_objs)
54
- layer_info = {
55
- label: layer['label'],
56
- layer: layer['layer'],
57
- cols: layer['cols'],
58
- timeRecord: layer['timeRecord'],
59
- fields: field_html
60
- }
61
- objs.push(layer_info)
62
- end
63
- objs
64
- rescue StandardError => e
65
- Labimotion.log_exception(e)
66
- byebug
67
- end
68
-
69
-
70
- def build_field_html(layer, field)
71
- case field['type']
72
- when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
73
- val = field['value']['el_label']
74
- else
75
- val = field['value']
76
- end
77
- "<td><b>#{field['label']}: </b><br />#{val}</td>"
78
- rescue StandardError => e
79
- Labimotion.log_exception(e)
80
- byebug
81
- end
82
-
83
- def build_fields_html(layer)
84
- fields = layer[Labimotion::Prop::FIELDS] || []
85
- field_objs = []
86
- cols = layer['cols']
87
- field_objs.push('<table><tr>')
88
- fields.each do |field|
89
- field_objs.push('</tr><tr>') if (cols === 0)
90
- field_obj = build_field_html(layer, field)
91
- field_objs.push(field_obj)
92
- cols -= 1
93
- end
94
- field_objs.push('</tr></table>')
95
- field_objs.join("")
96
- rescue StandardError => e
97
- Labimotion.log_exception(e)
98
- byebug
99
- end
100
-
101
- def build_layers_html
102
- layer_html = []
103
- first_line = true
104
- @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
105
- layer_html.push('</tr></table>') if first_line == false
106
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
107
- fields = layer[Labimotion::Prop::FIELDS] || []
108
- layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
109
- layer_html.push('<table><tr>')
110
- cols = layer['cols']
111
- fields.each do |field|
112
- if (cols === 0)
113
- layer_html.push("</tr><tr>")
114
- end
115
-
116
- val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
117
- layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
118
- cols -= 1
119
- end
120
- first_line = false
121
- end
122
- layer_html.push('</tr></table>')
123
- layer_html.join('')
124
-
125
-
126
- rescue StandardError => e
127
- Labimotion.log_exception(e)
128
- byebug
129
- end
130
-
131
- def html_labimotion
132
- layers_html = build_layers_html
133
- html_body = <<-HTML.strip
134
- #{layers_html}
135
- HTML
136
- html_body
137
- rescue StandardError => e
138
- Labimotion.log_exception(e)
139
- byebug
140
- end
141
-
142
- def to_docx
143
- location = Rails.root.join('lib', 'template', 'Labimotion.docx')
144
- File.exist?(location)
145
- template = Sablon.template(location)
146
- layers = build_layers
147
- context = {
148
- name: @element.name,
149
- short_label: @element.short_label,
150
- layers: layers
151
- }
152
- output = "Labimotion1.docx"
153
- template.render_to_file File.expand_path(output), context
154
- File.read(output)
155
- rescue StandardError => e
156
- Labimotion.log_exception(e)
157
- byebug
158
- end
159
- end
160
- end
@@ -1,118 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'export_table'
3
- require 'labimotion/version'
4
- require 'sablon'
5
-
6
- module Labimotion
7
- class ExportElementBak1
8
- def initialize(current_user, element, export_format)
9
- @current_user = current_user
10
- @element = element
11
- @properties = element.properties
12
- @export_format = export_format
13
- rescue StandardError => e
14
- Labimotion.log_exception(e)
15
- byebug
16
- end
17
-
18
- def build_field(layer, field)
19
- field_obj = {}
20
- field_obj[:label] = field['label']
21
- field_obj[:field] = field['field']
22
- field_obj[:type] = field['type']
23
-
24
- case field['type']
25
- when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
26
- field_obj[:value] = field['value']['el_label']
27
- else
28
- field_obj[:value] = field['value']
29
- end
30
- field_obj
31
- rescue StandardError => e
32
- Labimotion.log_exception(e)
33
- byebug
34
- end
35
-
36
- def build_fields(layer, fields)
37
- field_objs = []
38
- fields.each do |field|
39
- field_obj = build_field(layer, field)
40
- field_objs.push(field_obj)
41
- end
42
- field_objs
43
- rescue StandardError => e
44
- Labimotion.log_exception(e)
45
- byebug
46
- end
47
- def build_layers
48
- objs = []
49
- @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
50
- layer = @properties[Labimotion::Prop::LAYERS][key] || {}
51
- fields = layer[Labimotion::Prop::FIELDS] || []
52
- field_objs = build_fields(layer, fields)
53
-
54
- layer_info = {
55
- label: layer['label'],
56
- layer: layer['layer'],
57
- cols: layer['cols'],
58
- fields: field_objs
59
- }
60
- objs.push(layer_info)
61
- end
62
- objs
63
- rescue StandardError => e
64
- Labimotion.log_exception(e)
65
- byebug
66
- end
67
-
68
- def html_sample
69
- html_body = <<-HTML.strip
70
- <p style="text-align: right; background-color: #FFFF00">
71
- Right aligned content with a yellow background color.
72
- </p>
73
- <div>
74
- <span style="color: #123456">Inline styles</span> are possible as well
75
- </div>
76
- <table>
77
- <tr>
78
- <th>Company</th>
79
- <th>Contact</th>
80
- <th>Country</th>
81
- </tr>
82
- <tr>
83
- <td>Alfreds Futterkiste</td>
84
- <td>Maria Anders</td>
85
- <td>Germany</td>
86
- </tr>
87
- </table>
88
-
89
- HTML
90
- html_body
91
- rescue StandardError => e
92
- Labimotion.log_exception(e)
93
- byebug
94
- end
95
-
96
- def to_docx
97
- location = Rails.root.join('lib', 'template', 'Labimotion.docx')
98
- File.exist?(location)
99
- template = Sablon.template(location)
100
- layers = build_layers
101
- byebug
102
- html_body = html_sample
103
- html_content = Sablon.content(:html, html_body)
104
- context = {
105
- name: @element.name,
106
- short_label: @element.short_label,
107
- layers: layers,
108
- html_sample: html_content
109
- }
110
- output = "Labimotion1.docx"
111
- template.render_to_file File.expand_path(output), context
112
- File.read(output)
113
- rescue StandardError => e
114
- Labimotion.log_exception(e)
115
- byebug
116
- end
117
- end
118
- end