| 1 |
<?php |
| 2 |
|
| 3 |
use MbeExceptions\ValidationException; |
| 4 |
|
| 5 |
class Mbe_Shipping_Helper_Csv |
| 6 |
{ |
| 7 |
public function getCsvHeaderKnowDefinitions() |
| 8 |
{ |
| 9 |
return $this->_csvHeaderKnowDefinitions; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* @throws ValidationException |
| 14 |
*/ |
| 15 |
public function readFile($importFilePath, $headerDefinition = []) |
| 16 |
{ |
| 17 |
$file = fopen($importFilePath, "r"); |
| 18 |
$i = 0; |
| 19 |
$header = null; |
| 20 |
$result = []; |
| 21 |
while (!feof($file)) { |
| 22 |
$currentRow = fgetcsv($file, 0, ",", '"'); |
| 23 |
|
| 24 |
if (!empty($currentRow) && ([null]!==$currentRow)) { |
| 25 |
if ($i == 0) { |
| 26 |
//File heading |
| 27 |
$header = $currentRow; |
| 28 |
} else { |
| 29 |
try { |
| 30 |
$current = $this->readCsvRowToArray( $headerDefinition, $header, $currentRow ); |
| 31 |
$result[] = $current; |
| 32 |
} catch (ValidationException $e) { |
| 33 |
throw new ValidationException(esc_html($e->getMessage() . ' file:'. basename($importFilePath) )); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
$i++; |
| 38 |
} |
| 39 |
return $result; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @throws ValidationException |
| 44 |
*/ |
| 45 |
function readCsvRowToArray($headerDefinition, $header, $row) |
| 46 |
{ |
| 47 |
$result = []; |
| 48 |
if (is_array($row) && (count($row) === count($headerDefinition))) { |
| 49 |
foreach ($row as $index => $currentRowValue) { |
| 50 |
$headerName = $this->cleanString($header[$index]); |
| 51 |
$currentKey = $headerName; |
| 52 |
if (isset($headerDefinition[$headerName])) { |
| 53 |
$currentKey = $headerDefinition[$headerName]; |
| 54 |
} |
| 55 |
$currentRowValue = trim($currentRowValue); |
| 56 |
/* |
| 57 |
$currentRowValue = str_replace(" ", " ", $currentRowValue); |
| 58 |
$currentRowValue = str_replace(" ", " ", $currentRowValue); |
| 59 |
$currentRowValue = str_replace(" ", " ", $currentRowValue); |
| 60 |
$currentRowValue = str_replace("„",createShipment "\"", $currentRowValue); |
| 61 |
$currentRowValue = str_replace("“", "\"", $currentRowValue); |
| 62 |
$currentRowValue = str_replace("”", "\"", $currentRowValue); |
| 63 |
*/ |
| 64 |
$result[$currentKey] = $currentRowValue; |
| 65 |
} |
| 66 |
} else { |
| 67 |
throw new ValidationException(esc_html__('Expected ('. count($headerDefinition) .') columns, found ('. count($row) .').', 'mail-boxes-etc')); |
| 68 |
} |
| 69 |
return $result; |
| 70 |
} |
| 71 |
|
| 72 |
function cleanString($str) |
| 73 |
{ |
| 74 |
$result = $str; |
| 75 |
$result = trim($result); |
| 76 |
$result = strtolower($result); |
| 77 |
$result = str_replace(" ", "_", $result); |
| 78 |
$result = str_replace(chr(160), "_", $result); |
| 79 |
$result = str_replace(chr(194), "_", $result); |
| 80 |
$result = str_replace(chr(195), "_", $result); |
| 81 |
$result = str_replace(",", "_", $result); |
| 82 |
$result = str_replace("/", "", $result); |
| 83 |
$result = str_replace("(", "", $result); |
| 84 |
$result = str_replace(")", "", $result); |
| 85 |
$result = str_replace("__", "_", $result); |
| 86 |
return $result; |
| 87 |
} |
| 88 |
} |