-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\TranslationDataProvider; | ||
|
||
class ps_EventbusApiTranslationsModuleFrontController extends AbstractApiController | ||
{ | ||
public $type = Config::COLLECTION_TRANSLATIONS; | ||
|
||
/** | ||
* @return void | ||
* | ||
* @throws PrestaShopException | ||
*/ | ||
public function postProcess() | ||
{ | ||
/** @var TranslationDataProvider $translationDataProvider */ | ||
$translationDataProvider = $this->module->getService(TranslationDataProvider::class); | ||
|
||
$response = $this->handleDataSync($translationDataProvider); | ||
|
||
$this->exitWithResponse($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TranslationDecorator | ||
{ | ||
/** | ||
* @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 $translations | ||
* | ||
* @return void | ||
*/ | ||
public function decorateTranslations(array &$translations) | ||
{ | ||
foreach ($translations as &$translation) { | ||
$this->castPropertyValues($translation); | ||
} | ||
} | ||
|
||
/** | ||
* @param array $translation | ||
* | ||
* @return void | ||
*/ | ||
private function castPropertyValues(array &$translation) | ||
{ | ||
$translation['id_translation'] = (int) $translation['id_translation']; | ||
$translation['id_lang'] = (int) $translation['id_lang']; | ||
$translation['key'] = (string) $translation['key']; | ||
$translation['translation'] = (string) $translation['translation']; | ||
$translation['domain'] = (string) $translation['domain']; | ||
$translation['theme'] = (string) $translation['theme']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\TranslationDecorator; | ||
use PrestaShop\Module\PsEventbus\Repository\TranslationRepository; | ||
|
||
class TranslationDataProvider implements PaginatedApiDataProviderInterface | ||
{ | ||
/** | ||
* @var TranslationRepository | ||
*/ | ||
private $translationRepository; | ||
|
||
/** | ||
* @var TranslationDecorator | ||
*/ | ||
private $translationDecorator; | ||
|
||
public function __construct(TranslationRepository $translationRepository, TranslationDecorator $translationDecorator) | ||
{ | ||
$this->translationRepository = $translationRepository; | ||
$this->translationDecorator = $translationDecorator; | ||
} | ||
|
||
/** | ||
* @param int $offset | ||
* @param int $limit | ||
* @param string $langIso | ||
* | ||
* @return array | ||
* | ||
* @throws \PrestaShopDatabaseException | ||
*/ | ||
public function getFormattedData($offset, $limit, $langIso) | ||
{ | ||
$translations = $this->translationRepository->getTranslationsSync($offset, $limit); | ||
|
||
if (!is_array($translations)) { | ||
return []; | ||
} | ||
$this->translationDecorator->decorateTranslations($translations); | ||
|
||
return array_map(function ($translation) { | ||
return [ | ||
'id' => $translation['id_lang'], | ||
'collection' => Config::COLLECTION_TRANSLATIONS, | ||
'properties' => $translation, | ||
]; | ||
}, $translations); | ||
} | ||
|
||
/** | ||
* @param int $offset | ||
* @param string $langIso | ||
* | ||
* @return int | ||
*/ | ||
public function getRemainingObjectsCount($offset, $langIso) | ||
{ | ||
return (int) $this->translationRepository->getRemainingTranslationsCount($offset); | ||
} | ||
|
||
/** | ||
* @param int $limit | ||
* @param string $langIso | ||
* @param array $objectIds | ||
* | ||
* @return array | ||
* | ||
* @throws \PrestaShopDatabaseException | ||
*/ | ||
public function getFormattedDataIncremental($limit, $langIso, $objectIds) | ||
{ | ||
$translations = $this->translationRepository->getTranslationsIncremental($limit, $objectIds); | ||
|
||
if (!is_array($translations)) { | ||
return []; | ||
} | ||
$this->translationDecorator->decorateTranslations($translations); | ||
|
||
return array_map(function ($translation) { | ||
return [ | ||
'id' => $translation['id_lang'], | ||
'collection' => Config::COLLECTION_TRANSLATIONS, | ||
'properties' => $translation, | ||
]; | ||
}, $translations); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace PrestaShop\Module\PsEventbus\Repository; | ||
|
||
class TranslationRepository | ||
{ | ||
/** | ||
* @var \Db | ||
*/ | ||
private $db; | ||
|
||
/** | ||
* @var \Context | ||
*/ | ||
private $context; | ||
|
||
public function __construct(\Db $db, \Context $context) | ||
{ | ||
$this->db = $db; | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* @param int $shopId | ||
* | ||
* @return \DbQuery | ||
*/ | ||
public function getBaseQuery($shopId) | ||
{ | ||
$query = new \DbQuery(); | ||
$query->from('translation', 'c') | ||
->where('c.id_shop = ' . (int) $shopId); | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* @param int $offset | ||
* @param int $limit | ||
* | ||
* @return array|bool|\mysqli_result|\PDOStatement|resource|null | ||
* | ||
* @throws \PrestaShopDatabaseException | ||
*/ | ||
public function getTranslations($offset, $limit) | ||
{ | ||
/** @var int $shopId */ | ||
$shopId = $this->context->shop->id; | ||
$query = $this->getBaseQuery($shopId); | ||
|
||
$this->addSelectParameters($query); | ||
|
||
$query->limit($limit, $offset); | ||
|
||
return $this->db->executeS($query); | ||
} | ||
|
||
/** | ||
* @param int $offset | ||
* | ||
* @return int | ||
*/ | ||
public function getRemainingTranslationsCount($offset) | ||
{ | ||
/** @var int $shopId */ | ||
$shopId = $this->context->shop->id; | ||
$query = $this->getBaseQuery($shopId) | ||
->select('(COUNT(c.id_translation) - ' . (int) $offset . ') as count'); | ||
|
||
return (int) $this->db->getValue($query); | ||
} | ||
|
||
/** | ||
* @param int $limit | ||
* @param array $translationIds | ||
* | ||
* @return array|bool|\mysqli_result|\PDOStatement|resource|null | ||
* | ||
* @throws \PrestaShopDatabaseException | ||
*/ | ||
public function getTranslationsIncremental($limit, $translationIds) | ||
{ | ||
/** @var int $shopId */ | ||
$shopId = $this->context->shop->id; | ||
$query = $this->getBaseQuery($shopId); | ||
|
||
$this->addSelectParameters($query); | ||
|
||
$query->where('c.id_translation IN(' . implode(',', array_map('intval', $translationIds)) . ')') | ||
->limit($limit); | ||
|
||
return $this->db->executeS($query); | ||
} | ||
|
||
/** | ||
* @param \DbQuery $query | ||
* | ||
* @return void | ||
*/ | ||
private function addSelectParameters(\DbQuery $query) | ||
{ | ||
$query->select('c.id_translation, c.id_lang, c.key, c.translation, c.domain, c.theme'); | ||
} | ||
} |