Module: Plugins::ContactForm::ContactFormHelper

Defined in:
app/apps/plugins/contact_form/contact_form_helper.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


10
11
12
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 10

def self.included(klass)
  klass.helper_method :get_plugin_form rescue ""
end

Instance Method Details

#contact_form_admin_before_loadObject


76
77
78
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 76

def contact_form_admin_before_load
  admin_menu_append_menu_item("settings", {icon: "envelope-o", title: t('plugin.contact_form.contact_form'), url: admin_plugins_contact_form_admin_forms_path, datas: "data-intro='This plugin permit you to create you contact forms with desired fields and paste your short_code in any content.' data-position='right'"})
end

#contact_form_app_before_loadObject


80
81
82
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 80

def contact_form_app_before_load
  shortcode_add('forms', plugin_view("forms_shorcode"), "This is a shortocode for contact form to permit you to put your contact form in any content. Sample: [forms slug='key-for-my-form']")
end

#contact_form_front_before_loadObject


84
85
86
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 84

def contact_form_front_before_load

end

#contact_form_on_active(plugin) ⇒ Object

here all actions on going to active you can run sql commands like this: results = ActiveRecord::Base.connection.execute(query); plugin: plugin model


58
59
60
61
62
63
64
65
66
67
68
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 58

def contact_form_on_active(plugin)
  unless ActiveRecord::Base.connection.table_exists? 'plugins_contact_forms'

    ActiveRecord::Base.connection.create_table :plugins_contact_forms do |t|
      t.integer :site_id, :count, :parent_id
      t.string :name, :slug
      t.text :description, :value, :settings
      t.timestamps
    end
  end
end

#contact_form_on_destroy(plugin) ⇒ Object

here all actions on plugin destroying plugin: plugin model


50
51
52
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 50

def contact_form_on_destroy(plugin)
  ActiveRecord::Base.connection.execute('DROP TABLE plugins_contact_forms;');
end

#contact_form_on_export(args) ⇒ Object


18
19
20
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 18

def contact_form_on_export(args)
  args[:obj][:plugins][self_plugin_key] = JSON.parse(current_site.contact_forms.to_json(:include => [:responses]))
end

#contact_form_on_import(args) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 22

def contact_form_on_import(args)
  plugins = args[:data][:plugins]
  if plugins[self_plugin_key.to_sym].present?
    plugins[self_plugin_key.to_sym].each do |contact|
      unless current_site.contact_forms.where(slug: contact[:slug]).first.present?
        sba_data = ActionController::Parameters.new(contact)
        contact_new = current_site.contact_forms.new(sba_data.permit(:name, :slug, :count, :description, :value, :settings))
        if contact_new.save!
          if contact[:get_field_groups] # save group fields
            save_field_group(contact_new, contact[:get_field_groups])
          end
          save_field_values(contact_new, contact[:field_values])

          if contact[:responses].present? # saving responses for this contact
            contact[:responses].each do |response|
              sba_data = ActionController::Parameters.new(response)
              contact_new.responses.create!(sba_data.permit(:name, :slug, :count, :description, :value, :settings))
            end
          end
          args[:messages] << "Saved Plugin Contact Form: #{contact_new.name}"
        end
      end
    end
  end
end

#contact_form_on_inactive(plugin) ⇒ Object

here all actions on going to inactive plugin: plugin model


72
73
74
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 72

def contact_form_on_inactive(plugin)

end

#fix_meta_value(value) ⇒ Object


170
171
172
173
174
175
176
177
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 170

def fix_meta_value(value)
  if (value.is_a?(Array) || value.is_a?(Hash))
    value = value.to_json
  elsif value.is_a?(String)
    value = value.to_var
  end
  value
end

#get_plugin_formObject


14
15
16
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 14

def get_plugin_form
  plugin = current_plugin
end

#perform_save_form(form, values, fields, settings, success, errors) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 88

def perform_save_form(form, values, fields, settings, success, errors)
  attachments = []
  if validate_to_save_form(values, fields, settings, errors)
    values[:fields].each do |f|
      cid = f[:cid].to_sym
      if f[:field_type] == 'file'
        res = upload_file(fields[cid], {maximum: 5.megabytes, folder: 'contact_form'})
        if res[:error].present?
          errors << res[:error]
        else
          attachments << res['file']
        end
      end
    end

    new_settings = {"fields" => fields, "created_at" => Time.now.strftime("%Y-%m-%d %H:%M:%S").to_s}.to_json
    form_new = current_site.contact_forms.new(name: "response-#{Time.now}", description: form.description, settings: fix_meta_value(new_settings), site_id: form.site_id, parent_id: form.id)

    if form_new.save
      begin
        content = render_to_string partial: 'contact_form/submission',
                                   layout: false,
                                   locals: {
                                       file_attachments: attachments,
                                       fields: convert_form_values(
                                           values[:fields],
                                           fields
                                       )
                                   }
      rescue ActionView::MissingTemplate
        content = render_to_string partial: 'contact_form/views/contact_form/submission',
                                   layout: false,
                                   locals: {
                                       file_attachments: attachments,
                                       fields: convert_form_values(
                                           values[:fields],
                                           fields
                                       )
                                   }
      end

      sendmail(
          settings[:railscf_mail][:to], settings[:railscf_mail][:subject],
          content,
          settings[:railscf_mail][:to],
          attachments
      )
      success << settings[:railscf_message][:mail_sent_ok]
    else
      errors << settings[:railscf_message][:mail_sent_ng]
    end
  end
end

#validate_to_save_form(values, fields, settings, errors) ⇒ Object


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/apps/plugins/contact_form/contact_form_helper.rb', line 142

def validate_to_save_form(values, fields, settings, errors)
  validate = true
  values[:fields].each do |f|
    cid = f[:cid].to_sym
    label = f[:label].to_sym

    case f[:field_type].to_s
      when 'text', 'website', 'paragraph', 'textarea', 'email', 'radio', 'checkboxes', 'dropdown', 'file'
        if f[:required] && !fields[cid].present?
          errors << "#{label}: #{settings[:railscf_message][:invalid_required]}"
          validate = false
        end
        if f[:field_type].to_s == "email"
          if !fields[cid].match(/@/)
            errors << "#{label}: #{settings[:railscf_message][:invalid_email]}"
            validate = false
          end
        end
      when 'captcha'
        unless cama_captcha_verified?
          errors << "#{label}: #{settings[:railscf_message][:captcha_not_match]}"
          validate = false
        end
    end
  end
  validate
end