-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet-upload-nested-forms.php
95 lines (77 loc) · 3.12 KB
/
spreadsheet-upload-nested-forms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Plugin Name: GP Nested Forms CSV Upload
* Version: 1.0
* Author: Nathanael Urmoneit
*/
require 'class-gpnf-session-override.php';
// all your forms that use CSV:
// [Parent Form ID, Nested Form Field ID, Nested Form ID, CSV Upload Form ID (you have to create one)]
$csv_forms = [
[2, 77, 11, 16],
[12, 101, 13, 17],
];
add_action("wp_enqueue_scripts", function() use($csv_forms){
wp_register_script("csv-upload", plugin_dir_url(__FILE__)."spreadsheet-upload.js");
foreach($csv_forms as &$arr){
$arr[4] = gravity_form($arr[3], false, false, true, null, false, 0, false);
}
wp_localize_script("csv-upload", "CSVUpload", ["forms"=>$csv_forms]);
wp_enqueue_script("csv-upload");
});
//Change button: CSS class and label
function csv_button($args){
$args["add_button"] .= ' <button type="button" class="gpnf-csv-upload"
data-bind="attr: { disabled: isMaxed }">
CSV Upload
</button>';
return $args;
}
//A separate function for each nested form field
function csv_upload_101($form){
$session = new GPNF_CSV_Session(12); //Change to parent form ID
$path = $GLOBALS["csv_path"] . sanitize_file_name($_FILES["input_1"]["name"]); //Change file upload field ID
$lines = file($path);
if(!empty(rgpost("input_3_1"))) array_shift($lines); //Change checkbox field ID which allows to ignore the first line
foreach($lines as $csv_entry){
$csv_entry = trim($csv_entry);
if(empty($csv_entry)) continue;
if(preg_match('//u', $csv_entry) === false){ //If there are non-UTF-8 chars, encode them
$csv_entry = utf8_encode($csv_entry);
}
$csv_arr = explode(";", $csv_entry); //Change selector
$e = [
"form_id"=>13, //Change to nested form ID
"created_by"=>get_current_user_id(),
"1"=>$csv_entry[0] //Map CSV fields to form fields, look in the database and form creator
];
//Invoke action
$post_cache = $_POST;
do_action('gform_pre_submission_13',GFAPI::get_form(13)); //Change to nested form ID
$new_keys = array_diff_key($_POST, $post_cache);
foreach(array_keys($_POST) as $inp){
if(preg_match("/^input_([0-9]+)(_[0-9]+)?/", $inp, $m)){
$e[$m[1] . (@$m[2] ? '.'.$m[2] : '')] = $_POST[$m[0]];
}
}
$entry_id = GFAPI::add_entry($e);
$entry = new GPNF_Entry(GFAPI::get_entry($entry_id));
$entry->set_parent_form(12); //Change to parent form ID
$entry->set_nested_form_field(101); //Change to nested form field ID
$entry->set_expiration();
$session->add_child_entry($entry_id);
}
$session->set_cookie();
unlink($path);
header("Location: ".$_SERVER["REQUEST_URI"]);
}
foreach($csv_forms as $arr){
add_filter("gpnf_template_args_".$arr[0], "csv_button"); //Echo CSV button
add_filter('gform_submit_button_'.$arr[3], '__return_false'); //Disable Submit button (use tingle's one instead)
add_action("gform_post_process_".$arr[3], "csv_upload_".$arr[1], 1); //Func name: csv_upload_(nested form field id)
}
//retrieve file upload path
add_filter('gform_upload_path', function($path_info, $form_id) use(&$csv_path){
$csv_path = $path_info["path"];
return $path_info;
}, 1, 2);