<?php
namespace AppBundle\Service;
use Pimcore\Model\DataObject\ClassDefinition\Data as FieldsetData;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\DataObject\Data\ElementMetadata;
use Pimcore\Model\DataObject\Download;
class ProductsService {
/**
* @param $product
* @return array
*/
public function buildKeyBenefitsArrayToDisplay($product) {
$keyBenefits = [];
strlen( trim( $product->getKeybenefit1())) ? $keyBenefits[] = $product->getKeybenefit1() : null;
strlen( trim( $product->getKeybenefit2())) ? $keyBenefits[] = $product->getKeybenefit2() : null;
strlen( trim( $product->getKeybenefit3())) ? $keyBenefits[] = $product->getKeybenefit3() : null;
strlen( trim( $product->getKeybenefit4())) ? $keyBenefits[] = $product->getKeybenefit4() : null;
return $keyBenefits;
}
public function buildBedienkonzepteList($bedienkonzepte, $locale) {
$bedienkonzepteInfo = [];
foreach ($bedienkonzepte as $item) {
$bedienkonzepteInfo[] = [
'name' => $item->getLocalizedfields()->getLocalizedValue('name', $locale),
'description' => $item->getLocalizedfields()->getLocalizedValue('description', $locale),
'keyBenefits' => $item->getLocalizedfields()->getLocalizedValue('keybenefits', $locale),
'plusBenefits' => $item->getLocalizedfields()->getLocalizedValue('plusbenefits', $locale),
'image' => $item->getImage(),
];
}
return $bedienkonzepteInfo;
}
public function buildHeaderGallery($product) {
$images = [];
if ($product->getGallery() && $galleryHeaderImages = $product->getGallery()->getItems()) {
foreach ($galleryHeaderImages as $image) {
if ($image !== null && $image->getImage()) {
$images[] = $image->getImage();
}
}
}
return $images;
}
public function buildApplicationGallery($product) {
$images = [];
if ($product->getGalleryapplication() && $galleryImages = $product->getGalleryapplication()->getItems()) {
foreach ($galleryImages as $image) {
if ($image !== null && $image->getImage()) {
$images[] = [
'image' => $image->getImage(),
'description' => $image->getImage()->getMetadata('title'),
'copyright' => $image->getImage()->getMetadata('copyright')
];
}
}
}
return $images;
}
public function buildHighlightsList($product) {
$highlights = [];
foreach ($product->getHighlights() as $highlight) {
$highlights[$highlight['highlights_headline']->getData()] = $highlight['highlights_text']->getData();
}
return $highlights;
}
public function buildSimilarProductsList($product, $locale) {
$otherProducts = [];
$similarProducts = $product->getParent()->getChildren([AbstractObject::OBJECT_TYPE_VARIANT]);
foreach ($similarProducts as $similarProduct) {
if ($product !== $similarProduct) {
$otherProducts[] = [
'productObject' => $similarProduct,
'name' => $similarProduct->getKey(),
'image' => $similarProduct->getTeaserImage(),
'technology' => $similarProduct->getProduct_cat() ? $similarProduct->getProduct_cat()->getLocalizedfields()->getLocalizedValue('name', $locale) : '',
'details' => $this->buildKeyBenefitsArrayToDisplay($similarProduct)
];
}
}
return $otherProducts;
}
public function buildTechnicalDataTable($product) {
$technicalDataDetails = [];
if ($product->getAttributes() && $attributes = $product->getAttributes()->getItems()) {
foreach ($attributes as $fieldcollection) {
switch( get_class($fieldcollection)) {
case 'Pimcore\Model\DataObject\Fieldcollection\Data\Netz':
//$technicalDataDetails = $this->buildNetzTechnicalDataTables($fieldcollection, $technicalDataDetails);
$technicalDataDetails = $this->buildTechnicalDataTableContent( $technicalDataDetails, $fieldcollection);
break;
default:
$technicalDataDetails = $this->buildTechnicalDataTableContent( $technicalDataDetails, $fieldcollection);
}
}
}
return $technicalDataDetails;
}
private function datatableSetEmptyValuesNull( $tableData, $columnTypes) {
foreach( $tableData as $rowIdx=>$row ) {
foreach( $row as $col=>$value) {
$colType = $columnTypes[$col];
$tableData[$rowIdx][$col] = $colType;
switch( $colType) {
case 'text':
if (strlen($value)) {
$tableData[$rowIdx][$col] = array( 'text' => $value);
} else {
$tableData[$rowIdx][$col] = null;
}
break;
case 'number':
if (is_string( $value)) {
if (ctype_digit($value)) {
$value = (int)$value;
} else if (is_numeric( $value)){
$value = (float)$value;
}
}
if (is_string( $value) && ( ! strlen($value) || $value === '0')) {
$tableData[$rowIdx][$col] = null;
} else if (is_int($value) && $value === 0) {
$tableData[$rowIdx][$col] = null;
} else if (is_float($value) && $value === 0.0) {
$tableData[$rowIdx][$col] = null;
} else {
if (is_int( $value)) {
$tableData[$rowIdx][$col] = array( 'int' => $value);
} else if (is_float( $value)) {
$tableData[$rowIdx][$col] = array( 'float' => $value);
} else if (is_string( $value)) {
$tableData[$rowIdx][$col] = array( 'number' => $value);
}
}
break;
case 'bool':
if ($value) {
$tableData[$rowIdx][$col] = array( 'bool' => true);
} else {
$tableData[$rowIdx][$col] = null;
}
break;
default:
}
}
}
return $tableData;
}
private function datatableRemoveEmptyRows( $tableData) {
$ret = array();
foreach( $tableData as $rowIdx=>$row ) {
$valueIsSet = false;
foreach( $row as $col=>$value) {
if ($value !== null) {
$valueIsSet = true;
break;
}
}
if ($valueIsSet) {
$ret[$rowIdx] = $row;
}
}
return $ret;
}
private function datatableMergeRange( $tableData, $colValue, $colMin, $colMax) {
$ret = array();
foreach( $tableData as $rowIdx=>$row ) {
$value = array();
if (isset( $row[$colMin])) {
$value[] = $row[$colMin];
} else {
$value[] = null;
}
if (isset( $row[$colMax])) {
$value[] = $row[$colMax];
} else {
$value[] = null;
}
$ret[$rowIdx][$colValue] = array('range'=>$value);
}
return $ret;
}
private function datatableMergeDimensions( $tableData, $colValue, $cols) {
$ret = array();
foreach( $tableData as $rowIdx=>$row ) {
$value = array();
foreach( $cols as $col) {
if (isset( $row[$col])) {
$value[] = $row[$col];
} else {
$value[] = null;
}
}
$ret[$rowIdx][$colValue] = array('dimensions'=>$value);
}
return $ret;
}
private function datatableGetColumnNames( $tableData) {
$columnNames = array();
foreach( $tableData as $rowIdx=>$row ) {
foreach( $row as $col=>$value) {
if (!in_array( $col, $columnNames) && $value !== null) {
$columnNames[] = $col;
}
}
}
return $columnNames;
}
private function buildTechnicalDataTableContent($technicalDataDetails, $fieldcollection) {
foreach( $fieldcollection->getDefinition()->getFielddefinitions() as $field=>$fieldData) {
if ($fieldData instanceof FieldsetData\StructuredTable) {
$technicalDataDetails = $this->buildTechnicalDataTableContentDatatable($technicalDataDetails, $fieldcollection, $field, $fieldcollection->get($field), $fieldData);
} else if ($fieldData instanceof FieldsetData\Multiselect) {
$technicalDataDetails = $this->buildTechnicalDataTableContentMultiselect($technicalDataDetails, $fieldcollection, $field, $fieldcollection->get($field));
} else if ($fieldData instanceof FieldsetData\Block) {
// Ignore for now
} else {
throw new \Exception('Unhandles Fieldset attribute: '.get_class($fieldData));
}
}
return $technicalDataDetails;
}
private function buildTechnicalDataTableContentDatatable($technicalDataDetails, $fieldcollection, $tabelName, $table, $tableDefinition) {
$columnTypes = array();
foreach( $tableDefinition->getCols() as $col) {
$columnTypes[$col['key']] = $col['type'];
}
$tableData = $this->datatableSetEmptyValuesNull( $table->getData(), $columnTypes);
$tableData = $this->datatableRemoveEmptyRows( $tableData);
$columnNames = $this->datatableGetColumnNames( $tableData);
if (in_array('min', $columnNames) && in_array('max', $columnNames)) {
$tableData = $this->datatableMergeRange( $tableData, 'wert', 'min', 'max');
$columnNames = $this->datatableGetColumnNames( $tableData);
} else if (in_array('length', $columnNames) && in_array('width', $columnNames) && in_array('height', $columnNames)) {
$tableData = $this->datatableMergeDimensions( $tableData, 'wert', array( 'length', 'width', 'height'));
$columnNames = $this->datatableGetColumnNames( $tableData);
}
$entry = array(
'class' => get_class($fieldcollection),
'object' => $fieldcollection,
'title' => 'product.technical_data.table_header.'.$fieldcollection->getType().'.'.$tabelName,
'tables' => $tableData,
'multipleColumns' => false,
'columnNames' => $columnNames,
'info' => $tableData
);
if (count($columnNames) && count($tableData)) {
$technicalDataDetails[$entry['title'].'.'.count($technicalDataDetails)] = $entry;
}
return $technicalDataDetails;
}
private function buildTechnicalDataTableContentMultiselect($technicalDataDetails, $fieldcollection, $fieldName, $field) {
$entry = array(
'class' => get_class($fieldcollection),
'object' => $fieldcollection,
'title' => 'product.technical_data.table_header.'.$fieldcollection->getType().'.'.$fieldName,
//So wars vorher und so ists einfach falsch, da $tableData nicht definiert ist
//'tables' => $tableData,
'tables' => array( $fieldName => array( 'wert' => array( 'csv'=> $field))),
'multipleColumns' => false,
'columnNames' => array('wert'),
'info' => array( $fieldName => array( 'wert' => array( 'csv'=> $field)))
);
$technicalDataDetails[$entry['title'].'.'.count($technicalDataDetails)] = $entry;
return $technicalDataDetails;
}
public function buildDownloadList($product, $locale) {
$downloadList = [];
if ($downloads = $product->getDownloadsobjects()) {
/** @var Download $item */
foreach ($downloads as $item) {
if ($item->getDownloadObject()) {
$downloadList[] = [
'title' => $item->getTitle() ? $item->getTitle() : $item->getDownloadObject()->getKey(),
'downloadObject' => $item->getDownloadObject()
];
}
}
}
if ($downloads = $product->getDownloads()) {
/** @var ElementMetadata $asset */
foreach ($downloads as $asset) {
if ($asset->getElement()) {
$downloadList[] = [
'title' => $asset->getElement()->getKey(),
'downloadObject' => $asset->getElement()
];
}
}
}
return $downloadList;
}
}