<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Model\Product\Calculator;
use Doctrine\DBAL\Exception;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\ClassDefinition\CalculatorClassInterface;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Data\CalculatedValue;
use Pimcore\Model\Document;
class DivisionCalculator implements CalculatorClassInterface
{
/**
* @param $object Concrete
* @param $context \Pimcore\Model\DataObject\Data\CalculatedValue
*
* @return string
* @throws Exception
*/
public function compute($object, $context): string
{
$category = $object->getMainCategory();
if (!$category) {
return '';
}
//get division document (for header, foooter)
while ($category->getParent()->getCategoryName() != 'Root category') {
$category = $category->getParent();
if (!$category instanceof Category) {
break;
}
}
$db = \Pimcore\Db::get();
$rootDocumentId = $db->fetchOne('SELECT cid FROM properties WHERE name = "category" and data = "' .
$category->getId() . '"');
if ($rootDocumentId) {
$rootDocument = Document::getById($rootDocumentId);
if ($rootDocument) {
return $rootDocument->getKey();
}
}
return '';
}
public function getCalculatedValueForEditMode(Concrete $object, CalculatedValue $context): string
{
$category = $object->getMainCategory();
if (!$category) {
return '';
}
//get division document (for header, foooter)
while ($category->getParent()->getCategoryName() != 'Root category') {
$category = $category->getParent();
if (!$category instanceof Category) {
break;
}
}
$db = \Pimcore\Db::get();
$rootDocumentId = $db->fetchOne('SELECT cid FROM properties WHERE name = "category" and data = "' .
$category->getId() . '"');
if ($rootDocumentId) {
$rootDocument = Document::getById($rootDocumentId);
if ($rootDocument) {
return $rootDocument->getKey();
}
}
return '';
}
}