<?php
declare(strict_types=1);
namespace Klaravik\Product\BasicData;
use Klaravik\Enum\ProductFieldsEnum;
use Klaravik\Exception\RepositoryException;
use Klaravik\Model\ProductField;
use Klaravik\Model\ProductValue;
use Klaravik\Repository\ProductFieldRepository;
use Klaravik\Repository\ProductValueRepository;
use Klaravik\Translate\Trans;
class BasicProductData
{
private Trans $trans;
public function __construct(Trans $trans)
{
$this->trans = $trans;
}
/**
* @param int $products_id
* @param int $categories_id
* @return array
*/
public function getProductBasicData(int $products_id, int $categories_id): array
{
try {
$getProductBasicDataByCategoriesId = (new ProductFieldRepository())->getProductBasicDataByCategoriesId($categories_id);
} catch (RepositoryException $e) {
$getProductBasicDataByCategoriesId = [];
}
$elements = [];
$elements['basicData'] = [];
$elements['basicData'][$this->trans->get('text_label_page-product_basicdata-information-headline-group')] = [];
$elements['highLight'] = [];
$elements['loadingHelp'] = '';
$elements['loadingHelpText'] = '';
foreach ($getProductBasicDataByCategoriesId as $basicDataMain) {
if (in_array($basicDataMain->getType(), [1, 2, 3, 5], true)) {
$elements = $this->getTextFieldsBasicData($basicDataMain, $products_id, $elements);
}
if ($basicDataMain->getType() === 4) { // Checkboxes
$elements = $this->getCheckBoxDataBasic($basicDataMain, $products_id, $elements);
}
}
return $elements;
}
/**
* @param ProductField $productField
* @param int $products_id
* @param array $elements
* @return array
*/
protected function getTextFieldsBasicData(ProductField $productField, int $products_id, array $elements) : array
{
try {
$productValues = (new ProductValueRepository())->getPublicFieldsByTypeText($productField->getId(), $products_id);
} catch (RepositoryException $e) {
$productValues = [];
}
if
(empty($productField->getHeadlineGroup()) || // Map all empty headline-groups that has no name yet
(strpos($productField->getHeadlineGroup(), '!') === 0)) { // Map headLineGroups that start with '!'
$productField->setHeadlineGroup(
$this->trans->get('text_label_page-product_basicdata-information-headline-group')
);
}
foreach ($productValues as $productFieldModel) {
if ($productField->getSystemName() === ProductFieldsEnum::LOADINGHELP_TITLE()->label) {
try {
$loadingHelp = (new ProductFieldRepository())->getSelectedSystemName($productFieldModel->getRefid());
$elements['loadingHelp'] = $loadingHelp;
$elements['loadingHelpText'] = $productFieldModel->getValue();
} catch (RepositoryException $e) {}
continue;
}
$productValuesModel = new ProductValue();
$productValuesModel->setValue($productFieldModel->getValue());
if ($productField->getHighlight() === 1) {
$elements['highLight'][$productField->getValue()] = $productValuesModel;
} else {
$elements['basicData'][$productField->getHeadlineGroup()][$productField->getValue()][] = $productValuesModel;
}
}
return $elements;
}
/**
* @param ProductField $productField
* @param int $products_id
* @param array $elements
* @return array
*/
protected function getCheckBoxDataBasic(ProductField $productField, int $products_id, array $elements): array
{
try {
$productValueRepository = new ProductValueRepository();
$productValues = $productValueRepository->getPublicFieldsByTypeCheckbox($productField->getId(), $products_id);
} catch (RepositoryException $e) {
return $elements;
}
$checkBoxProductFields = '';
foreach ($productValues as $productFieldModel) {
// Checkboxes can have multiple values, so let's merge them.
$checkBoxProductFields .= $productFieldModel->getValue() . ' ' . $productFieldModel->getExtraValue() . PHP_EOL;
}
if
(empty($productField->getHeadlineGroup()) || // Map all empty headline-groups that has no name yet
(strpos($productField->getHeadlineGroup(), '!') === 0)) { // Map headLineGroups that start with '!'
$productField->setHeadlineGroup(
$this->trans->get('text_label_page-product_basicdata-information-headline-group')
);
}
$productValuesModel = new ProductValue();
$productValuesModel->setValue(trim($checkBoxProductFields));
if ($productField->getHighlight() === 1) {
$elements['highLight'][$productField->getValue()] = $productValuesModel;
} else {
$elements['basicData'][$productField->getHeadlineGroup()][$productField->getValue()][] = $productValuesModel;
}
return $elements;
}
}