Initial commit

This commit is contained in:
2018-04-02 08:07:38 +02:00
commit 7330c1ed3e
2054 changed files with 405203 additions and 0 deletions

View File

@@ -0,0 +1,188 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Utility\ResourceUtility;
use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
/**
* Base class for image related view helpers adapted from FLUID
* original image viewhelper.
*
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
abstract class AbstractImageViewHelper extends AbstractResourceViewHelper {
/**
* @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
*/
protected $tsfeBackup;
/**
* @var string
*/
protected $workingDirectoryBackup;
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
*/
protected $configurationManager;
/**
* @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
*/
protected $contentObject;
/**
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
* @return void
*/
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) {
$this->configurationManager = $configurationManager;
$this->contentObject = $this->configurationManager->getContentObject();
}
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('relative', 'boolean', 'If FALSE resource URIs are rendered absolute. URIs in backend mode are always absolute.', FALSE, TRUE);
$this->registerArgument('width', 'string', 'Width of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
$this->registerArgument('height', 'string', 'Height of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
$this->registerArgument('minWidth', 'string', 'Minimum width of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
$this->registerArgument('minHeight', 'string', 'Minimum height of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
$this->registerArgument('maxWidth', 'string', 'Maximum width of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
$this->registerArgument('maxHeight', 'string', 'Maximum height of the image. Numeric value in pixels or simple calculations. See imgResource.width for possible options.', FALSE, NULL);
}
/**
* @param array $files
* @param boolean $onlyProperties
* @throws Exception
* @return array|NULL
*/
public function preprocessImages($files, $onlyProperties = FALSE) {
if (TRUE === empty($files)) {
return NULL;
}
if ('BE' === TYPO3_MODE) {
$this->simulateFrontendEnvironment();
}
$setup = array(
'width' => $this->arguments['width'],
'height' => $this->arguments['height'],
'minW' => $this->arguments['minWidth'],
'minH' => $this->arguments['minHeight'],
'maxW' => $this->arguments['maxWidth'],
'maxH' => $this->arguments['maxHeight'],
'treatIdAsReference' => FALSE
);
$images = array();
foreach ($files as $file) {
$imageInfo = $this->contentObject->getImgResource($file->getUid(), $setup);
$GLOBALS['TSFE']->lastImageInfo = $imageInfo;
if (FALSE === is_array($imageInfo)) {
throw new Exception('Could not get image resource for "' . htmlspecialchars($file->getCombinedIdentifier()) . '".', 1253191060);
}
if ((float) substr(TYPO3_version, 0, 3) < 7.1) {
$imageInfo[3] = GeneralUtility::png_to_gif_by_imagemagick($imageInfo[3]);
} else {
$imageInfo[3] = GraphicalFunctions::pngToGifByImagemagick($imageInfo[3]);
}
$imageInfo[3] = GeneralUtility::png_to_gif_by_imagemagick($imageInfo[3]);
$GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
if (TRUE === GeneralUtility::isValidUrl($imageInfo[3])) {
$imageSource = $imageInfo[3];
} else {
$imageSource = $GLOBALS['TSFE']->absRefPrefix . GeneralUtility::rawUrlEncodeFP($imageInfo[3]);
}
if (TRUE === $onlyProperties) {
$file = ResourceUtility::getFileArray($file);
}
$images[] = array(
'info' => $imageInfo,
'source' => $imageSource,
'file' => $file
);
}
if ('BE' === TYPO3_MODE) {
$this->resetFrontendEnvironment();
}
return $images;
}
/**
* Prepares $GLOBALS['TSFE'] for Backend mode
* This somewhat hacky work around is currently needed because the getImgResource() function of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on those variables to be set
*
* @return void
*/
protected function simulateFrontendEnvironment() {
$this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
$this->workingDirectoryBackup = getcwd();
chdir(constant('PATH_site'));
$typoScriptSetup = $this->configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$GLOBALS['TSFE'] = new \stdClass();
$template = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
$template->tt_track = 0;
$template->init();
$template->getFileName_backPath = constant('PATH_site');
$GLOBALS['TSFE']->tmpl = $template;
$GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
$GLOBALS['TSFE']->config = $typoScriptSetup;
}
/**
* Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
*
* @return void
* @see simulateFrontendEnvironment()
*/
protected function resetFrontendEnvironment() {
$GLOBALS['TSFE'] = $this->tsfeBackup;
chdir($this->workingDirectoryBackup);
}
/**
* Turns a relative source URI into an absolute URL
* if required
*
* @param string $source
* @return string
*/
public function preprocessSourceUri($source) {
if (FALSE === empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
$source = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $source;
} elseif ('BE' === TYPO3_MODE || FALSE === (boolean) $this->arguments['relative']) {
$source = GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $source;
}
return $source;
}
}

View File

@@ -0,0 +1,143 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Utility\ResourceUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
/**
* Base class for resource related view helpers
*
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
abstract class AbstractResourceViewHelper extends AbstractTagBasedViewHelper {
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
$this->registerArgument('identifier', 'mixed', 'The FAL combined identifiers (either CSV, array or implementing Traversable).', FALSE, NULL);
$this->registerArgument('categories', 'mixed', 'The sys_category records to select the resources from (either CSV, array or implementing Traversable).', FALSE, NULL);
$this->registerArgument('treatIdAsUid', 'boolean', 'If TRUE, the identifier argument is treated as resource uids.', FALSE, FALSE);
$this->registerArgument('treatIdAsReference', 'boolean', 'If TRUE, the identifier argument is treated as reference uids and will be resolved to resources via sys_file_reference.', FALSE, FALSE);
}
/**
* Returns the files
*
* @param boolean $onlyProperties
* @param mixed $identifier
* @param mixed $categories
* @return array|NULL
* @throws \RuntimeException
*/
public function getFiles($onlyProperties = FALSE, $identifier = NULL, $categories = NULL) {
$identifier = $this->arrayForMixedArgument($identifier, 'identifier');
$categories = $this->arrayForMixedArgument($categories, 'categories');
$treatIdAsUid = (boolean) $this->arguments['treatIdAsUid'];
$treatIdAsReference = (boolean) $this->arguments['treatIdAsReference'];
if (TRUE === $treatIdAsUid && TRUE === $treatIdAsReference) {
throw new \RuntimeException('The arguments "treatIdAsUid" and "treatIdAsReference" may not both be TRUE.', 1384604695);
}
if (TRUE === empty($identifier) && TRUE === empty($categories)) {
return NULL;
}
$files = array();
$resourceFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
if (FALSE === empty($categories)) {
$sqlCategories = implode(',', $GLOBALS['TYPO3_DB']->fullQuoteArray($categories, 'sys_category_record_mm'));
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid_foreign', 'sys_category_record_mm', 'tablenames = \'sys_file\' AND uid_local IN (' . $sqlCategories . ')');
$fileUids = array();
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$fileUids[] = intval($row['uid_foreign']);
}
$fileUids = array_unique($fileUids);
if (TRUE === empty($identifier)) {
foreach ($fileUids as $fileUid) {
try {
$file = $resourceFactory->getFileObject($fileUid);
if (TRUE === $onlyProperties) {
$file = ResourceUtility::getFileArray($file);
}
$files[] = $file;
} catch (\Exception $e) {
continue;
}
}
return $files;
}
}
foreach ($identifier as $i) {
try {
if (TRUE === $treatIdAsUid) {
$file = $resourceFactory->getFileObject(intval($i));
} elseif (TRUE === $treatIdAsReference) {
$fileReference = $resourceFactory->getFileReferenceObject(intval($i));
$file = $fileReference->getOriginalFile();
} else {
$file = $resourceFactory->getFileObjectFromCombinedIdentifier($i);
}
if (TRUE === isset($fileUids) && FALSE === in_array($file->getUid(), $fileUids)) {
continue;
}
if (TRUE === $onlyProperties) {
$file = ResourceUtility::getFileArray($file);
}
$files[] = $file;
} catch (\Exception $e) {
continue;
}
}
return $files;
}
/**
* Mixed argument with CSV, array, Traversable
*
* @param mixed $argument
* @param string $name
* @return array
*/
public function arrayForMixedArgument($argument, $name) {
if (NULL === $argument) {
$argument = $this->arguments[$name];
}
if (TRUE === $argument instanceof \Traversable) {
$argument = iterator_to_array($argument);
} elseif (TRUE === is_string($argument)) {
$argument = GeneralUtility::trimExplode(',', $argument, TRUE);
} else {
$argument = (array) $argument;
}
return $argument;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use TYPO3\CMS\Core\Collection\RecordCollectionRepository;
/**
* ### Collection ViewHelper
* This viewhelper returns a collection referenced by uid.
* For more information look here:
* http://docs.typo3.org/typo3cms/CoreApiReference/6.2/ApiOverview/Collections/Index.html#collections-api
*
* ### Example
* {v:resource.collection(uid:'123') -> v:var.set(name: 'someCollection')}
*
* @category ViewHelpers
* @package Vhs
* @author Dmitri Pisarev <dimaip@gmail.com>
*/
class CollectionViewHelper extends AbstractResourceViewHelper {
/**
* @var RecordCollectionRepository
*/
protected $collectionRepository;
/**
* @param RecordCollectionRepository $collectionRepository
* @return void
*/
public function injectCollectionRepository(RecordCollectionRepository $collectionRepository) {
$this->collectionRepository = $collectionRepository;
}
/**
* Returns a specific collection referenced by uid.
*
* @param integer $uid
* @return mixed
*/
public function render($uid) {
if (NULL !== $uid) {
/** @var \TYPO3\CMS\Core\Collection\AbstractRecordCollection $collection */
$collection = $this->collectionRepository->findByUid($uid);
if (NULL !== $collection) {
return $collection->loadContents();
} else {
return NULL;
}
}
return NULL;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
/**
* ViewHelper to output or assign FAL sys_file records
*
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
class FileViewHelper extends AbstractResourceViewHelper {
use TemplateVariableViewHelperTrait;
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerAsArgument();
}
/**
* @return mixed
*/
public function render() {
$files = $this->getFiles(TRUE);
if (1 === count($files)) {
$files = array_shift($files);
}
return $this->renderChildrenWithVariableOrReturnInput($files);
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
/**
* ViewHelper to output or assign a image from FAL
*
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
class ImageViewHelper extends AbstractImageViewHelper {
use TemplateVariableViewHelperTrait;
/**
* name of the tag to be created by this view helper
*
* @var string
* @api
*/
protected $tagName = 'img';
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('usemap', 'string', 'A hash-name reference to a map element with which to associate the image.', FALSE, NULL);
$this->registerTagAttribute('ismap', 'string', 'Specifies that its img element provides access to a server-side image map.', FALSE, NULL);
$this->registerTagAttribute('alt', 'string', 'Equivalent content for those who cannot process images or who have image loading disabled.', FALSE, NULL);
$this->registerArgument('as', 'string', 'If specified, a template variable with this name containing the requested data will be inserted instead of returning it.', FALSE, NULL);
}
/**
* Render method
*
* @return string
*/
public function render() {
$files = $this->getFiles();
$images = $this->preprocessImages($files, TRUE);
if (TRUE === empty($images)) {
return NULL;
}
$info = array();
$tags = array();
foreach ($images as &$image) {
$source = $this->preprocessSourceUri($image['source']);
$width = $image['info'][0];
$height = $image['info'][1];
$alt = $this->arguments['alt'];
if (TRUE === empty($alt)) {
$alt = $image['file']['alternative'];
}
$this->tag->addAttribute('src', $source);
$this->tag->addAttribute('width', $width);
$this->tag->addAttribute('height', $height);
$this->tag->addAttribute('alt', $alt);
$tag = $this->tag->render();
$image['tag'] = $tag;
$tags[] = $tag;
$info[] = array(
'source' => $source,
'width' => $width,
'height' => $height,
'tag' => $tag
);
}
$as = $this->arguments['as'];
if (TRUE === empty($as)) {
return implode('', $tags);
}
return $this->renderChildrenWithVariableOrReturnInput($info);
}
}

View File

@@ -0,0 +1,179 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
/**
* Resource: Language
*
* Reads a certain language file with returning not just one single label,
* but all the translated labels.
*
* ### Examples
*
* <!-- Tag usage for force getting labels in a specific language (different to current is possible too) -->
* <v:resource.language extensionName="myext" path="Path/To/Locallang.xlf" languageKey="en"/>
*
* <!-- Tag usage for getting labels of current language -->
* <v:resource.language extensionName="myext" path="Path/To/Locallang.xlf"/>
*
* @author Cornel Boppart <cornel@bopp-art.com>
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
class LanguageViewHelper extends AbstractViewHelper {
use TemplateVariableViewHelperTrait;
const LOCALLANG_DEFAULT = 'locallang.xlf';
/**
* Registers all arguments for this ViewHelper.
*
* @return void
*/
public function initializeArguments() {
$this->registerAsArgument();
$this->registerArgument('extensionName', 'string', 'Name of the extension', FALSE, NULL);
$this->registerArgument('path', 'string', 'Absolute or relative path to the locallang file', FALSE, self::LOCALLANG_DEFAULT);
$this->registerArgument('languageKey', 'string', 'Key for getting translation of a different than current initialized language', FALSE, NULL);
}
/**
* The main render method of this ViewHelper.
*
* @return array|string
*/
public function render() {
$path = $this->getResolvedPath();
$languageKey = $this->getLanguageKey();
$locallang = GeneralUtility::readLLfile($path, $languageKey);
$labels = $this->getLabelsByLanguageKey($locallang, $languageKey);
$labels = $this->getLabelsFromTarget($labels);
return $this->renderChildrenWithVariableOrReturnInput($labels);
}
/**
* Gets the extension name from defined argument or
* tries to resolve it from the controller context if not set.
*
* @return string
* @throws Exception
*/
protected function getResolvedExtensionName() {
$extensionName = $this->arguments['extensionName'];
if ((NULL === $extensionName) && (TRUE === $this->controllerContext instanceof ControllerContext)) {
$request = $this->controllerContext->getRequest();
$extensionName = $request->getControllerExtensionName();
}
if (TRUE === empty($extensionName)) {
throw new Exception('Unable to read extension name from ControllerContext and value not manually specified');
}
return $extensionName;
}
/**
* Gets the resolved file path with trying to resolve relative paths even if no
* extension key is defined.
*
* @return string
*/
protected function getResolvedPath() {
$path = $this->arguments['path'];
$absoluteFileName = GeneralUtility::getFileAbsFileName($this->arguments['path']);
if (FALSE === file_exists($absoluteFileName)) {
$extensionName = $this->getResolvedExtensionName();
$extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName);
$absoluteFileName = ExtensionManagementUtility::extPath($extensionKey, $path);
}
return $absoluteFileName;
}
/**
* Gets the translated labels by a specific language key
* or fallback to 'default'.
*
* @param array $locallang
* @param string $languageKey
* @return array
*/
protected function getLabelsByLanguageKey($locallang, $languageKey) {
$labels = array();
if (FALSE === empty($locallang[$languageKey])) {
$labels = $locallang[$languageKey];
} elseif (FALSE === empty($locallang['default'])) {
$labels = $locallang['default'];
}
return $labels;
}
/**
* Simplify label array with just taking the value from target.
*
* @param array $labels
* @return array
*/
protected function getLabelsFromTarget($labels) {
if (TRUE === is_array($labels)) {
foreach ($labels as $labelKey => $label) {
$labels[$labelKey] = $label[0]['target'];
}
}
return $labels;
}
/**
* Gets the language key from arguments or from current
* initialized language if argument is not defined.
*
* @return string
*/
protected function getLanguageKey() {
$languageKey = $this->arguments['languageKey'];
if (NULL === $languageKey) {
$languageKey = $this->getInitializedLanguage();
}
return $languageKey;
}
/**
* Gets the key of current initialized language
* or fallback to 'default'.
*
* @return string
*/
protected function getInitializedLanguage() {
$language = 'default';
if ('FE' === TYPO3_MODE) {
$language = $GLOBALS['TSFE']->lang;
} elseif (TRUE === is_object($GLOBALS['LANG'])) {
$language = $GLOBALS['LANG']->lang;
}
return $language;
}
}

View File

@@ -0,0 +1,185 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource\Record;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
/**
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource\Record
*/
abstract class AbstractRecordResourceViewHelper extends AbstractViewHelper implements RecordResourceViewHelperInterface {
use TemplateVariableViewHelperTrait;
/**
* @var string
*/
protected $table;
/**
* @var string
*/
protected $field;
/**
* @var string
*/
protected $idField = 'uid';
/**
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
*/
protected $configurationManager;
/**
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
* @return void
*/
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager) {
$this->configurationManager = $configurationManager;
}
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
$this->registerArgument('table', 'string', 'The table to lookup records.', TRUE);
$this->registerArgument('field', 'string', 'The field of the table associated to resources.', TRUE);
$this->registerArgument('record', 'array', 'The actual record. Alternatively you can use the "uid" argument.', FALSE, NULL);
$this->registerArgument('uid', 'integer', 'The uid of the record. Alternatively you can use the "record" argument.', FALSE, NULL);
$this->registerArgument('as', 'string', 'If specified, a template variable with this name containing the requested data will be inserted instead of returning it.', FALSE, NULL);
}
/**
* @param mixed $identity
* @return mixed
*/
public function getResource($identity) {
return $identity;
}
/**
* @param array $record
* @return array
* @throws Exception
*/
public function getResources($record) {
$field = $this->getField();
if (FALSE === isset($record[$field])) {
throw new Exception('The "field" argument was not found on the selected record.', 1384612728);
}
if (TRUE === empty($record[$field])) {
return array();
}
return GeneralUtility::trimExplode(',', $record[$field]);
}
/**
* @return string
* @throws Exception
*/
public function getTable() {
$table = $this->arguments['table'];
if (NULL === $table) {
$table = $this->table;
}
if (TRUE === empty($table) || FALSE === is_string($table)) {
throw new Exception('The "table" argument must be specified and must be a string.', 1384611336);
}
return $table;
}
/**
* @return string
* @throws Exception
*/
public function getField() {
$field = $this->arguments['field'];
if (NULL === $field) {
$field = $this->field;
}
if (TRUE === empty($field) || FALSE === is_string($field)) {
throw new Exception('The "field" argument must be specified and must be a string.', 1384611355);
}
return $field;
}
/**
* @param mixed $id
* @return array
*/
public function getRecord($id) {
$table = $this->getTable();
$idField = $this->idField;
$sqlIdField = $GLOBALS['TYPO3_DB']->quoteStr($idField, $table);
$sqlId = $GLOBALS['TYPO3_DB']->fullQuoteStr($id, $table);
return reset($GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $table, $sqlIdField . ' = ' . $sqlId));
}
/**
* @return array
*/
public function getActiveRecord() {
return $this->configurationManager->getContentObject()->data;
}
/**
* @return mixed
* @throws Exception
*/
public function render() {
$record = $this->arguments['record'];
$uid = $this->arguments['uid'];
if (NULL === $record) {
if (NULL === $uid) {
$record = $this->getActiveRecord();
} else {
$record = $this->getRecord($uid);
}
}
if (NULL === $record) {
throw new Exception('No record was found. The "record" or "uid" argument must be specified.', 1384611413);
}
// attempt to load resources. If any Exceptions happen, transform them to
// ViewHelperExceptions which render as an inline text error message.
try {
$resources = $this->getResources($record);
} catch (\Exception $error) {
// we are doing the pokemon-thing and catching the very top level
// of Exception because the range of Exceptions that are possibly
// thrown by the getResources() method in subclasses are not
// extended from a shared base class like RuntimeException. Thus,
// we are forced to "catch them all" - but we also output them.
throw new Exception($error->getMessage(), $error->getCode());
}
return $this->renderChildrenWithVariableOrReturnInput($resources);
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource\Record;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Utility\ResourceUtility;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
/**
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource\Record
*/
class FalViewHelper extends AbstractRecordResourceViewHelper {
/**
* @var \TYPO3\CMS\Core\Resource\ResourceFactory
*/
protected $resourceFactory;
/**
* @var \TYPO3\CMS\Core\Resource\FileRepository
*/
protected $fileRepository;
/**
* Constructor
*/
public function __construct() {
$this->resourceFactory = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
$this->fileRepository = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
}
/**
* @param mixed $identity
* @return mixed
*/
public function getResource($fileReference) {
$file = $fileReference->getOriginalFile();
$fileReferenceProperties = $fileReference->getProperties();
$fileProperties = ResourceUtility::getFileArray($file);
ArrayUtility::mergeRecursiveWithOverrule($fileProperties, $fileReferenceProperties, TRUE, FALSE, FALSE);
return $fileProperties;
}
/**
* Fetch a fileRefernce from the file repository
*
* @param $table name of the table to get the file reference for
* @param $field name of the field referencing a file
* @param $uid uid of the related record
* @return array
*/
protected function getFileReferences($table, $field, $uid) {
$fileObjects = $this->fileRepository->findByRelation($table, $field, $uid);
return $fileObjects;
}
/**
* @param array $record
* @return array
*/
public function getResources($record) {
/** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $contentObj */
$contentObjectRenderer = $this->configurationManager->getContentObject();
/** @var \TYPO3\CMS\Core\Database\DatabaseConnection $databaseConnection */
$databaseConnection = $this->getDatabaseConntection();
if (isset($record['t3ver_oid']) && (integer) $record['t3ver_oid'] !== 0) {
$sqlRecordUid = $record['t3ver_oid'];
} else {
$sqlRecordUid = $record[$this->idField];
}
if (FALSE === empty($GLOBALS['TSFE']->sys_page)) {
$images = $this->getFileReferences($this->getTable(), $this->getField(), $sqlRecordUid);
} else {
if ($GLOBALS['BE_USER']->workspaceRec['uid']) {
$versionWhere = 'AND sys_file_reference.deleted=0 AND (sys_file_reference.t3ver_wsid=0 OR sys_file_reference.t3ver_wsid=' . $GLOBALS['BE_USER']->workspaceRec['uid'] . ') AND sys_file_reference.pid<>-1';
} else {
$versionWhere = 'AND sys_file_reference.deleted=0 AND sys_file_reference.t3ver_state<=0 AND sys_file_reference.pid<>-1 AND sys_file_reference.hidden=0';
}
$references = $databaseConnection->exec_SELECTgetRows(
'uid',
'sys_file_reference',
'tablenames=' . $databaseConnection->fullQuoteStr($this->getTable(), 'sys_file_reference') .
' AND uid_foreign=' . (int) $sqlRecordUid .
' AND fieldname=' . $databaseConnection->fullQuoteStr($this->getField(), 'sys_file_reference')
. $versionWhere,
'',
'sorting_foreign',
'',
'uid'
);
if (FALSE === empty($references)) {
$referenceUids = array_keys($references);
}
$images = array();
if (FALSE === empty($referenceUids)) {
foreach ($referenceUids as $referenceUid) {
try {
// Just passing the reference uid, the factory is doing workspace
// overlays automatically depending on the current environment
$images[] = $this->resourceFactory->getFileReferenceObject($referenceUid);
} catch (ResourceDoesNotExistException $exception) {
// No handling, just omit the invalid reference uid
continue;
}
}
}
}
$resources = array();
foreach ($images as $file) {
$resources[] = $this->getResource($file);
}
return $resources;
}
/**
* @return DatabaseConnection
*/
protected function getDatabaseConntection() {
return $GLOBALS['TYPO3_DB'];
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource\Record;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\ViewHelpers\Resource\ResourceViewHelperInterface;
/**
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource\Record
*/
interface RecordResourceViewHelperInterface extends ResourceViewHelperInterface {
/**
* @param array $record
* @return array
*/
public function getResources($record);
/**
* @return string
*/
public function getTable();
/**
* @return string
*/
public function getField();
/**
* @param mixed $id
* @return array
*/
public function getRecord($id);
/**
* @return array
*/
public function getActiveRecord();
}

View File

@@ -0,0 +1,20 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\ViewHelpers\Resource\Record\AbstractRecordResourceViewHelper;
/**
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
class RecordViewHelper extends AbstractRecordResourceViewHelper {
}

View File

@@ -0,0 +1,24 @@
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
/**
* @author Danilo Bürger <danilo.buerger@hmspl.de>, Heimspiel GmbH
* @package Vhs
* @subpackage ViewHelpers\Resource
*/
interface ResourceViewHelperInterface {
/**
* @param mixed $identity
* @return mixed
*/
public function getResource($identity);
}