You are here

function lingotek_update_7502 in Lingotek Translation 7.6

Same name and namespace in other branches
  1. 7.7 lingotek.install \lingotek_update_7502()
  2. 7.5 lingotek.install \lingotek_update_7502()

Migrates comment profile settings

File

./lingotek.install, line 698

Code

function lingotek_update_7502(&$sandbox) {
  lingotek_extend_metadata_tables();

  // migrate the old variable to the new
  $comment_settings = variable_get('lingotek_translate_comments_node_types');

  // old variable
  if (!is_null($comment_settings)) {
    $profiles = variable_get('lingotek_entity_profiles');

    // new variable where profiles will be stored
    $profiles['comment'] = isset($profiles['comment']) && is_array($profiles['comment']) ? $profiles['comment'] : array();
    foreach ($comment_settings as $setting_key => $setting_val) {
      $profiles['comment']['comment_node_' . $setting_key] = $setting_val === $setting_key ? LingotekSync::PROFILE_AUTOMATIC : LingotekSync::PROFILE_DISABLED;
    }
    variable_set('lingotek_entity_profiles', $profiles);
  }

  // deletes the old, now migrated, variables
  variable_del('lingotek_translate_comments_node_types');
  variable_del('lingotek_translate_comments');

  // set assumed status of CURRENT for all comment translations
  // select all comment entity ids in the lingotek_entity_metadata table
  $entity_type = 'comment';
  $query = db_select('lingotek_entity_metadata', 'l');
  $query
    ->fields('l', array(
    'entity_id',
  ))
    ->condition('entity_type', $entity_type);
  $query
    ->distinct('entity_id');
  $result = $query
    ->execute();
  $entity_ids = $result
    ->fetchCol();

  // foreach entity, set the target statuses
  if (!empty($entity_ids)) {
    $entities = entity_load('comment', $entity_ids);
    foreach ($entities as $entity) {
      $entity_id = $entity->cid;
      foreach (array_keys(Lingotek::getLanguages()) as $lingotek_locale) {
        if (Lingotek::convertDrupal2Lingotek($entity->language) != $lingotek_locale) {

          // only add status when not the source language
          LingotekSync::setTargetStatus($entity_type, $entity_id, $lingotek_locale, LingotekSync::STATUS_CURRENT);
        }
        else {

          // remove status when source language equal to target language
          lingotek_keystore_delete($entity_type, $entity_id, "target_sync_status_{$lingotek_locale}");
          lingotek_keystore($entity_type, $entity_id, 'upload_status', LingotekSync::STATUS_CURRENT);
        }
      }
    }
  }
}