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