-
Notifications
You must be signed in to change notification settings - Fork 92
gpfup-count-files-groups.js: Added snippet to count files in groups.
#1116
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
Changes from 2 commits
a8200b0
68c80f2
5e02037
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Gravity Perks // File Upload Pro // Count Files in Groups | ||
| * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/ | ||
| * | ||
| * Instruction Video: https://www.loom.com/share/2328e327125844bebdcabf7c9baaabca | ||
| */ | ||
| const formId = GFFORMID; | ||
|
|
||
| // Map count field IDs to arrays of file upload field IDs | ||
| const countMapping = { | ||
| 5: [1, 3, 4], // Number Field ID 5 counts files from File Upload Field IDs 1, 3, 4 | ||
| 10: [7, 8, 9] // Number Field ID 10 counts files from File Upload Field IDs 7, 8, 9 | ||
| // Add more mappings as needed: countFieldID: [uploadFieldID1, uploadFieldID2, ...] | ||
| }; | ||
|
|
||
| // Find all GPFUP keys for the form | ||
| var gpfupInstances = Object.keys(window).filter(function (key) { | ||
| return key.startsWith('GPFUP_' + formId + '_'); | ||
| }); | ||
|
|
||
| // Build reverse lookup: uploadFieldID => associated countFieldIDs | ||
| const uploadToCountMap = {}; | ||
| Object.entries(countMapping).forEach(([countFieldID, uploadFieldIDs]) => { | ||
| uploadFieldIDs.forEach((uploadFieldID) => { | ||
| if (!uploadToCountMap[uploadFieldID]) { | ||
| uploadToCountMap[uploadFieldID] = []; | ||
| } | ||
| uploadToCountMap[uploadFieldID].push(parseInt(countFieldID)); | ||
saifsultanc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }); | ||
|
|
||
| // Function to update all relevant count fields | ||
| function updateAllCountFields() { | ||
| Object.entries(countMapping).forEach(([countFieldID, uploadFieldIDs]) => { | ||
| const total = uploadFieldIDs.reduce((sum, uploadFieldID) => { | ||
| const key = 'GPFUP_' + formId + '_' + uploadFieldID; | ||
| const store = window[key]?.$store; | ||
| return sum + (store ? (store.state.files.length || 0) : 0); | ||
| }, 0); | ||
|
|
||
| const selector = '#input_' + formId + '_' + countFieldID; | ||
| const $field = jQuery(selector); | ||
saifsultanc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($field.length) { | ||
| $field.val(total).change(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| if (gpfupInstances.length) { | ||
| // Subscribe to relevant GPFUP fields | ||
| gpfupInstances.forEach((key) => { | ||
| const parts = key.split('_'); | ||
| const fieldID = parseInt(parts[2]); // GPFUP_formId_fieldId | ||
| const store = window[key].$store; | ||
|
|
||
| if (uploadToCountMap[fieldID]) { | ||
| store.subscribe((mutation, state) => { | ||
| if (mutation.type === 'SET_FILES') { | ||
| updateAllCountFields(); | ||
|
||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Initial count on load | ||
| updateAllCountFields(); | ||
Uh oh!
There was an error while loading. Please reload this page.