diff --git a/Classes/Controller/ActionController.php b/Classes/Controller/ActionController.php index c9b74f0..e5599f1 100644 --- a/Classes/Controller/ActionController.php +++ b/Classes/Controller/ActionController.php @@ -21,6 +21,7 @@ use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController as BaseController; use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper; +use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager; use TYPO3\CMS\Extbase\Property\PropertyMapper; use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationBuilder; use TYPO3\CMS\Extbase\Service\ExtensionService; @@ -99,6 +100,11 @@ class ActionController extends BaseController */ protected $redirect = null; + /** + * if to reload + */ + protected $reload = null; + /** * errors */ @@ -244,6 +250,16 @@ class ActionController extends BaseController ); } + /** + * returns an instance of uribuilder + */ + public function persistAll() + { + ($this->objectManager->get( + PersistenceManager::class + ))->persistAll(); + } + /** * shortcut * @@ -406,6 +422,19 @@ class ActionController extends BaseController return null; } + /** + * + */ + protected function getDomainModelString($object) + { + $extensionName = $this->request->getControllerExtensionName(); + $reflection = new \ReflectionClass($object); + return 'tx_' . + strtolower($this->request->getControllerExtensionName()) . + '_domain_model_' . + strtolower($reflection->getShortName()); + } + /** * gets error label based on field and keyword, uses predefined extensionkey */ @@ -424,6 +453,7 @@ class ActionController extends BaseController protected function addValidationError( $field, $keyword, $overwrite = false ) { + $this->isValid = false; $this->responseStatus = [400 => 'validationError']; if (!array_key_exists($field, $this->errors) || $overwrite == true @@ -554,6 +584,7 @@ class ActionController extends BaseController ->getUriBuilder() ->reset() ->setCreateAbsoluteUri(true) + ->setAddQueryString(true) ->setTargetPageType($this->ajaxPageType) ->setArguments(['cid' => $this->contentObjectUid]) ->uriFor($this->request->getControllerActionName()); @@ -667,9 +698,12 @@ class ActionController extends BaseController * @param array $result * @return void */ - protected function returnFunction($result = [], $errorStatus = null) - { - $this->setAjaxEnv(); + protected function returnFunction( + $result = [], + $errorStatus = null, + $object = 'data' + ) { + $this->setAjaxEnv($object); if ($result == null) { $result = []; } @@ -706,6 +740,9 @@ class ActionController extends BaseController if ($this->redirect) { $result['redirect'] = $this->redirect; } + if ($this->reload) { + $result['reload'] = true; + } return json_encode($result); } $result = array_merge( diff --git a/Classes/Utility/ApiUtility.php b/Classes/Utility/ApiUtility.php index a448854..9bac2b3 100644 --- a/Classes/Utility/ApiUtility.php +++ b/Classes/Utility/ApiUtility.php @@ -93,15 +93,18 @@ class ApiUtility ) { $rowResult[$attributeName] = $methodResult; } - if (array_key_exists($rowClass, $mapping) - && array_key_exists($attributeName, $mapping[$rowClass]) - ) { - $mappingFunction = $mapping[$rowClass][$attributeName]; - $rowResult[$attributeName] = $mappingFunction( - $methodResult, + } + // --- + if (array_key_exists($rowClass, $mapping)) { + foreach ($mapping[$rowClass] as $attributeName => $function) { + $rowResult[$attributeName] = $function( + $rowResult[$attributeName], $row ); } + } + // --- + foreach ($propertieResults as $attributeName => $methodResult) { if (gettype($methodResult) == 'object' && get_class($methodResult) == 'DateTime' ) { diff --git a/Classes/Utility/MailUtility.php b/Classes/Utility/MailUtility.php index 9976c9d..19129ce 100644 --- a/Classes/Utility/MailUtility.php +++ b/Classes/Utility/MailUtility.php @@ -26,6 +26,49 @@ use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; */ class MailUtility { + /** + * Parse text with a simple "template system" to be used as data for + * sendMail function + * + * @param string $text + * @param array $markers + * @return array + */ + public static function parseContentTemplate( + $text, + $markers = [] + ) { + $textParts = explode("\r\n\r\n", $text); + $result = []; + foreach ($textParts as $textPart) { + $type = 'text'; + if (substr($textPart, 0, 2) === '# ') { + $type = 'headline'; + $textPart = substr($textPart, 2); + } + if (substr($textPart, 0, 3) === '## ') { + $type = 'headline2'; + $textPart = substr($textPart, 3); + } + if (substr($textPart, 0, 4) === '### ') { + $type = 'headline3'; + $textPart = substr($textPart, 4); + } + foreach ($markers as $markerName => $markerContent) { + $textPart = str_replace( + '###' . $markerName . '###', + $markerContent, + $textPart + ); + } + $result[] = [ + 'type' => $type, + 'data' => $textPart, + ]; + } + return $result; + } + /** * tages maildata, builds html and text mails an decides where to send them * allows to intercept sender for testing @@ -89,11 +132,12 @@ class MailUtility case 'text': case 'textbold': case 'headline': + case 'headline2': + case 'headline3': $htmlRow = $row; $htmlRow['data'] = preg_replace_callback( '/\[.*\]/mU', function($matches) { - foreach ($matches as $match) { return preg_replace_callback( '/\[(\S*)\s(.*)\]/mU', @@ -196,6 +240,13 @@ class MailUtility $htmlView->assign('domain', $domain); $textBody = $textView->render(); $htmlBody = $htmlView->render(); + if ($domain) { + $htmlBody = str_replace( + 'src="/assets', + 'src="' . $domain . '/assets', + $htmlBody + ); + } $mail->setBody($textBody); $mail->addPart($htmlBody, 'text/html'); $recipients = explode( diff --git a/Classes/Utility/ObjectUtility.php b/Classes/Utility/ObjectUtility.php new file mode 100644 index 0000000..4cfaa03 --- /dev/null +++ b/Classes/Utility/ObjectUtility.php @@ -0,0 +1,87 @@ + + * + ***/ + +use Cjel\TemplatesAide\Utility\ObjectUtility; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; +use TYPO3\CMS\Extbase\Persistence\ObjectStorage; + +/** + * + */ +class ObjectUtility +{ + /** + * fills object from array + * + * @return void + */ + public static function fromArray( + &$object, $data, $storageMapping = [] + ) { + $objectManager = GeneralUtility::makeInstance( + ObjectManager::class + ); + $reflectionClass = new \ReflectionClass(get_class($object)); + foreach ($data as $property => $value) { + $methodName = 'set' . ucfirst($property); + if (!$reflectionClass->hasMethod($methodName)) { + continue; + } + $method = $reflectionClass->getMethod($methodName); + $params = $method->getParameters(); + $methodType = $params[0]->getType(); + if (is_array($value)) { + if (array_key_exists($property, $storageMapping)) { + $storage = $object->_getProperty($property); + $storageUpdated = $objectManager->get( + ObjectStorage::class + ); + foreach ($value as $row) { + $item = null; + if ($row['uid']) { + foreach ($storage as $storageIitem) { + if ($storageIitem->getUid() == $row['uid']) { + $item = $storageIitem; + } + } + $storageUpdated->attach($item); + } + if (!$item) { + $item = new $storageMapping[$property](); + $storageUpdated->attach($item); + } + self::fromArray($item, $row); + } + $object->_setProperty($property, $storageUpdated); + } + } else { + if ($methodType == null) { + $value = StringUtility::checkAndfixUtf8($value); + $object->_setProperty($property, $value); + } else { + $typeParts = explode('\\', (string)$methodType); + $typeParts[count($typeParts) - 2] = 'Repository'; + $repositoryClass = join('\\', $typeParts); + $repositoryClass .= 'Repository'; + if (class_exists($repositoryClass)) { + $repository = $objectManager->get($repositoryClass); + $relatedObject = $repository->findByUid($value); + $object->_setProperty($property, $relatedObject); + } + } + } + } + } +} diff --git a/Classes/Utility/SiteConfigUtility.php b/Classes/Utility/SiteConfigUtility.php new file mode 100644 index 0000000..5508fde --- /dev/null +++ b/Classes/Utility/SiteConfigUtility.php @@ -0,0 +1,50 @@ +get( + ConfigurationManagerInterface::class + ); + $typoscript = $configurationManager->getConfiguration( + ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT + ); + $typoscript = GeneralUtility::removeDotsFromTS($typoscript); + $siteConfig = $typoscript['config']['site']; + $current = &$siteConfig; + foreach ($pathParts as $key) { + $current = &$current[$key]; + } + return $current; + } +} diff --git a/Classes/Utility/StringUtility.php b/Classes/Utility/StringUtility.php index 2d96d17..f157e59 100644 --- a/Classes/Utility/StringUtility.php +++ b/Classes/Utility/StringUtility.php @@ -64,4 +64,11 @@ class StringUtility } return implode('', $pieces); } + + public static function checkAndfixUtf8($string){ + if (!mb_detect_encoding($string, 'UTF-8', true)) { + $string = mb_convert_encoding($string , 'UTF-8', 'ASCII'); + } + return $string; + } } diff --git a/Classes/Utility/TcaUtility.php b/Classes/Utility/TcaUtility.php new file mode 100644 index 0000000..c188135 --- /dev/null +++ b/Classes/Utility/TcaUtility.php @@ -0,0 +1,70 @@ + + * + ***/ + +use TYPO3\CMS\Extbase\Utility\LocalizationUtility; + +/** + * + */ +class TcaUtility +{ + /** + * fills object from array + * + * @return void + */ + public static function configureSelect( + &$tca, $column, $options, $extensionKey = null + ) { + foreach ($options as &$option) { + $translation = self::getTranslation( + 'option.' . $option[0], $extensionKey + ); + if ($translation) { + $option[0] = $translation; + } + } + $tca['columns'][$column]['config']['type'] = 'select'; + $tca['columns'][$column]['config']['renderType'] = 'selectSingle'; + $tca['columns'][$column]['config']['size'] = 6; + $tca['columns'][$column]['config']['appearance'] = []; + $tca['columns'][$column]['config']['items'] = $options; + } + + /** + * shortcut to get translation + * + * @return void + */ + public static function getTranslation($key, $extensionKey) + { + if ($extensionKey) { + $translation = LocalizationUtility::translate( + $key, + $extensionKey + ); + if ($translation) { + return $translation; + } + } + $translation = LocalizationUtility::translate( + $key, + 'site_templates' + ); + if ($translation) { + return $translation; + } + return null; + } +} diff --git a/Classes/ViewHelpers/DbTableViewHelper.php b/Classes/ViewHelpers/DbTableViewHelper.php index 7e07c80..0f2540d 100644 --- a/Classes/ViewHelpers/DbTableViewHelper.php +++ b/Classes/ViewHelpers/DbTableViewHelper.php @@ -4,27 +4,46 @@ namespace Cjel\TemplatesAide\ViewHelpers; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Connection; +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; -class DbTableViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { +class DbTableViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper { /** - * As this ViewHelper renders HTML, the output must not be escaped. - * - * @var bool - */ + * As this ViewHelper renders HTML, the output must not be escaped. + * + * @var bool + */ protected $escapeOutput = false; /** - * @param string $table The filename - * @param string $fields The filename - * @param string $where - * @param string $orderBy + * Initialize arguments + */ + public function initializeArguments() { + parent::initializeArguments(); + $this->registerArgument('table', 'string', '', true); + $this->registerArgument('fields', 'array', '', true, ['*']); + $this->registerArgument('where', 'array', '', false); + $this->registerArgument('orderBy', 'array', '', false); + } + + /** + * @param string $table + * @param array $fields + * @param array $where + * @param array $orderBy * @return string HTML Content */ - public function render($table, $fields = ['*'], $where = [], $orderBy = []){ + public static function renderStatic( + array $arguments, + \Closure $renderChildrenClosure, + RenderingContextInterface $renderingContext + ) { + $table = $arguments['table']; + $fields = $arguments['fields']; + $where = $arguments['where']; + $orderBy = $arguments['orderBy']; - - $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages')->createQueryBuilder(); + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table)->createQueryBuilder(); $whereExpressions = []; if (!empty($where)) { diff --git a/Classes/ViewHelpers/DotsToBracketsViewHelper.php b/Classes/ViewHelpers/DotsToBracketsViewHelper.php new file mode 100644 index 0000000..b97ed76 --- /dev/null +++ b/Classes/ViewHelpers/DotsToBracketsViewHelper.php @@ -0,0 +1,30 @@ + - +