drg_cms 0.7.0.2 → 0.7.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +63 -18
  3. data/app/assets/javascripts/drg_cms/jquery.bpopup.js +372 -0
  4. data/app/assets/javascripts/drg_cms_application.js +1 -1
  5. data/app/assets/javascripts/drg_cms_cms.js +1 -1
  6. data/app/assets/stylesheets/drg_cms/drg_cms.css +37 -5
  7. data/app/assets/stylesheets/drg_cms/select-multiple.css +2 -2
  8. data/app/controllers/cmsedit_controller.rb +22 -24
  9. data/app/controllers/dc_application_controller.rb +8 -8
  10. data/app/controllers/dc_common_controller.rb +9 -6
  11. data/app/controllers/dc_main_controller.rb +0 -1
  12. data/app/controls/{dc_category_control.rb → dc_gallery_control.rb} +15 -30
  13. data/app/controls/dc_image_control.rb +180 -0
  14. data/app/controls/dc_page_control.rb +3 -3
  15. data/app/controls/dc_poll_result_control.rb +7 -8
  16. data/app/controls/dc_report.rb +9 -4
  17. data/app/controls/design_element_settings_control.rb +88 -37
  18. data/app/forms/all_options.yml +18 -7
  19. data/app/forms/cms_menu.yml +7 -2
  20. data/app/forms/dc_gallery.yml +1 -1
  21. data/app/forms/dc_image.yml +122 -0
  22. data/app/forms/dc_image_search.yml +72 -0
  23. data/app/forms/dc_page.yml +11 -8
  24. data/app/forms/dc_steps_template.yml +2 -1
  25. data/app/helpers/cms_common_helper.rb +24 -16
  26. data/app/helpers/cms_edit_helper.rb +26 -33
  27. data/app/helpers/cms_helper.rb +21 -5
  28. data/app/helpers/cms_index_helper.rb +53 -38
  29. data/app/helpers/dc_application_helper.rb +64 -72
  30. data/app/helpers/dc_image_helper.rb +127 -0
  31. data/app/models/concerns/dc_policy_rule_concern.rb +1 -1
  32. data/app/models/concerns/dc_user_concern.rb +12 -4
  33. data/app/models/dc_category.rb +12 -0
  34. data/app/models/dc_design.rb +5 -4
  35. data/app/models/dc_filter.rb +19 -18
  36. data/app/models/dc_image.rb +237 -0
  37. data/app/models/dc_internals.rb +5 -9
  38. data/app/models/dc_policy_role.rb +8 -8
  39. data/app/models/drgcms_form_fields/drgcms_field.rb +9 -26
  40. data/app/models/drgcms_form_fields/embedded.rb +11 -8
  41. data/app/models/drgcms_form_fields/journal_diff.rb +2 -2
  42. data/app/models/drgcms_form_fields/multitext_autocomplete.rb +51 -47
  43. data/app/models/drgcms_form_fields/select.rb +20 -14
  44. data/app/models/drgcms_form_fields/text_with_select.rb +5 -9
  45. data/app/renderers/dc_gallery_renderer.rb +10 -4
  46. data/app/renderers/dc_page_renderer.rb +7 -7
  47. data/app/renderers/dc_poll_renderer.rb +13 -12
  48. data/app/views/cmsedit/_edit_stuff.html.erb +1 -1
  49. data/app/views/cmsedit/edit.html.erb +1 -1
  50. data/app/views/cmsedit/index.html.erb +1 -1
  51. data/app/views/cmsedit/new.html.erb +1 -0
  52. data/config/locales/drgcms_en.yml +14 -2
  53. data/config/locales/drgcms_sl.yml +13 -2
  54. data/config/locales/models_en.yml +33 -0
  55. data/config/locales/models_sl.yml +44 -1
  56. data/drg_cms.gemspec +1 -1
  57. data/lib/drg_cms/version.rb +1 -1
  58. data/lib/drg_cms.rb +19 -5
  59. data/lib/generators/convert_to_ar/USAGE +8 -0
  60. data/lib/generators/convert_to_ar/convert_to_ar_generator.rb +158 -0
  61. metadata +16 -10
  62. data/app/assets/javascripts/drg_cms/jquery.bpopup.min.js +0 -7
  63. data/app/views/layouts/__cmsedit.html.erb +0 -16
@@ -44,17 +44,11 @@ def dc_value_for_parameter(param, current_document = nil)#:nodoc:
44
44
  end
45
45
 
46
46
  ############################################################################
47
- # Creates actions div for edit form.
48
- #
49
- # Displaying readonly form turned out to be challenge. For now when readonly parameter
50
- # has value 2, back link will force readonly form. Value 1 or not set will result in
51
- # normal link.
47
+ # Determine if action button on the form is active. Action will be greyed out otherwise.
48
+ #
49
+ # @return : Boolean : defined by 'active' option'. Default is True.
52
50
  ############################################################################
53
51
  def dc_is_action_active?(options)
54
- if options['when_new']
55
- dc_deprecate("when_option will be deprecated and replaced by active: not_new_record! Form #{CmsHelper.form_param(params)}")
56
- return !(dc_dont?(options['when_new']) && @record.new_record?)
57
- end
58
52
  return true unless options['active']
59
53
 
60
54
  # alias record and document so both can be used in eval
@@ -322,25 +316,25 @@ def dc_head_for_form
322
316
  caption = options['caption']
323
317
  span = options['span'] || 1
324
318
  @css << "\n#{options['css']}" unless options['css'].blank?
325
- label = if caption.blank?
326
- ''
327
- elsif options['name'] == caption
328
- t_label_for_field(options['name'], options['name'].capitalize.gsub('_',' ') )
329
- else
330
- t(caption, caption)
331
- end
319
+ label = if caption.blank?
320
+ ''
321
+ elsif options['name'] == caption
322
+ t_label_for_field(options['name'], options['name'].capitalize.gsub('_', ' ') )
323
+ else
324
+ t(caption, caption)
325
+ end
332
326
  # Field value
333
327
  begin
334
328
  field = if options['eval']
335
- dc_process_column_eval(options, @record)
336
- else
337
- @record.send(options['name'])
338
- end
329
+ dc_process_column_eval(options, @record)
330
+ else
331
+ dc_format_value(@record.send(options['name']), options['format'])
332
+ end
339
333
  rescue Exception => e
340
334
  dc_log_exception(e, 'dc_head_for_form')
341
335
  field = '!!!Error'
342
336
  end
343
- #
337
+
344
338
  klass = dc_style_or_class(nil, options['class'], field, @record)
345
339
  style = dc_style_or_class(nil, options['style'], field, @record)
346
340
  html << %(<div class="dc-column #{klass}" style="width:#{percent*span}%;#{style}">
@@ -487,18 +481,17 @@ def dc_input_form_create(fields_on_tab) #:nodoc:
487
481
  </div> )
488
482
  else
489
483
  # no label
490
- if dc_dont?(options['caption'])
491
- label = ''
492
- label_width = 0
493
- data_width = 100
494
- elsif group_option > 1
495
- label_width = group_option != group_count ? 10 : 14
496
- data_width = 21
497
- else
498
- label_width = 14
499
- data_width = 85
500
- end
501
- help.gsub!('<br>',"\n") if help.present?
484
+ label_width, data_width = if dc_dont?(options['caption'])
485
+ label = ''
486
+ [0, 100]
487
+ elsif group_option == 1
488
+ [14, 85]
489
+ elsif group_option == 2
490
+ group_count == 2 ? [14, 21] : [10, 45]
491
+ elsif group_option == 3
492
+ group_count == 3 ? [14, 21] : [10, 21]
493
+ end
494
+ help.gsub!('<br>', "\n") if help.present?
502
495
  %(
503
496
  <div class="dc-form-label dc-color-#{odd_even} dc-align-#{labels_pos} dc-width-#{label_width}" title="#{help}">
504
497
  <label for="record_#{options['name']}">#{label} </label>
@@ -110,7 +110,7 @@ end
110
110
  ############################################################################
111
111
  def dc_label_help(options)
112
112
  # no label or help in comments
113
- return [nil, nil] if %w(comment action).include?(options['type'])
113
+ return [nil, nil] if %w[comment action].include?(options['type'])
114
114
 
115
115
  label = options['caption'] || options['text'] || options['label']
116
116
  if options['name']
@@ -122,8 +122,16 @@ def dc_label_help(options)
122
122
  end
123
123
  # help text can be defined in form or in translations starting with helpers. or as helpers.help.collection.field
124
124
  help = options['help']
125
- help ||= "helpers.help.#{@form['table']}.#{options['name']}" if options['name']
126
- help = t(help, ' ') if help.to_s.match(/helpers\./)
125
+ if help.blank?
126
+ help = if options['name']
127
+ # if defined as i18n_prefix replace "label" with "help"
128
+ prefix = @form['i18n_prefix'] ? @form['i18n_prefix'].sub('label', 'help') : "helpers.help.#{@form['table']}"
129
+ "#{prefix}.#{options['name']}"
130
+ end
131
+ help = help.to_s
132
+ end
133
+ help = t(help, ' ') if help.to_s.match(/help\./)
134
+
127
135
  [label, help]
128
136
  end
129
137
 
@@ -300,10 +308,18 @@ def dc_log_exception(exception, where = '')
300
308
  end
301
309
 
302
310
  ############################################################################
303
- # Will return form id, to be used on each form for simpler css selecting.
311
+ # Will return form id. id can be used for css selecting of fields on form.
312
+ # Form id is by default form_name || table parameter.
304
313
  ############################################################################
305
314
  def dc_form_id
306
- %( id=#{CmsHelper.form_param(params) || CmsHelper.table_param(params)} )
315
+ %(id="#{CmsHelper.form_param(params) || CmsHelper.table_param(params)}" ).html_safe
316
+ end
317
+
318
+ ############################################################################
319
+ # Will return class for form. Class can be used for different styling of forms.
320
+ ############################################################################
321
+ def dc_form_class(additional_class = nil)
322
+ %(class="#{additional_class} #{@form['class']}" ).html_safe
307
323
  end
308
324
 
309
325
  ############################################################################
@@ -35,12 +35,10 @@ module CmsIndexHelper
35
35
  def dc_actions_for_index
36
36
  @js = @form['script'] || @form['js'] || ''
37
37
  @css = @form['css'] || ''
38
- return '' if @form['index'].nil?
39
-
40
- actions = @form['index']['actions']
38
+ actions = @form.dig('index', 'actions')
41
39
  return '' if actions.blank?
42
40
 
43
- std_actions = {2 => 'new', 3 => 'sort', 4 => 'filter' }
41
+ std_actions = { 2 => 'new', 3 => 'sort', 4 => 'filter' }
44
42
  std_actions.delete(2) if @form['readonly']
45
43
 
46
44
  if actions.class == String
@@ -51,22 +49,16 @@ def dc_actions_for_index
51
49
  end
52
50
 
53
51
  html_left, html_right = '', ''
54
- # Remove actions settings and sort
55
- only_actions = []
56
- actions.each { |key, value| only_actions << [key, value] if key.class == Integer }
57
- only_actions.sort_by!(&:first)
58
- only_actions.each do |key, options|
52
+ # remove settings and sort
53
+ actions_only = actions.inject([]) { |r, action| r << action if action.first.class == Integer; r }.sort_by(&:first)
54
+ actions_only.each do |key, options|
59
55
  session[:form_processing] = "index:actions: #{key}=#{options}"
60
56
  next if options.nil? # must be
61
57
 
62
- url = @form_params.clone
63
- yaml = options.class == String ? {'type' => options} : options # if single definition simulate type parameter
58
+ url = @form_params.clone
59
+ yaml = options.class == String ? { 'type' => options } : options # sinle action. Simulate type parameter
64
60
  action = yaml['type'].to_s.downcase
65
- if action == 'url'
66
- dc_deprecate "action: url will be deprecated. Use action: link in index: actions! Form #{params['form_name']}"
67
- action = 'link'
68
- end
69
- # if return_to is present link directly to URL
61
+
70
62
  if action == 'link' && yaml['url']
71
63
  url = yaml['url']
72
64
  else
@@ -89,10 +81,8 @@ def dc_actions_for_index
89
81
  choices << [ t("helpers.label.#{@form['table']}.#{e}"), e ]
90
82
  end
91
83
  end
92
- data = t('drgcms.sort') + select('sort', 'sort', choices, { include_blank: true }, { class: 'dc-sort-select',
93
- 'data-table' => @form['table'], 'data-form' => CmsHelper.form_param(params)} )
94
- data = mi_icon('sort') + select('sort', 'sort', choices, { include_blank: true }, { class: 'dc-sort-select',
95
- 'data-table' => @form['table'], 'data-form' => CmsHelper.form_param(params)} )
84
+ data = mi_icon('sort') + select('sort', 'sort', choices, { include_blank: true },
85
+ { class: 'dc-sort-select', 'data-table': @form['table'], 'data-form': CmsHelper.form_param(params)} )
96
86
  html_right << %(<li title="#{t('drgcms.sort')}"><div class="dc-sort">#{data}</li>)
97
87
 
98
88
  # filter
@@ -263,9 +253,16 @@ def dc_actions_column
263
253
  actions.delete('standard')
264
254
  end
265
255
  # check must be first action
266
- has_check = actions[0] && actions[0]['type'] == 'check'
267
- width = actions.size == 1 ? 22 : 44
268
- width = 22 if actions.size > 2 && !has_check
256
+ has_check = has_action_type('check', actions)
257
+ # when readonly only check and show are allowed
258
+ if @form['readonly']
259
+ width = has_action_type('show', actions) ? 22 : 0
260
+ width += 22 if has_check
261
+ else
262
+ width = actions.size == 1 ? 22 : 44
263
+ width = 22 if actions.size > 2 && !has_check
264
+ end
265
+
269
266
  [actions, width, has_check]
270
267
  end
271
268
 
@@ -275,7 +272,7 @@ end
275
272
  def dc_actions_column_for_footer
276
273
  return '' unless @form['result_set']['actions']
277
274
 
278
- ignore, width, ignore2 = dc_actions_column
275
+ ignore, width, ignore2 = dc_actions_column()
279
276
  %(<div class="dc-result-actions" style="width: #{width}px;"></div>).html_safe
280
277
  end
281
278
 
@@ -284,10 +281,10 @@ end
284
281
  ############################################################################
285
282
  def dc_actions_for_result(document)
286
283
  actions = @form['result_set']['actions']
287
- return '' if actions.nil? || @form['readonly']
284
+ return '' if actions.nil? #|| @form['readonly']
288
285
 
289
286
  actions, width, has_check = dc_actions_column()
290
- has_sub_menu = actions.size > 2 || (has_check && actions.size > 1)
287
+ has_sub_menu = actions.size > 2 #|| (has_check && actions.size > 1)
291
288
 
292
289
  main_menu, sub_menu = '', ''
293
290
  actions.sort_by(&:first).each do |num, action|
@@ -296,14 +293,16 @@ def dc_actions_for_result(document)
296
293
  # if single definition simulate type parameter
297
294
  yaml = action.class == String ? { 'type' => action } : action
298
295
 
296
+ next if @form['readonly'] && !%[show check].include?(yaml['type'])
297
+
299
298
  if %w(ajax link window popup submit).include?(yaml['type'])
300
299
  @record = document # otherwise document fields can't be used as parameters
301
300
  html = dc_link_ajax_window_submit_action(yaml, document)
302
301
  else
303
302
  caption = dc_get_caption(yaml) || "drgcms.#{yaml['type']}"
304
- title = t(yaml['help'] || caption, '')
303
+ title = t(yaml['title'] || yaml['help'] || caption)
305
304
  caption = has_sub_menu ? t(caption, '') : nil
306
- html = '<li>'
305
+ html = '<li>'
307
306
  html << case yaml['type']
308
307
  when 'check' then
309
308
  main_menu << '<li>' + check_box_tag("check-#{document.id}", false, false, { class: 'dc-check' }) + '</li>'
@@ -312,13 +311,15 @@ def dc_actions_for_result(document)
312
311
  when 'edit' then
313
312
  parms['action'] = 'edit'
314
313
  parms['id'] = document.id
314
+ parms['readonly'] = yaml['readonly']
315
315
  dc_link_to( caption, 'edit-o', parms, title: title )
316
316
 
317
317
  when 'show' then
318
318
  parms['action'] = 'show'
319
319
  parms['id'] = document.id
320
320
  parms['readonly'] = true
321
- dc_link_to( caption, 'eye', parms, title: title )
321
+ icon = yaml['icon'] || 'eye-o'
322
+ dc_link_to( caption, icon, parms, title: title )
322
323
 
323
324
  when 'duplicate' then
324
325
  parms['id'] = document.id
@@ -362,7 +363,7 @@ end
362
363
  ############################################################################
363
364
  def dc_header_for_result
364
365
  html = '<div class="dc-result-header">'
365
- if @form['result_set']['actions'] && !@form['readonly']
366
+ if @form['result_set']['actions'] #&& !@form['readonly']
366
367
  ignore, width, has_check = dc_actions_column()
367
368
  check_all = fa_icon('check-box-o', class: 'dc-check-all') if has_check
368
369
  html << %(<div class="dc-result-actions" style="width:#{width}px;">#{check_all}</div>)
@@ -436,7 +437,7 @@ end
436
437
  ############################################################################
437
438
  # Formats value according to format supplied or data type. There is lots of things missing here.
438
439
  ############################################################################
439
- def dc_format_value(value, format=nil)
440
+ def dc_format_value(value, format = nil)
440
441
  return '' if value.nil?
441
442
 
442
443
  klass = value.class.to_s
@@ -444,7 +445,7 @@ def dc_format_value(value, format=nil)
444
445
 
445
446
  format = format.to_s.upcase
446
447
  if format[0] == 'N'
447
- return '' if value == 0 && format.match('Z')
448
+ return '' if format.match('Z') && value.to_s.to_f == 0.0
448
449
 
449
450
  format.gsub!('Z', '')
450
451
  dec = format[1].blank? ? nil : format[1].to_i
@@ -491,7 +492,9 @@ def dc_columns_for_result(document)
491
492
  dc_process_column_eval(v, document)
492
493
  # as field
493
494
  elsif document.respond_to?(v['name'])
494
- dc_format_value(document.send( v['name'] ), v['format'])
495
+ dc_format_value(document.send(v['name']), v['format'])
496
+ elsif respond_to?(v['name'])
497
+ dc_format_value(send(v['name'], document), v['format'])
495
498
  else
496
499
  "??? #{v['name']}"
497
500
  end
@@ -501,7 +504,7 @@ def dc_columns_for_result(document)
501
504
  end
502
505
  html << '<div class="spacer"></div>'
503
506
  # set class
504
- clas = dc_style_or_class(nil, v['td_class'], value, document)
507
+ clas = dc_style_or_class(nil, v['td_class'] || v['class'], value, document)
505
508
  # set width and align an additional style
506
509
  style = dc_style_or_class(nil, v['td_style'] || v['style'], value, document)
507
510
  flex_align = v['align'].to_s == 'right' ? 'flex-direction:row-reverse;' : ''
@@ -522,15 +525,13 @@ def dc_eval_to_array(expression)
522
525
  expression.split(/\ |\,|\(|\)/).delete_if {|e| e.blank? }.map {|e| e.gsub(/\'|\"/,'').strip }
523
526
  end
524
527
 
525
- private
526
-
527
528
  ############################################################################
528
529
  # Process eval. Breaks eval option and calls with send method.
529
530
  # Parameters:
530
531
  # evaluate : String : Expression to be evaluated
531
532
  # parameters : Array : array of parameters which will be send to method
532
533
  ############################################################################
533
- def dc_process_eval(evaluate, parameters)
534
+ def dc_process_eval(evaluate, parameters = nil)
534
535
  # evaluate by calling send method
535
536
  clas, method = evaluate.split('.')
536
537
  if method.nil?
@@ -541,6 +542,8 @@ def dc_process_eval(evaluate, parameters)
541
542
  end
542
543
  end
543
544
 
545
+ private
546
+
544
547
  ############################################################################
545
548
  # Process eval option for field value.
546
549
  # Used for processing single field column on result_set or form head.
@@ -594,7 +597,7 @@ end
594
597
  # Defines style or class for row (tr) or column (td)
595
598
  ############################################################################
596
599
  def dc_style_or_class(selector, yaml, value, record)
597
- return '' if yaml.nil?
600
+ return '' if yaml.blank?
598
601
 
599
602
  # alias record and value so both names can be used in eval
600
603
  field, document = value, record
@@ -662,4 +665,16 @@ def form_has_input_field?(field_name)
662
665
  !(field['type'] == 'readonly' || field['readonly'])
663
666
  end
664
667
 
668
+ ############################################################################
669
+ # return true if actions define check or show option
670
+ ############################################################################
671
+ def has_action_type(type, actions)
672
+ h = { 'check' => 0, 'show' => 1 }
673
+ return false unless identifier = h[type]
674
+ return false unless actions[identifier]
675
+
676
+ type_value = actions[identifier].class == String ? actions[identifier] : actions[identifier]['type']
677
+ type_value == type
678
+ end
679
+
665
680
  end
@@ -260,7 +260,7 @@ def dc_table_title(text, result_set = nil)
260
260
  c = %(<div class="dc-title">#{text})
261
261
  c << dc_help_button(result_set)
262
262
 
263
- if result_set && result_set.respond_to?(:current_page)
263
+ if result_set&.respond_to?(:current_page)
264
264
  c << %(<div class="dc-paginate">#{paginate(result_set, :params => {action: 'index', clear: 'no', filter: nil})}</div>)
265
265
  end
266
266
  c << '<div style="clear: both;"></div></div>'
@@ -268,26 +268,27 @@ def dc_table_title(text, result_set = nil)
268
268
  end
269
269
 
270
270
  ############################################################################
271
- # Creates title for cmsedit edit action dialog.
272
- #
271
+ # Creates title for cmsedit edit action dialog.
272
+ #
273
273
  # Returns:
274
274
  # String. HTML code for title.
275
275
  ############################################################################
276
276
  def dc_edit_title
277
277
  session[:form_processing] = "form:title:"
278
- title = @form['form']['title']
278
+ title_data = @form['form']['title']
279
+ if title_data.class == String
280
+ t(title_data, title_data)
279
281
  # defined as form:title:edit
280
- if title && title['edit'] && !@form['readonly']
281
- t( title['edit'], title['edit'] )
282
- elsif title && title['show'] && @form['readonly']
283
- t( title['show'], title['show'] )
282
+ elsif title_data&.dig('edit') && !@form['readonly']
283
+ t( title_data['edit'], title_data['edit'] )
284
+ elsif title_data&.dig('show') && @form['readonly']
285
+ t( title_data['show'], title_data['show'] )
284
286
  else
285
- # concatenate title
286
287
  c = (@form['readonly'] ? t('drgcms.show') : t('drgcms.edit')) + " : "
287
288
  c << (@form['title'].class == String ? t( @form['title'], @form['title'] ) : t_tablename(@form['table']))
288
- title = title.try('field')
289
-
290
- c << "#{@record[ title ]}" if title && @record.respond_to?(title)
289
+ # add description field value to title
290
+ field_name = title_data['field'] if title_data
291
+ c << " : #{@record[ field_name ]}" if field_name && @record.respond_to?(field_name)
291
292
  c
292
293
  end
293
294
  end
@@ -300,14 +301,18 @@ end
300
301
  ############################################################################
301
302
  def dc_new_title
302
303
  session[:form_processing] = "form:title:"
303
- title = @form['form']['title']
304
+ title_data = @form['form']['title']
305
+ if title_data.class == String
306
+ t(title_data, title_data)
304
307
  # defined as form:title:new
305
- if title && title['new']
306
- t( title['new'], title['new'] )
308
+ elsif title_data&.dig('new')
309
+ t( title_data['new'], title_data['new'] )
307
310
  else
308
311
  # in memory structures
309
312
  if @form['table'] == 'dc_memory'
310
- t( @form['title'], @form['title'] )
313
+ return t( @form['title'], @form['title'] ) if @form['title']
314
+
315
+ t("#{@form['i18n_prefix']}.tabletitle", '')
311
316
  else
312
317
  "#{t('drgcms.new')} : #{t_tablename(@form['table'])}"
313
318
  end
@@ -684,51 +689,40 @@ end
684
689
  ############################################################################
685
690
  # Returns list of all collections (tables) as array of choices for usage in select fields.
686
691
  # List is collected from cms_menu.yml files and may not include all collections used in application.
687
- # Currently list is only used for helping defining collection names on dc_permission form.
688
- #
692
+ # Currently list is only used for helping defining collection names on dc_permission form.
693
+ #
689
694
  # Example (as used in forms):
690
695
  # form:
691
696
  # fields:
692
697
  # 10:
693
698
  # name: table_name
694
699
  # type: text_with_select
695
- # eval: dc_choices4_all_collections
700
+ # eval: dc_choices4_all_collections
696
701
  ############################################################################
697
702
  def dc_choices4_all_collections
698
- choices = {}
699
- DrgCms.paths(:forms).reverse.each do |path|
700
- filename = "#{path}/cms_menu.yml"
701
- next unless File.exist?(filename)
702
-
703
- menu = YAML.load_file(filename) rescue nil # load menu
704
- next if menu.nil? or !menu['menu'] # not menu or error
705
- menu['menu'].each do |section|
706
- next unless section.last['items'] # next if no items
707
- section.last['items'].each do |k, v| # look for caption and
708
- key = v['table']
709
- choices[key] ||= "#{key} - #{t(v['caption'], v['caption'])}"
710
- end
711
- end
712
- end
713
- choices.invert.to_a.sort # hash has to be inverted for values to be returned right
703
+ models = Mongoid.models.map(&:to_s).uniq.map(&:underscore).delete_if { |e| e.match('/') }
704
+ models.sort.inject([]) do |r, model_name|
705
+ r << ["#{model_name} - #{t("helpers.label.#{model_name}.tabletitle", '')}", model_name]
706
+ end
714
707
  end
715
708
 
716
709
  ##########################################################################
717
- # Returns choices for creating collection edit select field on CMS top menu.
710
+ # Code for top CMS menu.
718
711
  ##########################################################################
719
- def dc_choices4_cmsmenu
712
+ def dc_cms_menu
720
713
  menus = {}
721
714
  DrgCms.paths(:forms).reverse.each do |path|
722
715
  filename = "#{path}/cms_menu.yml"
723
- next unless File.exist?(filename)
724
- menu = YAML.load_file(filename) rescue nil # load menu
725
- next if menu.nil? or !menu['menu'] # not menu or error
726
- menus = CmsHelper.forms_merge(menu['menu'], menus) # ignore top level part
727
- end
716
+ next if !File.exist?(filename)
717
+
718
+ menu = YAML.load_file(filename) rescue nil # load menu
719
+ menus = CmsHelper.forms_merge(menu['menu'], menus) if menu.dig('menu') # merge menus
720
+ end
728
721
 
729
722
  html = '<ul>'
730
723
  menus.to_a.sort.each do |index, menu| # sort menus, result is array of sorted hashes
731
724
  next unless menu['caption']
725
+
732
726
  icon = menu['icon'].match('/') ? image_tag(menu['icon']) : fa_icon(menu['icon']) #external or fa- image
733
727
  html << %(<li class="cmsedit-top-level-menu"><div>#{icon}#{t(menu['caption'])}</div><ul>)
734
728
  menu['items'].to_a.sort.each do |index1, value| # again, sort menu items first
@@ -736,19 +730,18 @@ def dc_choices4_cmsmenu
736
730
  opts = { target: value['target'] || 'iframe_cms' }
737
731
  "<li>#{dc_link_to(t(value['caption']), value['icon'] || '', value['link'], opts)}</li>"
738
732
  else
739
- opts =
740
- { controller: value['controller'],
741
- action: value['action'],
742
- table: value['table'],
743
- form_name: value['form_name'] || value['table'],
744
- target: value['target'] || 'iframe_cms',
745
- }
733
+ opts = { controller: value['controller'], action: value['action'],
734
+ table: value['table'], form_name: value['form_name'] || value['table'],
735
+ target: value['target'] || 'iframe_cms',
736
+ }
737
+ # additional parameters
738
+ value['params'].each { |k, v| opts[k] = dc_value_for_parameter(v) } if value['params']
746
739
  "<li>#{dc_link_to(t(value['caption']), value['icon'] || '', opts)}</li>"
747
740
  end
748
741
  end
749
742
  html << '</ul></li>'
750
743
  end
751
- html
744
+ html.html_safe
752
745
  end
753
746
 
754
747
  ############################################################################
@@ -760,7 +753,7 @@ def dc_choices4_folders_list
760
753
  home = File.join(public,dc_get_site.files_directory)
761
754
  choices = Dir.glob(home + '/**/*/').select { |fn| File.directory?(fn) }
762
755
  choices << home # add home
763
- choices.collect! {|e| e.gsub(public,'')} # remove public part
756
+ choices.collect! { |e| e.gsub(public,'') } # remove public part
764
757
  choices.sort
765
758
  end
766
759
 
@@ -782,7 +775,7 @@ end
782
775
  # type: select
783
776
  # eval: dc_choices4('dc_poll','name','_id')
784
777
  ############################################################################
785
- def dc_choices4(model, name, id='_id', options = {})
778
+ def dc_choices4(model, name, id = '_id', options = {})
786
779
  model = model.classify.constantize
787
780
  qry = model.only(id, name)
788
781
  if (param = options[:site])
@@ -792,8 +785,6 @@ def dc_choices4(model, name, id='_id', options = {})
792
785
  end
793
786
  qry = qry.and(active: true) if model.method_defined?(:active)
794
787
  qry = qry.order_by(name => 1).collation(locale: I18n.locale.to_s)
795
- #choices = qry.inject([]) {|result,e| result << [ e[name], e[id] ]}
796
- #choices.sort_alphabetical_by(&:first) # use UTF-8 sort
797
788
  qry.map { |e| [e[name], e[id]] }
798
789
  end
799
790
 
@@ -1073,25 +1064,25 @@ end
1073
1064
  ########################################################################
1074
1065
  def dc_internal_var(object, var_name, current_document = nil)
1075
1066
  begin
1076
- case
1077
- when object == 'session' then _origin.session[var_name]
1078
- when object == 'params' then _origin.params[var_name]
1079
- when object == 'site' then _origin.dc_get_site.send(var_name)
1080
- when object == 'page' then _origin.page.send(var_name)
1081
- when object == 'record' then
1067
+ case object
1068
+ when 'session' then _origin.session[var_name]
1069
+ when 'params' then _origin.params[var_name]
1070
+ when 'site' then _origin.dc_get_site.send(var_name)
1071
+ when 'page' then _origin.page.send(var_name)
1072
+ when 'record' then
1082
1073
  current_document ? current_document.send(var_name) : _origin.record.send(var_name)
1083
- when object == 'class' then
1074
+ when 'class' then
1084
1075
  clas, method_name = var_name.split('.')
1085
1076
  klas = clas.classify.constantize
1086
1077
  # call method. Error will be caught below.
1087
1078
  klas.send(method_name)
1088
1079
  else
1089
- 'VARIABLE: UNKNOWN OBJECT'
1080
+ 'dc_internal: UNKNOWN OBJECT'
1090
1081
  end
1091
1082
  rescue Exception => e
1092
1083
  Rails.logger.debug "\ndc_internal_var. Runtime error. #{e.message}\n"
1093
1084
  Rails.logger.debug(e.backtrace.join($/)) if Rails.env.development?
1094
- 'VARIABLE: ERROR'
1085
+ 'dc_internal: ERROR'
1095
1086
  end
1096
1087
  end
1097
1088
 
@@ -1122,11 +1113,10 @@ end
1122
1113
  def dc_get_json_ld()
1123
1114
  return '' if @json_ld.nil? or @json_ld.size == 0
1124
1115
 
1125
- %Q[
1116
+ %(
1126
1117
  <script type="application/ld+json">
1127
1118
  #{JSON.pretty_generate({'@context' => 'http://schema.org', '@graph' => @json_ld})}
1128
- </script>
1129
- ].html_safe
1119
+ </script>).html_safe
1130
1120
  end
1131
1121
 
1132
1122
  ########################################################################
@@ -1150,12 +1140,12 @@ end
1150
1140
  # Returns:
1151
1141
  # HTML data to be embedded into page header
1152
1142
  #######################################################################
1153
- def dc_get_seo_meta_tags()
1143
+ def dc_get_seo_meta_tags
1154
1144
  html = ''
1155
- html << "<link rel=\"canonical\" href=\"#{@page.canonical_link}\">\n " unless @page&.canonical_link.blank?
1145
+ html << %(<link rel="canonical" href="#{@page.canonical_link}">\n ) if @page&.canonical_link.present?
1156
1146
 
1157
1147
  html << @meta_tags.inject('') do |r, hash|
1158
- r << "<meta #{hash.first} content=\"#{hash.last}\">\n "
1148
+ r << %(<meta #{hash.first} content="#{hash.last}">\n )
1159
1149
  end if @meta_tags
1160
1150
  html.html_safe
1161
1151
  end
@@ -1170,6 +1160,7 @@ end
1170
1160
  ########################################################################
1171
1161
  def dc_add_meta_tag(type, name, content)
1172
1162
  return if content.blank?
1163
+
1173
1164
  @meta_tags ||= {}
1174
1165
  key = "#{type}=\"#{name}\""
1175
1166
  @meta_tags[key] = content
@@ -1188,7 +1179,7 @@ end
1188
1179
  # [String] alt="image-tag"
1189
1180
  #######################################################################
1190
1181
  def dc_img_alt_tag(file_name, text=nil)
1191
- " alt=\"#{dc_img_alt(file_name, text)}\" ".html_safe
1182
+ %( alt="#{dc_img_alt(file_name, text)}" ).html_safe
1192
1183
  end
1193
1184
 
1194
1185
  #######################################################################
@@ -1204,9 +1195,10 @@ end
1204
1195
  # [String] alt_image_name
1205
1196
  #######################################################################
1206
1197
  def dc_img_alt(file_name, text=nil)
1207
- return text unless text.blank?
1198
+ return text if text.present?
1199
+
1208
1200
  name = File.basename(file_name.to_s)
1209
- name[0,name.index('.')].downcase rescue name
1201
+ name[0, name.index('.')].downcase rescue name
1210
1202
  end
1211
1203
 
1212
1204
  private