| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ImportListService |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
// phpcs:ignoreFile WordPress.Security.EscapeOutput.OutputNotEscaped |
| 11 |
class ImportListService |
| 12 |
{ |
| 13 |
protected AddProductToImportListProcess $AddProductToImportListProcess; |
| 14 |
|
| 15 |
public function __construct( |
| 16 |
AddProductToImportListProcess $AddProductToImportListProcess, |
| 17 |
) { |
| 18 |
$this->AddProductToImportListProcess = $AddProductToImportListProcess; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @throws ServiceException |
| 23 |
*/ |
| 24 |
public function addProductsFromCsvFile(string $fileName): ProductsFromFileResult |
| 25 |
{ |
| 26 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen |
| 27 |
$f = fopen($fileName, 'r'); |
| 28 |
|
| 29 |
if ($f === false) { |
| 30 |
throw new ServiceException( |
| 31 |
_x( "Can not read the file.", 'error text', 'ali2woo') |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
$externalProductIds = []; |
| 36 |
|
| 37 |
while ($row = fgetcsv($f, 1024, ';', '"', "\\")) { |
| 38 |
$id_or_url = urldecode(trim($row[0])); |
| 39 |
|
| 40 |
if (preg_match('/.*\/([0-9]+)\.html/', $id_or_url, $matches)) { //get id from url |
| 41 |
$id = (int)$matches[1]; |
| 42 |
} else { //is not url |
| 43 |
//trim all not number symbols |
| 44 |
$id = (int)preg_replace('/[\D]+/', '', $id_or_url); |
| 45 |
} |
| 46 |
|
| 47 |
if (!$id) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
$externalProductIds[] = $id; |
| 52 |
} |
| 53 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose |
| 54 |
fclose($f); |
| 55 |
|
| 56 |
foreach ($externalProductIds as $externalProductId) { |
| 57 |
$this->AddProductToImportListProcess->pushToQueue( |
| 58 |
$externalProductId, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
$this->AddProductToImportListProcess->dispatch(); |
| 63 |
|
| 64 |
return new ProductsFromFileResult(count($externalProductIds), []); |
| 65 |
} |
| 66 |
|
| 67 |
public function getCountryFromList(array $importedProduct): array |
| 68 |
{ |
| 69 |
$countryFromList = ['CN']; |
| 70 |
if (!empty($product[ImportedProductService::FIELD_COUNTRY_FROM_LIST])) { |
| 71 |
$countryFromList = implode(";", $product[ImportedProductService::FIELD_COUNTRY_FROM_LIST]); |
| 72 |
} |
| 73 |
|
| 74 |
$result = []; |
| 75 |
|
| 76 |
foreach ($countryFromList as $countryCode) { |
| 77 |
$result[$countryCode] = Country::get_country($countryCode); |
| 78 |
} |
| 79 |
|
| 80 |
return $result; |
| 81 |
} |
| 82 |
} |
| 83 |
|