diff --git a/src/Image/Image.php b/src/Image/Image.php index 0be0bbd5..96baab1f 100644 --- a/src/Image/Image.php +++ b/src/Image/Image.php @@ -18,9 +18,16 @@ class Image { - public function addToTemplateData(string $imageField, string $imageSelectorField, array &$templateData, array $item, int $maxWidth = null, string $lightboxId = null, - string $lightboxName = null, FilesModel $model = null) - { + public function addToTemplateData( + string $imageField, + string $imageSelectorField, + array &$templateData, + array $item, + int $maxWidth = null, + string $lightboxId = null, + string $lightboxName = null, + FilesModel $model = null + ) { $containerUtil = System::getContainer()->get('huh.utils.container'); try { @@ -81,8 +88,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField } try { - $src = - System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT.'/'.$item[$imageField], $size)->getUrl(TL_ROOT); + $src = System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT.'/'.$item[$imageField], $size)->getUrl(TL_ROOT); $picture = System::getContainer()->get('contao.image.picture_factory')->create(TL_ROOT.'/'.$item[$imageField], $size); $picture = [ @@ -101,7 +107,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField } // Image dimensions - if ($file->exists() && false !== ($imgSize = $file->imageSize)) { + if (false !== ($imgSize = $file->imageSize) && $file->exists()) { $templateData['arrSize'] = $imgSize; $templateData['imgSize'] = ' width="'.$imgSize[0].'" height="'.$imgSize[1].'"'; } @@ -197,9 +203,7 @@ public function addToTemplateData(string $imageField, string $imageSelectorField $templateData['attributes'] = ' target="_blank"'; } } - } - - // Fullsize view + } // Fullsize view elseif ($item['fullsize'] && $containerUtil->isFrontend()) { $templateData[$hrefKey] = TL_FILES_URL.System::urlEncode($item[$imageField]); $templateData['attributes'] = ' data-lightbox="'.substr($lightboxId, 9, -1).'"'; diff --git a/src/Url/UrlUtil.php b/src/Url/UrlUtil.php index 397ccce4..94a1ffb7 100644 --- a/src/Url/UrlUtil.php +++ b/src/Url/UrlUtil.php @@ -135,67 +135,31 @@ public function getJumpToPageObject(int $jumpTo, bool $fallbackToObjPage = true) } /** - * Prepare URL from ID and keep query string from current string. - * - * @param string|int|null - * - * @return string - */ - protected function prepareUrl($url) - { - if (null === $url) { - $url = Environment::get('requestUri'); - } elseif (is_numeric($url)) { - if (null === ($jumpTo = PageModel::findByPk($url))) { - throw new \InvalidArgumentException('Given page id does not exist.'); - } - - $url = Controller::generateFrontendUrl($jumpTo->row()); - - list(, $queryString) = explode('?', Environment::get('request'), 2); - - if ('' != $queryString) { - $url .= '?'.$queryString; - } - } - - $url = ampersand($url, false); - - return $url; - } - - /** - * Redirect to another page + * Redirect to another page. * - * @param string $strLocation The target URL - * @param integer $intStatus The HTTP status code (defaults to 303) + * @param string $strLocation The target URL + * @param int $intStatus The HTTP status code (defaults to 303) */ - public function redirect($strLocation, $intStatus=303) + public function redirect($strLocation, $intStatus = 303) { - if (headers_sent()) - { + if (headers_sent()) { exit; } $strLocation = str_replace('&', '&', $strLocation); // Make the location an absolute URL - if (!preg_match('@^https?://@i', $strLocation)) - { - $strLocation = \Environment::get('base') . ltrim($strLocation, '/'); + if (!preg_match('@^https?://@i', $strLocation)) { + $strLocation = \Environment::get('base').ltrim($strLocation, '/'); } // Ajax request - if (\Environment::get('isAjaxRequest')) - { + if (\Environment::get('isAjaxRequest')) { header('HTTP/1.1 204 No Content'); - header('X-Ajax-Location: ' . $strLocation); - } - else - { + header('X-Ajax-Location: '.$strLocation); + } else { // Add the HTTP header - switch ($intStatus) - { + switch ($intStatus) { case 301: header('HTTP/1.1 301 Moved Permanently'); break; @@ -213,10 +177,39 @@ public function redirect($strLocation, $intStatus=303) break; } - header('Location: ' . $strLocation); + header('Location: '.$strLocation); } exit; } -} + /** + * Prepare URL from ID and keep query string from current string. + * + * @param string|int|null + * + * @return string + */ + protected function prepareUrl($url) + { + if (null === $url) { + $url = Environment::get('requestUri'); + } elseif (is_numeric($url)) { + if (null === ($jumpTo = PageModel::findByPk($url))) { + throw new \InvalidArgumentException('Given page id does not exist.'); + } + + $url = Controller::generateFrontendUrl($jumpTo->row()); + + list(, $queryString) = explode('?', Environment::get('request'), 2); + + if ('' != $queryString) { + $url .= '?'.$queryString; + } + } + + $url = ampersand($url, false); + + return $url; + } +} diff --git a/tests/Image/ImageTest.php b/tests/Image/ImageTest.php index fc8f4257..94d7ac36 100644 --- a/tests/Image/ImageTest.php +++ b/tests/Image/ImageTest.php @@ -62,10 +62,12 @@ public function testAddToTemplateDataWithoutModel() $imageArray['singleSRC'] = __DIR__.'/../data/screenshot.jpg'; $imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}'; $imageArray['alt'] = ''; - $imageArray['fullsize'] = false; + $imageArray['fullsize'] = true; $imageArray['floating'] = false; $imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg'; - $imageArray['linkTitle'] = true; + $imageArray['imageTitle'] = 'imageTitle'; + $imageArray['linkTitle'] = false; + $imageArray['id'] = 12; $templateData['href'] = true; $templateData['singleSRC'] = []; @@ -75,6 +77,9 @@ public function testAddToTemplateDataWithoutModel() $this->assertNotSame(['href' => true, 'singleSRC' => []], $templateData); $this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['singleSRC']); + $this->assertSame('imageTitle', $templateData['linkTitle']); + $this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['imageHref']); + $this->assertSame(' data-lightbox="5dc05b"', $templateData['attributes']); } public function testAddToTemplateDataWithModel() @@ -91,9 +96,9 @@ public function testAddToTemplateDataWithModel() $imageArray['size'] = 'a:3:{i:0;s:0:"2";i:1;s:0:"2";i:2;s:0:"2";}'; $imageArray['alt'] = ''; $imageArray['fullsize'] = true; - $imageArray['floating'] = false; $imageArray['imageUrl'] = __DIR__.'/../data/screenshot.jpg'; $imageArray['linkTitle'] = 'linkTitle'; + $imageArray['floating'] = 'floating'; $imageArray['overwriteMeta'] = false; $imageArray['caption'] = []; $imageArray['id'] = 12; @@ -111,6 +116,7 @@ public function testAddToTemplateDataWithModel() $this->assertSame(__DIR__.'/../data/screenshot.jpg', $templateData['singleSRC']); $this->assertSame('margin:10px;', $templateData['margin']); $this->assertSame('Diebstahl', $templateData['imageTitle']); + $this->assertSame(' float_floating', $templateData['floatClass']); } public function testAddToTemplateDataError() @@ -150,5 +156,18 @@ public function testAddToTemplateDataError() $image = new Image(); $image->addToTemplateData('singleSRC', 'addImage', $templateData, $imageArray, 400, 12, 'lightBoxName', $model); $this->assertSame('', $templateData['src']); + + $imageArray['singleSRC'] = ''; + $templateData = []; + $templateData['href'] = true; + $imageArray['overwriteMeta'] = true; + $imageArray['fullsize'] = true; + $imageArray['imageUrl'] = __DIR__.'/../data/screensho'; + $templateData['singleSRC'] = []; + + $image->addToTemplateData('singleSRC', 'addImage', $templateData, $imageArray, 400, 12, 'lightBoxName', $model); + $this->assertNull($templateData['width']); + $this->assertNull($templateData['height']); + $this->assertSame(' target="_blank"', $templateData['attributes']); } }