vendor/pimcore/pimcore/models/DataObject/ClassDefinition/Data/Image.php line 88

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 Commercial License (PCL)
  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 PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\ClassDefinition\Data;
  15. use Pimcore\Model;
  16. use Pimcore\Model\Asset;
  17. use Pimcore\Model\DataObject\ClassDefinition\Data;
  18. use Pimcore\Model\Element;
  19. use Pimcore\Normalizer\NormalizerInterface;
  20. class Image extends Data implements ResourcePersistenceAwareInterfaceQueryResourcePersistenceAwareInterfaceTypeDeclarationSupportInterfaceEqualComparisonInterfaceVarExporterInterfaceNormalizerInterfaceIdRewriterInterface
  21. {
  22.     use Extension\ColumnType;
  23.     use ImageTrait;
  24.     use Extension\QueryColumnType;
  25.     use Data\Extension\RelationFilterConditionParser;
  26.     /**
  27.      * Static type of this element
  28.      *
  29.      * @internal
  30.      *
  31.      * @var string
  32.      */
  33.     public $fieldtype 'image';
  34.     /**
  35.      * Type for the column to query
  36.      *
  37.      * @internal
  38.      *
  39.      * @var string
  40.      */
  41.     public $queryColumnType 'int(11)';
  42.     /**
  43.      * Type for the column
  44.      *
  45.      * @internal
  46.      *
  47.      * @var string
  48.      */
  49.     public $columnType 'int(11)';
  50.     /**
  51.      * @see ResourcePersistenceAwareInterface::getDataForResource
  52.      *
  53.      * @param Asset\Image|null $data
  54.      * @param null|Model\DataObject\Concrete $object
  55.      * @param mixed $params
  56.      *
  57.      * @return int|null
  58.      */
  59.     public function getDataForResource($data$object null$params = [])
  60.     {
  61.         if ($data instanceof Asset\Image) {
  62.             return $data->getId();
  63.         }
  64.         return null;
  65.     }
  66.     /**
  67.      * @see ResourcePersistenceAwareInterface::getDataFromResource
  68.      *
  69.      * @param int|null $data
  70.      * @param null|Model\DataObject\Concrete $object
  71.      * @param mixed $params
  72.      *
  73.      * @return Asset|null
  74.      */
  75.     public function getDataFromResource($data$object null$params = [])
  76.     {
  77.         if ((int)$data 0) {
  78.             return Asset\Image::getById($data);
  79.         }
  80.         return null;
  81.     }
  82.     /**
  83.      * @see QueryResourcePersistenceAwareInterface::getDataForQueryResource
  84.      *
  85.      * @param Asset\Image|null $data
  86.      * @param null|Model\DataObject\Concrete $object
  87.      * @param mixed $params
  88.      *
  89.      * @return int|null
  90.      */
  91.     public function getDataForQueryResource($data$object null$params = [])
  92.     {
  93.         if ($data instanceof Asset\Image) {
  94.             return $data->getId();
  95.         }
  96.         return null;
  97.     }
  98.     /**
  99.      * @see Data::getDataForEditmode
  100.      *
  101.      * @param Asset\Image|null $data
  102.      * @param null|Model\DataObject\Concrete $object
  103.      * @param array $params
  104.      *
  105.      * @return array|null
  106.      */
  107.     public function getDataForEditmode($data$object null$params = [])
  108.     {
  109.         if ($data instanceof Asset\Image) {
  110.             return $data->getObjectVars();
  111.         }
  112.         return null;
  113.     }
  114.     /**
  115.      * @param Asset\Image $data
  116.      * @param null|Model\DataObject\Concrete $object
  117.      * @param mixed $params
  118.      *
  119.      * @return array
  120.      */
  121.     public function getDataForGrid($data$object null$params = [])
  122.     {
  123.         return $this->getDataForEditmode($data$object$params);
  124.     }
  125.     /**
  126.      * @see Data::getDataFromEditmode
  127.      *
  128.      * @param array|null $data
  129.      * @param null|Model\DataObject\Concrete $object
  130.      * @param mixed $params
  131.      *
  132.      * @return Asset\Image|null
  133.      */
  134.     public function getDataFromEditmode($data$object null$params = [])
  135.     {
  136.         if ($data && (int)$data['id'] > 0) {
  137.             return Asset\Image::getById($data['id']);
  138.         }
  139.         return null;
  140.     }
  141.     /**
  142.      * @param mixed $data
  143.      * @param bool $omitMandatoryCheck
  144.      * @param array $params
  145.      *
  146.      * @throws Element\ValidationException
  147.      */
  148.     public function checkValidity($data$omitMandatoryCheck false$params = [])
  149.     {
  150.         if (!$omitMandatoryCheck && $this->getMandatory() && !$data instanceof Asset\Image) {
  151.             throw new Element\ValidationException('Empty mandatory field [ '.$this->getName().' ]');
  152.         }
  153.         if ($data !== null && !$data instanceof Asset\Image) {
  154.             throw new Element\ValidationException('Invalid data in field `'.$this->getName().'`');
  155.         }
  156.     }
  157.     /**
  158.      * @param array|null $data
  159.      * @param null|Model\DataObject\Concrete $object
  160.      * @param mixed $params
  161.      *
  162.      * @return Asset
  163.      */
  164.     public function getDataFromGridEditor($data$object null$params = [])
  165.     {
  166.         return $this->getDataFromEditmode($data$object$params);
  167.     }
  168.     /**
  169.      * @see Data::getVersionPreview
  170.      *
  171.      * @param Asset\Image|null $data
  172.      * @param null|Model\DataObject\Concrete $object
  173.      * @param mixed $params
  174.      *
  175.      * @return string|null
  176.      */
  177.     public function getVersionPreview($data$object null$params = [])
  178.     {
  179.         if ($data instanceof Asset\Image) {
  180.             return '<img src="/admin/asset/get-image-thumbnail?id=' $data->getId() . '&width=100&height=100&aspectratio=true" />';
  181.         }
  182.         return null;
  183.     }
  184.     /**
  185.      * {@inheritdoc}
  186.      */
  187.     public function getForCsvExport($object$params = [])
  188.     {
  189.         $data $this->getDataFromObjectParam($object$params);
  190.         if ($data instanceof Element\ElementInterface) {
  191.             return $data->getRealFullPath();
  192.         }
  193.         return '';
  194.     }
  195.     /**
  196.      * {@inheritdoc}
  197.      */
  198.     public function getDataForSearchIndex($object$params = [])
  199.     {
  200.         return '';
  201.     }
  202.     /**
  203.      * {@inheritdoc}
  204.      */
  205.     public function getCacheTags($data, array $tags = [])
  206.     {
  207.         if ($data instanceof Asset\Image) {
  208.             if (!array_key_exists($data->getCacheTag(), $tags)) {
  209.                 $tags $data->getCacheTags($tags);
  210.             }
  211.         }
  212.         return $tags;
  213.     }
  214.     /**
  215.      * @param Asset|null $data
  216.      *
  217.      * @return array
  218.      */
  219.     public function resolveDependencies($data)
  220.     {
  221.         $dependencies = [];
  222.         if ($data instanceof Asset) {
  223.             $dependencies['asset_' $data->getId()] = [
  224.                 'id' => $data->getId(),
  225.                 'type' => 'asset',
  226.             ];
  227.         }
  228.         return $dependencies;
  229.     }
  230.     /**
  231.      * {@inheritdoc}
  232.      */
  233.     public function isDiffChangeAllowed($object$params = [])
  234.     {
  235.         return true;
  236.     }
  237.     /** Generates a pretty version preview (similar to getVersionPreview) can be either html or
  238.      * a image URL. See the https://github.com/pimcore/object-merger bundle documentation for details
  239.      *
  240.      * @param Asset\Image|null $data
  241.      * @param Model\DataObject\Concrete|null $object
  242.      * @param mixed $params
  243.      *
  244.      * @return array|string
  245.      */
  246.     public function getDiffVersionPreview($data$object null$params = [])
  247.     {
  248.         $versionPreview null;
  249.         if ($data instanceof Asset\Image) {
  250.             $versionPreview '/admin/asset/get-image-thumbnail?id=' $data->getId() . '&width=150&height=150&aspectratio=true';
  251.         }
  252.         if ($versionPreview) {
  253.             $value = [];
  254.             $value['src'] = $versionPreview;
  255.             $value['type'] = 'img';
  256.             return $value;
  257.         } else {
  258.             return '';
  259.         }
  260.     }
  261.     /**
  262.      * { @inheritdoc }
  263.      */
  264.     public function rewriteIds(/** mixed */ $container/** array */ $idMapping/** array */ $params = []) /** :mixed */
  265.     {
  266.         $data $this->getDataFromObjectParam($container$params);
  267.         if ($data instanceof Asset\Image) {
  268.             if (array_key_exists('asset'$idMapping) && array_key_exists($data->getId(), $idMapping['asset'])) {
  269.                 return Asset::getById($idMapping['asset'][$data->getId()]);
  270.             }
  271.         }
  272.         return $data;
  273.     }
  274.     /**
  275.      * @param Model\DataObject\ClassDefinition\Data\Image $mainDefinition
  276.      */
  277.     public function synchronizeWithMainDefinition(Model\DataObject\ClassDefinition\Data $mainDefinition)
  278.     {
  279.         $this->uploadPath $mainDefinition->uploadPath;
  280.     }
  281.     /**
  282.      * @deprecated will be removed in Pimcore 11
  283.      *
  284.      * @param Model\DataObject\ClassDefinition\Data\Image $masterDefinition
  285.      */
  286.     public function synchronizeWithMasterDefinition(Model\DataObject\ClassDefinition\Data $masterDefinition)
  287.     {
  288.         trigger_deprecation(
  289.             'pimcore/pimcore',
  290.             '10.6.0',
  291.             sprintf('%s is deprecated and will be removed in Pimcore 11. Use %s instead.'__METHOD__str_replace('Master''Main'__METHOD__))
  292.         );
  293.         $this->synchronizeWithMainDefinition($masterDefinition);
  294.     }
  295.     /**
  296.      * {@inheritdoc}
  297.      */
  298.     public function isFilterable(): bool
  299.     {
  300.         return true;
  301.     }
  302.     /**
  303.      * @param Asset|null $oldValue
  304.      * @param Asset|null $newValue
  305.      *
  306.      * @return bool
  307.      */
  308.     public function isEqual($oldValue$newValue): bool
  309.     {
  310.         $oldValue $oldValue instanceof Asset $oldValue->getId() : null;
  311.         $newValue $newValue instanceof Asset $newValue->getId() : null;
  312.         return $oldValue === $newValue;
  313.     }
  314.     /**
  315.      * {@inheritdoc}
  316.      */
  317.     public function getParameterTypeDeclaration(): ?string
  318.     {
  319.         return '?\\' Asset\Image::class;
  320.     }
  321.     /**
  322.      * {@inheritdoc}
  323.      */
  324.     public function getReturnTypeDeclaration(): ?string
  325.     {
  326.         return '?\\' Asset\Image::class;
  327.     }
  328.     /**
  329.      * {@inheritdoc}
  330.      */
  331.     public function getPhpdocInputType(): ?string
  332.     {
  333.         return '\\' Asset\Image::class . '|null';
  334.     }
  335.     /**
  336.      * {@inheritdoc}
  337.      */
  338.     public function getPhpdocReturnType(): ?string
  339.     {
  340.         return '\\' Asset\Image::class . '|null';
  341.     }
  342.     /**
  343.      * {@inheritdoc}
  344.      */
  345.     public function normalize($value$params = [])
  346.     {
  347.         if ($value instanceof \Pimcore\Model\Asset\Image) {
  348.             return [
  349.                 'type' => 'asset',
  350.                 'id' => $value->getId(),
  351.             ];
  352.         }
  353.         return null;
  354.     }
  355.     /**
  356.      * {@inheritdoc}
  357.      */
  358.     public function denormalize($value$params = [])
  359.     {
  360.         if (isset($value['id'])) {
  361.             return Asset\Image::getById($value['id']);
  362.         }
  363.         return null;
  364.     }
  365.     /**
  366.      * Filter by relation feature
  367.      *
  368.      * @param array|string|null $value
  369.      * @param string            $operator
  370.      * @param array             $params
  371.      *
  372.      * @return string
  373.      */
  374.     public function getFilterConditionExt($value$operator$params = [])
  375.     {
  376.         $name $params['name'] ?: $this->name;
  377.         return $this->getRelationFilterCondition($value$operator$name);
  378.     }
  379. }