Skip to content

Commit

Permalink
Version 11.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent FOULLOY committed Feb 19, 2023
1 parent aca6fed commit 8dc92e0
Show file tree
Hide file tree
Showing 355 changed files with 8,289 additions and 14,118 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
4 changes: 4 additions & 0 deletions .settings/org.eclipse.php.core.prefs
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
include_path=0;/SAV Library Mvc
phpVersion=php7.2
use_asp_tags_as_php=false
7 changes: 7 additions & 0 deletions .settings/org.eclipse.wst.common.project.facet.core.xml
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="php.component"/>
<fixed facet="php.core.component"/>
<installed facet="php.core.component" version="1"/>
<installed facet="php.component" version="7.2"/>
</faceted-project>
5 changes: 0 additions & 5 deletions Classes/.svn/all-wcprops

This file was deleted.

82 changes: 0 additions & 82 deletions Classes/.svn/entries

This file was deleted.

191 changes: 191 additions & 0 deletions Classes/Adders/AbstractAdder.php
@@ -0,0 +1,191 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with TYPO3 source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace YolfTypo3\SavLibraryMvc\Adders;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\ClassNamingUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\RepositoryInterface;
use YolfTypo3\SavLibraryMvc\Managers\FieldConfigurationManager;
use YolfTypo3\SavLibraryMvc\Parser\OrderByClauseParser;
use YolfTypo3\SavLibraryMvc\Parser\WhereClauseParser;

abstract class AbstractAdder
{

/**
* Field configuration manager
*
* @var FieldConfigurationManager
*/
protected $fieldConfigurationManager;

/**
* Field configuration
*
* @var array
*/
protected $fieldConfiguration;

abstract public function render(): array;

/**
* Constructor
*
* @param FieldConfigurationManager $fieldConfigurationManager
* @return void
*/
public function __construct(FieldConfigurationManager $fieldConfigurationManager)
{
$this->fieldConfigurationManager = $fieldConfigurationManager;
$this->fieldConfiguration = $fieldConfigurationManager->getFieldConfiguration();
}

/**
* Gets the repository
*
* @return RepositoryInterface
*/
protected function getRepository(): RepositoryInterface
{
$foreignModel = $this->fieldConfiguration['foreignModel'];
$repositoryClassName = ClassNamingUtility::translateModelNameToRepositoryName($foreignModel);
$repository = GeneralUtility::makeInstance(ltrim($repositoryClassName, '\\'));
if (method_exists($repository, 'setController')) {
$controller = $this->fieldConfigurationManager->getController();
$repository->setController($controller);
}

return $repository;
}

/**
* Gets the query for a given repositoy
*
* @param RepositoryInterface $repository
* @return QueryInterface
*/
protected function getQuery($repository): QueryInterface
{
// Sets the ordering if any
if (! empty($this->fieldConfiguration['orderSelect'])) {
$orderByClauseParser = GeneralUtility::makeInstance(OrderByClauseParser::class);
$orderByClause = $orderByClauseParser->processOrderByClause($this->fieldConfiguration['orderSelect']);
$repository->setDefaultOrderings($orderByClause);
}

// Creates the query
$query = $repository->createQuery();

// Adds restrictions if any
if (! empty($this->fieldConfiguration['whereSelect'])) {
$whereClauseParser = GeneralUtility::makeInstance(WhereClauseParser::class);
$whereClauseParser->injectRepository($repository);
$query = $query->matching($whereClauseParser->processWhereClause($query, $this->fieldConfiguration['whereSelect']));
}

return $query;
}

/**
* Parses label ###field### tags.
*
* @param object $object
* @param string $input
* The string to process *
* @return string
*/
protected function parseLabel($object, string $label): string
{
// Checks if the value must be parsed
$matches = [];
if (! preg_match_all('/###([^#]+)###/', $label, $matches)) {
$getter = 'get' . GeneralUtility::underscoredToUpperCamelCase($label);
$label = $object->$getter();
return $label;
} else {
foreach ($matches[1] as $matchKey => $match) {
// Gets the value
$getter = 'get' . GeneralUtility::underscoredToUpperCamelCase($match);
$result = $object->$getter();
$label = str_replace($matches[0][$matchKey], $result, $label);
}
}

return $label;
}

/**
* Gets the label from a request.
*
* @param string $query
* @return array
* @throws \Exception
*/
protected function getLabelFromRequest(string $query): array
{
// Processes localization and field tags
$query = $this->fieldConfigurationManager->parseLocalizationTags($query);
$query = $this->fieldConfigurationManager->parseFieldTags($query);

// Checks if the query is a select query and finds the first table in the FROM clause
$match = [];
if (preg_match('/^(?is:SELECT.*?FROM\s+(\w+))/', $query, $match) > 0) {
$tableForConnection = $match[1];
} else {
throw new \Exception(sprintf(
'Only SELECT query is allowed in property "reqLabel" of field "%s".',
$this->fieldConfiguration['fieldName']
)
);
}

/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$rows = $connectionPool->getConnectionForTable($tableForConnection)
->executeQuery($query)
->fetchAll();
if ($rows === null) {
throw new \Exception(sprintf(
'Incorrect query in property "reqLabel" of field "%s".',
$this->fieldConfiguration['fieldName']
)
);
}

// Processes the rows.
$options = [];
foreach ($rows as $row) {
// The aliases "Label" and "Uid" must exist.
if (array_key_exists('Label', $row) && array_key_exists('Uid', $row)) {
$uid = $row['Uid'];
$options[$uid] = $row['Label'];
} else {
throw new \Exception(sprintf(
'The SELECT clause of the request in property "reqLabel" must contain the aliases "Label" and "Uid" in field "%s"',
$this->fieldConfiguration['fieldName']
)
);
}
}

return $options;
}

}
100 changes: 100 additions & 0 deletions Classes/Adders/CheckboxAdder.php
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with TYPO3 source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace YolfTypo3\SavLibraryMvc\Adders;

use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

/**
* Field configuration adder for Checkbox type.
*/
final class CheckboxAdder extends AbstractAdder
{

/**
* Renders the adder
*
* @return array
*/
public function render(): array
{
$addedFieldConfiguration = [];

if ($this->fieldConfiguration['edit']) {
return $addedFieldConfiguration;
}

$value = $this->fieldConfiguration['value'];
$addedFieldConfiguration['renderedValue'] = self::renderValueInDefaultMode($value, $this->fieldConfiguration);

return $addedFieldConfiguration;
}

/**
* Renders the value in default mode
*
* @param bool $value
* @param array $fieldConfiguration
* @return mixed
*/
public static function renderValueInDefaultMode(bool $value, array $fieldConfiguration)
{
if ($value) {
// The checkbox is checked
if ($fieldConfiguration['displayAsImage']) {
if ($fieldConfiguration['checkboxSelectedImage']) {
$iconIdentifier = $fieldConfiguration['checkboxSelectedImage'];
} else {
$iconIdentifier = 'checkbox-checked';
}
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$renderedValue = $iconFactory->getIcon(
$iconIdentifier,
Icon::SIZE_SMALL
);
} else {
$renderedValue = LocalizationUtility::translate('itemviewer.yes', 'sav_library_mvc');
}
} else {
// The checkbox is not checked and must not be displayed
if ($fieldConfiguration['doNotDisplayIfNotChecked']) {
return '';
}

// The checkbox is not checked and diplayed
if ($fieldConfiguration['displayAsImage']) {
if ($fieldConfiguration['checkboxNotSelectedImage']) {
$iconIdentifier = $fieldConfiguration['checkboxNotSelectedImage'];
} else {
$iconIdentifier = 'checkbox-empty';
}
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$renderedValue = $iconFactory->getIcon(
$iconIdentifier,
Icon::SIZE_SMALL
);
} else {
$renderedValue = LocalizationUtility::translate('itemviewer.no', 'sav_library_mvc');
}
}

return $renderedValue;
}
}

0 comments on commit 8dc92e0

Please sign in to comment.