Using generators in the ICML generation process

This commit is contained in:
max-baranikov 2020-12-28 14:51:01 +03:00 committed by GitHub
parent 65fb25703b
commit f2a96e92d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 278 additions and 224 deletions

View File

@ -1,4 +1,5 @@
<?php
/**
* MIT License
*
@ -40,35 +41,76 @@ class RetailcrmCatalog
public $default_lang;
public $default_currency;
public $default_country;
public $protocol;
public $version;
public $link;
public $home_category;
public function __construct()
{
$this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$this->default_currency = (int)Configuration::get('PS_CURRENCY_DEFAULT');
$this->default_country = (int)Configuration::get('PS_COUNTRY_DEFAULT');
$this->protocol = (Configuration::get('PS_SSL_ENABLED')) ? "https://" : "http://";
$this->version = substr(_PS_VERSION_, 0, 3);
$this->link = new Link();
$this->home_category = Configuration::get('PS_HOME_CATEGORY');
}
public function getData()
{
$version = substr(_PS_VERSION_, 0, 3);
$versionSplit = explode('.', $version);
$id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
$currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
$shop_url = (Configuration::get('PS_SSL_ENABLED') ? _PS_BASE_URL_SSL_ : _PS_BASE_URL_);
$protocol = (Configuration::get('PS_SSL_ENABLED')) ? "https://" : "http://";
$homeCategory = Configuration::get('PS_HOME_CATEGORY');
return array($this->getCategories(), $this->getOffers());
}
$items = array();
public function getCategories()
{
$categories = array();
$categoriesIds = array();
$types = Category::getCategories($this->default_lang, true, false);
foreach ($types as $category) {
$categoryId = (empty($category['id_category']) && isset($category['id']))
? $category['id'] : $category['id_category'];
if (!self::isCategoryActive(new Category($categoryId))) {
continue;
}
$picture = $this->link->getCatImageLink($category['link_rewrite'], $categoryId, 'category_default');
$categoriesIds[] = $categoryId;
$categories[] = array(
'id' => $categoryId,
'parentId' => self::getParentCategoryId($categoryId, $category['id_parent']),
'name' => htmlspecialchars($category['name']),
'picture' => $picture ? $this->protocol . $picture : ''
);
}
foreach ($categories as $key => $innerCategory) {
if (isset($innerCategory['parentId'])
&& !empty($innerCategory['parentId'])
&& !in_array($innerCategory['parentId'], $categoriesIds)
) {
$innerCategory['parentId'] = $this->home_category;
$categories[$key] = $innerCategory;
}
}
return $categories;
}
public function getOffers()
{
$id_lang = $this->default_lang;
$homeCategory = $this->home_category;
$inactiveCategories = array();
$categoriesIds = array();
if ($currency->iso_code == 'RUB') {
$currency->iso_code = 'RUR';
}
$currencies = Currency::getCurrencies();
$link = new Link();
$types = Category::getCategories($id_lang, true, false);
foreach ($types as $category) {
@ -83,30 +125,18 @@ class RetailcrmCatalog
continue;
}
$picture = $link->getCatImageLink($category['link_rewrite'], $categoryId, 'category_default');
$categoriesIds[] = $categoryId;
$categories[] = array(
'id' => $categoryId,
'parentId' => self::getParentCategoryId($categoryId, $category['id_parent']),
'name' => $category['name'],
'picture' => $picture ? $protocol . $picture : ''
);
}
foreach ($categories as $key => $innerCategory) {
if (isset($innerCategory['parentId'])
&& !empty($innerCategory['parentId'])
&& !in_array($innerCategory['parentId'], $categoriesIds)
) {
$innerCategory['parentId'] = $homeCategory;
$categories[$key] = $innerCategory;
}
}
$limit = 2000;
$start = 0;
$count = self::getProductsCount($id_lang);
$products = Product::getProducts($id_lang, 0, 0, 'name', 'asc');
if ($count > 0)
do {
$products = Product::getProducts($id_lang, $start, $limit, 'name', 'asc');
foreach ($products AS $product) {
foreach ($products as $product) {
$category = $product['id_category_default'];
if (!in_array($category, $categoriesIds)) {
@ -133,14 +163,14 @@ class RetailcrmCatalog
continue;
}
if ($version == "1.3") {
if ($this->version == "1.3") {
$available_for_order = $product['active'] && $product['quantity'];
} else {
$available_for_order = $product['active'] && $product['available_for_order'];
}
$crewrite = Category::getLinkRewrite($product['id_category_default'], $id_lang);
$url = $link->getProductLink($product['id_product'], $product['link_rewrite'], $crewrite);
$url = $this->link->getProductLink($product['id_product'], $product['link_rewrite'], $crewrite);
if (!empty($product['wholesale_price'])) {
$purchasePrice = round($product['wholesale_price'], 2);
@ -150,8 +180,7 @@ class RetailcrmCatalog
$price = !empty($product['rate'])
? round($product['price'], 2) + (round($product['price'], 2) * $product['rate'] / 100)
: round($product['price'], 2)
;
: round($product['price'], 2);
if (!empty($product['manufacturer_name'])) {
$vendor = $product['manufacturer_name'];
@ -166,7 +195,7 @@ class RetailcrmCatalog
}
$weight = round($product['weight'], 2);
if (empty($weight)) {
if ($weight === 0.0) {
$weight = null;
}
@ -174,17 +203,17 @@ class RetailcrmCatalog
$height = round($product['height'], 3);
$depth = round($product['depth'], 3);
if (!empty($width) && !empty($height)) {
if ($width !== 0.0 && $height !== 0.0) {
$dimensions = implode('/', array($width, $height, $depth));
} else {
$dimensions = null;
}
$productForCombination = new Product($product['id_product']);
$offers = Product::getProductAttributesIds($product['id_product']);
if (!empty($offers)) {
$productForCombination = new Product($product['id_product']);
foreach ($offers as $offer) {
$combinations = $productForCombination->getAttributeCombinationsById($offer['id_product_attribute'], $id_lang);
@ -203,17 +232,17 @@ class RetailcrmCatalog
$covers = Image::getImages($id_lang, $product['id_product'], $offer['id_product_attribute']);
foreach ($covers as $cover) {
$picture = $protocol . $link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$picture = $this->protocol . $this->link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$pictures[] = $picture;
}
if (!$pictures) {
$image = Image::getCover($product['id_product']);
$picture = $protocol . $link->getImageLink($product['link_rewrite'], $image['id_image'], 'large_default');
$picture = $this->protocol . $this->link->getImageLink($product['link_rewrite'], $image['id_image'], 'large_default');
$pictures[] = $picture;
}
if ($version == "1.3") {
if ($this->version == "1.3") {
$quantity = $product['quantity'];
} else {
$quantity = (int)StockAvailable::getQuantityAvailableByProduct($product['id_product'], $offer['id_product_attribute']);
@ -264,23 +293,24 @@ class RetailcrmCatalog
}
}
$items[] = $item;
yield $item;
}
} else {
$pictures = array();
$covers = Image::getImages($id_lang, $product['id_product'], null);
foreach ($covers as $cover) {
$picture = $protocol . $link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$picture = $this->protocol . $this->link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $cover['id_image'], 'large_default');
$pictures[] = $picture;
}
if ($version == "1.3") {
if ($this->version == "1.3") {
$quantity = $product['quantity'];
} else {
$quantity = (int)StockAvailable::getQuantityAvailableByProduct($product['id_product']);
}
$item = array(
yield array(
'id' => $product['id_product'],
'productId' => $product['id_product'],
'productActivity' => ($available_for_order) ? 'Y' : 'N',
@ -297,12 +327,35 @@ class RetailcrmCatalog
'weight' => $weight,
'dimensions' => $dimensions
);
$items[] = $item;
}
}
return array($categories, $items);
$start += $limit;
} while ($start < $count && count($products) > 0);
}
private static function getProductsCount(
$id_lang,
Context $context = null
)
{
if (!$context) {
$context = Context::getContext();
}
$front = true;
if (!in_array($context->controller->controller_type, array('front', 'modulefront'))) {
$front = false;
}
$sql = 'SELECT COUNT(product_shop.`id_product`) AS nb
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (p.`id_product` = pl.`id_product` ' . Shop::addSqlRestrictionOnLang('pl') . ')
WHERE pl.`id_lang` = ' . (int)$id_lang .
($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '');
return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
}
/**

View File

@ -21,7 +21,7 @@ class RetailcrmCatalogTest extends RetailcrmTestCase
$products = $this->data[1];
$this->assertNotEmpty($categories);
$this->assertNotEmpty($products);
$this->assertTrue($products->valid());
foreach ($categories as $category) {
$this->assertNotEmpty($category);

View File

@ -26,7 +26,7 @@ class RetailcrmHistoryTest extends RetailcrmTestCase
$catalog = new RetailcrmCatalog();
$data = $catalog->getData();
$this->product = $data[1][0];
$this->product = $data[1]->current();
Configuration::updateValue('RETAILCRM_API_DELIVERY_DEFAULT', 2);
Configuration::updateValue('RETAILCRM_API_PAYMENT_DEFAULT', 'bankwire');

View File

@ -25,8 +25,9 @@ class RetailcrmInventoriesTest extends RetailcrmTestCase
$catalog = new RetailcrmCatalog();
$data = $catalog->getData();
$this->product1 = $data[1][0];
$this->product2 = $data[1][1];
$this->product1 = $data[1]->current();
$data[1]->next();
$this->product2 = $data[1]->current();
}
/**