View source
<?php
class advancedStringOverridesContext implements ArrayAccess {
protected $langcode;
protected $context;
protected $cachedStrings;
function __construct(advancedStringOverridesLangcode $langcode, $context) {
$this->langcode = $langcode;
$this->context = $context;
}
public function getContext() {
return $this->context;
}
public function offsetExists($offset) {
$this
->ensureCacheLoaded();
if (!isset($this->cachedStrings[$offset])) {
$translation = db_query("SELECT s.lid, t.translation, s.version FROM {stringoverrides_advanced_source} s LEFT JOIN {stringoverrides_advanced_target} t ON s.lid = t.lid AND t.language = :language WHERE s.source = :source AND s.context = :context AND s.textgroup = 'default'", array(
':language' => $this
->getLangcode(),
':source' => $offset,
':context' => (string) $this
->getContext(),
))
->fetchObject();
if ($translation) {
$this->cachedStrings[$offset] = empty($translation->translation) ? FALSE : $translation->translation;
if ($translation->version != VERSION) {
db_update('stringoverrides_advanced_source')
->fields(array(
'version' => VERSION,
))
->condition('lid', $translation->lid)
->execute();
cache_clear_all($this
->getCacheKey(), $this
->getCacheBinName());
}
}
else {
db_merge('stringoverrides_advanced_source')
->insertFields(array(
'location' => request_uri(),
'version' => VERSION,
))
->key(array(
'source' => $offset,
'context' => (string) $this
->getContext(),
'textgroup' => 'default',
))
->execute();
$this->cachedStrings[$offset] = FALSE;
cache_clear_all($this
->getCacheKey(), $this
->getCacheBinName());
}
}
return $this->cachedStrings[$offset] !== FALSE;
}
public function offsetGet($offset) {
$this
->ensureCacheLoaded();
return $this->cachedStrings[$offset] !== FALSE ? $this->cachedStrings[$offset] : $offset;
}
protected function getCacheKey() {
return 'advancedStringOverrides:' . $this
->getLangcode() . ':' . $this
->getContext();
}
protected function getCacheBinName() {
return 'cache';
}
public function getLangcode() {
return $this->langcode
->getLangcode();
}
protected function ensureCacheLoaded() {
if (!isset($this->cachedStrings)) {
if ($cache = cache_get($this
->getCacheKey(), $this
->getCacheBinName())) {
$this->cachedStrings = $cache->data;
}
elseif (lock_acquire($this
->getCacheKey())) {
$result = db_query("SELECT s.source, t.translation, t.language FROM {stringoverrides_advanced_source} s LEFT JOIN {stringoverrides_advanced_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = 'default' AND s.version = :version AND LENGTH(s.source) < :length AND s.context = :context", array(
':language' => $this
->getLangcode(),
':version' => VERSION,
':length' => variable_get('stringoverrides_advanced_cache_length', 75),
':context' => $this
->getContext(),
));
foreach ($result as $data) {
$this->cachedStrings[$data->source] = empty($data->translation) ? FALSE : $data->translation;
}
cache_set($this
->getCacheKey(), $this->cachedStrings, $this
->getCacheBinName());
lock_release($this
->getCacheKey());
}
}
}
public function getAllOverrides() {
$strings = array();
$result = db_query("SELECT s.source, t.translation, t.language FROM {stringoverrides_advanced_source} s INNER JOIN {stringoverrides_advanced_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = 'default' AND s.version = :version AND s.context = :context", array(
':language' => $this
->getLangcode(),
':version' => VERSION,
':context' => $this
->getContext(),
));
foreach ($result as $data) {
$strings[$data->source] = $data->translation;
}
return $strings;
}
public function offsetSet($offset, $value) {
}
public function offsetUnset($offset) {
}
}