Beispiel #1
0
 /**
  * Generate the content element
  */
 protected function compile()
 {
     /** @var PageModel $objPage */
     global $objPage;
     $images = array();
     $auxDate = array();
     $objFiles = $this->objFiles;
     // Get all images
     while ($objFiles->next()) {
         // Continue if the files has been processed or does not exist
         if (isset($images[$objFiles->path]) || !file_exists(TL_ROOT . '/' . $objFiles->path)) {
             continue;
         }
         // Single files
         if ($objFiles->type == 'file') {
             $objFile = new \File($objFiles->path);
             if (!$objFile->isImage) {
                 continue;
             }
             $arrMeta = $this->getMetaData($objFiles->meta, $objPage->language);
             if (empty($arrMeta)) {
                 if ($this->metaIgnore) {
                     continue;
                 } elseif ($objPage->rootFallbackLanguage !== null) {
                     $arrMeta = $this->getMetaData($objFiles->meta, $objPage->rootFallbackLanguage);
                 }
             }
             // Use the file name as title if none is given
             if ($arrMeta['title'] == '') {
                 $arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
             }
             // Add the image
             $images[$objFiles->path] = array('id' => $objFiles->id, 'uuid' => $objFiles->uuid, 'name' => $objFile->basename, 'singleSRC' => $objFiles->path, 'title' => \StringUtil::specialchars($arrMeta['title']), 'alt' => \StringUtil::specialchars($arrMeta['alt']), 'imageUrl' => $arrMeta['link'], 'caption' => $arrMeta['caption']);
             $auxDate[] = $objFile->mtime;
         } else {
             $objSubfiles = \FilesModel::findByPid($objFiles->uuid);
             if ($objSubfiles === null) {
                 continue;
             }
             while ($objSubfiles->next()) {
                 // Skip subfolders
                 if ($objSubfiles->type == 'folder') {
                     continue;
                 }
                 $objFile = new \File($objSubfiles->path);
                 if (!$objFile->isImage) {
                     continue;
                 }
                 $arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->language);
                 if (empty($arrMeta)) {
                     if ($this->metaIgnore) {
                         continue;
                     } elseif ($objPage->rootFallbackLanguage !== null) {
                         $arrMeta = $this->getMetaData($objSubfiles->meta, $objPage->rootFallbackLanguage);
                     }
                 }
                 // Use the file name as title if none is given
                 if ($arrMeta['title'] == '') {
                     $arrMeta['title'] = \StringUtil::specialchars($objFile->basename);
                 }
                 // Add the image
                 $images[$objSubfiles->path] = array('id' => $objSubfiles->id, 'uuid' => $objSubfiles->uuid, 'name' => $objFile->basename, 'singleSRC' => $objSubfiles->path, 'title' => \StringUtil::specialchars($arrMeta['title']), 'alt' => \StringUtil::specialchars($arrMeta['alt']), 'imageUrl' => $arrMeta['link'], 'caption' => $arrMeta['caption']);
                 $auxDate[] = $objFile->mtime;
             }
         }
     }
     // Sort array
     switch ($this->sortBy) {
         default:
         case 'name_asc':
             uksort($images, 'basename_natcasecmp');
             break;
         case 'name_desc':
             uksort($images, 'basename_natcasercmp');
             break;
         case 'date_asc':
             array_multisort($images, SORT_NUMERIC, $auxDate, SORT_ASC);
             break;
         case 'date_desc':
             array_multisort($images, SORT_NUMERIC, $auxDate, SORT_DESC);
             break;
             // Deprecated since Contao 4.0, to be removed in Contao 5.0
         // Deprecated since Contao 4.0, to be removed in Contao 5.0
         case 'meta':
             @trigger_error('The "meta" key in ContentGallery::compile() has been deprecated and will no longer work in Contao 5.0.', E_USER_DEPRECATED);
             // no break;
         // no break;
         case 'custom':
             if ($this->orderSRC != '') {
                 $tmp = \StringUtil::deserialize($this->orderSRC);
                 if (!empty($tmp) && is_array($tmp)) {
                     // Remove all values
                     $arrOrder = array_map(function () {
                     }, array_flip($tmp));
                     // Move the matching elements to their position in $arrOrder
                     foreach ($images as $k => $v) {
                         if (array_key_exists($v['uuid'], $arrOrder)) {
                             $arrOrder[$v['uuid']] = $v;
                             unset($images[$k]);
                         }
                     }
                     // Append the left-over images at the end
                     if (!empty($images)) {
                         $arrOrder = array_merge($arrOrder, array_values($images));
                     }
                     // Remove empty (unreplaced) entries
                     $images = array_values(array_filter($arrOrder));
                     unset($arrOrder);
                 }
             }
             break;
         case 'random':
             shuffle($images);
             $this->Template->isRandomOrder = true;
             break;
     }
     $images = array_values($images);
     // Limit the total number of items (see #2652)
     if ($this->numberOfItems > 0) {
         $images = array_slice($images, 0, $this->numberOfItems);
     }
     $offset = 0;
     $total = count($images);
     $limit = $total;
     // Paginate the result of not randomly sorted (see #8033)
     if ($this->perPage > 0 && $this->sortBy != 'random') {
         // Get the current page
         $id = 'page_g' . $this->id;
         $page = \Input::get($id) !== null ? \Input::get($id) : 1;
         // Do not index or cache the page if the page number is outside the range
         if ($page < 1 || $page > max(ceil($total / $this->perPage), 1)) {
             throw new PageNotFoundException('Page not found: ' . \Environment::get('uri'));
         }
         // Set limit and offset
         $offset = ($page - 1) * $this->perPage;
         $limit = min($this->perPage + $offset, $total);
         $objPagination = new \Pagination($total, $this->perPage, \Config::get('maxPaginationLinks'), $id);
         $this->Template->pagination = $objPagination->generate("\n  ");
     }
     $rowcount = 0;
     $colwidth = floor(100 / $this->perRow);
     $intMaxWidth = TL_MODE == 'BE' ? floor(640 / $this->perRow) : floor(\Config::get('maxImageWidth') / $this->perRow);
     $strLightboxId = 'lightbox[lb' . $this->id . ']';
     $body = array();
     // Rows
     for ($i = $offset; $i < $limit; $i = $i + $this->perRow) {
         $class_tr = '';
         if ($rowcount == 0) {
             $class_tr .= ' row_first';
         }
         if ($i + $this->perRow >= $limit) {
             $class_tr .= ' row_last';
         }
         $class_eo = $rowcount % 2 == 0 ? ' even' : ' odd';
         // Columns
         for ($j = 0; $j < $this->perRow; $j++) {
             $class_td = '';
             if ($j == 0) {
                 $class_td .= ' col_first';
             }
             if ($j == $this->perRow - 1) {
                 $class_td .= ' col_last';
             }
             $objCell = new \stdClass();
             $key = 'row_' . $rowcount . $class_tr . $class_eo;
             // Empty cell
             if (!is_array($images[$i + $j]) || $j + $i >= $limit) {
                 $objCell->colWidth = $colwidth . '%';
                 $objCell->class = 'col_' . $j . $class_td;
             } else {
                 // Add size and margin
                 $images[$i + $j]['size'] = $this->size;
                 $images[$i + $j]['imagemargin'] = $this->imagemargin;
                 $images[$i + $j]['fullsize'] = $this->fullsize;
                 $this->addImageToTemplate($objCell, $images[$i + $j], $intMaxWidth, $strLightboxId);
                 // Add column width and class
                 $objCell->colWidth = $colwidth . '%';
                 $objCell->class = 'col_' . $j . $class_td;
             }
             $body[$key][$j] = $objCell;
         }
         ++$rowcount;
     }
     $strTemplate = 'gallery_default';
     // Use a custom template
     if (TL_MODE == 'FE' && $this->galleryTpl != '') {
         $strTemplate = $this->galleryTpl;
     }
     /** @var FrontendTemplate|object $objTemplate */
     $objTemplate = new \FrontendTemplate($strTemplate);
     $objTemplate->setData($this->arrData);
     $objTemplate->body = $body;
     $objTemplate->headline = $this->headline;
     // see #1603
     $this->Template->images = $objTemplate->parse();
 }