vendor/pimcore/pimcore/bundles/AdminBundle/Controller/Admin/DataObject/QuantityValueController.php line 162

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.  * @category   Pimcore
  12.  *
  13.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  14.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  15.  */
  16. namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject;
  17. use Pimcore\Bundle\AdminBundle\Controller\AdminController;
  18. use Pimcore\Model\DataObject\Data\QuantityValue;
  19. use Pimcore\Model\DataObject\QuantityValue\Unit;
  20. use Pimcore\Model\DataObject\QuantityValue\UnitConversionService;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. class QuantityValueController extends AdminController
  25. {
  26.     /**
  27.      * @Route("/quantity-value/unit-proxy", name="pimcore_admin_dataobject_quantityvalue_unitproxyget", methods={"GET"})
  28.      *
  29.      * @param Request $request
  30.      *
  31.      * @return JsonResponse
  32.      *
  33.      * @throws \Exception
  34.      */
  35.     public function unitProxyGetAction(Request $request)
  36.     {
  37.         $list = new Unit\Listing();
  38.         $orderKey 'abbreviation';
  39.         $order 'asc';
  40.         $allParams array_merge($request->request->all(), $request->query->all());
  41.         $sortingSettings = \Pimcore\Bundle\AdminBundle\Helper\QueryParams::extractSortingSettings($allParams);
  42.         if ($sortingSettings['orderKey']) {
  43.             $orderKey $sortingSettings['orderKey'];
  44.         }
  45.         if ($sortingSettings['order']) {
  46.             $order $sortingSettings['order'];
  47.         }
  48.         $list->setOrder($order);
  49.         $list->setOrderKey($orderKey);
  50.         $list->setLimit($request->get('limit'));
  51.         $list->setOffset($request->get('start'));
  52.         $condition '1 = 1';
  53.         if ($request->get('filter')) {
  54.             $filterString $request->get('filter');
  55.             $filters json_decode($filterString);
  56.             $db = \Pimcore\Db::get();
  57.             foreach ($filters as $f) {
  58.                 if ($f->type == 'string') {
  59.                     $condition .= ' AND ' $db->quoteIdentifier($f->property) . ' LIKE ' $db->quote('%' $f->value '%');
  60.                 } elseif ($f->type == 'numeric') {
  61.                     $operator $this->getOperator($f->comparison);
  62.                     $condition .= ' AND ' $db->quoteIdentifier($f->property) . ' ' $operator ' ' $db->quote($f->value);
  63.                 }
  64.             }
  65.             $list->setCondition($condition);
  66.         }
  67.         $list->load();
  68.         $units = [];
  69.         foreach ($list->getUnits() as $u) {
  70.             $units[] = get_object_vars($u);
  71.         }
  72.         return $this->adminJson(['data' => $units'success' => true'total' => $list->getTotalCount()]);
  73.     }
  74.     /**
  75.      * @Route("/quantity-value/unit-proxy", name="pimcore_admin_dataobject_quantityvalue_unitproxy", methods={"POST", "PUT"})
  76.      *
  77.      * @param Request $request
  78.      *
  79.      * @return JsonResponse
  80.      *
  81.      * @throws \Exception
  82.      */
  83.     public function unitProxyAction(Request $request)
  84.     {
  85.         if ($request->get('data')) {
  86.             if ($request->get('xaction') == 'destroy') {
  87.                 $data json_decode($request->get('data'), true);
  88.                 $id $data['id'];
  89.                 $unit = \Pimcore\Model\DataObject\QuantityValue\Unit::getById($id);
  90.                 if (!empty($unit)) {
  91.                     $unit->delete();
  92.                     return $this->adminJson(['data' => [], 'success' => true]);
  93.                 } else {
  94.                     throw new \Exception('Unit with id ' $id ' not found.');
  95.                 }
  96.             } elseif ($request->get('xaction') == 'update') {
  97.                 $data json_decode($request->get('data'), true);
  98.                 $unit Unit::getById($data['id']);
  99.                 if (!empty($unit)) {
  100.                     if ($data['baseunit'] === -1) {
  101.                         $data['baseunit'] = null;
  102.                     }
  103.                     $unit->setValues($data);
  104.                     $unit->save();
  105.                     return $this->adminJson(['data' => get_object_vars($unit), 'success' => true]);
  106.                 } else {
  107.                     throw new \Exception('Unit with id ' $data['id'] . ' not found.');
  108.                 }
  109.             } elseif ($request->get('xaction') == 'create') {
  110.                 $data json_decode($request->get('data'), true);
  111.                 if (isset($data['baseunit']) && $data['baseunit'] === -1) {
  112.                     $data['baseunit'] = null;
  113.                 }
  114.                 unset($data['id']);
  115.                 $unit = new Unit();
  116.                 $unit->setValues($data);
  117.                 $unit->save();
  118.                 return $this->adminJson(['data' => get_object_vars($unit), 'success' => true]);
  119.             }
  120.         }
  121.         return $this->adminJson(['success' => false]);
  122.     }
  123.     /**
  124.      * @param string $comparison
  125.      *
  126.      * @return string
  127.      */
  128.     private function getOperator($comparison)
  129.     {
  130.         $mapper = [
  131.             'lt' => '<',
  132.             'gt' => '>',
  133.             'eq' => '=',
  134.         ];
  135.         return $mapper[$comparison];
  136.     }
  137.     /**
  138.      * @Route("/quantity-value/unit-list", name="pimcore_admin_dataobject_quantityvalue_unitlist", methods={"GET"})
  139.      *
  140.      * @param Request $request
  141.      *
  142.      * @return JsonResponse
  143.      */
  144.     public function unitListAction(Request $request)
  145.     {
  146.         $list = new Unit\Listing();
  147.         $list->setOrderKey('abbreviation');
  148.         $list->setOrder('ASC');
  149.         if ($request->get('filter')) {
  150.             $array explode(','$request->get('filter'));
  151.             $quotedArray = [];
  152.             $db = \Pimcore\Db::get();
  153.             foreach ($array as $a) {
  154.                 $quotedArray[] = $db->quote($a);
  155.             }
  156.             $string implode(','$quotedArray);
  157.             $list->setCondition('id IN (' $string ')');
  158.         }
  159.         $units $list->getUnits();
  160.         /** @var Unit $unit */
  161.         foreach ($units as $unit) {
  162.             try {
  163.                 if ($unit->getAbbreviation()) {
  164.                     $unit->setAbbreviation(\Pimcore\Model\Translation\Admin::getByKeyLocalized($unit->getAbbreviation(),
  165.                         truetrue));
  166.                 }
  167.                 if ($unit->getLongname()) {
  168.                     $unit->setLongname(\Pimcore\Model\Translation\Admin::getByKeyLocalized($unit->getLongname(), true,
  169.                         true));
  170.                 }
  171.             } catch (\Exception $e) {
  172.                 // nothing to do ...
  173.             }
  174.         }
  175.         return $this->adminJson(['data' => $units'success' => true'total' => $list->getTotalCount()]);
  176.     }
  177.     /**
  178.      * @Route("/quantity-value/convert", name="pimcore_admin_dataobject_quantityvalue_convert", methods={"GET"})
  179.      *
  180.      * @param Request $request
  181.      *
  182.      * @return JsonResponse
  183.      */
  184.     public function convertAction(Request $request)
  185.     {
  186.         $fromUnitId $request->get('fromUnit');
  187.         $toUnitId $request->get('toUnit');
  188.         $fromUnit Unit::getById($fromUnitId);
  189.         $toUnit Unit::getById($toUnitId);
  190.         if (!$fromUnit instanceof Unit || !$toUnit instanceof Unit) {
  191.             return $this->adminJson(['success' => false]);
  192.         }
  193.         /** @var UnitConversionService $converter */
  194.         $converter $this->container->get(UnitConversionService::class);
  195.         try {
  196.             $convertedValue $converter->convert(new QuantityValue($request->get('value'), $fromUnit), $toUnit);
  197.         } catch (\Exception $e) {
  198.             return $this->adminJson(['success' => false]);
  199.         }
  200.         return $this->adminJson(['value' => $convertedValue->getValue(), 'success' => true]);
  201.     }
  202.     /**
  203.      * @Route("/quantity-value/convert-all", name="pimcore_admin_dataobject_quantityvalue_convertall", methods={"GET"})
  204.      *
  205.      * @param Request $request
  206.      *
  207.      * @return JsonResponse
  208.      */
  209.     public function convertAllAction(Request $request)
  210.     {
  211.         $unitId $request->get('unit');
  212.         $fromUnit Unit::getById($unitId);
  213.         if (!$fromUnit instanceof Unit) {
  214.             return $this->adminJson(['success' => false]);
  215.         }
  216.         $baseUnit $fromUnit->getBaseunit() ?? $fromUnit;
  217.         $units = new Unit\Listing();
  218.         $units->setCondition('baseunit = '.$units->quote($baseUnit->getId()).' AND id != '.$units->quote($fromUnit->getId()));
  219.         $units $units->load();
  220.         $convertedValues = [];
  221.         /** @var UnitConversionService $converter */
  222.         $converter $this->container->get(UnitConversionService::class);
  223.         /** @var Unit $targetUnit */
  224.         foreach ($units as $targetUnit) {
  225.             try {
  226.                 $convertedValue $converter->convert(new QuantityValue($request->get('value'), $fromUnit), $targetUnit);
  227.                 $convertedValues[] = ['unit' => $targetUnit->getAbbreviation(), 'unitName' => $targetUnit->getLongname(), 'value' => round($convertedValue->getValue(), 4)];
  228.             } catch (\Exception $e) {
  229.                 return $this->adminJson(['success' => false'message' => $e->getMessage()]);
  230.             }
  231.         }
  232.         return $this->adminJson(['value' => $request->get('value'), 'fromUnit' => $fromUnit->getAbbreviation(), 'values' => $convertedValues'success' => true]);
  233.     }
  234. }