You are here

function lingotek_keystore in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.util.inc \lingotek_keystore()
  2. 7.6 lingotek.util.inc \lingotek_keystore()
29 calls to lingotek_keystore()
LingotekApi::addAdvancedParameters in lib/Drupal/lingotek/LingotekApi.php
Adds advanced parameters for use with addContentDocument and updateContentDocument.
LingotekApi::addContentDocument in lib/Drupal/lingotek/LingotekApi.php
Add a document to the Lingotek platform.
LingotekEntity::preDownload in lib/Drupal/lingotek/LingotekEntity.php
LingotekEntity::__get in lib/Drupal/lingotek/LingotekEntity.php
Magic get for access to node and node properties.
LingotekSync::getTargetStatus in lib/Drupal/lingotek/LingotekSync.php

... See full list

1 string reference to 'lingotek_keystore'
lingotek_setup_node_translation_settings_form_submit in ./lingotek.setup.inc
Node Translation Settings - Form Submit

File

./lingotek.util.inc, line 47
Utility functions.

Code

function lingotek_keystore($entity_type, $entity_id, $key = "", $value = "", $update_on_dup = TRUE) {
  if ($entity_id == 'all') {
    $lingo_node = array();
    $result = db_select('{lingotek_entity_metadata}', 'n')
      ->condition('entity_type', $entity_type)
      ->fields('n', array(
      db_escape_field('entity_id'),
      db_escape_field('entity_key'),
      db_escape_field('value'),
    ))
      ->execute();
    foreach ($result as $row) {
      $lingo_node[$row->nid][$row->entity_key] = check_plain($row->value);
    }
    return $lingo_node;
  }
  if ($entity_id == -1) {
    LingotekLog::error("Invalid -1 entity ID passed to lingotek_keystore().", array(
      '@nid' => $entity_id,
      '@key' => $key,
      '@value' => $value,
    ));
    return FALSE;
  }
  if (is_numeric($entity_id) && $entity_id) {

    //Return an array with all of the keys and values.
    if ($key === "") {
      $lingo_node = array();
      $result = db_select('{lingotek_entity_metadata}', 'n')
        ->fields('n', array(
        db_escape_field('entity_key'),
        db_escape_field('value'),
      ))
        ->condition('entity_type', $entity_type)
        ->condition(db_escape_field('entity_id'), $entity_id)
        ->execute();
      foreach ($result as $row) {
        $lingo_node[$row->entity_key] = check_plain($row->value);
      }
      return $lingo_node;
    }
    elseif ($value === "") {
      $result = db_select('{lingotek_entity_metadata}', 'n')
        ->fields('n', array(
        db_escape_field('value'),
      ))
        ->condition('entity_type', $entity_type)
        ->condition(db_escape_field('entity_id'), $entity_id)
        ->condition(db_escape_field('entity_key'), $key)
        ->execute();
      $row = $result
        ->fetchObject();
      if ($row) {
        return check_plain($row->value);
      }
      else {
        return FALSE;
      }
    }
    else {
      $timestamp = time();
      $row = array(
        db_escape_field('entity_type') => $entity_type,
        db_escape_field('entity_id') => $entity_id,
        db_escape_field('entity_key') => $key,
        db_escape_field('value') => $value,
        db_escape_field('created') => $timestamp,
        db_escape_field('modified') => $timestamp,
      );
      $existing_value = lingotek_keystore($entity_type, $entity_id, $key);
      if ($existing_value === FALSE) {

        // insert
        $success = drupal_write_record('lingotek_entity_metadata', $row);
        return $success ? "{$entity_id} : {$key} => {$value} (INSERTED)" : "{$entity_id} : {$key} !=> {$value} (INSERT FAILED)";
      }
      elseif ($update_on_dup) {

        // key exists -> update
        unset($row[db_escape_field('created')]);

        // retain original created timestamp
        $success = drupal_write_record('lingotek_entity_metadata', $row, array(
          db_escape_field('entity_type'),
          db_escape_field('entity_id'),
          db_escape_field('entity_key'),
        ));
        return $success ? "{$entity_id} : {$key} => {$value} (UPDATED)" : "{$entity_id} : {$key} !=> {$value} (UPDATE FAILED)";
      }
      else {

        // key exists -> ignore (ignore on duplicate key)
        return "{$entity_id} : {$key} => {$existing_value} (IGNORED)";
      }
    }
  }
  else {
    LingotekLog::error("Invalid entity ID (@entity_id) passed to lingotek_keystore().", array(
      '@entity_id' => $entity_id,
      '@key' => $key,
      '@value' => $value,
    ));
    return FALSE;
  }
}