* All rights reserved * * This script is part of the Typo3 project. The Typo3 project is * free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * The GNU General Public License can be found at * http://www.gnu.org/copyleft/gpl.html. * A copy is found in the textfile GPL.txt and important notices to the license * from the author is found in LICENSE.txt distributed with these scripts. * * * This script is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * This copyright notice MUST APPEAR in all copies of the script! */ use Psr\Http\Message\ServerRequestInterface; use SJBR\StaticInfoTables\Domain\Model\Currency; use SJBR\StaticInfoTables\Domain\Repository\CountryRepository; use SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository; use SJBR\StaticInfoTables\Utility\HtmlElementUtility; use SJBR\StaticInfoTables\Utility\LocalizationUtility; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryHelper; use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer; use TYPO3\CMS\Core\Http\ApplicationType; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Frontend\Plugin\AbstractPlugin; /** * Class for handling static info tables: countries, and subdivisions, currencies, languages and taxes */ class PiBaseApi extends AbstractPlugin { /** * The backReference to the mother cObj object set at call time */ public $cObj; // Same as class name public $prefixId = 'tx_staticinfotables_pi1'; // Path to this script relative to the extension dir. public $scriptRelPath = 'pi1/class.tx_staticinfotables_pi1.php'; /** * The extension key * * @var string */ public $extKey = 'static_info_tables'; public $conf = []; // Default currency public $currency; public $currencyInfo = []; public $defaultCountry; public $defaultCountryZone; public $defaultLanguage; public $types = ['TERRITORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES']; public $tables = [ 'TERRITORIES' => 'static_territories', 'COUNTRIES' => 'static_countries', 'SUBDIVISIONS' => 'static_country_zones', 'CURRENCIES' => 'static_currencies', 'LANGUAGES' => 'static_languages', ]; /** * @var CountryRepository */ protected $countryRepository; /** * @var CurrencyRepository */ protected $currencyRepository; /** * Whether the class has been initialized * * @var bool */ protected $bHasBeenInitialised = false; /** * Returns info if the tx_staticinfotables_pi1 object has already been initialised. * You need to initialise this object only once. * * @return bool Always returns true */ public function needsInit() { return !$this->bHasBeenInitialised; } /** * Initializing the class: sets the language based on the TS configuration language property * * @param array $conf ... overwriting setup of extension * @return bool Always returns true */ public function init($conf = []) { if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { $this->conf = $GLOBALS['TSFE']->tmpl->setup['plugin.'][$this->prefixId . '.'] ?? []; } $this->countryRepository = GeneralUtility::makeInstance(CountryRepository::class); $this->currencyRepository = GeneralUtility::makeInstance(CurrencyRepository::class); //Get the default currency and make sure it does exist in table static_currencies $this->currency = $conf['currencyCode'] ?? ''; if (!$this->currency) { $this->currency = (trim($this->conf['currencyCode'] ?? '')) ? trim($this->conf['currencyCode']) : 'EUR'; } //If nothing is set, we use the Euro because TYPO3 is spread more in this area if (!$this->getStaticInfoName('CURRENCIES', $this->currency)) { $this->currency = 'EUR'; } $this->currencyInfo = $this->loadCurrencyInfo($this->currency); $this->defaultCountry = $conf['countryCode'] ?? ''; if (!$this->defaultCountry) { $this->defaultCountry = trim($this->conf['countryCode'] ?? ''); } if (!$this->getStaticInfoName('COUNTRIES', $this->defaultCountry)) { $this->defaultCountry = 'DEU'; } $this->defaultCountryZone = $conf['countryZoneCode'] ?? ''; if (!$this->defaultCountryZone) { $this->defaultCountryZone = trim($this->conf['countryZoneCode'] ?? ''); } if (!$this->getStaticInfoName('SUBDIVISIONS', $this->defaultCountryZone, $this->defaultCountry)) { if ($this->defaultCountry == 'DEU') { $this->defaultCountryZone = 'NW'; } else { $this->defaultCountryZone = ''; } } $this->defaultLanguage = $conf['languageCode'] ?? ''; if (!$this->defaultLanguage) { $this->defaultLanguage = trim($this->conf['languageCode'] ?? ''); } if (!$this->getStaticInfoName('LANGUAGES', $this->defaultLanguage)) { $this->defaultLanguage = 'EN'; } $this->bHasBeenInitialised = true; return true; } /** * Getting the name of a country, country subdivision, currency, language, tax * * @param string Defines the type of entry of the requested name: 'TERRITORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES' * @param string The ISO alpha-3 code of a territory, country or currency, or the ISO alpha-2 code of a language or the code of a country subdivision, can be a comma ',' separated string, then all the single items are looked up and returned * @param string The value of the country code (cn_iso_3) for which a name of type 'SUBDIVISIONS' is requested (meaningful only in this case) * @param string Not used * @param bool local name only - if set local title is returned * @param mixed $type * @param mixed $code * @param mixed $country * @param mixed $countrySubdivision * @param mixed $local * * @return string|bool The name of the object in the current language or false */ public function getStaticInfoName($type = 'COUNTRIES', $code = '', $country = '', $countrySubdivision = '', $local = false) { $names = false; if (in_array($type, $this->types) && trim($code)) { $codeArray = GeneralUtility::trimExplode(',', ($code)); $tableName = $this->tables[$type]; if (!$tableName) { return false; } $nameArray = []; foreach ($codeArray as $item) { $isoCodeArray = []; $isoCodeArray[] = $item; switch ($type) { case 'SUBDIVISIONS': $isoCodeArray[] = trim($country) ? trim($country) : $this->defaultCountry; break; case 'LANGUAGES': $isoCodeArray = GeneralUtility::trimExplode('_', $code, 1); break; } $nameArray[] = LocalizationUtility::translate(['iso' => $isoCodeArray], $tableName, $local); } $names = implode(',', $nameArray); } return $names; } /** * Buils a HTML drop-down selector of countries, country subdivisions, currencies or languages * * @param string $type: Defines the type of entries to be presented in the drop-down selector: 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES' or 'LANGUAGES' * @param string $name: A value for the name attribute of the tag * @param array $selectedArray: The values of the code of the entries to be pre-selected in the drop-down selector: value of cn_iso_3, zn_code, cu_iso_3 or lg_iso_2 * @param string $country: The value of the country code (cn_iso_3) for which a drop-down selector of type 'SUBDIVISIONS' is requested (meaningful only in this case) * @param boolean/string $submit: If set to 1, an onchange attribute will be added to the tag * @param string $title: A value for the title attribute of the and