cama_contact_form 0.0.12 → 0.0.13

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: 958e0958c802d18c8cdc6fb122c59ee1cbe9f9c2
4
- data.tar.gz: 2e3356ddca2e8315498a47a2d6745d9c7b5d79ef
3
+ metadata.gz: 754dfa197c146d62c5ef2feb79a72b8e7714d9b7
4
+ data.tar.gz: fbc7cbeb3eeeea99a27af944a9812cc132b60e93
5
5
  SHA512:
6
- metadata.gz: 189bbdcbb6bc4f41b9dfa959dcd425b484098103d552ddc807d2851b12e3420e68f43f50f0c3511b41e8af2504893a096e34a8bc30e8745ae08756be3240961b
7
- data.tar.gz: 406aa5606a100b438fd724515a8dedc14972fa7926943327a7732bcc11c8ae77358d863544e66bb341fdea57a4c7461f3870f542d30baba0abf1a80ff52165fe
6
+ metadata.gz: a8dbc560c7fee9f3cd38ad87bc39751109444724dad67264c98833e20d7de6ea3363966fdf9eb31a27d37f9888b9c017c74db8a66dab258f8a4ba969b4b0b824
7
+ data.tar.gz: 17be3c9ae429dfff03db251dcb3e4f68f494e1c39477762f61aa6cf4dc673654ffc1863f8a9866492669f0de811ad601b6f798f9e5486fae7bf314fd04aa2ed8
@@ -0,0 +1,83 @@
1
+ module Plugins::CamaContactForm::ContactFormControllerConcern
2
+ def perform_save_form(form, fields, success, errors)
3
+ attachments = []
4
+ if validate_to_save_form(form, fields, errors)
5
+ form.fields.each do |f|
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]
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)
13
+ end
14
+ end
15
+ end
16
+ new_settings = {"fields" => fields, "created_at" => Time.current.strftime("%Y-%m-%d %H:%M:%S").to_s}.to_json
17
+ form_new = current_site.contact_forms.new(name: "response-#{Time.now}", description: form.description, settings: new_settings, site_id: form.site_id, parent_id: form.id)
18
+ if form_new.save
19
+ fields_data = convert_form_values(form, fields)
20
+ message_body = form.the_settings[:railscf_mail][:body].to_s.translate.cama_replace_codes(fields)
21
+ content = render_to_string(partial: plugin_view('contact_form/email_content'), layout: false, locals: {file_attachments: attachments, fields: fields_data, values: fields, message_body: message_body, form: form})
22
+ cama_send_email(form.the_settings[:railscf_mail][:to], form.the_settings[:railscf_mail][:subject].to_s.translate.cama_replace_codes(fields), {attachments: attachments, content: content, extra_data: {fields: fields_data}})
23
+ success << form.the_message('mail_sent_ok', t('.success_form_val', default: 'Your message has been sent successfully. Thank you very much!'))
24
+ args = {form: form, values: fields}; hooks_run("contact_form_after_submit", args)
25
+ if form.the_settings[:railscf_mail][:to_answer].present? && (answer_to = fields[form.the_settings[:railscf_mail][:to_answer].gsub(/(\[|\])/, '').to_sym]).present?
26
+ content = form.the_settings[:railscf_mail][:body_answer].to_s.translate.cama_replace_codes(fields)
27
+ cama_send_email(answer_to, form.the_settings[:railscf_mail][:subject_answer].to_s.translate.cama_replace_codes(fields), {content: content})
28
+ end
29
+ else
30
+ errors << form.the_message('mail_sent_ng', t('.error_form_val', default: 'Occurred an error, please try again.'))
31
+ end
32
+ end
33
+ end
34
+
35
+ # form validations
36
+ def validate_to_save_form(form, fields, errors)
37
+ validate = true
38
+ form.fields.each do |f|
39
+ cid = f[:cid].to_sym
40
+ label = f[:label].to_sym
41
+ case f[:field_type].to_s
42
+ when 'text', 'website', 'paragraph', 'textarea', 'email', 'radio', 'checkboxes', 'dropdown', 'file'
43
+ if f[:required].to_s.cama_true? && !fields[cid].present?
44
+ errors << "#{label}: #{form.the_message('invalid_required', t('.error_validation_val', default: 'This value is required'))}"
45
+ validate = false
46
+ end
47
+ if f[:field_type].to_s == "email"
48
+ if !fields[cid].match(/@/)
49
+ errors << "#{label}: #{form.the_message('invalid_email', t('.email_invalid_val', default: 'The e-mail address appears invalid'))}"
50
+ validate = false
51
+ end
52
+ end
53
+ when 'captcha'
54
+ unless cama_captcha_verified?
55
+ errors << "#{label}: #{form.the_message('captcha_not_match', t('.captch_error_val', default: 'The entered code is incorrect'))}"
56
+ validate = false
57
+ end
58
+ end
59
+ end
60
+ validate
61
+ end
62
+
63
+ # form values with labels + values to save
64
+ def convert_form_values(form, fields)
65
+ values = {}
66
+ form.fields.each do |field|
67
+ cid = field[:cid].to_sym
68
+ label = values.keys.include?(field[:label]) ? "#{field[:label]} (#{cid})" : field[:label].to_s.translate
69
+ values[label] = []
70
+ if field[:field_type] == 'submit' || field[:field_type] == 'button'
71
+ elsif field[:field_type] == 'file'
72
+ values[label] << fields[cid].split('/').last if fields[cid].present?
73
+ elsif field[:field_type] == 'captcha'
74
+ values[label] << '--'
75
+ elsif field[:field_type] == 'radio' || field[:field_type] == 'checkboxes'
76
+ values[label] << fields[cid].join(',') if fields[cid].present?
77
+ else
78
+ values[label] << fields[cid] if fields[cid].present?
79
+ end
80
+ end
81
+ return values
82
+ end
83
+ end
@@ -1,7 +1,10 @@
1
1
  class Plugins::CamaContactForm::FrontController < CamaleonCms::Apps::PluginsFrontController
2
2
  include Plugins::CamaContactForm::MainHelper
3
+ include Plugins::CamaContactForm::ContactFormControllerConcern
4
+
3
5
  # here add your custom functions
4
6
  def save_form
7
+ flash[:contact_form] = {}
5
8
  @form = current_site.contact_forms.find_by_id(params[:id])
6
9
  fields = params[:fields]
7
10
  errors = []
@@ -9,93 +12,12 @@ class Plugins::CamaContactForm::FrontController < CamaleonCms::Apps::PluginsFron
9
12
 
10
13
  perform_save_form(@form, fields, success, errors)
11
14
  if success.present?
12
- flash[:notice] = success.join('<br>')
15
+ flash[:contact_form][:notice] = success.join('<br>')
13
16
  else
14
- flash[:error] = errors.join('<br>')
17
+ flash[:contact_form][:error] = errors.join('<br>')
15
18
  flash[:values] = fields.delete_if{|k, v| v.class.name == 'ActionDispatch::Http::UploadedFile' }
16
19
  end
17
20
  redirect_to :back
18
21
  end
19
22
 
20
- def perform_save_form(form, fields, success, errors)
21
- attachments = []
22
- if validate_to_save_form(form, fields, errors)
23
- form.fields.each do |f|
24
- if f[:field_type] == 'file'
25
- res = cama_tmp_upload(fields[f[:cid].to_sym], {maximum: 5.megabytes, path: Rails.public_path.join("contact_form", current_site.id.to_s)})
26
- if res[:error].present?
27
- errors << res[:error]
28
- else
29
- attachments << res[:file_path]
30
- fields[f[:cid].to_sym] = res[:file_path].sub(Rails.public_path.to_s, cama_root_url)
31
- end
32
- end
33
- end
34
- new_settings = {"fields" => fields, "created_at" => Time.current.strftime("%Y-%m-%d %H:%M:%S").to_s}.to_json
35
- form_new = current_site.contact_forms.new(name: "response-#{Time.now}", description: form.description, settings: new_settings, site_id: form.site_id, parent_id: form.id)
36
- if form_new.save
37
- fields_data = convert_form_values(form, fields)
38
- message_body = form.the_settings[:railscf_mail][:body].to_s.translate.cama_replace_codes(fields)
39
- content = render_to_string(partial: plugin_view('contact_form/email_content'), layout: false, locals: {file_attachments: attachments, fields: fields_data, values: fields, message_body: message_body, form: form})
40
- cama_send_email(form.the_settings[:railscf_mail][:to], form.the_settings[:railscf_mail][:subject].to_s.translate.cama_replace_codes(fields), {attachments: attachments, content: content, extra_data: {fields: fields_data}})
41
- success << form.the_message('mail_sent_ok', t('.success_form_val', default: 'Your message has been sent successfully. Thank you very much!'))
42
- args = {form: form, values: fields}; hooks_run("contact_form_after_submit", args)
43
- if form.the_settings[:railscf_mail][:to_answer].present? && (answer_to = fields[form.the_settings[:railscf_mail][:to_answer].gsub(/(\[|\])/, '').to_sym]).present?
44
- content = form.the_settings[:railscf_mail][:body_answer].to_s.translate.cama_replace_codes(fields)
45
- cama_send_email(answer_to, form.the_settings[:railscf_mail][:subject_answer].to_s.translate.cama_replace_codes(fields), {content: content})
46
- end
47
- else
48
- errors << form.the_message('mail_sent_ng', t('.error_form_val', default: 'Occurred an error, please try again.'))
49
- end
50
- end
51
- end
52
-
53
- # form validations
54
- def validate_to_save_form(form, fields, errors)
55
- validate = true
56
- form.fields.each do |f|
57
- cid = f[:cid].to_sym
58
- label = f[:label].to_sym
59
- case f[:field_type].to_s
60
- when 'text', 'website', 'paragraph', 'textarea', 'email', 'radio', 'checkboxes', 'dropdown', 'file'
61
- if f[:required].to_s.cama_true? && !fields[cid].present?
62
- errors << "#{label}: #{form.the_message('invalid_required', t('.error_validation_val', default: 'This value is required'))}"
63
- validate = false
64
- end
65
- if f[:field_type].to_s == "email"
66
- if !fields[cid].match(/@/)
67
- errors << "#{label}: #{form.the_message('invalid_email', t('.email_invalid_val', default: 'The e-mail address appears invalid'))}"
68
- validate = false
69
- end
70
- end
71
- when 'captcha'
72
- unless cama_captcha_verified?
73
- errors << "#{label}: #{form.the_message('captcha_not_match', t('.captch_error_val', default: 'The entered code is incorrect'))}"
74
- validate = false
75
- end
76
- end
77
- end
78
- validate
79
- end
80
-
81
- # form values with labels + values to save
82
- def convert_form_values(form, fields)
83
- values = {}
84
- form.fields.each do |field|
85
- cid = field[:cid].to_sym
86
- label = values.keys.include?(field[:label]) ? "#{field[:label]} (#{cid})" : field[:label].to_s.translate
87
- values[label] = []
88
- if field[:field_type] == 'submit' || field[:field_type] == 'button'
89
- elsif field[:field_type] == 'file'
90
- values[label] << fields[cid].split('/').last if fields[cid].present?
91
- elsif field[:field_type] == 'captcha'
92
- values[label] << '--'
93
- elsif field[:field_type] == 'radio' || field[:field_type] == 'checkboxes'
94
- values[label] << fields[cid].join(',') if fields[cid].present?
95
- else
96
- values[label] << fields[cid] if fields[cid].present?
97
- end
98
- end
99
- return values
100
- end
101
23
  end
@@ -1,6 +1,6 @@
1
1
  class Plugins::CamaContactForm::CamaContactForm < ActiveRecord::Base
2
2
  self.table_name = 'plugins_contact_forms'
3
- belongs_to :site, class_name: "CamleonCms::Site"
3
+ belongs_to :site, class_name: "CamaleonCms::Site"
4
4
  # attr_accessible :site_id, :name, :description, :count, :slug, :value, :settings, :parent_id
5
5
 
6
6
  has_many :responses, :class_name => "Plugins::CamaContactForm::CamaContactForm", :foreign_key => :parent_id, dependent: :destroy
@@ -9,9 +9,8 @@
9
9
  <section>
10
10
  <%= raw r[:before_form] %>
11
11
  <%= form_for @form, url: plugins_cama_contact_form_save_form_path, html: {method: "post", class: r[:form_class], multipart: true} do |f| %>
12
-
13
12
  <%= hidden_field_tag "id", @form.id %>
14
- <%= render :partial => "camaleon_cms/flash_messages" %>
13
+ <%= render :partial => "camaleon_cms/flash_messages", locals:{ flash: flash[:contact_form] } %>
15
14
  <%= raw cama_form_element_bootstrap_object(r[:form], values[:fields], values_fields) %>
16
15
  <% if values[:fields].present? && !values[:fields].delete_if{|field| field[:field_type] != 'submit' }.present? %>
17
16
  <%= raw r[:submit].sub('[submit_label]', @form.the_settings[:railscf_form_button][:name_button])%>
@@ -1,3 +1,3 @@
1
1
  module CamaContactForm
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
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.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen Peredo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-03 00:00:00.000000000 Z
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -48,6 +48,7 @@ files:
48
48
  - MIT-LICENSE
49
49
  - Rakefile
50
50
  - app/assets/javascripts/plugins/cama_contact_form/admin_editor.js.coffee
51
+ - app/controllers/concerns/plugins/cama_contact_form/contact_form_controller_concern.rb
51
52
  - app/controllers/plugins/cama_contact_form/admin_forms_controller.rb
52
53
  - app/controllers/plugins/cama_contact_form/front_controller.rb
53
54
  - app/helpers/plugins/cama_contact_form/main_helper.rb
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  requirements: []
127
128
  rubyforge_project:
128
- rubygems_version: 2.5.1
129
+ rubygems_version: 2.4.8
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: Contact Form Plugin for Camaleon CMS