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

add: add support for single upload field deletion, multifile upload f… #24

Closed
wants to merge 1 commit into from

Conversation

medfreeman
Copy link
Contributor

…ields, add javascript to unbind gravity forms setup function, add modified javascript file from gravity forms source, add corresponding tests

fixes #20

…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');
Copy link
Contributor Author

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;
Copy link
Contributor Author

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){
Copy link
Contributor Author

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).

Copy link
Contributor Author

@medfreeman medfreeman Jan 30, 2017

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();
Copy link
Contributor Author

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);' "
Copy link
Contributor Author

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&quot;:{&quot;form_id&quot;' => 'multipart_params&quot;:{&quot;random_id&quot;:' . $random_id . ',&quot;form_id&quot;',
Copy link
Contributor Author

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 . ',',
Copy link
Contributor Author

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.

@medfreeman medfreeman closed this Feb 4, 2017
@medfreeman medfreeman deleted the multifile-upload branch February 6, 2017 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conflict with Multiple File Upload field
1 participant