Enable form to include file attachments (#586)

Enable form controller and widget to store file attachments.
This commit is contained in:
olivergrahl 2017-01-09 15:16:29 +01:00 committed by Martin Edenhofer
parent 4601993a15
commit 7221cae487
2 changed files with 42 additions and 18 deletions

View file

@ -110,6 +110,21 @@ class FormController < ApplicationController
internal: false, internal: false,
) )
if params[:file]
params[:file].each { |file|
Store.add(
object: 'Ticket::Article',
o_id: article.id,
data: File.read(file.tempfile),
filename: file.original_filename,
preferences: {
'content-alternative' => true,
'Mime-Type' => file.content_type
}
)
}
end
UserInfo.current_user_id = 1 UserInfo.current_user_id = 1
result = { result = {

View file

@ -40,6 +40,13 @@ $(function() {
placeholder: 'Your Message...', placeholder: 'Your Message...',
rows: 7, rows: 7,
}, },
{
display: 'Attachments',
name: 'file[]',
tag: 'input',
type: 'file',
repeat: 3,
},
] ]
}); });
}); });
@ -56,8 +63,8 @@ $(function() {
showTitle: false, showTitle: false,
messageTitle: 'Zammad Form', messageTitle: 'Zammad Form',
messageSubmit: 'Submit', messageSubmit: 'Submit',
messageThankYou: 'Thank you for your inquiry! We\'ll contact you soon as possible.', messageThankYou: 'Thank you for your inquiry! We\'ll contact you as soon as possible.',
messageNoConfig: 'Unable to load form config from server. Maybe featrue is disabled.', messageNoConfig: 'Unable to load form config from server. Maybe feature is disabled.',
attributes: [ attributes: [
{ {
display: 'Name', display: 'Name',
@ -222,6 +229,9 @@ $(function() {
method: 'post', method: 'post',
url: _this.endpoint_submit, url: _this.endpoint_submit,
data: _this.getParams(), data: _this.getParams(),
cache: false,
contentType: false,
processData: false,
}).done(function(data) { }).done(function(data) {
// removed errors // removed errors
@ -247,22 +257,19 @@ $(function() {
// get params // get params
Plugin.prototype.getParams = function() { Plugin.prototype.getParams = function() {
var _this = this, var _this = this
params = {}
$.each( _this.$form.serializeArray(), function(index, item) { var formData = new FormData(_this.$form[0])
params[item.name] = item.value
})
if (!params.title) { if (!formData.has('title')) {
params.title = this.options.messageTitle formData.append('title', this.options.messageTitle)
} }
if (this.options.test) { if (this.options.test) {
params.test = true formData.append('test', true)
} }
_this.log('debug', 'params', params) _this.log('debug', 'formData', formData)
return params return formData
} }
Plugin.prototype.closeModal = function() { Plugin.prototype.closeModal = function() {
@ -296,12 +303,14 @@ $(function() {
} }
$.each(this.options.attributes, function(index, value) { $.each(this.options.attributes, function(index, value) {
var item = $('<div class="form-group"><label>' + _this.T(value.display) + '</label></div>') var item = $('<div class="form-group"><label>' + _this.T(value.display) + '</label></div>')
for (var i=0; i < (value.repeat ? value.repeat : 1); i++) {
if (value.tag == 'input') { if (value.tag == 'input') {
item.append('<input class="form-control" name="' + value.name + '" type="' + value.type + '" placeholder="' + _this.T(value.placeholder) + '">') item.append('<input class="form-control" name="' + value.name + '" type="' + value.type + '" placeholder="' + _this.T(value.placeholder) + '">')
} }
else if (value.tag == 'textarea') { else if (value.tag == 'textarea') {
item.append('<textarea class="form-control" name="' + value.name + '" placeholder="' + _this.T(value.placeholder) + '" rows="' + value.rows + '"></textarea>') item.append('<textarea class="form-control" name="' + value.name + '" placeholder="' + _this.T(value.placeholder) + '" rows="' + value.rows + '"></textarea>')
} }
}
$form.append(item) $form.append(item)
}) })
$form.append('<button type="submit" class="btn">' + this.options.messageSubmit + '</button') $form.append('<button type="submit" class="btn">' + this.options.messageSubmit + '</button')