ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCachedLanguage.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/GlobalCache/classes/class.ilGlobalCache.php');
3 
11 
12  protected $global_cache;
16  protected $loaded = false;
20  protected $language_key = 'en';
24  protected $translations = array();
28  protected static $instances = array();
29 
30 
34  protected function __construct($language_key) {
41  $this->readFromCache();
42  if (! $this->getLoaded()) {
43  $this->readFromDB();
44  $this->writeToCache();
45  $this->setLoaded(true);
46  }
47  }
48 
49 
53  public function isActive() {
54  return $this->global_cache->isActive();
55  }
56 
57 
58  protected function readFromCache() {
59  if ($this->global_cache->isActive()) {
60  $translations = $this->global_cache->get('translations_' . $this->getLanguageKey());
61  if (is_array($translations)) {
63  $this->setLoaded(true);
64  }
65  }
66  }
67 
68 
69  public function writeToCache() {
70  if ($this->global_cache->isActive()) {
71  $this->global_cache->set('translations_' . $this->getLanguageKey(), $this->getTranslations());
72  }
73  }
74 
75 
76  protected function readFromDB() {
77  global $ilDB;
81  $q = 'SELECT module, lang_array FROM lng_modules WHERE lang_key = %s';
82  $res = $ilDB->queryF($q, array( 'text' ), array( $this->getLanguageKey() ));
83  $translations = array();
84  while ($set = $ilDB->fetchObject($res)) {
85  $lang_array = unserialize($set->lang_array);
86  if (is_array($lang_array)) {
87  $translations[$set->module] = $lang_array;
88  }
89  }
91  }
92 
93 
99  public static function getInstance($key) {
100  if (! isset(self::$instances[$key])) {
101  self::$instances[$key] = new self($key);
102  }
103 
104  return self::$instances[$key];
105  }
106 
107 
108  public function flush() {
109  if ($this->global_cache->isActive()) {
110  $this->global_cache->flush();
111  }
112  $this->readFromDB();
113  $this->writeToCache();
114  }
115 
116 
120  public function setLanguageKey($language_key) {
121  $this->language_key = $language_key;
122  }
123 
124 
128  public function getLanguageKey() {
129  return $this->language_key;
130  }
131 
132 
136  public function setLoaded($loaded) {
137  $this->loaded = $loaded;
138  }
139 
140 
144  public function getLoaded() {
145  return $this->loaded;
146  }
147 
148 
152  public function setTranslations($translations) {
153  $this->translations = $translations;
154  }
155 
156 
160  public function getTranslations() {
161  return $this->translations;
162  }
163 }
164 
165 ?>