vendor/contao/core-bundle/src/Resources/contao/elements/ContentGallery.php line 75

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\CoreBundle\Exception\PageNotFoundException;
  11. use Contao\Model\Collection;
  12. /**
  13.  * Front end content element "gallery".
  14.  */
  15. class ContentGallery extends ContentElement
  16. {
  17.     /**
  18.      * Files object
  19.      * @var Collection|FilesModel
  20.      */
  21.     protected $objFiles;
  22.     /**
  23.      * Template
  24.      * @var string
  25.      */
  26.     protected $strTemplate 'ce_gallery';
  27.     /**
  28.      * Return if there are no files
  29.      *
  30.      * @return string
  31.      */
  32.     public function generate()
  33.     {
  34.         // Use the home directory of the current user as file source
  35.         if ($this->useHomeDir && System::getContainer()->get('contao.security.token_checker')->hasFrontendUser())
  36.         {
  37.             $this->import(FrontendUser::class, 'User');
  38.             if ($this->User->assignDir && $this->User->homeDir)
  39.             {
  40.                 $this->multiSRC = array($this->User->homeDir);
  41.             }
  42.         }
  43.         else
  44.         {
  45.             $this->multiSRC StringUtil::deserialize($this->multiSRC);
  46.         }
  47.         // Return if there are no files
  48.         if (empty($this->multiSRC) || !\is_array($this->multiSRC))
  49.         {
  50.             return '';
  51.         }
  52.         // Get the file entries from the database
  53.         $this->objFiles FilesModel::findMultipleByUuids($this->multiSRC);
  54.         if ($this->objFiles === null)
  55.         {
  56.             return '';
  57.         }
  58.         return parent::generate();
  59.     }
  60.     /**
  61.      * Generate the content element
  62.      */
  63.     protected function compile()
  64.     {
  65.         $images = array();
  66.         $auxDate = array();
  67.         $objFiles $this->objFiles;
  68.         // Get all images
  69.         while ($objFiles->next())
  70.         {
  71.             // Continue if the files has been processed or does not exist
  72.             if (isset($images[$objFiles->path]) || !file_exists(System::getContainer()->getParameter('kernel.project_dir') . '/' $objFiles->path))
  73.             {
  74.                 continue;
  75.             }
  76.             // Single files
  77.             if ($objFiles->type == 'file')
  78.             {
  79.                 $objFile = new File($objFiles->path);
  80.                 if (!$objFile->isImage)
  81.                 {
  82.                     continue;
  83.                 }
  84.                 // Add the image
  85.                 $images[$objFiles->path] = $objFiles->current();
  86.                 $auxDate[] = $objFile->mtime;
  87.             }
  88.             // Folders
  89.             else
  90.             {
  91.                 $objSubfiles FilesModel::findByPid($objFiles->uuid, array('order' => 'name'));
  92.                 if ($objSubfiles === null)
  93.                 {
  94.                     continue;
  95.                 }
  96.                 while ($objSubfiles->next())
  97.                 {
  98.                     // Skip subfolders
  99.                     if ($objSubfiles->type == 'folder')
  100.                     {
  101.                         continue;
  102.                     }
  103.                     $objFile = new File($objSubfiles->path);
  104.                     if (!$objFile->isImage)
  105.                     {
  106.                         continue;
  107.                     }
  108.                     // Add the image
  109.                     $images[$objSubfiles->path] = $objSubfiles->current();
  110.                     $auxDate[] = $objFile->mtime;
  111.                 }
  112.             }
  113.         }
  114.         // Sort array
  115.         switch ($this->sortBy)
  116.         {
  117.             default:
  118.             case 'name_asc':
  119.                 uksort($images, static function ($a$b): int
  120.                 {
  121.                     return strnatcasecmp(basename($a), basename($b));
  122.                 });
  123.                 break;
  124.             case 'name_desc':
  125.                 uksort($images, static function ($a$b): int
  126.                 {
  127.                     return -strnatcasecmp(basename($a), basename($b));
  128.                 });
  129.                 break;
  130.             case 'date_asc':
  131.                 array_multisort($imagesSORT_NUMERIC$auxDateSORT_ASC);
  132.                 break;
  133.             case 'date_desc':
  134.                 array_multisort($imagesSORT_NUMERIC$auxDateSORT_DESC);
  135.                 break;
  136.             // Deprecated since Contao 4.0, to be removed in Contao 5.0
  137.             case 'meta':
  138.                 trigger_deprecation('contao/core-bundle''4.0''The "meta" key in "Contao\ContentGallery::compile()" has been deprecated and will no longer work in Contao 5.0.');
  139.                 // no break
  140.             case 'custom':
  141.                 $images ArrayUtil::sortByOrderField($images$this->orderSRC);
  142.                 break;
  143.             case 'random':
  144.                 shuffle($images);
  145.                 $this->Template->isRandomOrder true;
  146.                 break;
  147.         }
  148.         $images array_values($images);
  149.         // Limit the total number of items (see #2652)
  150.         if ($this->numberOfItems 0)
  151.         {
  152.             $images = \array_slice($images0$this->numberOfItems);
  153.         }
  154.         $offset 0;
  155.         $total = \count($images);
  156.         $limit $total;
  157.         // Paginate the result of not randomly sorted (see #8033)
  158.         if ($this->perPage && $this->sortBy != 'random')
  159.         {
  160.             // Get the current page
  161.             $id 'page_g' $this->id;
  162.             $page Input::get($id) ?? 1;
  163.             // Do not index or cache the page if the page number is outside the range
  164.             if ($page || $page max(ceil($total/$this->perPage), 1))
  165.             {
  166.                 throw new PageNotFoundException('Page not found: ' Environment::get('uri'));
  167.             }
  168.             // Set limit and offset
  169.             $offset = ($page 1) * $this->perPage;
  170.             $limit min($this->perPage $offset$total);
  171.             $objPagination = new Pagination($total$this->perPageConfig::get('maxPaginationLinks'), $id);
  172.             $this->Template->pagination $objPagination->generate("\n  ");
  173.         }
  174.         $rowcount 0;
  175.         $colwidth floor(100/$this->perRow);
  176.         $body = array();
  177.         $figureBuilder System::getContainer()
  178.             ->get('contao.image.studio')
  179.             ->createFigureBuilder()
  180.             ->setSize($this->size)
  181.             ->setLightboxGroupIdentifier('lb' $this->id)
  182.             ->enableLightbox((bool) $this->fullsize);
  183.         // Rows
  184.         for ($i=$offset$i<$limit$i+=$this->perRow)
  185.         {
  186.             $class_tr '';
  187.             if ($rowcount == 0)
  188.             {
  189.                 $class_tr .= ' row_first';
  190.             }
  191.             if (($i $this->perRow) >= $limit)
  192.             {
  193.                 $class_tr .= ' row_last';
  194.             }
  195.             $class_eo = (($rowcount 2) == 0) ? ' even' ' odd';
  196.             // Columns
  197.             for ($j=0$j<$this->perRow$j++)
  198.             {
  199.                 $class_td '';
  200.                 if ($j == 0)
  201.                 {
  202.                     $class_td .= ' col_first';
  203.                 }
  204.                 if ($j == ($this->perRow 1))
  205.                 {
  206.                     $class_td .= ' col_last';
  207.                 }
  208.                 // Image / empty cell
  209.                 if (($j $i) < $limit && null !== ($image $images[$i $j] ?? null))
  210.                 {
  211.                     $figure $figureBuilder
  212.                         ->fromFilesModel($image)
  213.                         ->build();
  214.                     $cellData $figure->getLegacyTemplateData($this->imagemargin);
  215.                     $cellData['figure'] = $figure;
  216.                 }
  217.                 else
  218.                 {
  219.                     $cellData = array('addImage' => false);
  220.                 }
  221.                 // Add column width and class
  222.                 $cellData['colWidth'] = $colwidth '%';
  223.                 $cellData['class'] = 'col_' $j $class_td;
  224.                 $body['row_' $rowcount $class_tr $class_eo][$j] = (object) $cellData;
  225.             }
  226.             ++$rowcount;
  227.         }
  228.         $request System::getContainer()->get('request_stack')->getCurrentRequest();
  229.         // Always use the default template in the back end
  230.         if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  231.         {
  232.             $this->galleryTpl '';
  233.         }
  234.         $objTemplate = new FrontendTemplate($this->galleryTpl ?: 'gallery_default');
  235.         $objTemplate->setData($this->arrData);
  236.         $objTemplate->body $body;
  237.         $objTemplate->headline $this->headline// see #1603
  238.         $this->Template->images $objTemplate->parse();
  239.     }
  240. }
  241. class_alias(ContentGallery::class, 'ContentGallery');