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