function csvToArray($csvFile){ $file_to_read = fopen($csvFile, 'r'); while (!feof($file_to_read) ) { $lines[] = fgetcsv($file_to_read, 1000, ','); } fclose($file_to_read); return $lines; } add_action( 'gform_after_submission_8', 'csv_import_gravity', 10, 2 ); function csv_import_gravity($entry, $form){ $csv = array(); $upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] ); $upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] ); $filename = str_replace( $upload_url, $upload_path, $entry[ '1' ] ); $csvFile = $filename; $csv = csvToArray($csvFile); for($i=1; $i<count($csv); $i++) { $count_col = @count($csv[$i]); if($count_col!=18 || empty($csv[$i][0])){ echo '<script>alert("Error in CSV format, please check and verify CSV file");</script><style>.erro-csv{display:block!important;}.success-csv{display:none;}</style>'; $error = true; break ; }else{ $post_data = array( 'post_title' => 'CSV', 'post_content' => 'Manual Entry', 'post_type' => 'POST', 'post_status' => 'publish', ); $post_id = wp_insert_post( $post_data ); $post_ids[] = $post_id; $grower_name = $csv[$i][0]; $lot_number = $csv[$i][1]; $inventory = $csv[$i][2]; $seed_class = $csv[$i][3]; $grown_in = $csv[$i][4]; $crop_year = $csv[$i][5]; $variety = $csv[$i][6]; $cwt_amount = $csv[$i][7]; $tested_by = $csv[$i][8]; $lab_number = $csv[$i][9]; $certifying_agency = $csv[$i][10]; $certification_number = $csv[$i][11]; $interagency_states = $csv[$i][12]; $tag_grade = $csv[$i][13]; $is_experimental = $csv[$i][14]; $eligible_pending_winter_test = $csv[$i][15]; $size_specification = $csv[$i][16]; $plant_variety_protection = $csv[$i][17]; if(!is_wp_error($post_id)){ $user = get_user_by('login',$grower_name); if($user){ update_field('grower_name', $user->ID, $post_id); } update_field('lot_number', $lot_number, $post_id); update_field('inventory', $inventory, $post_id); update_field('seed_class', $seed_class, $post_id); update_field('grown_in', $grown_in, $post_id); update_field('crop_year', $crop_year, $post_id); update_field('variety', $variety, $post_id); update_field('amount_of_cwt_units_in_lot', $cwt_amount, $post_id); update_field('tested_by_lab', $tested_by, $post_id); update_field('lab_number', $lab_number, $post_id); update_field('certifying_agency', $certifying_agency, $post_id); update_field('certification_number', $certification_number, $post_id); update_field('interagency_states', $interagency_states, $post_id); update_field('tag_grade', $tag_grade, $post_id); update_field('is_experimental', $is_experimental, $post_id); update_field('eligible_pending_winter_test', $eligible_pending_winter_test, $post_id); update_field('size_specification', $size_specification, $post_id); update_field('plant_variety_protection', $plant_variety_protection, $post_id); $title = 'CSV '.get_field('crop_year',$post_id).' - '.get_field('lot_number',$post_id); $content = array( 'ID' => $post_id, 'post_title' => $title ); wp_update_post($content); } } } if($error==true){ //echo '<script>console.log("Error ids'.implode(", ",$post_ids).'");</script>'; } }
Jagdish Sarma Edited question November 22, 2022