|
<?php |
|
namespace Ttree\OfficialWebsite\TypoScript; |
|
|
|
/* * |
|
* This script belongs to the TYPO3 Flow package "Ttree.OfficialWebsite". * |
|
* * |
|
* It is free software; you can redistribute it and/or modify it under * |
|
* the terms of the GNU General Public License, either version 3 of the * |
|
* License, or (at your option) any later version. * |
|
* * |
|
* The TYPO3 project - inspiring people to share! * |
|
* */ |
|
|
|
use TYPO3\Eel\Helper\StringHelper; |
|
use TYPO3\Flow\Annotations as Flow; |
|
use TYPO3\Flow\Utility\Unicode\Functions; |
|
use TYPO3\TYPO3CR\Domain\Model\NodeInterface; |
|
use TYPO3\TypoScript\TypoScriptObjects\AbstractTypoScriptObject; |
|
|
|
/** |
|
* A Document Teaser Implementation |
|
* |
|
* @Flow\Scope("prototype") |
|
*/ |
|
class DocumentTeaserImplementation extends AbstractTypoScriptObject { |
|
|
|
/** |
|
* @var NodeInterface |
|
*/ |
|
protected $documentNode; |
|
|
|
/** |
|
* @var integer |
|
*/ |
|
protected $maximumCharacters; |
|
|
|
/** |
|
* @var string |
|
*/ |
|
protected $suffix; |
|
|
|
/** |
|
* @param \TYPO3\TYPO3CR\Domain\Model\NodeInterface $documentNode |
|
*/ |
|
public function setDocumentNode($documentNode) { |
|
$this->documentNode = $documentNode; |
|
} |
|
|
|
/** |
|
* @param int $maxCharacters |
|
*/ |
|
public function setMaximumCharacters($maxCharacters) { |
|
$this->maximumCharacters = $maxCharacters; |
|
} |
|
|
|
/** |
|
* @param string $suffix |
|
*/ |
|
public function setSuffix($suffix) { |
|
$this->suffix = $suffix; |
|
} |
|
|
|
/** |
|
* Just return the processed value |
|
* |
|
* @return mixed |
|
*/ |
|
public function evaluate() { |
|
$stringToTruncate = ''; |
|
$documentNode = $this->tsValue('documentNode'); |
|
$maximumCharacters = $this->tsValue('maximumCharacters') ?: 600; |
|
|
|
foreach ($documentNode->getNode('main')->getChildNodes('TYPO3.Neos.NodeTypes:Text') as $contentNode) { |
|
/** @var NodeInterface $contentNode */ |
|
foreach ($contentNode->getProperties() as $propertyValue) { |
|
if (!is_object($propertyValue) || method_exists($propertyValue, '__toString')) { |
|
$stringToTruncate .= $propertyValue; |
|
} |
|
if (Functions::strlen($stringToTruncate) > $maximumCharacters) { |
|
break; |
|
} |
|
} |
|
} |
|
$stringToTruncate = $this->stripUnwantedTags($stringToTruncate); |
|
$stringHelper = new StringHelper(); |
|
|
|
return $stringHelper->crop($stringToTruncate, $maximumCharacters, $this->tsValue('suffix')); |
|
} |
|
|
|
/** |
|
* @param string $content The original content |
|
* @return string The stripped content |
|
*/ |
|
protected function stripUnwantedTags($content) { |
|
$content = preg_replace('/(?:<|<)\/?([a-z]+) *[^\/(?:<|<)]*?(?:>|>)/', '', $content); |
|
$content = str_replace(' ', ' ', $content); |
|
|
|
return trim($content); |
|
} |
|
} |