File manager - Edit - /home/autoph/public_html/data03252025consolidation/api/upload/upload_spreadsheet_cat_nob.php
Back
<?php ini_set('max_execution_time', '0'); set_time_limit(0); ini_set('memory_limit','-1'); include_once("../../cfg/db.php"); include_once("../../app/language/word_mapping.php"); include_once('../../vendor/autoload.php'); include_once("../../api/controllers/utility.php"); include_once("../../api/controllers/customer.php"); include_once("../../api/controllers/import_catnob.php"); $utility = new Utility(); $customer_class = new Customer(); $import = new Import(); if (!file_exists('files')) { mkdir('files', 0777, true); } if (!file_exists('logs')) { mkdir('logs', 0777, true); } if(!isset($_SESSION['user']['id'])){ echo "Session Expired."; exit; } if(isset($_REQUEST["request"])){ $request = $_REQUEST["request"]; if($request === 'delete'){ $file_name = $_REQUEST["name"]; $location = "files/".$file_name; if(file_exists($location)){ unlink($location); } exit; } } if(!$utility -> isNotEmpty($_FILES['file']['name'])){ $return_arr["status"]=0; $return_arr["message"]=" Error has occured."; exit; } // Getting file name $filename = $_FILES['file']['name']; //Valid extension $valid_ext = array('xls', 'csv', 'xlsx'); // file extension $file_extension = pathinfo($filename, PATHINFO_EXTENSION); $file_extension = strtolower($file_extension); $location = "files/".$filename = $_FILES['file']['name']; // Check extension if(!in_array($file_extension,$valid_ext)){ $return_arr["status"]=0; $return_arr["message"]=" Invalid file type."; exit; } $maxsize = 50097152; $minsize = 1; if($_FILES['file']['size'] >= $maxsize || $_FILES['file']['size'] == 0 ){ $return_arr["status"]=0; $return_arr["message"]=" File too large. Image must less than ".round(($maxsize/1000000), 0)." megabytes."; echo json_encode($return_arr); exit; } // globals // countings $GLOBALS['total_data_count'] = 0; $GLOBALS['no_nob_id_count'] = 0; $GLOBALS['no_nob_name_count'] = 0; $GLOBALS['no_category_count'] = 0; $GLOBALS['done_count'] = 0; $GLOBALS['updated_count'] = 0; $GLOBALS['not_updated_count'] = 0; // array $GLOBALS['row_nobcat_headers'] = array(); $GLOBALS['not_inserted_list_v2'] = array(); // error messages $GLOBALS['no_nob_id'] = "No NoB ID found"; $GLOBALS['no_nob_name'] = "No NoB Name found"; $GLOBALS['no_category'] = "No Category found"; $GLOBALS['no_nob_existing'] = "No NoB Existing"; $GLOBALS['unknown_category'] = "Unknown category"; $GLOBALS['done'] = "Already set"; // array to be inserted $GLOBALS['user_id']= $_SESSION['user']['id']; // $GLOBALS['customer_affiliation'] = array(); // $GLOBALS['vehicle_data'] = array(); // $GLOBALS['vehicle_data_plate'] = array(); // $GLOBALS['vehicle_data_cs'] = array(); // $GLOBALS['sales_data'] = array(); // $GLOBALS['vehicle_offered'] = array(); // reporting $GLOBALS['data_count_list'] = ""; $GLOBALS['data_count_list_v2'] = array(); //process excel file move_uploaded_file($_FILES['file']['tmp_name'], $location); $file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($location); $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type); try { $spreadsheet = $reader->load($location); //remove excel after store to phpspreadsheet unlink($location); $data = $spreadsheet->getActiveSheet()->toArray(); } catch (\Exception $e) { $return_arr["status"]=2; $return_arr["header_column_not_found_message"] = array(array('message'=>'<strong>Invalid spreadsheet file. Contact your system administrator.<br><span class="text-danger">Error:</span></strong> '.$e)); echo json_encode($return_arr); exit; } $data = array_map(array($utility, 'upperCaseNestedArray'), $data); $data = array_map(array($db, 'escape'), $data); $highestRow = $spreadsheet->getActiveSheet()->getHighestRow(); $highestColumn = $spreadsheet->getActiveSheet()->getHighestColumn(); $ColumnNumber = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); //get available fieldnames from database $array_nob_id = $import->getArrayFields(1, $db); $array_nob_name = $import->getArrayFields(2, $db); $array_given_category = $import->getArrayFields(3, $db); $header_column_found = false; foreach($data AS $row){ if(!$header_column_found){ $issue_summary_index = $import->getIssueSummaryField('ISSUE SUMMARY', $ColumnNumber, $row); //get matched field from database and excel globals $GLOBALS['nob_id']= $import->getFieldFromExcel($row, $ColumnNumber, $array_nob_id); $GLOBALS['nob_name']= $import->getFieldFromExcel($row, $ColumnNumber, $array_nob_name); $GLOBALS['category']= $import->getFieldFromExcel($row, $ColumnNumber, $array_given_category); //check required fields if( $utility->isNotEmpty($GLOBALS['nob_id']) && $utility->isNotEmpty($GLOBALS['nob_name']) && $utility->isNotEmpty($GLOBALS['category']) ){ $header_column_found = true; $GLOBALS['row_nobcat_headers'] = $row; continue; } } else{ $GLOBALS['total_data_count']++; // start counting of data if header is found $import->start_import($row, $customer_class, $utility,$db); } } if(!$header_column_found){ $return_arr["status"] = 2; $return_arr["header_column_not_found_message"] = array(array('message'=>'<strong>Invalid spreadsheet file. Required field not found, download the template or contact your system administrator.</strong>')); echo json_encode($return_arr); exit; } else { if(intval($GLOBALS['no_nob_id_count']) > 0){ $count_record = max(0, $GLOBALS['no_nob_id_count']); $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>".$GLOBALS['no_nob_id']."</td><td class='text-center'><h5><span class='badge bg-danger' >".$count_record."</span></h5></td></tr>"; $GLOBALS['data_count_list_v2'][] = array($GLOBALS['no_nob_id'], $count_record); } if(intval($GLOBALS['no_nob_name_count']) > 0){ $count_record = max(0, $GLOBALS['no_nob_name_count']); $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>".$GLOBALS['no_nob_name']."</td><td class='text-center'><h5><span class='badge bg-danger' >".$count_record."</span></h5></td></tr>"; $GLOBALS['data_count_list_v2'][] = array($GLOBALS['no_nob_name'], $count_record); } if(intval($GLOBALS['no_category_count']) > 0){ $count_record = max(0, $GLOBALS['no_nob_id_count']); $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>".$GLOBALS['no_category']."</td><td class='text-center'><h5><span class='badge bg-danger' >".$count_record."</span></h5></td></tr>"; $GLOBALS['data_count_list_v2'][] = array($GLOBALS['no_category'], $count_record); } $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>Total Not Updated</td> <td class='text-center'><h5><span class='badge bg-danger' >".intval($GLOBALS['not_updated_count'])."</span></h5></td></tr>"; $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>Total Updated</td> <td class='text-center'><h5><span class='badge bg-success' >".max(0, $GLOBALS['updated_count'])."</span></h5></td></tr>"; $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>Total Data</td> <td class='text-center'><h5><span class='badge bg-primary' >".$GLOBALS['total_data_count']."</span></h5></td></tr>"; $GLOBALS['data_count_list_v2'][] = array("Total Not Updated", (string) intval($GLOBALS['not_updated_count'])); $GLOBALS['data_count_list_v2'][] = array("Total Updated", (string) max(0, $GLOBALS['updated_count'])); $GLOBALS['data_count_list_v2'][] = array("Total Data", (string) $GLOBALS['total_data_count']); if(intval($GLOBALS['not_updated_count']) > 0){ $GLOBALS['data_count_list'] = $GLOBALS['data_count_list']."<tr><td>Data with issues</td> <td class='text-center'><h5><a href='api/upload/logs/".$filename."'><span class='badge bg-primary' style='cursor:pointer;'>Download</span></a></h5></td></tr>"; } returnData($filename, $highestColumn, $issue_summary_index); } function returnData($file_name, $highestColumn, $issue_summary_index){ $return_arr["status"] = 1; $return_arr["message"] = " Data Imported Successfully."; $return_arr["invalid_data"] = $GLOBALS['not_updated_count']; $return_arr["data_count_list"] = $GLOBALS['data_count_list']; $return_json = json_encode($return_arr); $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $data_count_list_v2_count = count($GLOBALS['data_count_list_v2']); $spreadsheet ->getActiveSheet() // ->getStyle('B'.($data_count_list_v2_count+1).':'.(string) $highestColumn.'1') ->getStyle('B'.($data_count_list_v2_count+1).':'.(string) $highestColumn.($data_count_list_v2_count+1)) ->getFill() ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) ->getStartColor() ->setARGB('FFFF00'); $spreadsheet ->getActiveSheet() ->getStyle('A'.($data_count_list_v2_count+1).'') ->getFill() ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) ->getStartColor() ->setARGB('FF0000'); $spreadsheet ->getActiveSheet() ->getStyle('A'.($data_count_list_v2_count+1)) ->getFont() ->getColor() ->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE); $sheet = $spreadsheet->getActiveSheet(); for ($i = 'A'; $i != $highestColumn ; $i++) { $sheet ->getColumnDimension($i) ->setAutoSize(true); } foreach ($GLOBALS['not_inserted_list_v2'] as $key => $subArr){ unset($subArr[intval($issue_summary_index)+1]); $GLOBALS['not_inserted_list_v2'][$key] = $subArr; } unset($GLOBALS['row_nobcat_headers'][intval($issue_summary_index)]); usort($GLOBALS['not_inserted_list_v2'], function($a, $b){ return $a[0] <=> $b[0]; }); $GLOBALS['not_inserted_list_v2'] = array_merge(array(array_map("strtoupper", array_merge(array('ISSUE SUMMARY'),$GLOBALS['row_nobcat_headers']))) , $GLOBALS['not_inserted_list_v2']); if($data_count_list_v2_count> 0){ $GLOBALS['not_inserted_list_v2'] = array_merge($GLOBALS['data_count_list_v2'],$GLOBALS['not_inserted_list_v2']); } $sheet->fromArray($GLOBALS['not_inserted_list_v2'], NULL, 'A1'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); $writer->save('logs/'.$file_name); echo $return_json; } ?>
| ver. 1.4 |
.
| PHP 7.3.33 | Generation time: 0 |
proxy
|
phpinfo
|
Settings