cama_contact_form 0.0.24 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79f5408cc5871edfdbd3ed0fb697114eeb2e25c3
4
- data.tar.gz: 23273662b1911e32621c4620a4653a940a950acc
3
+ metadata.gz: fe2363fedca3389a35c18dd01afa5f3490cd3388
4
+ data.tar.gz: 874ff2d71598d58c8fddc2cc08396557ca718aaa
5
5
  SHA512:
6
- metadata.gz: 42a40daf5a2f72c7a25f2ba0fd165c7440443fa2023b554f74c38b4587b918e27ceb6be770c516a3773d53aec00645e0833ccfdb09b58825060ab29827441406
7
- data.tar.gz: 4c5e6aaa633869ea020f8c1adea7c852cedd41acfe9287607b91d64f2d6b77c8f04bf50b1fafd553e663b33834ca6f122000e60599133f83ce8480b2fa6f6d04
6
+ metadata.gz: 40120eed98b0562e75542e158e5ed9fabbb67ec1783ed885490a6c53ac80995d88365df3c0721df9c3372be00b18921619ce72d723b52cada74f0d76e89a09da
7
+ data.tar.gz: 94465e8e2e4c07d0efc8e5fb18382dc9603431e4d4802aa064cebf9a9e53a1e7beb350205b00411ef93fafc28220099e39b0151aa72fad08da892bab941a23d0
@@ -4,13 +4,22 @@ module Plugins::CamaContactForm::ContactFormControllerConcern
4
4
  if validate_to_save_form(form, fields, errors)
5
5
  form.fields.each do |f|
6
6
  if f[:field_type] == 'file'
7
- res = cama_tmp_upload(fields[f[:cid].to_sym], {maximum: 5.megabytes, path: Rails.public_path.join("contact_form", current_site.id.to_s)})
8
- if res[:error].present?
9
- errors << res[:error].to_s.translate
10
- else
11
- attachments << res[:file_path]
12
- fields[f[:cid].to_sym] = res[:file_path].sub(Rails.public_path.to_s, cama_root_url)
7
+ file_paths = []
8
+ fields[f[:cid].to_sym].each do |file|
9
+ res = cama_tmp_upload(file, {
10
+ maximum: current_site.get_option('filesystem_max_size', 100).megabytes,
11
+ path: Rails.public_path.join("contact_form", current_site.id.to_s),
12
+ name: file.original_filename
13
+ }
14
+ )
15
+ if res[:error].present?
16
+ errors << res[:error].to_s.translate
17
+ else
18
+ attachments << res[:file_path]
19
+ file_paths << res[:file_path].sub(Rails.public_path.to_s, cama_root_url)
20
+ end
13
21
  end
22
+ fields[f[:cid].to_sym] = file_paths
14
23
  end
15
24
  end
16
25
  new_settings = {"fields" => fields, "created_at" => Time.current.strftime("%Y-%m-%d %H:%M:%S").to_s}.to_json
@@ -27,7 +36,7 @@ module Plugins::CamaContactForm::ContactFormControllerConcern
27
36
  cama_send_email(answer_to, form.the_settings[:railscf_mail][:subject_answer].to_s.translate.cama_replace_codes(fields), {content: content})
28
37
  end
29
38
  else
30
- errors << form.the_message('mail_sent_ng', t('.error_form_val', default: 'Occurred an error, please try again.'))
39
+ errors << form.the_message('mail_sent_ng', t('.error_form_val', default: 'An error occurred, please try again.'))
31
40
  end
32
41
  end
33
42
  end
@@ -44,8 +53,8 @@ module Plugins::CamaContactForm::ContactFormControllerConcern
44
53
  errors << "#{label.to_s.translate}: #{form.the_message('invalid_required', t('.error_validation_val', default: 'This value is required'))}"
45
54
  validate = false
46
55
  end
47
- if f[:field_type].to_s == "email"
48
- if !fields[cid].match(/@/)
56
+ if f[:field_type].to_s == 'email'
57
+ unless fields[cid].match(/@/)
49
58
  errors << "#{label.to_s.translate}: #{form.the_message('invalid_email', t('.email_invalid_val', default: 'The e-mail address appears invalid'))}"
50
59
  validate = false
51
60
  end
@@ -70,7 +79,8 @@ module Plugins::CamaContactForm::ContactFormControllerConcern
70
79
  label = values.keys.include?(field[:label]) ? "#{field[:label]} (#{cid})" : field[:label].to_s.translate
71
80
  values[label] = []
72
81
  if ft == 'file'
73
- values[label] << fields[cid].split('/').last if fields[cid].present?
82
+ nr_files = fields[cid].size
83
+ values[label] << "#{nr_files} #{"file".pluralize(nr_files)} (attached)" if fields[cid].present?
74
84
  elsif ft == 'radio' || ft == 'checkboxes'
75
85
  values[label] << fields[cid].map { |f| f.to_s.translate }.join(', ') if fields[cid].present?
76
86
  else
@@ -20,7 +20,10 @@ class Plugins::CamaContactForm::FrontController < CamaleonCms::Apps::PluginsFron
20
20
  flash[:values] = fields.delete_if{|k, v| v.class.name == 'ActionDispatch::Http::UploadedFile' }
21
21
  end
22
22
  end
23
- params[:format] == 'json' ? render(json: flash.discard(:contact_form).to_hash) : (redirect_to :back)
23
+ if Rails::VERSION::MAJOR >= 5
24
+ params[:format] == 'json' ? render(json: flash.discard(:contact_form).to_hash) : (redirect_back fallback_location: cama_root_path)
25
+ else
26
+ params[:format] == 'json' ? render(json: flash.discard(:contact_form).to_hash) : (redirect_to :back)
27
+ end
24
28
  end
25
-
26
- end
29
+ end
@@ -111,7 +111,7 @@ module Plugins::CamaContactForm::MainHelper
111
111
  when 'captcha'
112
112
  temp2 = cama_captcha_tag(5, {}, {class: "#{ob[:custom_class].presence || 'form-control'} field-captcha required"}.merge(ob[:custom_attrs]))
113
113
  when 'file'
114
- temp2 = "<input multiple=\"multiple\" type=\"file\" value=\"\" name=\"#{f_name}\" #{ob[:custom_attrs].to_attr_format} class=\"#{ob[:custom_class].presence || 'form-control'}\">"
114
+ temp2 = "<input multiple=\"multiple\" type=\"file\" value=\"\" name=\"#{f_name}[]\" #{ob[:custom_attrs].to_attr_format} class=\"#{ob[:custom_class].presence || 'form-control'}\">"
115
115
  when 'dropdown'
116
116
  temp2 = cama_form_select_multiple_bootstrap(ob, ob[:label], "select",values)
117
117
  else
@@ -1,4 +1,5 @@
1
1
  class Plugins::CamaContactForm::CamaContactForm < ActiveRecord::Base
2
+ include Plugins::CamaContactForm::MainHelper
2
3
  self.table_name = 'plugins_contact_forms'
3
4
  belongs_to :site, class_name: "CamaleonCms::Site"
4
5
  # attr_accessible :site_id, :name, :description, :count, :slug, :value, :settings, :parent_id
@@ -9,6 +10,7 @@ class Plugins::CamaContactForm::CamaContactForm < ActiveRecord::Base
9
10
 
10
11
  before_validation :before_validating
11
12
  before_create :fix_save_settings
13
+ before_destroy :delete_uploaded_files
12
14
 
13
15
  default_scope { order(created_at: :desc) }
14
16
 
@@ -42,4 +44,18 @@ class Plugins::CamaContactForm::CamaContactForm < ActiveRecord::Base
42
44
  self.value = {"fields" => []}.to_json unless self.value.present?
43
45
  self.settings = {}.to_json unless self.settings.present?
44
46
  end
47
+
48
+ def delete_uploaded_files
49
+ return if self.parent_id.nil?
50
+ form = self.class.find_by_id self.parent_id
51
+ response_data = the_settings[:fields]
52
+ file_cids = form.fields
53
+ .select { |f| f[:field_type] == 'file' }
54
+ .map { |f| f[:cid].to_sym }
55
+
56
+ file_cids
57
+ .flat_map { |cid| response_data[cid] }
58
+ .map { |file| file.sub Rails.application.routes.url_helpers.cama_root_url, Rails.public_path.to_s }
59
+ .each { |file| File.delete file if File.exists? file }
60
+ end
45
61
  end
@@ -32,7 +32,12 @@
32
32
  <% @op_fields.each do |default| %>
33
33
  <% cid = default[:cid].to_sym %>
34
34
  <% if default[:field_type] == "file" %>
35
- <td><%= link_to(value[:fields][cid], value[:fields][cid]) %></td>
35
+ <td>
36
+ <%# flatten is necessary for backwards compatibility: ["file"].flatten == [["file]].flatten %>
37
+ <% [value[:fields][cid]].flatten.each do |file| %>
38
+ <%= link_to file.split('/').last, file, target: '_blank' %><br>
39
+ <% end %>
40
+ </td>
36
41
  <% elsif default[:field_type] == "radio" || default[:field_type] == "checkboxes" %>
37
42
  <td><%= value[:fields][cid].map { |f| f.to_s.translate }.join(', ') if value[:fields][cid].present? %></td>
38
43
  <% else %>
@@ -1,3 +1,3 @@
1
1
  module CamaContactForm
2
- VERSION = "0.0.24"
2
+ VERSION = "0.0.25"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cama_contact_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.24
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen Peredo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-09 00:00:00.000000000 Z
11
+ date: 2017-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails