PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.3.2-1/lib/pkp/classes/i18n/CountryDAO.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 99 lines | 51 code | 16 blank | 32 comment | 7 complexity | 70e2deddf7300628e029c80d66e1937c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file classes/i18n/CountryDAO.inc.php
  4. *
  5. * Copyright (c) 2000-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class CountryDAO
  9. * @package i18n
  10. *
  11. * @brief Provides methods for loading localized country name data.
  12. *
  13. */
  14. // $Id: CountryDAO.inc.php,v 1.6 2009/04/08 21:34:54 asmecher Exp $
  15. class CountryDAO extends DAO {
  16. var $cache;
  17. /**
  18. * Constructor.
  19. */
  20. function CountryDAO() {
  21. }
  22. /**
  23. * Get the filename of the countries registry file for the given locale.
  24. * @param $locale string Name of locale (optional)
  25. */
  26. function getFilename($locale = null) {
  27. if ($locale === null) $locale = Locale::getLocale();
  28. return "lib/pkp/locale/$locale/countries.xml";
  29. }
  30. function &_getCountryCache($locale = null) {
  31. $caches =& Registry::get('allCountries', true, array());
  32. if (!isset($locale)) $locale = Locale::getLocale();
  33. if (!isset($caches[$locale])) {
  34. import('cache.CacheManager');
  35. $cacheManager =& CacheManager::getManager();
  36. $caches[$locale] = $cacheManager->getFileCache(
  37. 'country', $locale,
  38. array(&$this, '_countryCacheMiss')
  39. );
  40. // Check to see if the data is outdated
  41. $cacheTime = $caches[$locale]->getCacheTime();
  42. if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename())) {
  43. $caches[$locale]->flush();
  44. }
  45. }
  46. return $caches[$locale];
  47. }
  48. function _countryCacheMiss(&$cache, $id) {
  49. $countries =& Registry::get('allCountriesData', true, array());
  50. if (!isset($countries[$id])) {
  51. // Reload country registry file
  52. $xmlDao = new XMLDAO();
  53. $data = $xmlDao->parseStruct($this->getFilename(), array('countries', 'country'));
  54. if (isset($data['countries'])) {
  55. foreach ($data['country'] as $countryData) {
  56. $countries[$id][$countryData['attributes']['code']] = $countryData['attributes']['name'];
  57. }
  58. }
  59. asort($countries[$id]);
  60. $cache->setEntireCache($countries[$id]);
  61. }
  62. return null;
  63. }
  64. /**
  65. * Return a list of all countries.
  66. * @param $locale string Name of locale (optional)
  67. * @return array
  68. */
  69. function &getCountries($locale = null) {
  70. $cache =& $this->_getCountryCache($locale);
  71. return $cache->getContents();
  72. }
  73. /**
  74. * Return a translated country name, given a code.
  75. * @param $locale string Name of locale (optional)
  76. * @return array
  77. */
  78. function getCountry($code, $locale = null) {
  79. $cache =& $this->_getCountryCache($locale);
  80. return $cache->get($code);
  81. }
  82. }
  83. ?>