src/Model/Product/Calculator/DivisionCalculator.php line 35

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Model\Product\Calculator;
  15. use Doctrine\DBAL\Exception;
  16. use Pimcore\Model\DataObject\Category;
  17. use Pimcore\Model\DataObject\ClassDefinition\CalculatorClassInterface;
  18. use Pimcore\Model\DataObject\Concrete;
  19. use Pimcore\Model\DataObject\Data\CalculatedValue;
  20. use Pimcore\Model\Document;
  21. class DivisionCalculator implements CalculatorClassInterface
  22. {
  23.     /**
  24.      * @param $object  Concrete
  25.      * @param $context \Pimcore\Model\DataObject\Data\CalculatedValue
  26.      *
  27.      * @return string
  28.      * @throws Exception
  29.      */
  30.     public function compute($object$context): string
  31.     {
  32.         $category $object->getMainCategory();
  33.         if (!$category) {
  34.             return '';
  35.         }
  36.         //get division document (for header, foooter)
  37.         while ($category->getParent()->getCategoryName() != 'Root category') {
  38.             $category $category->getParent();
  39.             if (!$category instanceof Category) {
  40.                 break;
  41.             }
  42.         }
  43.         $db \Pimcore\Db::get();
  44.         $rootDocumentId $db->fetchOne('SELECT cid FROM properties WHERE name = "category" and data = "' .
  45.             $category->getId() . '"');
  46.         if ($rootDocumentId) {
  47.             $rootDocument Document::getById($rootDocumentId);
  48.             if ($rootDocument) {
  49.                 return $rootDocument->getKey();
  50.             }
  51.         }
  52.         return '';
  53.     }
  54.     public function getCalculatedValueForEditMode(Concrete $objectCalculatedValue $context): string
  55.     {
  56.         $category $object->getMainCategory();
  57.         if (!$category) {
  58.             return '';
  59.         }
  60.         //get division document (for header, foooter)
  61.         while ($category->getParent()->getCategoryName() != 'Root category') {
  62.             $category $category->getParent();
  63.             if (!$category instanceof Category) {
  64.                 break;
  65.             }
  66.         }
  67.         $db \Pimcore\Db::get();
  68.         $rootDocumentId $db->fetchOne('SELECT cid FROM properties WHERE name = "category" and data = "' .
  69.             $category->getId() . '"');
  70.         if ($rootDocumentId) {
  71.             $rootDocument Document::getById($rootDocumentId);
  72.             if ($rootDocument) {
  73.                 return $rootDocument->getKey();
  74.             }
  75.         }
  76.         return '';
  77.     }
  78. }