vendor/pimcore/pimcore/models/Asset/Folder.php line 80

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\Asset;
  15. use Pimcore\Db\Helper;
  16. use Pimcore\File;
  17. use Pimcore\Messenger\AssetPreviewImageMessage;
  18. use Pimcore\Model;
  19. use Pimcore\Model\Asset;
  20. use Pimcore\Tool\Storage;
  21. /**
  22.  * @method \Pimcore\Model\Asset\Dao getDao()
  23.  */
  24. class Folder extends Model\Asset
  25. {
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     protected $type 'folder';
  30.     /**
  31.      * @internal
  32.      *
  33.      * @var Asset[]|null
  34.      */
  35.     protected $children;
  36.     /**
  37.      * @internal
  38.      *
  39.      * @var bool|null
  40.      */
  41.     protected $hasChildren;
  42.     /**
  43.      * set the children of the document
  44.      *
  45.      * @param Asset[]|null $children
  46.      *
  47.      * @return $this
  48.      */
  49.     public function setChildren($children)
  50.     {
  51.         $this->children $children;
  52.         if (is_array($children) && count($children) > 0) {
  53.             $this->hasChildren true;
  54.         } else {
  55.             $this->hasChildren false;
  56.         }
  57.         return $this;
  58.     }
  59.     /**
  60.      * @return Asset[]
  61.      */
  62.     public function getChildren()
  63.     {
  64.         if ($this->children === null) {
  65.             if ($this->getId()) {
  66.                 $list = new Asset\Listing();
  67.                 $list->setCondition('parentId = ?'$this->getId());
  68.                 $list->setOrderKey('filename');
  69.                 $list->setOrder('asc');
  70.                 $this->children $list->getAssets();
  71.             } else {
  72.                 $this->children = [];
  73.             }
  74.         }
  75.         return $this->children;
  76.     }
  77.     /**
  78.      * @return bool
  79.      */
  80.     public function hasChildren()
  81.     {
  82.         if (is_bool($this->hasChildren)) {
  83.             if (($this->hasChildren && empty($this->children)) || (!$this->hasChildren && !empty($this->children))) {
  84.                 return $this->getDao()->hasChildren();
  85.             }
  86.             return $this->hasChildren;
  87.         }
  88.         return $this->getDao()->hasChildren();
  89.     }
  90.     /**
  91.      * @internal
  92.      *
  93.      * @param bool $force
  94.      *
  95.      * @return resource|null
  96.      *
  97.      * @throws \Doctrine\DBAL\Exception
  98.      * @throws \League\Flysystem\FilesystemException
  99.      */
  100.     public function getPreviewImage(bool $force false)
  101.     {
  102.         $storage Storage::get('thumbnail');
  103.         $cacheFilePath sprintf(
  104.             '%s/%s/image-thumb__%s__-folder-preview%s.jpg',
  105.             rtrim($this->getRealPath(), '/'),
  106.             $this->getId(),
  107.             $this->getId(),
  108.             '-hdpi'
  109.         );
  110.         $tileThumbnailConfig Asset\Image\Thumbnail\Config::getPreviewConfig();
  111.         $limit 42;
  112.         $db \Pimcore\Db::get();
  113.         $condition "path LIKE :path AND type IN ('image', 'video', 'document')";
  114.         $conditionParams = [
  115.             'path' => Helper::escapeLike($this->getRealFullPath()) . '/%',
  116.         ];
  117.         if ($storage->fileExists($cacheFilePath)) {
  118.             $lastUpdate $db->fetchOne('SELECT MAX(modificationDate) FROM assets WHERE ' $condition ' ORDER BY filename ASC LIMIT ' $limit$conditionParams);
  119.             if ($lastUpdate $storage->lastModified($cacheFilePath)) {
  120.                 return $storage->readStream($cacheFilePath);
  121.             }
  122.         }
  123.         $list = new Asset\Listing();
  124.         $list->setCondition($condition$conditionParams);
  125.         $list->setOrderKey('id');
  126.         $list->setOrder('asc');
  127.         $list->setLimit($limit);
  128.         $totalImages $list->getCount();
  129.         $count 0;
  130.         $gutter 5;
  131.         $squareDimension 130;
  132.         $offsetTop 0;
  133.         $colums 3;
  134.         $skipped false;
  135.         if ($totalImages) {
  136.             $collage imagecreatetruecolor(($squareDimension $colums) + ($gutter * ($colums 1)), (int) ceil(($totalImages $colums)) * ($squareDimension $gutter));
  137.             $background imagecolorallocate($collage121518);
  138.             imagefill($collage00$background);
  139.             foreach ($list as $asset) {
  140.                 if ($asset instanceof Document && !$asset->getPageCount()) {
  141.                     continue;
  142.                 }
  143.                 $offsetLeft = ($squareDimension $gutter) * ($count $colums);
  144.                 $tileThumb null;
  145.                 if ($asset instanceof Image) {
  146.                     $tileThumb $asset->getThumbnail($tileThumbnailConfig);
  147.                 } elseif ($asset instanceof Document || $asset instanceof Video) {
  148.                     $tileThumb $asset->getImageThumbnail($tileThumbnailConfig);
  149.                 }
  150.                 if ($tileThumb) {
  151.                     if (!$tileThumb->exists() && !$force) {
  152.                         // only generate if all necessary thumbs are available
  153.                         $skipped true;
  154.                         \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  155.                             new AssetPreviewImageMessage($this->getId())
  156.                         );
  157.                         break;
  158.                     }
  159.                     $stream $tileThumb->getStream();
  160.                     if (null === $stream) {
  161.                         break;
  162.                     }
  163.                     $width $tileThumb->getWidth();
  164.                     $height $tileThumb->getHeight();
  165.                     if (!$width || !$height) {
  166.                         break;
  167.                     }
  168.                     $tile imagecreatefromstring(stream_get_contents($stream));
  169.                     imagecopyresampled($collage$tile$offsetLeft$offsetTop00$squareDimension$squareDimension$width$height);
  170.                     $count++;
  171.                     if ($count $colums === 0) {
  172.                         $offsetTop += ($squareDimension $gutter);
  173.                     }
  174.                 }
  175.             }
  176.             if ($count && !$skipped) {
  177.                 $localFile File::getLocalTempFilePath('jpg');
  178.                 imagejpeg($collage$localFile60);
  179.                 if (filesize($localFile) > 0) {
  180.                     $storage->write($cacheFilePathfile_get_contents($localFile));
  181.                 }
  182.                 return $storage->readStream($cacheFilePath);
  183.             }
  184.         }
  185.         return null;
  186.     }
  187. }