Skip to content

Commit

Permalink
Languages sync (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
synicko authored Aug 28, 2023
1 parent 12c6fe3 commit 97516ca
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/front/decorator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ services:
public: true
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository'

PrestaShop\Module\PsEventbus\Decorator\LanguageDecorator:
class: PrestaShop\Module\PsEventbus\Decorator\LanguageDecorator
public: true
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository'
- '@PrestaShop\Module\PsEventbus\Repository\ShopRepository'
7 changes: 7 additions & 0 deletions config/front/provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,10 @@ services:
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\SupplierRepository'
- '@PrestaShop\Module\PsEventbus\Decorator\SupplierDecorator'

PrestaShop\Module\PsEventbus\Provider\LanguageDataProvider:
class: PrestaShop\Module\PsEventbus\Provider\LanguageDataProvider
public: true
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\LanguageRepository'
- '@PrestaShop\Module\PsEventbus\Decorator\LanguageDecorator'
3 changes: 3 additions & 0 deletions config/front/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ services:
PrestaShop\Module\PsEventbus\Repository\LanguageRepository:
class: PrestaShop\Module\PsEventbus\Repository\LanguageRepository
public: true
arguments:
- '@ps_eventbus.db'
- '@ps_eventbus.context'

PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository:
class: PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository
Expand Down
25 changes: 25 additions & 0 deletions controllers/front/apiLanguages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Controller\AbstractApiController;
use PrestaShop\Module\PsEventbus\Provider\LanguageDataProvider;

class ps_EventbusApiLanguagesModuleFrontController extends AbstractApiController
{
public $type = Config::COLLECTION_LANGUAGES;

/**
* @return void
*
* @throws PrestaShopException
*/
public function postProcess()
{
/** @var LanguageDataProvider $languageDataProvider */
$languageDataProvider = $this->module->getService(LanguageDataProvider::class);

$response = $this->handleDataSync($languageDataProvider);

$this->exitWithResponse($response);
}
}
62 changes: 62 additions & 0 deletions ps_eventbus.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class Ps_eventbus extends Module
'actionObjectImageAddAfter',
'actionObjectImageDeleteAfter',
'actionObjectImageUpdateAfter',
'actionObjectLanguageAddAfter',
'actionObjectLanguageDeleteAfter',
'actionObjectLanguageUpdateAfter',
'actionObjectManufacturerAddAfter',
'actionObjectManufacturerDeleteAfter',
'actionObjectManufacturerUpdateAfter',
Expand Down Expand Up @@ -285,6 +288,65 @@ public function hookActionObjectImageUpdateAfter($parameters)
}
}

/**
* @param array $parameters
*
* @return void
*/
public function hookActionObjectLanguageDeleteAfter($parameters)
{
$language = $parameters['object'];
if (isset($language->id)) {
// $this->sendLiveSync(['languages'], $language->id, 'delete');
$this->insertDeletedObject(
$language->id,
Config::COLLECTION_LANGUAGES,
date(DATE_ATOM),
$this->shopId
);
}
}

/**
* @param array $parameters
*
* @return void
*/
public function hookActionObjectLanguageAddAfter($parameters)
{
$language = $parameters['object'];
if (isset($language->id)) {
// $this->sendLiveSync(['languages'], $language->id_product, 'upsert');
$this->insertIncrementalSyncObject(
$language->id,
Config::COLLECTION_LANGUAGES,
date(DATE_ATOM),
$this->shopId,
true
);
}
}

/**
* @param array $parameters
*
* @return void
*/
public function hookActionObjectLanguageUpdateAfter($parameters)
{
$language = $parameters['object'];
if (isset($language->id)) {
// $this->sendLiveSync(['languages'], $language->id_product, 'upsert');
$this->insertIncrementalSyncObject(
$language->id,
Config::COLLECTION_LANGUAGES,
date(DATE_ATOM),
$this->shopId,
true
);
}
}

/**
* @param array $parameters
*
Expand Down
1 change: 1 addition & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Config
public const COLLECTION_MANUFACTURERS = 'manufacturers';
public const COLLECTION_SUPPLIERS = 'suppliers';
public const COLLECTION_PRODUCT_SUPPLIERS = 'product_suppliers';
public const COLLECTION_LANGUAGES = 'languages';

/**
* @param mixed $message
Expand Down
54 changes: 54 additions & 0 deletions src/Decorator/LanguageDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PrestaShop\Module\PsEventbus\Decorator;

use PrestaShop\Module\PsEventbus\Repository\ConfigurationRepository;
use PrestaShop\Module\PsEventbus\Repository\ShopRepository;

class LanguageDecorator
{
/**
* @var string
*/
private $timezone;

/**
* @var string
*/
private $createdAt;

public function __construct(
ConfigurationRepository $configurationRepository,
ShopRepository $shopRepository
) {
$this->timezone = (string) $configurationRepository->get('PS_TIMEZONE');
$this->createdAt = $shopRepository->getCreatedAt();
}

/**
* @param array $languages
*
* @return void
*/
public function decorateLanguages(array &$languages)
{
foreach ($languages as &$language) {
$this->castPropertyValues($language);
}
}

/**
* @param array $language
*
* @return void
*/
private function castPropertyValues(array &$language)
{
$language['id_lang'] = (int) $language['id_lang'];
$language['active'] = (bool) $language['active'];
$language['is_rtl'] = (bool) $language['is_rtl'];
$language['id_shop'] = (int) $language['id_shop'];
$language['created_at'] = (new \DateTime($this->createdAt, new \DateTimeZone($this->timezone)))->format('Y-m-d\TH:i:sO');
$language['updated_at'] = (new \DateTime($this->createdAt, new \DateTimeZone($this->timezone)))->format('Y-m-d\TH:i:sO');
}
}
91 changes: 91 additions & 0 deletions src/Provider/LanguageDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace PrestaShop\Module\PsEventbus\Provider;

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Decorator\LanguageDecorator;
use PrestaShop\Module\PsEventbus\Repository\LanguageRepository;

class LanguageDataProvider implements PaginatedApiDataProviderInterface
{
/**
* @var LanguageRepository
*/
private $languageRepository;

/**
* @var LanguageDecorator
*/
private $languageDecorator;

public function __construct(LanguageRepository $languageRepository, LanguageDecorator $languageDecorator)
{
$this->languageRepository = $languageRepository;
$this->languageDecorator = $languageDecorator;
}

/**
* @param int $offset
* @param int $limit
* @param string $langIso
*
* @return array
*
* @throws \PrestaShopDatabaseException
*/
public function getFormattedData($offset, $limit, $langIso)
{
$languages = $this->languageRepository->getLanguagesSync($offset, $limit, $langIso);

if (!is_array($languages)) {
return [];
}
$this->languageDecorator->decorateLanguages($languages);

return array_map(function ($language) {
return [
'id' => $language['id_lang'],
'collection' => Config::COLLECTION_LANGUAGES,
'properties' => $language,
];
}, $languages);
}

/**
* @param int $offset
* @param string $langIso
*
* @return int
*/
public function getRemainingObjectsCount($offset, $langIso)
{
return (int) $this->languageRepository->getRemainingLanguagesCount($offset, $langIso);
}

/**
* @param int $limit
* @param string $langIso
* @param array $objectIds
*
* @return array
*
* @throws \PrestaShopDatabaseException
*/
public function getFormattedDataIncremental($limit, $langIso, $objectIds)
{
$languages = $this->languageRepository->getLanguagesIncremental($limit, $langIso, $objectIds);

if (!is_array($languages)) {
return [];
}
$this->languageDecorator->decorateLanguages($languages);

return array_map(function ($language) {
return [
'id' => $language['id_lang'],
'collection' => Config::COLLECTION_LANGUAGES,
'properties' => $language,
];
}, $languages);
}
}
103 changes: 103 additions & 0 deletions src/Repository/LanguageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,109 @@

class LanguageRepository
{
/**
* @var \Db
*/
private $db;

/**
* @var \Context
*/
private $context;

public function __construct(\Db $db, \Context $context)
{
$this->db = $db;
$this->context = $context;
}

/**
* @param int $offset
* @param int $limit
* @param string $langIso
*
* @return array|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getLanguagesSync($offset, $limit, $langIso)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId, $langIso);

$this->addSelectParameters($query);

$query->limit($limit, $offset);

return $this->db->executeS($query);
}

/**
* @param int $offset
* @param string $langIso
*
* @return int
*/
public function getRemainingLanguagesCount($offset, $langIso)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId, $langIso)
->select('(COUNT(la.id_lang) - ' . (int) $offset . ') as count');

return (int) $this->db->getValue($query);
}

/**
* @param int $limit
* @param string $langIso
* @param array $languageIds
*
* @return array|bool|\mysqli_result|\PDOStatement|resource|null
*
* @throws \PrestaShopDatabaseException
*/
public function getLanguagesIncremental($limit, $langIso, $languageIds)
{
/** @var int $shopId */
$shopId = $this->context->shop->id;
$query = $this->getBaseQuery($shopId, $langIso);

$this->addSelectParameters($query);

$query->where('la.id_lang IN(' . implode(',', array_map('intval', $languageIds)) . ')')
->limit($limit);

return $this->db->executeS($query);
}

/**
* @param int $shopId
* @param string $langIso
*
* @return \DbQuery
*/
public function getBaseQuery($shopId, $langIso)
{
$query = new \DbQuery();
$query->from('lang', 'la')
->innerJoin('lang_shop', 'las', 'la.id_lang = las.id_lang AND las.id_shop = ' . (int) $shopId);

return $query;
}

/**
* @param \DbQuery $query
*
* @return void
*/
private function addSelectParameters(\DbQuery $query)
{
$query->select('la.id_lang, la.name, la.active, la.iso_code, la.language_code, la.locale, la.date_format_lite,
la.date_format_full, la.is_rtl, las.id_shop');
}

/**
* @return array
*/
Expand Down
Loading

0 comments on commit 97516ca

Please sign in to comment.