-
Notifications
You must be signed in to change notification settings - Fork 22
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
add: add support for single upload field deletion, multifile upload f… #24
Conversation
…ields, add javascript to unbind gravity forms setup function, add modified javascript file from gravity forms source, add corresponding tests fixes tyxla#20
var strings = typeof gform_gravityforms != 'undefined' ? gform_gravityforms.strings : {}; | ||
var imagesUrl = typeof gform_gravityforms != 'undefined' ? gform_gravityforms.vars.images_url : ""; | ||
|
||
$(document).unbind('gform_post_render'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we unbind the original gravity forms setup function hooked to the document's 'gform_post_render' function.
The fact that this script depends on 'gform_gravityforms' script (cf. https://github.com/tyxla/Gravity-Forms-Multiple-Form-Instances/pull/24/files#diff-7d573d66d37a99e5734d905449d49972R130), ensures that the unbind will be properly executed after the original binding has been done.
var settings = $(uploadElement).data('settings'); | ||
|
||
var uploader = new plupload.Uploader(settings); | ||
formID = uploader.settings.multipart_params.random_id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we assign the formID used througout this file to the form's random id, injected here (https://github.com/tyxla/Gravity-Forms-Multiple-Form-Instances/pull/24/files#diff-7d573d66d37a99e5734d905449d49972R100) into the multifile upload field multipart_param.
It could also be added to another attribute, to avoid sending the value to the server while adding files, but i let it this way to simplify the code.
var imagesUrl = typeof gform_gravityforms != 'undefined' ? gform_gravityforms.vars.images_url : ""; | ||
|
||
$(document).unbind('gform_post_render'); | ||
$(document).bind('gform_post_render', function(e, formID){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we bind our function, that will call copies of our original gravityforms functions, but they will use the random form id instead of the original (see next comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since everything is in a closure on the original file, it is not possible to call the original functions instead, not being referenced in any accessible javascript variable.
|
||
|
||
up.settings.multipart_params.gform_unique_id = uniqueID; | ||
up.start(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since gravity's php code processing file uploads has no relevant hooks to allow modyfing the submitting form id, and that an upload referencing the random form id will fail, the upload process is strictly the same as without this plugin.
The multipart_param 'form_id' parameter is not modified, so the server receives the original form id, and processes the upload properly.
html = "<img " | ||
+ "class='gform_delete' " | ||
+ "src='" + imagesUrl + "/delete.png' " | ||
+ "onclick='gformDeleteUploadedFile(" + formId + "," + fieldId + ", this);' " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and on the next line, the formID is, as everywhere else, the random form id assigned by this plugin.
This allows proper deletion of the uploaded file.
@@ -95,6 +97,14 @@ public function gform_get_form_filter( $form_string, $form ) { | |||
'GFCalc(' . $form['id'] . ',' => 'GFCalc(' . $random_id . ',', | |||
'gf_global["number_formats"][' . $form['id'] . ']' => 'gf_global["number_formats"][' . $random_id . ']', | |||
'gform_next_button_' . $form['id'] . '_' => 'gform_next_button_' . $random_id . '_', | |||
'multipart_params":{"form_id"' => 'multipart_params":{"random_id":' . $random_id . ',"form_id"', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we add the 'random_id' multipart_param, that is then simply accessible in the code, since plupload will have a reference to it in its 'settings.multipart_params.random_id' property.
The drawback is that it is also sent to the server while uploading files, and doesn't have any purpose in this context.
'gform_preview_' . $form['id'] . '_' => 'gform_preview_' . $random_id . '_', | ||
'gform_multifile_messages_' . $form['id'] . '_' => 'gform_multifile_messages_' . $random_id . '_', | ||
'gform_uploaded_files_' . $form['id'] => 'gform_uploaded_files_' . $random_id, | ||
'gformDeleteUploadedFile(' . $form['id'] . ',' => 'gformDeleteUploadedFile(' . $random_id . ',', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support for single upload field file deletion.
…ields, add javascript to unbind gravity forms setup function, add modified javascript file from gravity forms source, add corresponding tests
fixes #20