drg_cms 0.7.0.2 → 0.7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/drg_cms/drg_cms.js +110 -35
  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 +126 -36
  7. data/app/assets/stylesheets/drg_cms/select-multiple.css +6 -7
  8. data/app/controllers/cmsedit_controller.rb +78 -47
  9. data/app/controllers/dc_application_controller.rb +22 -25
  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 +38 -39
  16. data/app/controls/dc_report.rb +9 -4
  17. data/app/controls/dc_setup_control.rb +53 -0
  18. data/app/controls/design_element_settings_control.rb +88 -37
  19. data/app/forms/all_options.yml +20 -9
  20. data/app/forms/cms_menu.yml +14 -2
  21. data/app/forms/dc_gallery.yml +1 -1
  22. data/app/forms/dc_image.yml +122 -0
  23. data/app/forms/dc_image_search.yml +72 -0
  24. data/app/forms/dc_page.yml +11 -8
  25. data/app/forms/dc_poll.yml +2 -1
  26. data/app/forms/dc_poll_result.yml +10 -7
  27. data/app/forms/dc_setup.yml +45 -0
  28. data/app/forms/dc_steps_template.yml +6 -2
  29. data/app/helpers/cms_common_helper.rb +36 -24
  30. data/app/helpers/cms_edit_helper.rb +26 -33
  31. data/app/helpers/cms_helper.rb +29 -12
  32. data/app/helpers/cms_index_helper.rb +109 -80
  33. data/app/helpers/dc_application_helper.rb +108 -86
  34. data/app/helpers/dc_image_helper.rb +127 -0
  35. data/app/models/concerns/dc_policy_rule_concern.rb +1 -1
  36. data/app/models/concerns/dc_user_concern.rb +13 -5
  37. data/app/models/dc_big_table.rb +1 -1
  38. data/app/models/dc_category.rb +12 -0
  39. data/app/models/dc_design.rb +5 -4
  40. data/app/models/dc_filter.rb +24 -27
  41. data/app/models/dc_image.rb +237 -0
  42. data/app/models/dc_internals.rb +5 -9
  43. data/app/models/dc_memory.rb +2 -2
  44. data/app/models/dc_policy_role.rb +8 -8
  45. data/app/models/dc_setup.rb +111 -0
  46. data/app/models/drgcms_form_fields/datetime_picker.rb +1 -1
  47. data/app/models/drgcms_form_fields/drgcms_field.rb +9 -26
  48. data/app/models/drgcms_form_fields/embedded.rb +28 -17
  49. data/app/models/drgcms_form_fields/journal_diff.rb +2 -2
  50. data/app/models/drgcms_form_fields/multitext_autocomplete.rb +88 -76
  51. data/app/models/drgcms_form_fields/select.rb +41 -19
  52. data/app/models/drgcms_form_fields/text_with_select.rb +5 -9
  53. data/app/renderers/dc_big_menu_renderer.rb +18 -20
  54. data/app/renderers/dc_gallery_renderer.rb +10 -4
  55. data/app/renderers/dc_menu_renderer.rb +21 -58
  56. data/app/renderers/dc_page_renderer.rb +7 -7
  57. data/app/renderers/dc_poll_renderer.rb +13 -12
  58. data/app/renderers/dc_simple_menu_renderer.rb +1 -1
  59. data/app/views/cmsedit/_edit_stuff.html.erb +4 -1
  60. data/app/views/cmsedit/edit.html.erb +1 -1
  61. data/app/views/cmsedit/index.html.erb +1 -1
  62. data/app/views/cmsedit/new.html.erb +1 -0
  63. data/config/locales/drgcms_en.yml +22 -2
  64. data/config/locales/drgcms_sl.yml +25 -6
  65. data/config/locales/models_en.yml +50 -1
  66. data/config/locales/models_sl.yml +60 -1
  67. data/drg_cms.gemspec +1 -1
  68. data/lib/drg_cms/version.rb +1 -1
  69. data/lib/drg_cms.rb +40 -27
  70. data/lib/generators/convert_to_ar/USAGE +8 -0
  71. data/lib/generators/convert_to_ar/convert_to_ar_generator.rb +158 -0
  72. data/lib/generators/new_drg_form/new_drg_form_generator.rb +32 -14
  73. metadata +19 -10
  74. data/app/assets/javascripts/drg_cms/jquery.bpopup.min.js +0 -7
  75. data/app/views/layouts/__cmsedit.html.erb +0 -16
@@ -27,7 +27,6 @@
27
27
  ##########################################################################
28
28
  class DcApplicationController < ActionController::Base
29
29
  protect_from_forgery with: :null_session, only: Proc.new { |c| c.request.format.json? }
30
- before_action :dc_reload_patches if Rails.env.development?
31
30
  before_action :dc_set_locale
32
31
 
33
32
  ########################################################################
@@ -86,12 +85,12 @@ def dc_get_site
86
85
  uri = URI.parse(request.url)
87
86
  cache_key = ['dc_site', uri.host]
88
87
 
89
- @site = dc_cache_read(cache_key)
88
+ @site = DrgCms.cache_read(cache_key)
90
89
  return @site if @site
91
90
 
92
91
  @site = DcSite.find_by(name: uri.host)
93
92
  # Site can be aliased
94
- if @site && !@site.alias_for.blank?
93
+ if @site&.alias_for.present?
95
94
  @site = DcSite.find_by(name: @site.alias_for)
96
95
  end
97
96
  # Development environment. Check if site with name test exists and use
@@ -101,7 +100,7 @@ def dc_get_site
101
100
  @site = DcSite.find_by(name: @site.alias_for) if @site
102
101
  end
103
102
  @site = nil if @site && !@site.active # site is disabled
104
- dc_cache_write(cache_key, @site)
103
+ DrgCms.cache_write(cache_key, @site)
105
104
  end
106
105
 
107
106
  ##########################################################################
@@ -161,15 +160,11 @@ protected
161
160
  # if dc_user_can(DcPermission::CAN_VIEW, params[:table]) then ...
162
161
  ############################################################################
163
162
  def dc_user_can(permission, table = params[:table])
163
+ return false if session[:user_roles].nil?
164
+
164
165
  table = table.underscore
165
- cache_key = ['dc_permission', table, session[:user_id], dc_get_site.id]
166
- permissions = dc_cache_read(cache_key)
167
- if permissions.nil?
168
- permissions = DcPermission.permissions_for_table(table)
169
- dc_cache_write(cache_key, permissions)
170
- end
171
- session[:user_roles].each { |r| return true if permissions[r] && permissions[r] >= permission }
172
- false
166
+ permissions = DrgCms.cache_read(['dc_permission', table, dc_get_site.id]) { DcPermission.permissions_for_table(table) }
167
+ session[:user_roles].inject(false) { |r, rule| break true if permissions[rule] && permissions[rule] >= permission }
173
168
  end
174
169
 
175
170
  ####################################################################
@@ -440,7 +435,7 @@ def dc_error_messages_for(document)
440
435
  msg = ''
441
436
  document.errors.each do |error|
442
437
  label = t("helpers.label.#{decamelize_type(document.class)}.#{error.attribute}")
443
- label = error.attribute if label.match( 'translation missing' )
438
+ label = error.attribute if label.match(/translation missing/i)
444
439
  msg << "<li>#{label} : #{error.message}</li>"
445
440
  end
446
441
 
@@ -800,7 +795,7 @@ private
800
795
  #
801
796
  # @return [Boolean] : True if redis cache store is active
802
797
  ########################################################################
803
- def redis_cache_store?
798
+ def xredis_cache_store?
804
799
  (Rails.application.config.cache_store.first == :redis_cache_store) rescue false
805
800
  end
806
801
 
@@ -809,31 +804,33 @@ end
809
804
  #
810
805
  # @return [Object] : Redis object
811
806
  ########################################################################
812
- def redis
807
+ def xredis
813
808
  Rails.cache.redis
814
809
  end
815
810
 
816
811
  ########################################################################
817
- # Extends DRGCMS form file. Extended file is processed first and then merged
812
+ # Extends DRG CMS form file. Extended file is processed first and then merged
818
813
  # with code in this form file. Form can extend only single form file.
819
814
  #
820
815
  # [Parameters:]
821
816
  # [extend_option] : Value of @form['extend'] option
822
817
  ########################################################################
823
818
  def dc_form_extend(extend_option)
824
- form_file_name = CmsHelper.form_file_find(extend_option)
825
- @form_js << read_js_drg_form(form_file_name)
826
- form = YAML.load_file( form_file_name )
827
- @form = CmsHelper.forms_merge(form, @form)
828
- # If combined form contains tabs and fields options, merge fields into tabs
829
- if @form['form']['tabs'] && @form['form']['fields']
830
- @form['form']['tabs']['fields'] = @form['form']['fields']
831
- @form['form']['fields'] = nil
819
+ extend_option.chomp.split(',').each do |a_file|
820
+ form_file_name = CmsHelper.form_file_find(a_file.strip)
821
+ @form_js << read_js_drg_form(form_file_name)
822
+ form = YAML.load_file( form_file_name )
823
+ @form = CmsHelper.forms_merge(form, @form)
824
+ # If combined form contains tabs and fields options, move fields into fields tab
825
+ if @form.dig('form', 'tabs') && @form.dig('form', 'fields')
826
+ @form['form']['tabs']['fields'] = @form['form']['fields']
827
+ @form['form']['fields'] = nil
828
+ end
832
829
  end
833
830
  end
834
831
 
835
832
  ########################################################################
836
- # Include code from another DRGCMS form file. Included code is merged
833
+ # Include code from another DRG CMS form file. Included code is merged
837
834
  # with current form file code. Form can include more than one other DRGCMS forms.
838
835
  #
839
836
  # [Parameters:]
@@ -124,19 +124,20 @@ end
124
124
  ####################################################################
125
125
  def process_login
126
126
  # Somebody is probably playing
127
- return dc_render_404 unless ( params[:record] && params[:record][:username] && params[:record][:password] )
127
+ return dc_render_404 unless ( params.dig(:record, :username) && params.dig(:record, :password) )
128
128
 
129
- unless params[:record][:password].blank? #password must not be empty
129
+ return_to = request.env['HTTP_REFERER'] || '/'
130
+ if params[:record][:password].present? #password must not be empty
130
131
  user = DcUser.find_by(username: params[:record][:username], active: true)
131
132
  if user && user.authenticate(params[:record][:password])
132
133
  fill_login_data(user, params[:record][:remember_me].to_i == 1)
133
- return redirect_to(params[:return_to] || '/', allow_other_host: true)
134
+ return redirect_to(params[:return_to] || return_to, allow_other_host: true)
134
135
  else
135
136
  clear_login_data # on the safe side
136
137
  end
137
138
  end
138
139
  flash[:error] = t('drgcms.invalid_username')
139
- redirect_to(params[:return_to] || '/', allow_other_host: true)
140
+ redirect_to return_to
140
141
  end
141
142
 
142
143
  ####################################################################
@@ -219,8 +220,10 @@ def copy_clipboard
219
220
  format.html do
220
221
  table = CmsHelper.table_param(params)
221
222
  doc = dc_find_document(table, params[:id], params[:ids])
222
- text = "<br><br>[#{table},#{params[:id]},#{params[:ids]}]<br>"
223
- render plain: text + doc.as_document.to_json
223
+ text = '<style>body {font-family: monospace;}</style><pre>'
224
+ text << "JSON:<br>[#{table},#{params[:id]},#{params[:ids]}]<br>#{doc.as_document.to_json}<br><br>"
225
+ text << "YAML:<br>#{doc.as_document.to_hash.to_yaml.gsub("\n", '<br>')}</pre>"
226
+ render plain: text
224
227
  end
225
228
  end
226
229
  end
@@ -1,4 +1,3 @@
1
- #coding: utf-8
2
1
  #--
3
2
  # Copyright (c) 2012+ Damjan Rems
4
3
  #
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2014+ Damjan Rems
2
+ # Copyright (c) 2022+ Damjan Rems
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -22,40 +22,25 @@
22
22
  #++
23
23
 
24
24
  ######################################################################
25
- # DrgcmsControls for DcPage model.
25
+ # DRG CMS controls for DcGallery collection
26
26
  ######################################################################
27
- module DcCategoryControl
27
+ module DcGalleryControl
28
28
 
29
29
  ######################################################################
30
- # Called when new empty record is created
30
+ # Remove picture from gallery.
31
31
  ######################################################################
32
- def dc_new_record
33
- if params[:selected]
34
-
35
- end
36
- if params[:from_menu]
37
- # find menu and submenu. Menu class is defined in Site.
38
- menu_a = params[:id].split(';')
39
- menu = dc_get_site.menu_klass.find(menu_a.shift)
40
- menu_items_method = "#{dc_get_site.menu_class}_items".underscore
41
- menu_i = menu.send(menu_items_method).find(menu_a.shift)
42
- while menu_a.size > 0 do menu_i = menu_i.send(menu_items_method).find(menu_a.shift) end
43
- # Fill values for form
44
- @record.subject = menu_i.caption
45
- @record.dc_site_id = menu.dc_site_id
46
- @record.menu_id = params[:id]
47
- # set update_menu on save parameter
48
- params['p__update_menu'] = '1'
49
- else
50
- @record.design_id = params[:design_id] if params[:design_id]
51
- return unless params[:page_id]
52
- # inherit some values from currently active page
53
- if page = DcPage.find(params[:page_id])
54
- @record.design_id = page.design_id
55
- @record.menu = page.menu
56
- @record.dc_site_id = page.dc_site_id
57
- end
32
+ def picture_remove
33
+ unless dc_user_can(DcPermission::CAN_DELETE, 'dc_gallery')
34
+ return render json: { alert: t('drgcms.not_authorized') }
58
35
  end
36
+
37
+ doc = DcGallery.find(params[:id])
38
+ pic = Rails.root.join('public', doc.picture.delete_prefix('/'))
39
+ File.delete(pic) if File.exist?(pic)
40
+ pic = Rails.root.join('public', doc.thumbnail.delete_prefix('/'))
41
+ File.delete(pic) if File.exist?(pic)
42
+ doc.delete
43
+ render json: { reload: true }
59
44
  end
60
45
 
61
46
  end
@@ -0,0 +1,180 @@
1
+ #--
2
+ # Copyright (c) 2022+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ ######################################################################
25
+ # DrgcmsControls for DcImage data entry.
26
+ ######################################################################
27
+ module DcImageControl
28
+
29
+ ######################################################################
30
+ #
31
+ ######################################################################
32
+ def search_filter
33
+ flash[:record] ||= {}
34
+
35
+ created_by = flash[:record][:created_by]
36
+ qry = created_by.present? ? DcImage.where(created_by: created_by) : DcImage.all
37
+
38
+ short_name = flash[:record][:short]
39
+ qry = qry.and(short: /#{short_name}/i) if short_name.present?
40
+
41
+ category = flash[:record][:categories]
42
+ qry = qry.and(categories: category) if category.present?
43
+ qry.limit(30).order_by(created_at: -1)
44
+ end
45
+
46
+ ######################################################################
47
+ # Invoke images search. Just forward parameters and reload form. Filter parameters will
48
+ # be taken into account on reload.
49
+ ######################################################################
50
+ def images_search
51
+ flash[:record] = {}
52
+ flash[:record][:short] = params[:record][:short]
53
+ flash[:record][:created_by] = params[:record][:created_by]
54
+ flash[:record][:categories] = params[:record][:categories]
55
+
56
+ url = url_for(controller: :cmsedit, table: :dc_image, form_name: :dc_image_search, field_name: params[:field_name])
57
+ render json: { url: url }
58
+ end
59
+
60
+ ######################################################################
61
+ # Set some default values when new record
62
+ ######################################################################
63
+ def dc_new_record
64
+ default_sizes = dc_get_site.params.dig('dc_image', 'sizes').to_s.split(',')
65
+ @record.size_ls = default_sizes.shift
66
+ @record.size_ms = default_sizes.shift
67
+ @record.size_ss = default_sizes.shift
68
+ end
69
+
70
+ ######################################################################
71
+ # Save uploaded file if selected and extract properties data
72
+ ######################################################################
73
+ def dc_before_save
74
+ return if @record.size_o.present? || !params[:upload_file]
75
+
76
+ input_file_name = params[:upload_file].original_filename
77
+ type = File.extname(input_file_name).to_s.downcase.gsub('.', '').strip
78
+ unless %w(jpg jpeg png gif svg webp).include?(type)
79
+ flash[:error] = t 'drgcms.dc_image.wrong_type'
80
+ return false
81
+ end
82
+ name = File.basename(input_file_name)
83
+ path = File.dirname(params[:upload_file].tempfile)
84
+
85
+ @record.img_type = dc_get_site.params.dig('dc_image', 'img_type') || type
86
+ @record.short = File.basename(input_file_name, '.*') if @record.short.blank?
87
+ @record.name = File.join(path, name)
88
+ FileUtils.mv(params[:upload_file].tempfile, @record.name, force: nil)
89
+ end
90
+
91
+ ######################################################################
92
+ # Prepare additional images
93
+ ######################################################################
94
+ def dc_after_save
95
+ %w[l m s o].each { |size| image_convert(size) }
96
+ end
97
+
98
+ private
99
+
100
+ ######################################################################
101
+ #
102
+ ######################################################################
103
+ def image_convert(which)
104
+ new_file_name = "#{@record.id}-#{which}.#{@record.img_type}"
105
+ new_file_name = Rails.root.join('public', images_location, new_file_name)
106
+ new_size = @record["size_#{which}"]
107
+ # remove file if not needed
108
+ if new_size.blank?
109
+ FileUtils.rm(new_file_name) if File.exist?(new_file_name)
110
+ return
111
+ end
112
+
113
+ original_file_name = Rails.root.join('public', images_location, "#{@record.id}-o.#{@record.img_type}")
114
+ @record.name = original_file_name unless @record.name.present? && File.exist?(@record.name)
115
+ if !File.exist?(@record.name)
116
+ flash[:warning] = t 'drgcms.dc_image.no_file'
117
+ return
118
+ end
119
+
120
+ image_magick_do(new_size, new_file_name)
121
+ end
122
+
123
+ ######################################################################
124
+ # Crop and resize image
125
+ #
126
+ # @new_size [String] new_size widthxheight+offsetx+offsety 300x200+1000+0
127
+ # @file_name [String] Image file name
128
+ ######################################################################
129
+ def image_magick_do(new_size, file_name)
130
+ image = MiniMagick::Image.open(@record.name)
131
+
132
+ a = new_size.split(/x|\+/).map(&:to_i)
133
+ a += [0, 0] if a.size < 4
134
+ image_offset(image, a[2, 2]) if a[2, 2] != [0, 0]
135
+
136
+ img_w, img_h = image.width, image.height
137
+ new_w, new_h = a[0, 2]
138
+ img_ratio = img_w.to_f / img_h
139
+ new_ratio = new_w.to_f / new_h
140
+ formula = if new_ratio > img_ratio
141
+ "#{img_w}x#{img_w/new_ratio}+0+0"
142
+ else
143
+ "#{img_h*new_ratio}x#{img_h}+0+0"
144
+ end
145
+ image.crop(formula)
146
+
147
+ image.resize("#{new_w}x#{new_h}")
148
+ image.write(file_name)
149
+ image_reduce(file_name)
150
+ end
151
+
152
+ ######################################################################
153
+ # Reduce image quality of image
154
+ ######################################################################
155
+ def image_reduce(file_name)
156
+ if (quality = dc_get_site.params.dig('dc_image', 'quality').to_i) > 0
157
+ convert = MiniMagick::Tool::Convert.new
158
+ convert << file_name
159
+ convert.quality(quality)
160
+ convert << file_name
161
+ convert.call
162
+ end
163
+ end
164
+
165
+ ######################################################################
166
+ # Offset image if requested
167
+ ######################################################################
168
+ def image_offset(image, offset)
169
+ img_w, img_h = image.width - offset[0], image.height - offset[1]
170
+ image.crop("#{img_w}x#{img_h}+#{offset[0]}+#{offset[1]}")
171
+ end
172
+
173
+ ######################################################################
174
+ # Returns location of images files relative to public directory
175
+ ######################################################################
176
+ def images_location
177
+ dc_get_site.params.dig('dc_image', 'location') || 'images'
178
+ end
179
+
180
+ end
@@ -22,14 +22,14 @@
22
22
  #++
23
23
 
24
24
  ######################################################################
25
- # DrgcmsControls for DcPage model.
25
+ # DRG Controls for DcPage model.
26
26
  ######################################################################
27
27
  module DcPageControl
28
28
 
29
29
  ######################################################################
30
30
  # Called when new empty record is created
31
31
  ######################################################################
32
- def dc_new_record()
32
+ def dc_new_record
33
33
  # Called from menu. Fill in values, that could be obtained from menu
34
34
  if params[:from_menu]
35
35
  # find menu and submenu. Menu class is defined in Site.
@@ -59,7 +59,7 @@ end
59
59
  ######################################################################
60
60
  # Called just after record is saved to DB.
61
61
  ######################################################################
62
- def dc_after_save()
62
+ def dc_after_save
63
63
  if params.dig(:_record,:_update_menu).to_s == '1'
64
64
  dc_get_site.menu_klass.update_menu_item_link(@record)
65
65
  end
@@ -1,4 +1,3 @@
1
- #encoding: utf-8
2
1
  #--
3
2
  # Copyright (c) 2014+ Damjan Rems
4
3
  #
@@ -23,66 +22,66 @@
23
22
  #++
24
23
 
25
24
  ######################################################################
26
- # DrgcmsControls for DcPage model.
25
+ # DRG Controls for DcPage model.
27
26
  ######################################################################
28
27
  module DcPollResultControl
29
28
 
30
29
  ######################################################################
31
- # Filter result data when filter is set
30
+ # Set filter action called from form.
32
31
  ######################################################################
33
- def poll_filter()
34
- get_query
35
- end
32
+ def filter_set
33
+ record = params[:record]
34
+ filter = "DcPollResult.where(dc_poll_id: '#{record[:dc_poll_id]}')"
35
+ filter << ".and(:created_at.gte => '#{Time.parse(record[:start_date]).beginning_of_day}')" if record[:start_date].present?
36
+ filter << ".and(:created_at.lte => '#{Time.parse(record[:end_date]).end_of_day}')" if record[:end_date].present?
36
37
 
37
- ######################################################################
38
- # Filter action called. Update url to reflect filter conditions and reload form.
39
- ######################################################################
40
- def do_filter
41
- url = url_for(controller: 'cmsedit', action: :index, table: :dc_poll_result,
42
- 'record[dc_poll_id]' => params[:record][:dc_poll_id],
43
- 'record[start_date]' => params[:record][:start_date],
44
- 'record[end_date]' => params[:record][:end_date]
45
- )
46
- dc_render_ajax(operation: :url, value: url)
38
+ session['dc_poll_result'][:filter] = {'field' => I18n.t('drgcms.filter_off'),
39
+ 'operation' => 'eval',
40
+ 'value' => filter,
41
+ 'input' => '',
42
+ 'table' => 'dc_poll_result' }.to_yaml
43
+ session['dc_poll_result'][:page] = 1 # must also be set
44
+
45
+ render json: { url: '/cmsedit?t=dc_poll_result'}
47
46
  end
48
47
 
49
48
  ######################################################################
50
49
  # Export data to file
51
50
  ######################################################################
52
- def do_export
51
+ def data_export
53
52
  c, keys = '', []
54
- get_query.to_a.each do |doc|
55
- # ensure, that fields are always in same order
53
+ data_get.each do |doc|
56
54
  data = YAML.load(doc.data)
55
+ # header and ensure fields are always in same order
57
56
  if c.blank?
58
- data.each {|k,v| keys << k}
59
- c << I18n.t('helpers.label.dc_poll_result.created_at') + "\t"
60
- c << keys.join("\t") + "\n"
57
+ keys = data.map(&:first)
58
+ c << I18n.t('helpers.label.dc_poll_result.created_at') + ";"
59
+ c << keys.join(";") + "\n"
61
60
  end
62
- c << doc.created_at.strftime(I18n.t('date.formats.default') ) + "\t"
63
- keys.each {|k| c << data[k] + "\t"}
64
- c << "\n"
61
+
62
+ c << doc.created_at.strftime(I18n.t('time.formats.default')) + ";"
63
+ keys.each do |k|
64
+ c << if data[k].class == String
65
+ %("#{data[k].gsub(/\"/, "'").gsub(';', ',')}";)
66
+ else
67
+ %("#{data[k]}";)
68
+ end
69
+ end
70
+ c << "\r\n"
65
71
  end
66
- File.write(Rails.root.join('public','export.csv'), c)
67
- dc_render_ajax(operation: :window, value: 'export.csv')
72
+ File.write(Rails.root.join('public', 'export.csv'), c)
73
+ render json: { 'window_' => 'export.csv' }
68
74
  end
69
75
 
70
76
  private
71
77
  ######################################################################
72
78
  # Creates query for Poll results
73
79
  ######################################################################
74
- def get_query()
75
- if params.dig(:record,:dc_poll_id).nil?
76
- qry = DcPollResult.all
77
- else
78
- qry = DcPollResult.where(dc_poll_id: params[:record][:dc_poll_id])
79
- unless params[:record][:start_date].blank?
80
- start_date = DrgcmsFormFields::DatePicker.get_data(params,'start_date').beginning_of_day
81
- end_date = DrgcmsFormFields::DatePicker.get_data(params,'end_date').end_of_day
82
- qry = qry.and(:created_at.gt => start_date).and(:created_at.lt => end_date)
83
- end
84
- end
85
- qry
80
+ def data_get
81
+ return [] if session.dig('dc_poll_result', :filter).blank?
82
+
83
+ filter = YAML.load(session['dc_poll_result'][:filter])
84
+ eval filter['value'] rescue []
86
85
  end
87
86
 
88
87
  end
@@ -55,7 +55,7 @@ def print
55
55
  render json: { msg_error: t('drgcms.runtime_error') } and return
56
56
  end
57
57
 
58
- pdf_file = "tmp/dokument-#{Time.now.to_i}.pdf"
58
+ pdf_file = "tmp/document-#{Time.now.to_i}.pdf"
59
59
  @pdf.render_file Rails.root.join('public', pdf_file)
60
60
 
61
61
  render json: print_response(pdf_file)
@@ -142,14 +142,19 @@ def export_to_excel(report_id)
142
142
  columns.each_with_index do |column, i|
143
143
  caption = column.last['caption'] || column.last['label']
144
144
  label = t(caption)
145
- excel[n, i] = label.match('translation missing') ? caption : label
145
+ excel[n, i] = label.match(/translation missing/i) ? caption : label
146
146
  end
147
147
 
148
148
  data_filter.each do |doc|
149
149
  n += 1
150
150
  columns.each_with_index do |column, i|
151
- value = doc[column.last['name']].to_s.gsub('<br>', ";")
152
- value = value.gsub(/\<\/strong\>|\<strong\>|\<\/b\>|\<b\>/, '')
151
+ value = doc[column.last['name']]
152
+ value = case value.class.to_s
153
+ when /Integer|Float/ then value
154
+ when /Decimal/ then value.to_s.to_f
155
+ else
156
+ value.to_s.gsub('<br>', ";").gsub(/\<\/strong\>|\<strong\>|\<\/b\>|\<b\>/, '')
157
+ end
153
158
  excel[n, i] = value
154
159
  end
155
160
  end
@@ -0,0 +1,53 @@
1
+ #--
2
+ # Copyright (c) 2024+ Damjan Rems
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ ################################################################################
25
+ # Controls for DcSetup edit form.
26
+ ################################################################################
27
+ module DcSetupControl
28
+
29
+ ################################################################################
30
+ # Update edit form. Admin sees everything while user sees only setup fields.
31
+ ################################################################################
32
+ def dc_update_form
33
+ return unless params[:id]
34
+
35
+ record = if BSON::ObjectId.legal?(params[:id])
36
+ DcSetup.find(params[:id])
37
+ else
38
+ DcSetup.find_by(name: params[:id])
39
+ end
40
+
41
+ unless dc_user_has_role('admin')
42
+ @form['form'].delete('tabs')
43
+ @form['readonly'] = true unless record.editors.include?(session[:user_id])
44
+ end
45
+
46
+ form = YAML.load(record.form) rescue nil
47
+ if form.present?
48
+ @form['form']['tabs'] ||= {}
49
+ @form['form']['tabs'].merge!(form)
50
+ end
51
+ end
52
+
53
+ end