Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to customize email submission template from theme #125

Merged
merged 1 commit into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 21 additions & 43 deletions app/apps/plugins/contact_form/contact_form_html_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_forms(id)
current_site.contact_forms.where({parent_id: id})
end

def form_value_rescue(form)
def form_value_rescue(form)
JSON.parse(form.settings).to_sym rescue form.value
end

Expand All @@ -34,7 +34,7 @@ def form_element_bootstrap_object(form, object, values)
object.each do |ob|
temp = "<div class='form-group'>
<label>[label ci]</span></label>
#{ '<p>[descr ci]</p>' if ob[:field_options][:description].present?}
#{'<p>[descr ci]</p>' if ob[:field_options][:description].present?}
<div>[ci]</div>
</div>"

Expand All @@ -59,18 +59,16 @@ def form_element_bootstrap_object(form, object, values)
temp2= form_select_multiple_bootstrap(ob, ob[:label], "checkbox",values)
when 'text', 'website', 'email'
class_type = ""

class_type = "railscf-field-#{ob[:field_type]}" if ob[:field_type]=="website"
class_type = "railscf-field-#{ob[:field_type]}" if ob[:field_type]=="email"

temp2= "<input #{ob[:custom_attrs].to_attr_format} type=\"#{ob[:field_type]}\" value=\"#{values[cid]}\" name=\"#{f_name}\" class=\"#{ob[:custom_class]} #{class_type}\">"
temp2 = "<input #{ob[:custom_attrs].to_attr_format} type=\"#{ob[:field_type]}\" value=\"#{values[cid]}\" name=\"#{f_name}\" class=\"#{ob[:custom_class]} #{class_type}\">"
when 'captcha'
temp2= captcha_tag(5, {}, {class: "#{ob[:custom_class]} field-captcha required"}.merge(ob[:custom_attrs]))
temp2 = captcha_tag(5, {}, {class: "#{ob[:custom_class]} field-captcha required"}.merge(ob[:custom_attrs]))
when 'file'
class_type = "railscf-field-#{ob[:field_type]}" if ob[:field_type]=="website"
temp2= "<input multiple=\"multiple\" type=\"file\" value=\"\" name=\"#{f_name}\" #{ob[:custom_attrs].to_attr_format} class=\"#{class_type} #{ob[:custom_class]}\">"
temp2 = "<input multiple=\"multiple\" type=\"file\" value=\"\" name=\"#{f_name}\" #{ob[:custom_attrs].to_attr_format} class=\"#{class_type} #{ob[:custom_class]}\">"
when 'dropdown'
temp2= form_select_multiple_bootstrap(ob, ob[:label], "select",values)
temp2 = form_select_multiple_bootstrap(ob, ob[:label], "select",values)
else
end
r[:template] = r[:template].sub('[label ci]', for_name).sub('[ci]', temp2)
Expand All @@ -81,7 +79,6 @@ def form_element_bootstrap_object(form, object, values)
end

def form_select_multiple_bootstrap(ob, title, type, values)

options = ob[:field_options][:options]
include_other_option = ob[:field_options][:include_other_option]
other_input = ""
Expand All @@ -91,7 +88,7 @@ def form_select_multiple_bootstrap(ob, title, type, values)
cid = ob[:cid].to_sym
html = ""

if type=="radio" || type=="checkbox"
if type == "radio" || type == "checkbox"

other_input = (include_other_option)? "<div class=\"#{type} #{ob[:custom_class]}\"> <label for=\"#{ob[:cid]}\"><input id=\"#{ob[:cid]}-other\" type=\"#{type}\" name=\"#{title.downcase}[]\" class=\"\">Other <input type=\"text\" /></label></div>" : " "

Expand All @@ -101,62 +98,43 @@ def form_select_multiple_bootstrap(ob, title, type, values)

options.each do |op|
if type == "radio" || type == "checkbox"
html += "<div class=\"#{type} #{ob[:custom_class]}\">
html += "<div class=\"#{type} #{ob[:custom_class]}\">
<label for=\"#{ob[:cid]}\">
<input #{ob[:custom_attrs].to_attr_format} type=\"#{type}\" name=\"#{f_name}[]\" class=\"\" value=\"#{op[:label].downcase}\">
#{op[:label]}
</label>
</div>"
else
html += "<option value=\"#{op[:label].downcase.gsub(" ", "_")}\" #{"selected" if "#{op[:label].downcase.gsub(" ", "_")}" == values[cid]} >#{op[:label]}</option>"
html += "<option value=\"#{op[:label].downcase.gsub(" ", "_")}\" #{"selected" if "#{op[:label].downcase.gsub(" ", "_")}" == values[cid]} >#{op[:label]}</option>"
end
end

if type=="radio" || type=="checkbox"
if type == "radio" || type == "checkbox"
html += other_input
else
html += " </select>"
end

end

def form_post_email_content(op_body, op_fields, fields, file_attachments)
@values = {}
def convert_form_values(op_fields, fields)
values = {}

op_fields.each do |field|
cid = field[:cid].to_sym
label = field[:label]

@values[label] = []
values[label] = []

if field[:field_type] == "file"
@values[label] << fields[cid].original_filename if fields[cid].present?
elsif field[:field_type] == "captcha"
@values[label] << session[:captcha]
elsif field[:field_type] == "radio" || field[:field_type] == "checkboxes"
@values[label] << fields[cid].join(",") if fields[cid].present?
if field[:field_type] == 'file'
values[label] << fields[cid].original_filename if fields[cid].present?
elsif field[:field_type] == 'captcha'
values[label] << session[:captcha]
elsif field[:field_type] == 'radio' || field[:field_type] == 'checkboxes'
values[label] << fields[cid].join(',') if fields[cid].present?
else
@values[label] << fields[cid] if fields[cid].present?
end

end

html = "<div class=\"data_body\"></div>"
html += "<table style=\"width:100%; float:left\"><tbody><tr><td style=\"width:50%; float:left\">"

@values.each do |ind, val|
if val.to_s != "[]"
html += "<div class=\"data_field\"><strong>"+ind + ":</strong>" + "#{val[0].to_s}" +"</div>"
values[label] << fields[cid] if fields[cid].present?
end
end

html += "</td><td style=\"width:48%; float:left; vertical-align: top;\">"

file_attachments.each do |attachment|
html += "<div class=\"data_link\"> <strong>Attachment:</strong> <a href=\"'#{attachment}'\">'#{File.basename(attachment)}'</a></div>"
end

html += "</td></tr></tbody></table>"
return values
end

end
83 changes: 47 additions & 36 deletions app/apps/plugins/contact_form/front_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
See the GNU Affero General Public License (GPLv3) for more details.
=end
class Plugins::ContactForm::FrontController < Apps::PluginsFrontController
before_filter :append_view_paths

def index
# here your actions for frontend module
Expand All @@ -19,10 +20,9 @@ def save_form
settings = JSON.parse(@form.settings).to_sym
fields = params[:fields]
attachments = []
file_attachments = []

errors = []
succes = []
success = []

validate = true

Expand All @@ -31,75 +31,86 @@ def save_form
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(/\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/)
errors << "#{label}: #{settings[:railscf_message][:invalid_email]}"
validate = false
end
end
when 'radio'

when 'checkboxes'

when 'dropdown'

when 'captcha'
if !captcha_verified?
errors << "#{label}: #{settings[:railscf_message][:captcha_not_match]}"
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(/\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/)
errors << "#{label}: #{settings[:railscf_message][:invalid_email]}"
validate = false
end
end
when 'captcha'
unless captcha_verified?
errors << "#{label}: #{settings[:railscf_message][:captcha_not_match]}"
validate = false
end
end
end

if validate
values[:fields].each do |f|
cid = f[:cid].to_sym
if f[:field_type] == "file"
if f[:field_type] == 'file'
res = upload_file(fields[cid], {maximum: 5.megabytes, folder: current_site.upload_directory("uploads")})
if res[:error].present?
errors << res[:error]
else
attachments << res["file"]
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)
@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
template_path = "html_mailer"

dir = Dir.glob("app/apps/themes/#{current_theme.the_slug.to_s}/views/html_mailer/*")
content = form_post_email_content(settings[:railscf_mail][:body], values[:fields], fields, attachments)
sendmail(settings[:railscf_mail][:to], settings[:railscf_mail][:subject], content, settings[:railscf_mail][:to], attachments) # send mail
succes << settings[:railscf_message][:mail_sent_ok]
content = render_to_string partial: 'contact_form/submission',
layout: false,
locals: {
file_attachments: attachments,
fields: convert_form_values(
values[:fields],
fields
)
}
# send mail
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

if succes.present?
flash[:notice] = succes.join("<br>")
if success.present?
flash[:notice] = success.join('<br>')
else
flash[:error] = errors.join("<br>")
flash[:error] = errors.join('<br>')
flash[:values] = fields
end

redirect_to :back
end

private

def append_view_paths
append_view_path File.join($camaleon_engine_dir, 'app', 'apps', 'plugins', self_plugin_key, 'views')
end

def fix_meta_value(value)
if (value.is_a?(Array) || value.is_a?(Hash))
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
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="data_body">
<table style="width: 100%; float: left;">
<tbody>
<tr>
<td style="width: 50%; float: left">
<% fields.each do |key, value| %>
<div class="data_field">
<strong><%= key %>:</strong>
<%= value[0] %>
</div>
<% end %>
</td>
<td style="width: 48%; float: left; vertical-align: top">
<% file_attachments.each do |attachment| %>
<div class="data_link">
<strong>Attachment: </strong>
<a href="<%= attachment %>">
<%= File.basename(attachment) %>
</a>
</div>
<% end %>
</td>
</tr>
</tbody>
</table>
</div>