You are here

public function advancedStringOverridesContext::offsetExists in String Overrides Advanced 7

(PHP 5 &gt;= 5.0.0)<br/> Whether a offset exists @link http://php.net/manual/en/arrayaccess.offsetexists.php

Parameters

mixed $offset <p>: An offset to check for. </p>

Return value

boolean true on success or false on failure. </p> <p> The return value will be casted to boolean if non-boolean was returned.

File

lib/advancedStringsContext.inc, line 33

Class

advancedStringOverridesContext

Code

public function offsetExists($offset) {

  // Ensure that we've loaded the strings from the cache.
  $this
    ->ensureCacheLoaded();

  // Skip the DB lookup if we can.
  if (!isset($this->cachedStrings[$offset])) {

    // We do not have this translation cached, so get it from the DB.
    $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) {

      // We have the source string at least.
      // Cache translation string or TRUE if no translation exists.
      $this->cachedStrings[$offset] = empty($translation->translation) ? FALSE : $translation->translation;
      if ($translation->version != VERSION) {

        // This is the first use of this string under current Drupal version. Save version
        // and clear cache, to include the string into caching next time. Saved version is
        // also a string-history information for later pruning of the tables.
        db_update('stringoverrides_advanced_source')
          ->fields(array(
          'version' => VERSION,
        ))
          ->condition('lid', $translation->lid)
          ->execute();
        cache_clear_all($this
          ->getCacheKey(), $this
          ->getCacheBinName());
      }
    }
    else {

      // We don't have the source string, cache this as untranslated.
      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;

      // Clear locale cache so this string can be added in a later request.
      cache_clear_all($this
        ->getCacheKey(), $this
        ->getCacheBinName());
    }
  }
  return $this->cachedStrings[$offset] !== FALSE;
}