Resolves #45809: Refactor code and introduce namespacing
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / PiBaseApi.php
1 <?php
2 namespace SJBR\StaticInfoTables;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2004-2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
7 * All rights reserved
8 *
9 * This script is part of the Typo3 project. The Typo3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 *
30 * Class for handling static info tables: countries, and subdivisions, currencies, languages and taxes
31 *
32 */
33 class PiBaseApi extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
34
35 var $cObj; // The backReference to the mother cObj object set at call time
36 var $prefixId = 'tx_staticinfotables_pi1'; // Same as class name
37 var $scriptRelPath = 'pi1/class.tx_staticinfotables_pi1.php'; // Path to this script relative to the extension dir.
38 var $extKey = 'static_info_tables'; // The extension key.
39 var $conf = array();
40 var $currency; // default currency
41 var $currencyInfo = array();
42 var $defaultCountry;
43 var $defaultCountryZone;
44 var $defaultLanguage;
45 var $types = array('TERRITORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES');
46 var $tables = array(
47 'TERRITORIES' => 'static_territories',
48 'COUNTRIES' => 'static_countries',
49 'SUBDIVISIONS' => 'static_country_zones',
50 'CURRENCIES' => 'static_currencies',
51 'LANGUAGES' => 'static_languages'
52 );
53 var $bHasBeenInitialised = FALSE;
54
55
56 /**
57 * Returns info if the tx_staticinfotables_pi1 object has already been initialised.
58 * You need to initialise this object only once.
59 *
60 * @return boolean Always returns true
61 */
62 public function needsInit () {
63 return !$this->bHasBeenInitialised;
64 }
65
66
67 /**
68 * Initializing the class: sets the language based on the TS configuration language property
69 *
70 * @param array $conf ... overwriting setup of extension
71 * @return boolean Always returns true
72 */
73 public function init ($conf=array()) {
74
75 $this->conf = $GLOBALS['TSFE']->tmpl->setup['plugin.'][$this->prefixId.'.'];
76
77 //Get the default currency and make sure it does exist in table static_currencies
78 $this->currency = $conf['currencyCode'];
79 if (!$this->currency) {
80 $this->currency = (trim($this->conf['currencyCode'])) ? trim($this->conf['currencyCode']) : 'EUR';
81 }
82 //If nothing is set, we use the Euro because TYPO3 is spread more in this area
83 if (!$this->getStaticInfoName('CURRENCIES', $this->currency)) {
84 $this->currency = 'EUR';
85 }
86 $this->currencyInfo = $this->loadCurrencyInfo($this->currency);
87 $this->defaultCountry = $conf['countryCode'];
88
89 if (!$this->defaultCountry) {
90 $this->defaultCountry = trim($this->conf['countryCode']);
91 }
92 if (!$this->getStaticInfoName('COUNTRIES', $this->defaultCountry)) {
93 $this->defaultCountry = 'DEU';
94 }
95
96 $this->defaultCountryZone = $conf['countryZoneCode'];
97 if (!$this->defaultCountryZone) {
98 $this->defaultCountryZone = trim($this->conf['countryZoneCode']);
99 }
100 if (!$this->getStaticInfoName('SUBDIVISIONS', $this->defaultCountryZone, $this->defaultCountry)) {
101 if ($this->defaultCountry == 'DEU') {
102 $this->defaultCountryZone = 'NW';
103 } else {
104 $this->defaultCountryZone = '';
105 }
106 }
107
108 $this->defaultLanguage = $conf['languageCode'];
109 if (!$this->defaultLanguage) {
110 $this->defaultLanguage = trim($this->conf['languageCode']);
111 }
112 if (!$this->getStaticInfoName('LANGUAGES', $this->defaultLanguage)) {
113 $this->defaultLanguage = 'EN';
114 }
115 $this->bHasBeenInitialised = TRUE;
116 return TRUE;
117 }
118
119 /**
120 * Getting the name of a country, country subdivision, currency, language, tax
121 *
122 * @param string Defines the type of entry of the requested name: 'TERRIRORIES', 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES', 'LANGUAGES'
123 * @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
124 * @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)
125 * @param string Not used
126 * @param boolean local name only - if set local title is returned
127 * @return string The name of the object in the current language
128 */
129 public function getStaticInfoName ($type = 'COUNTRIES', $code, $country = '', $countrySubdivision = '', $local = FALSE) {
130 $names = FALSE;
131 if (in_array($type, $this->types) && trim($code)) {
132 $codeArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', ($code));
133 $tableName = $this->tables[$type];
134 if (!$tableName) {
135 return FALSE;
136 }
137 $nameArray = array();
138 foreach ($codeArray as $item) {
139 $isoCodeArray = array();
140 $isoCodeArray[] = $item;
141 switch ($type) {
142 case 'SUBDIVISIONS':
143 $isoCodeArray[] = trim($country) ? trim($country) : $this->defaultCountry;
144 break;
145 case 'LANGUAGES':
146 $isoCodeArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode( '_', $code, 1);
147 break;
148 }
149 $nameArray[] = \SJBR\StaticInfoTables\Utility\LocalizationUtility::translate(array('iso' => $isoCodeArray), $tableName, $local);
150 }
151 $names = implode(',', $nameArray);
152 }
153 return $names;
154 }
155
156 /**
157 * Buils a HTML drop-down selector of countries, country subdivisions, currencies or languages
158 *
159 * @param string Defines the type of entries to be presented in the drop-down selector: 'COUNTRIES', 'SUBDIVISIONS', 'CURRENCIES' or 'LANGUAGES'
160 * @param string A value for the name attribute of the <select> tag
161 * @param string A value for the class attribute of the <select> tag
162 * @param array 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
163 * @param string 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)
164 * @param boolean/string If set to 1, an onchange attribute will be added to the <select> tag for immediate submit of the changed value; if set to other than 1, overrides the onchange script
165 * @param string A value for the id attribute of the <select> tag
166 * @param string A value for the title attribute of the <select> tag
167 * @param string A where clause for the records
168 * @param string language to be used
169 * @param boolean $local: If set, we are looking for the "local" title field
170 * @param array additional array to be merged as key => value pair
171 * @param int max elements that can be selected. Default: 1
172 * @param array out: resulting selected array with the ISO alpha-3 code of the countries
173 * @return string A set of HTML <select> and <option> tags
174 */
175 public function buildStaticInfoSelector ($type='COUNTRIES', $name='', $class='', $selectedArray=array(), $country='', $submit=0, $id='', $title='', $addWhere='', $lang='', $local=FALSE, $mergeArray=array(), $size=1, &$outSelectedArray=array()) {
176
177 if ($size > 1) {
178 $multiple = ' multiple="multiple"';
179 $name .= '[]';
180 } else {
181 $multiple="";
182 }
183 if (isset($selectedArray) && !is_array($selectedArray)) {
184 $selectedArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode (',', $selectedArray);
185 }
186
187 $charset = $GLOBALS['TSFE']->renderCharset;
188 $country = trim($country);
189 $nameAttribute = (trim($name)) ? 'name="'.htmlspecialchars(trim($name), ENT_COMPAT, $charset).'" ' : '';
190 $classAttribute = (trim($class)) ? 'class="'.htmlspecialchars(trim($class), ENT_COMPAT, $charset).'" ' : '';
191 $idAttribute = (trim($id)) ? 'id="'.htmlspecialchars(trim($id), ENT_COMPAT, $charset).'" ' : '';
192 $titleAttribute = (trim($title)) ? 'title="'.htmlspecialchars(trim($title), ENT_COMPAT, $charset).'" ' : '';
193 $onchangeAttribute = '';
194 if ($submit) {
195 if ($submit == 1) {
196 $onchangeAttribute = $this->conf['onChangeAttribute'];
197 } else {
198 $onchangeAttribute = $submit;
199 }
200 $onchangeAttribute = str_replace('"', '\'', $onchangeAttribute);
201 $onchangeAttribute = $this->quoteJSvalue($onchangeAttribute);
202 $onchangeAttribute = 'onchange='.$onchangeAttribute;
203 }
204 $selector = '<select size="'.$size.'" '.$idAttribute.$nameAttribute.$titleAttribute.$classAttribute.$onchangeAttribute.$multiple.'>'.chr(10);
205
206 switch ($type) {
207 case 'COUNTRIES':
208 $nameArray = $this->initCountries('ALL', $lang, $local, $addWhere);
209 $defaultSelectedArray = array($this->defaultCountry);
210 break;
211 case 'SUBDIVISIONS':
212 $param = (trim($country) ? trim($country) : $this->defaultCountry);
213 $nameArray = $this->initCountrySubdivisions($param, $addWhere);
214 if ($param == $this->defaultCountry) {
215 $defaultSelectedArray = array($this->defaultCountryZone);
216 }
217 break;
218 case 'CURRENCIES':
219 $nameArray = $this->initCurrencies($addWhere);
220 $defaultSelectedArray = array($this->currency);
221 break;
222 case 'LANGUAGES':
223 $nameArray = $this->initLanguages($addWhere);
224 $defaultSelectedArray = array($this->defaultLanguage);
225 break;
226 }
227
228 if (!$defaultSelectedArray) {
229 reset($nameArray);
230 $defaultSelectedArray = array(key($nameArray));
231 }
232 $bEmptySelected = (empty($selectedArray) || ((count($selectedArray) == 1) && empty($selectedArray[0])));
233 $selectedArray = ((!$bEmptySelected || count($mergeArray)) ? $selectedArray : $defaultSelectedArray);
234
235 if (count($mergeArray)) {
236 $nameArray = array_merge($nameArray, $mergeArray);
237 uasort($nameArray, 'strcoll');
238 }
239
240 if (count($nameArray) > 0) {
241 $selector .= $this->optionsConstructor($nameArray, $selectedArray, $outSelectedArray);
242 $selector .= '</select>'.chr(10);
243 } else {
244 $selector = '';
245 }
246 return $selector;
247 }
248
249 /**
250 * Getting all countries into an array
251 * where the key is the ISO alpha-3 code of the country
252 * and where the value is the name of the country in the current language
253 *
254 * @param string It defines a selection: 'ALL', 'UN', 'EU'
255 * @param string language to be used
256 * @param boolean If set, we are looking for the "local" title field
257 * @param string additional WHERE clause
258 * @return array An array of names of countries
259 */
260 public function initCountries ($param='UN', $lang='', $local=FALSE, $addWhere='') {
261
262 $table = $this->tables['COUNTRIES'];
263 if (!$lang) {
264 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getCurrentLanguage();
265 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getIsoLanguageKey($lang);
266 }
267 $nameArray = array();
268 $titleFields = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getLabelFields($table, $lang, $local);
269 $prefixedTitleFields = array();
270 $prefixedTitleFields[] = $table.'.cn_iso_3';
271 foreach ($titleFields as $titleField) {
272 $prefixedTitleFields[] = $table.'.'.$titleField;
273 }
274
275 array_unique($prefixedTitleFields);
276 $labelFields = implode(',', $prefixedTitleFields);
277 if ($param == 'UN') {
278 $where = 'cn_uno_member=1';
279 } elseif ($param == 'EU') {
280 $where = 'cn_eu_member=1';
281 } elseif ($param == 'ALL') {
282 $where = '1=1';
283 } else {
284 $where = '1=1';
285 }
286
287 $where .= ($addWhere ? ' AND '.$addWhere : '');
288 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
289 $labelFields,
290 $table,
291 $where.$GLOBALS['TSFE']->sys_page->enableFields($table)
292 );
293
294 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
295
296 foreach ($titleFields as $titleField) {
297 if ($row[$titleField]) {
298 $nameArray[$row['cn_iso_3']] = $GLOBALS['TSFE']->csConv($row[$titleField], 'utf-8');
299 break;
300 }
301 }
302 }
303 $GLOBALS['TYPO3_DB']->sql_free_result($res);
304
305 if ($this->conf['countriesAllowed'] != '') {
306 $countriesAllowedArray = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['countriesAllowed']);
307 $newNameArray = array();
308 foreach ($countriesAllowedArray as $iso3) {
309 if (isset($nameArray[$iso3])) {
310 $newNameArray[$iso3] = $nameArray[$iso3];
311 }
312 }
313 $nameArray = $newNameArray;
314 } else {
315 uasort($nameArray, 'strcoll');
316 }
317 return $nameArray;
318 }
319
320
321 /**
322 * Getting all country subdivisions of a given country into an array
323 * where the key is the code of the subdivision
324 * and where the value is the name of the country subdivision in the current language
325 * You can leave the ISO code empty and use the additional WHERE clause instead of it.
326 *
327 * @param string The ISO alpha-3 code of a country
328 * @param string additional WHERE clause
329 * @return array An array of names of country subdivisions
330 */
331 public function initCountrySubdivisions ($param, $addWhere='') {
332
333 $table = $this->tables['SUBDIVISIONS'];
334 if (strlen($param) == 3) {
335 $country = $param;
336 $where = 'zn_country_iso_3='.$GLOBALS['TYPO3_DB']->fullQuoteStr($country,$table);
337 } else {
338 $where = '1=1';
339 }
340 $where .= ($addWhere ? ' AND '.$addWhere : '');
341 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getCurrentLanguage();
342 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getIsoLanguageKey($lang);
343 $nameArray = array();
344 $titleFields = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getLabelFields($table, $lang);
345 $prefixedTitleFields = array();
346 foreach ($titleFields as $titleField) {
347 $prefixedTitleFields[] = $table.'.'.$titleField;
348 }
349 $labelFields = implode(',', $prefixedTitleFields);
350 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
351 $table.'.zn_code,'.$labelFields,
352 $table,
353 $where.
354 $GLOBALS['TSFE']->sys_page->enableFields($table)
355 );
356 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
357 foreach ($titleFields as $titleField) {
358 if ($row[$titleField]) {
359 $nameArray[$row['zn_code']] = $GLOBALS['TSFE']->csConv($row[$titleField], 'utf-8');
360 break;
361 }
362 }
363 }
364 $GLOBALS['TYPO3_DB']->sql_free_result($res);
365 uasort($nameArray, 'strcoll');
366 return $nameArray;
367 }
368
369 /**
370 * Getting all currencies into an array
371 * where the key is the ISO alpha-3 code of the currency
372 * and where the value are the name of the currency in the current language
373 *
374 * @param string additional WHERE clause
375 * @return array An array of names of currencies
376 */
377 public function initCurrencies ($addWhere='') {
378
379 $where = '1=1'.($addWhere ? ' AND '.$addWhere : '');
380 $table = $this->tables['CURRENCIES'];
381 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getCurrentLanguage();
382 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getIsoLanguageKey($lang);
383 $nameArray = array();
384 $titleFields = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getLabelFields($table, $lang);
385 $prefixedTitleFields = array();
386 foreach ($titleFields as $titleField) {
387 $prefixedTitleFields[] = $table.'.'.$titleField;
388 }
389 $labelFields = implode(',', $prefixedTitleFields);
390 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
391 $table.'.cu_iso_3,'.$labelFields,
392 $table,
393 $where.$GLOBALS['TSFE']->sys_page->enableFields($table)
394 );
395 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
396 foreach ($titleFields as $titleField) {
397 if ($row[$titleField]) {
398 $nameArray[$row['cu_iso_3']] = $GLOBALS['TSFE']->csConv($row[$titleField], 'utf-8');
399 break;
400 }
401 }
402 }
403 $GLOBALS['TYPO3_DB']->sql_free_result($res);
404 uasort($nameArray, 'strcoll');
405 return $nameArray;
406 }
407
408 /**
409 * Getting all languages into an array
410 * where the key is the ISO alpha-2 code of the language
411 * and where the value are the name of the language in the current language
412 * Note: we exclude sacred and constructed languages
413 *
414 * @param string additional WHERE clause
415 * @return array An array of names of languages
416 */
417 public function initLanguages ($addWhere='') {
418
419 $where = '1=1' . ($addWhere ? ' AND ' . $addWhere : '');
420 $table = $this->tables['LANGUAGES'];
421 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getCurrentLanguage();
422 $lang = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getIsoLanguageKey($lang);
423 $nameArray = array();
424 $titleFields = \SJBR\StaticInfoTables\Utility\LocalizationUtility::getLabelFields($table, $lang);
425 $prefixedTitleFields = array();
426 foreach ($titleFields as $titleField) {
427 $prefixedTitleFields[] = $table . '.' . $titleField;
428 }
429 $labelFields = implode(',', $prefixedTitleFields);
430 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
431 $table . '.lg_iso_2,' . $table . '.lg_country_iso_2,' . $labelFields,
432 $table,
433 $where . ' AND lg_sacred = 0 AND lg_constructed = 0 ' .
434 $GLOBALS['TSFE']->sys_page->enableFields($table)
435 );
436 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
437 $code = $row['lg_iso_2'].($row['lg_country_iso_2']?'_'.$row['lg_country_iso_2']:'');
438 foreach ($titleFields as $titleField) {
439 if ($row[$titleField]) {
440 $nameArray[$code] = $GLOBALS['TSFE']->csConv($row[$titleField], 'utf-8');
441 break;
442 }
443 }
444 }
445 $GLOBALS['TYPO3_DB']->sql_free_result($res);
446 uasort($nameArray, 'strcoll');
447 return $nameArray;
448 }
449
450 /**
451 * Builds a list of <option> tags
452 *
453 * @param array An array where the values will be the texts of an <option> tags and keys will be the values of the tags
454 * @param string A pre-selected value: if the value appears as a key, the <option> tag will bear a 'selected' attribute
455 * @param array out: resulting selected array with the ISO alpha-3 code of the countries
456 * @return string A string of HTML <option> tags
457 */
458 public function optionsConstructor ($nameArray, $selectedArray=array(), &$outSelectedArray=array()) {
459
460 $options = '';
461 foreach ($nameArray as $value => $name) {
462
463 $options .= '<option value="'.$value.'"';
464 if (in_array($value, $selectedArray)) {
465 $options .= ' selected="selected"';
466 $outSelectedArray[] = $value;
467 }
468 $options .= '>'.$name.'</option>'.chr(10);
469 }
470 if (!isset($outSelectedArray) || count($outSelectedArray) == 0) {
471 reset ($nameArray);
472 $outSelectedArray = array(key($nameArray));
473 }
474 return $options;
475 }
476
477 /**
478 * Loading currency display parameters from Static Info Tables
479 *
480 * @param string An ISO alpha-3 currency code
481 * @return array An array of information regarding the currrency
482 */
483 public function loadCurrencyInfo ($currencyCode) {
484
485 // Fetching the currency record
486 $this->currencyInfo['cu_iso_3'] = trim($currencyCode);
487 $this->currencyInfo['cu_iso_3'] = ($this->currencyInfo['cu_iso_3']) ? $this->currencyInfo['cu_iso_3'] : $this->currency;
488 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
489 '*',
490 'static_currencies',
491 'cu_iso_3='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->currencyInfo['cu_iso_3'],'static_currencies')
492 );
493 // If not found we fetch the default currency!
494 if (!$GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
495 $GLOBALS['TYPO3_DB']->sql_free_result($res);
496 $this->currencyInfo['cu_iso_3'] = $this->currency;
497 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
498 '*',
499 'static_currencies',
500 'cu_iso_3='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->currencyInfo['cu_iso_3'],'static_currencies')
501 );
502 }
503 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
504 $GLOBALS['TYPO3_DB']->sql_free_result($res);
505
506 $this->currencyInfo['cu_name'] = $this->getStaticInfoName('CURRENCIES', $this->currencyInfo['cu_iso_3']);
507 $this->currencyInfo['cu_symbol_left'] = $row['cu_symbol_left'];
508 $this->currencyInfo['cu_symbol_right'] = $row['cu_symbol_right'];
509 $this->currencyInfo['cu_decimal_digits'] = $row['cu_decimal_digits'];
510 $this->currencyInfo['cu_decimal_point'] = $row['cu_decimal_point'];
511 $this->currencyInfo['cu_thousands_point'] = $row['cu_thousands_point'];
512
513 return $this->currencyInfo;
514 }
515
516 /**
517 * Formatting an amount in the currency loaded by loadCurrencyInfo($currencyCode)
518 *
519 * '' - the currency code is not displayed
520 * 'RIGHT' - the code is displayed at the right of the amount
521 * 'LEFT' - the code is displayed at the left of the amount
522 *
523 * @param float An amount to be displayed in the loaded currency
524 * @param string A flag specifying if the the currency code should be displayed:
525 * @return string The formated amounted
526 */
527 public function formatAmount ($amount, $displayCurrencyCode='') {
528 $formatedAmount = '';
529 if ( $displayCurrencyCode == 'LEFT') {
530 $formatedAmount .= $this->currencyInfo['cu_iso_3'].chr(32);
531 }
532 $formatedAmount .= $this->currencyInfo['cu_symbol_left'];
533 $formatedAmount .= number_format($amount, intval($this->currencyInfo['cu_decimal_digits']), $this->currencyInfo['cu_decimal_point'], (($this->currencyInfo['cu_thousands_point'])?$this->currencyInfo['cu_thousands_point']:chr(32)));
534 $formatedAmount .= (($this->currencyInfo['cu_symbol_right'])?chr(32):'').$this->currencyInfo['cu_symbol_right'];
535 if ($displayCurrencyCode == 'RIGHT') {
536 $formatedAmount .= chr(32).$this->currencyInfo['cu_iso_3'];
537 }
538 return $formatedAmount;
539 }
540
541 /**
542 * Formatting an address in the format specified
543 *
544 * @param string A delimiter for the fields of the returned address
545 * @param string A street address
546 * @param string A city
547 * @param string A country subdivision code (zn_code)
548 * @param string A ISO alpha-3 country code (cn_iso_3)
549 * @param string A zip code
550 * @return string The formated address using the country address format (cn_address_format)
551 */
552 public function formatAddress ($delim, $streetAddress, $city, $zip, $subdivisionCode='', $countryCode='') {
553
554 $formatedAddress = '';
555 $countryCode = ($countryCode ? trim($countryCode) : $this->defaultCountry);
556 $subdivisionCode = ($subdivisionCode ? trim($subdivisionCode) : ($countryCode == $this->defaultCountry ? $this->defaultCountryZone : ''));
557
558 // Get country name
559 $countryName = $this->getStaticInfoName('COUNTRIES', $countryCode);
560 if (!$countryName) {
561 return $formatedAddress;
562 }
563
564 // Get address format
565 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
566 'cn_address_format',
567 'static_countries',
568 'cn_iso_3='.$GLOBALS['TYPO3_DB']->fullQuoteStr($countryCode,'static_countries')
569 );
570 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
571 $GLOBALS['TYPO3_DB']->sql_free_result($res);
572 $addressFormat = $row['cn_address_format'];
573
574 // Get country subdivision name
575 $countrySubdivisionName = $this->getStaticInfoName('SUBDIVISIONS', $subdivisionCode, $countryCode);
576
577 // Format the address
578 $formatedAddress = $this->conf['addressFormat.'][$addressFormat];
579 $formatedAddress = str_replace('%street', $streetAddress, $formatedAddress);
580 $formatedAddress = str_replace('%city', $city, $formatedAddress);
581 $formatedAddress = str_replace('%zip', $zip, $formatedAddress);
582 $formatedAddress = str_replace('%countrySubdivisionCode', $subdivisionCode, $formatedAddress);
583 $formatedAddress = str_replace('%countrySubdivisionName', $countrySubdivisionName, $formatedAddress);
584 $formatedAddress = str_replace('%countryName', strtoupper($countryName), $formatedAddress);
585 $formatedAddress = implode($delim, \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(';', $formatedAddress, 1));
586
587 return $formatedAddress;
588 }
589
590 /**
591 * Quotes a string for usage as JS parameter. Depends wheter the value is used in script tags (it must not get `htmlspecialchar'ed in this case because this is done in this function)
592 *
593 * @param string The string to encode.
594 * @param boolean If the values are used inside of <script> tags.
595 * @return string The encoded value already quoted
596 */
597 protected function quoteJSvalue ($value, $inScriptTags=FALSE) {
598 $value = addcslashes($value, '"'.chr(10).chr(13));
599 if (!$inScriptTags) {
600 $value = htmlspecialchars($value, ENT_COMPAT, $GLOBALS['TSFE']->renderCharset);
601 }
602 return '"'.$value.'"';
603 }
604 }
605 class_alias('SJBR\StaticInfoTables\PiBaseApi', 'tx_staticinfotables_pi1');
606 ?>