labimotion 1.2.0.1 → 1.3.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85459b416735dd8702c2764bd1ff996855ddfaf37c8b037737c7cf5dbb9403a4
4
- data.tar.gz: 8e659ab1b570a0d91dfd0e047720ddde4872eb8a3320c65500bd6b00e8be6e9f
3
+ metadata.gz: ee43dfba01f84786e6b1dc59d125c6c8cd3a7594e6f3d9e5179268995b8366dd
4
+ data.tar.gz: e15bf2f064a847b950f3320a2707de508f3c8169a966519280b71f8426a20f3e
5
5
  SHA512:
6
- metadata.gz: de6f3f92718a41d4d2a7bb4b7b3547c5a4996ea2e008beb3936e36d5606cabc5d1f6e73bf8525c9f0d306ff9af92d59eb08fce8d9e1fd6f0efcdd233f2bced33
7
- data.tar.gz: bad917552004d85e0ad14091f32f599b7edda626561b099917a65c5bf2b2e6a7b2611e37782127a1b5a81bf1ed463435d3d4f1d4782d694dc4b19f2ee5e2ba0a
6
+ metadata.gz: da54fef539dfc89675a40f1e1de1ab3a58a77f0e23a494912852ab78a29546af83ceeb1ef9e3b1a8f1d77d78adcc55e77a8d986b28f43810966264bdab89c230
7
+ data.tar.gz: db9c675738091bd749bdea7ece8eb3e555b6063694691d81a58832b19be3f92618d61a69228ad8a820c11a4b14d7ff8b77ef817378740e0f2af108aa43867316
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'labimotion/version'
4
+ require 'labimotion/libs/export_element'
5
+
4
6
  module Labimotion
5
7
  # Generic Element API
6
8
  class GenericElementAPI < Grape::API
@@ -41,6 +43,30 @@ module Labimotion
41
43
  end
42
44
  end
43
45
 
46
+ namespace :export do
47
+ desc 'export element'
48
+ params do
49
+ optional :id, type: Integer, desc: 'element id'
50
+ optional :export_format, type: String, desc: 'export format'
51
+ end
52
+ get do
53
+ element = Labimotion::Element.find(params[:id])
54
+ ## byebug
55
+ export = Labimotion::ExportElement.new current_user, element, params[:export_format]
56
+ env['api.format'] = :binary
57
+ content_type 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
58
+ el_filename = export.res_name
59
+ filename = URI.escape(el_filename)
60
+ # header['Content-Disposition'] = "attachment; filename=abc.docx"
61
+ header('Content-Disposition', "attachment; filename=\"#{filename}\"")
62
+
63
+ export.to_docx
64
+ rescue StandardError => e
65
+ Labimotion.log_exception(e, current_user)
66
+ { klass: [] }
67
+ end
68
+ end
69
+
44
70
  namespace :create_element_klass do
45
71
  desc 'create Generic Element Klass'
46
72
  params do
@@ -0,0 +1,204 @@
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
@@ -0,0 +1,251 @@
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
+ end
17
+
18
+ def build_layers
19
+ objs = []
20
+ @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by { |key| @properties[Labimotion::Prop::LAYERS][key]['position'] }&.each do |key|
21
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
22
+
23
+ ## Build fields html
24
+ # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive?
25
+ # field_html = Sablon.content(:html, field_objs) if field_objs.present?
26
+ field_objs = build_fields(layer)
27
+
28
+ layer_info = {
29
+ label: layer['label'],
30
+ layer: layer['layer'],
31
+ cols: layer['cols'],
32
+ timeRecord: layer['timeRecord'],
33
+ fields: field_objs
34
+ }
35
+ objs.push(layer_info)
36
+ end
37
+ objs
38
+ rescue StandardError => e
39
+ Labimotion.log_exception(e)
40
+ end
41
+
42
+ def build_field(layer, field)
43
+ field_obj = {}
44
+ field_obj[:label] = field['label']
45
+ field_obj[:field] = field['field']
46
+ field_obj[:type] = field['type']
47
+
48
+ field_obj.merge!(field.slice('value', 'value_system'))
49
+ field_obj[:is_table] = false
50
+ field_obj[:not_table] = true
51
+ case field['type']
52
+ when Labimotion::FieldType::DRAG_ELEMENT
53
+ field_obj[:value] = (field['value'] && field['value']['el_label']) || ''
54
+ field_obj[:obj] = field['value'] ### change to object
55
+ when Labimotion::FieldType::DRAG_SAMPLE
56
+ val = field.fetch('value', {})
57
+ instance = Sample.find_by(id: val['el_id'])
58
+ field_obj[:value] = val['el_label']
59
+ field_obj[:has_structure] = true
60
+ obj_os = Entities::SampleReportEntity.new(
61
+ instance,
62
+ current_user: @current_user,
63
+ detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels,
64
+ ).serializable_hash
65
+ obj_final = OpenStruct.new(obj_os)
66
+ field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_final, 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[:is_table] = true
81
+ field_obj[:not_table] = false
82
+ tbl = []
83
+ ## tbl_idx = []
84
+ header = {}
85
+
86
+ sub_fields = field.fetch('sub_fields', [])
87
+ sub_fields.each_with_index do |sub_field, idx|
88
+ header["col#{idx}"] = sub_field['col_name']
89
+ end
90
+ tbl.push(header)
91
+ field.fetch('sub_values', []).each do |sub_val|
92
+ data = {}
93
+ sub_fields.each_with_index do |sub_field, idx|
94
+ data["col#{idx}"] = build_table_field(sub_val, sub_field)
95
+ end
96
+ tbl.push(data)
97
+ end
98
+ field_obj[:data] = tbl
99
+ # field_obj[:value] = 'this is a table'
100
+ when Labimotion::FieldType::INPUT_GROUP
101
+ val = []
102
+ field.fetch('sub_fields', [])&.each do |sub_field|
103
+ if sub_field['type'] == Labimotion::FieldType::SYSTEM_DEFINED
104
+ val.push("#{sub_field['value']} #{sub_field['value_system']}")
105
+ else
106
+ val.push(sub_field['value'])
107
+ end
108
+ end
109
+ field_obj[:value] = val.join(' ')
110
+ end
111
+ field_obj
112
+ rescue StandardError => e
113
+ Labimotion.log_exception(e)
114
+ end
115
+
116
+ def build_table_field(sub_val, sub_field)
117
+ return '' if sub_field.fetch('id', nil).nil? || sub_val[sub_field['id']].nil?
118
+
119
+ case sub_field['type']
120
+ when Labimotion::FieldType::SELECT
121
+ sub_val[sub_field['id']]['value']
122
+ when Labimotion::FieldType::SYSTEM_DEFINED
123
+ "#{sub_val[sub_field['id']]['value']} #{sub_val[sub_field['id']]['value_system']}"
124
+ else
125
+ sub_val[sub_field['id']]
126
+ end
127
+ end
128
+
129
+ def build_fields(layer)
130
+ fields = layer[Labimotion::Prop::FIELDS] || []
131
+ field_objs = []
132
+ fields.each do |field|
133
+ next if field['type'] == 'dummy'
134
+
135
+ field_obj = build_field(layer, field)
136
+ field_objs.push(field_obj)
137
+ end
138
+ field_objs
139
+ rescue StandardError => e
140
+ Labimotion.log_exception(e)
141
+ end
142
+
143
+ def to_docx
144
+ # location = Rails.root.join('lib', 'template', 'Labimotion.docx')
145
+ location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx')
146
+ # location = Rails.root.join('lib', 'template', 'Labimotion_img.docx')
147
+ File.exist?(location)
148
+ template = Sablon.template(location)
149
+ layers = build_layers
150
+ context = {
151
+ name: @element.name,
152
+ short_label: @element.short_label,
153
+ date: Time.now.strftime('%d/%m/%Y'),
154
+ author: @current_user.name,
155
+ layers: layers
156
+ }
157
+ tempfile = Tempfile.new('labimotion.docx')
158
+ template.render_to_file File.expand_path(tempfile), context
159
+ content = File.read(tempfile)
160
+ content
161
+ rescue StandardError => e
162
+ Labimotion.log_exception(e)
163
+ ensure
164
+ # Close and delete the temporary file
165
+ tempfile.close
166
+ tempfile.unlink
167
+ end
168
+
169
+
170
+ def res_name
171
+ element_name = @element.short_label
172
+ "#{element_name}_#{Time.now.strftime('%Y%m%d%H%M')}.docx"
173
+ rescue StandardError => e
174
+ Labimotion.log_exception(e)
175
+ end
176
+
177
+ def build_field_html(layer, field, cols)
178
+ case field['type']
179
+ when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE
180
+ val = (field['value'] && field['value']['el_label']) || ''
181
+ when Labimotion::FieldType::UPLOAD
182
+ val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || ''
183
+ else
184
+ val = field['value']
185
+ end
186
+ htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>'
187
+ "#{htd}<b>#{field['label']}: </b><br />#{val}</td>"
188
+ rescue StandardError => e
189
+ Labimotion.log_exception(e)
190
+ end
191
+
192
+ def build_fields_html(layer)
193
+ fields = layer[Labimotion::Prop::FIELDS] || []
194
+ field_objs = []
195
+ cols = layer['cols'] || 0
196
+ field_objs.push('<table style="width: 4000dxa"><tr>')
197
+ fields.each do |field|
198
+ if cols&.zero? || field['hasOwnRow'] == true
199
+ field_objs.push('</tr><tr>')
200
+ cols = layer['cols']
201
+ end
202
+ field_obj = build_field_html(layer, field, layer['cols'])
203
+ field_objs.push(field_obj)
204
+ cols -= 1
205
+ cols = 0 if field['hasOwnRow'] == true
206
+ end
207
+ field_objs.push('</tr></table>')
208
+ field_objs&.join("")&.gsub('<tr></tr>', '')
209
+ rescue StandardError => e
210
+ Labimotion.log_exception(e)
211
+ end
212
+
213
+
214
+ def build_layers_html
215
+ layer_html = []
216
+ first_line = true
217
+ @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key|
218
+ layer_html.push('</tr></table>') if first_line == false
219
+ layer = @properties[Labimotion::Prop::LAYERS][key] || {}
220
+ fields = layer[Labimotion::Prop::FIELDS] || []
221
+ layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>")
222
+ layer_html.push('<table><tr>')
223
+ cols = layer['cols']
224
+ fields.each do |field|
225
+ if (cols === 0)
226
+ layer_html.push('</tr><tr>')
227
+ end
228
+
229
+ val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value']
230
+ layer_html.push("<td>#{field['label']}: <br />#{val}</td>")
231
+ cols -= 1
232
+ end
233
+ first_line = false
234
+ end
235
+ layer_html.push('</tr></table>')
236
+ layer_html.join('')
237
+ rescue StandardError => e
238
+ Labimotion.log_exception(e)
239
+ end
240
+
241
+ def html_labimotion
242
+ layers_html = build_layers_html
243
+ html_body = <<-HTML.strip
244
+ #{layers_html}
245
+ HTML
246
+ html_body
247
+ rescue StandardError => e
248
+ Labimotion.log_exception(e)
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,160 @@
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
@@ -0,0 +1,118 @@
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
@@ -7,6 +7,7 @@ module Labimotion
7
7
  DATETIME_RANGE = 'datetime-range'.freeze
8
8
  SELECT = 'select'.freeze
9
9
  INTEGER = 'integer'.freeze
10
+ INPUT_GROUP = 'input-group'.freeze
10
11
  SYSTEM_DEFINED = 'system-defined'.freeze
11
12
  DRAG = 'drag'.freeze
12
13
  DRAG_ELEMENT = 'drag_element'.freeze
@@ -36,7 +36,7 @@ module Labimotion
36
36
  value
37
37
  end
38
38
 
39
- def proc_assign_sample(value, sample, element)
39
+ def proc_assign_sample(value, sample)
40
40
  return {} if sample.nil?
41
41
 
42
42
  value = {} if value.nil? || value.is_a?(String)
@@ -45,16 +45,13 @@ module Labimotion
45
45
  value['el_tip'] = sample.short_label
46
46
  value['el_label'] = sample.short_label
47
47
  value['el_svg'] = sample.get_svg_path
48
- if element&.class&.name == Labimotion::Prop::L_ELEMENT && sample&.class&.name == Labimotion::Prop::SAMPLE
49
- Labimotion::ElementsSample.find_or_create_by(element_id: element.id, sample_id: sample.id)
50
- end
51
48
  value
52
49
  rescue StandardError => e
53
50
  Labimotion.log_exception(e)
54
51
  value
55
52
  end
56
53
 
57
- def proc_assign_element(value, element, _parent = nil)
54
+ def proc_assign_element(value, element)
58
55
  return {} if element.nil?
59
56
 
60
57
  value = {} if value.nil? || value.is_a?(String)
@@ -62,9 +59,6 @@ module Labimotion
62
59
  value['el_type'] = 'element' if value['el_type'].nil?
63
60
  value['el_tip'] = element.short_label
64
61
  value['el_label'] = element.short_label
65
- if _parent&.class&.name == Labimotion::Prop::L_ELEMENT && element&.class&.name == Labimotion::Prop::L_ELEMENT
66
- Labimotion::ElementsElement.find_or_create_by(parent_id: _parent.id, element_id: element.id)
67
- end
68
62
  value
69
63
  rescue StandardError => e
70
64
  Labimotion.log_exception(e)
@@ -98,20 +92,20 @@ module Labimotion
98
92
  properties
99
93
  end
100
94
 
101
- def proc_table_sample(properties, data, instances, svalue, key, tidx, vdx, col_id, element)
95
+ def proc_table_sample(properties, data, instances, svalue, key, tidx, vdx, col_id)
102
96
  return properties unless id = svalue.fetch('el_id', nil)
103
97
 
104
98
  # orig_s = data.fetch(Labimotion::Prop::SAMPLE, nil)&.fetch(svalue['el_id'], nil)
105
99
  sample = instances.fetch(Labimotion::Prop::SAMPLE, nil)&.fetch(id, nil)
106
100
  val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][tidx]['sub_values'][vdx][col_id]['value']
107
- properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][tidx]['sub_values'][vdx][col_id]['value'] = proc_assign_sample(val, sample, element)
101
+ properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][tidx]['sub_values'][vdx][col_id]['value'] = proc_assign_sample(val, sample)
108
102
  properties
109
103
  rescue StandardError => e
110
104
  Labimotion.log_exception(e)
111
105
  properties
112
106
  end
113
107
 
114
- def proc_table_prop(layer, key, data, instance, properties, field, tidx, type, element)
108
+ def proc_table_prop(layer, key, data, instance, properties, field, tidx, type)
115
109
  fields = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == type }
116
110
  return properties if fields.nil?
117
111
 
@@ -131,7 +125,7 @@ module Labimotion
131
125
  when Labimotion::FieldType::DRAG_MOLECULE
132
126
  properties = proc_table_molecule(properties, data, svalue, key, tidx, vdx, col_id)
133
127
  when Labimotion::FieldType::DRAG_SAMPLE
134
- properties = proc_table_sample(properties, data, instance, svalue, key, tidx, vdx, col_id, element)
128
+ properties = proc_table_sample(properties, data, instance, svalue, key, tidx, vdx, col_id)
135
129
  end
136
130
  end
137
131
  end
@@ -142,14 +136,14 @@ module Labimotion
142
136
  end
143
137
  end
144
138
 
145
- def self.proc_sample(layer, key, data, instances, properties, element)
139
+ def self.proc_sample(layer, key, data, instances, properties)
146
140
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
147
141
  fields.each do |field|
148
142
  idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
149
143
  id = field["value"] && field["value"]["el_id"] unless idx.nil?
150
144
  sample = instances.fetch(Labimotion::Prop::SAMPLE, nil)&.fetch(id, nil)
151
145
  val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
152
- val = proc_assign_sample(val, sample, element)
146
+ val = proc_assign_sample(val, sample)
153
147
  properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
154
148
  end
155
149
  properties
@@ -158,7 +152,7 @@ module Labimotion
158
152
  raise
159
153
  end
160
154
 
161
- def self.proc_element(layer, key, data, instances, properties, elements, element)
155
+ def self.proc_element(layer, key, data, instances, properties, elements)
162
156
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_ELEMENT }
163
157
  fields.each do |field|
164
158
  idx = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
@@ -168,7 +162,7 @@ module Labimotion
168
162
  properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = {}
169
163
  else
170
164
  val = properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']
171
- val = proc_assign_element(val, att_el, element)
165
+ val = proc_assign_element(val, att_el)
172
166
  properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value'] = val
173
167
  end
174
168
  end
@@ -231,14 +225,14 @@ module Labimotion
231
225
  end
232
226
 
233
227
 
234
- def self.proc_table(layer, key, data, instance, properties, element)
228
+ def self.proc_table(layer, key, data, instance, properties)
235
229
  fields = layer[Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
236
230
  fields&.each do |field|
237
231
  tidx = layer[Labimotion::Prop::FIELDS].index(field)
238
232
  next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?
239
233
 
240
- proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_MOLECULE, element)
241
- proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_SAMPLE, element)
234
+ proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_MOLECULE)
235
+ proc_table_prop(layer, key, data, instance, properties, field, tidx, Labimotion::FieldType::DRAG_SAMPLE)
242
236
  end
243
237
  properties
244
238
  rescue StandardError => e
@@ -252,9 +246,9 @@ module Labimotion
252
246
  layer = properties[Labimotion::Prop::LAYERS][key]
253
247
  properties = proc_molecule(layer, key, data, properties)
254
248
  properties = proc_upload(layer, key, data, instances, attachments, elmenet)
255
- properties = proc_sample(layer, key, data, instances, properties, elmenet)
256
- properties = proc_element(layer, key, data, instances, properties, elements, elmenet) # unless elements.nil?
257
- properties = proc_table(layer, key, data, instances, properties, elmenet)
249
+ properties = proc_sample(layer, key, data, instances, properties)
250
+ properties = proc_element(layer, key, data, instances, properties, elements) # unless elements.nil?
251
+ properties = proc_table(layer, key, data, instances, properties)
258
252
  end
259
253
  properties
260
254
  rescue StandardError => e
@@ -3,5 +3,5 @@
3
3
  ## Labimotion Version
4
4
  module Labimotion
5
5
  IS_RAILS5 = false
6
- VERSION = '1.2.0.1'
6
+ VERSION = '1.3.0.rc2'
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labimotion
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.1
4
+ version: 1.3.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chia-Lin Lin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-03-18 00:00:00.000000000 Z
12
+ date: 2024-03-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -66,6 +66,10 @@ files:
66
66
  - lib/labimotion/helpers/segment_helpers.rb
67
67
  - lib/labimotion/libs/converter.rb
68
68
  - lib/labimotion/libs/export_dataset.rb
69
+ - lib/labimotion/libs/export_element copy.rb
70
+ - lib/labimotion/libs/export_element.rb
71
+ - lib/labimotion/libs/export_element2.rb
72
+ - lib/labimotion/libs/export_element_bak1.rb
69
73
  - lib/labimotion/libs/nmr_mapper.rb
70
74
  - lib/labimotion/libs/properties_handler.rb
71
75
  - lib/labimotion/libs/sample_association.rb
@@ -117,9 +121,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
121
  version: '0'
118
122
  required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  requirements:
120
- - - ">="
124
+ - - ">"
121
125
  - !ruby/object:Gem::Version
122
- version: '0'
126
+ version: 1.3.1
123
127
  requirements: []
124
128
  rubygems_version: 3.1.6
125
129
  signing_key: