<?php
namespace Jabber\GenericBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Model\DataObject\Product;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class MandatoryCompleteness {
private ParameterBagInterface $params;
public function __construct(ParameterBagInterface $params) {
$this->params = $params;
}
public function onPreUpdateAndSave(ElementEventInterface $e) {
if (!$e->hasArgument('isAutoSave')) {
if ($e instanceof DataObjectEvent) {
// do something with the object
$dataObject = $e->getObject();
if ($dataObject instanceof Product) {
if ($dataObject->getProductLevel() == "PARENT") {
$dataObject = $this->CalculateParentCompleteness($dataObject);
} else if ($dataObject->getProductLevel() == "STYLE") {
$dataObject = $this->StyleCompleteness($dataObject);
}
}
}
}
}
private function StyleCompleteness(Product $dataObject) {
$params = $this->params->get('variant_mandatory_fields');
$dataObject->doGetInheritedValues();
$mandatoryFields = 0;
$filledMandatoryFields = 0;
if (!empty($params)) {
if (isset($params['Product']['Lvl2'])) {
foreach ($params['Product']['Lvl2'] as $field) {
$getter = "get" . $field;
if (method_exists($dataObject, $getter)) {
$mandatoryFields++;
if (!empty($dataObject->$getter())) {
$filledMandatoryFields++;
}
}
}
}
if (isset($params['Product']['Lvl3'])) {
$children = $dataObject->getChildren([Product::OBJECT_TYPE_VARIANT]);
if (!empty($children)) {
foreach ($children as $child) {
foreach ($params['Product']['Lvl3'] as $field) {
$getter = "get" . $field;
if (method_exists($child, $getter)) {
$mandatoryFields++;
if (!empty($child->$getter())) {
$filledMandatoryFields++;
}
}
}
}
}
}
}
$percentage = number_format(($filledMandatoryFields/$mandatoryFields), 2)*100;
$dataObject->setPercentageComplete($percentage);
return $dataObject;
}
private function CalculateParentCompleteness(Product $dataObject) {
$params = $this->params->get('variant_mandatory_fields');
$mandatoryFields = 0;
$filledMandatoryFields = 0;
if (!empty($params)) {
if (isset($params['Product']['Lvl1'])) {
foreach ($params['Product']['Lvl1'] as $field) {
$getter = "get" . $field;
if (method_exists($dataObject, $getter)) {
$mandatoryFields++;
if (!empty($dataObject->$getter())) {
$filledMandatoryFields++;
}
}
}
}
}
$percentage = number_format(($filledMandatoryFields/$mandatoryFields), 2)*100;
$dataObject->setPercentageComplete($percentage);
return $dataObject;
}
private function CalculateCompleteness(Product $dataObject) {
$mandatoryFields = 0;
$filledMandatoryFields = 0;
$dataObject->doGetInheritedValues();
$languages = \Pimcore\Tool::getValidLanguages();
foreach ($dataObject->getClass()->getFieldDefinitions() as $key=>$fieldDefinition) {
if ($fieldDefinition->getMandatory()) {
$mandatoryFields++;
$function = "get".$key;
if ($dataObject->$function()) {
$filledMandatoryFields++;
}
}
if ($fieldDefinition->getFieldtype() == "localizedfields") {
foreach ($fieldDefinition->childs as $localisedField) {
if ($localisedField->getMandatory()) {
foreach ($languages as $language) {
$mandatoryFields++;
$localisedFunction = "get".$localisedField->getName();
if ($dataObject->$localisedFunction($language)) {
$filledMandatoryFields++;
}
}
}
}
}
}
$percentage = number_format(($filledMandatoryFields/$mandatoryFields), 2)*100;
$dataObject->setPercentageComplete($percentage);
return $dataObject;
}
}