vendor/contao/core-bundle/src/Resources/contao/modules/ModuleRandomImage.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Contao\Model\Collection;
  11. /**
  12.  * Front end module "random image".
  13.  */
  14. class ModuleRandomImage extends Module
  15. {
  16.     /**
  17.      * Files object
  18.      * @var Collection|FilesModel
  19.      */
  20.     protected $objFiles;
  21.     /**
  22.      * Template
  23.      * @var string
  24.      */
  25.     protected $strTemplate 'mod_randomImage';
  26.     /**
  27.      * Check the source folder
  28.      *
  29.      * @return string
  30.      */
  31.     public function generate()
  32.     {
  33.         $this->multiSRC StringUtil::deserialize($this->multiSRC);
  34.         if (empty($this->multiSRC) || !\is_array($this->multiSRC))
  35.         {
  36.             return '';
  37.         }
  38.         $this->objFiles FilesModel::findMultipleByUuids($this->multiSRC);
  39.         if ($this->objFiles === null)
  40.         {
  41.             return '';
  42.         }
  43.         return parent::generate();
  44.     }
  45.     /**
  46.      * Generate the module
  47.      */
  48.     protected function compile()
  49.     {
  50.         $images = array();
  51.         $objFiles $this->objFiles;
  52.         // Get all images
  53.         while ($objFiles->next())
  54.         {
  55.             // Continue if the files has been processed or does not exist
  56.             if (isset($images[$objFiles->path]) || !file_exists(System::getContainer()->getParameter('kernel.project_dir') . '/' $objFiles->path))
  57.             {
  58.                 continue;
  59.             }
  60.             // Single files
  61.             if ($objFiles->type == 'file')
  62.             {
  63.                 $objFile = new File($objFiles->path);
  64.                 if (!$objFile->isImage)
  65.                 {
  66.                     continue;
  67.                 }
  68.                 // Add the image
  69.                 $images[$objFiles->path] = $objFiles->current();
  70.             }
  71.             // Folders
  72.             else
  73.             {
  74.                 $objSubfiles FilesModel::findByPid($objFiles->uuid, array('order' => 'name'));
  75.                 if ($objSubfiles === null)
  76.                 {
  77.                     continue;
  78.                 }
  79.                 while ($objSubfiles->next())
  80.                 {
  81.                     // Skip subfolders
  82.                     if ($objSubfiles->type == 'folder')
  83.                     {
  84.                         continue;
  85.                     }
  86.                     $objFile = new File($objSubfiles->path);
  87.                     if (!$objFile->isImage)
  88.                     {
  89.                         continue;
  90.                     }
  91.                     // Add the image
  92.                     $images[$objSubfiles->path] = $objSubfiles->current();
  93.                 }
  94.             }
  95.         }
  96.         if (empty($images))
  97.         {
  98.             return;
  99.         }
  100.         $figure System::getContainer()
  101.             ->get('contao.image.studio')
  102.             ->createFigureBuilder()
  103.             ->fromFilesModel($images[array_rand($images)])
  104.             ->setSize($this->imgSize)
  105.             ->enableLightbox((bool) $this->fullsize)
  106.             ->build();
  107.         $imageData $figure->getLegacyTemplateData();
  108.         $imageData['figure'] = $figure;
  109.         $this->Template->setData(array_merge(
  110.             $this->Template->getData(),
  111.             $imageData,
  112.             array('caption' => $this->useCaption $imageData['caption'] ?? '' null)
  113.         ));
  114.     }
  115. }
  116. class_alias(ModuleRandomImage::class, 'ModuleRandomImage');