You are here

function lingotek_batch_update_entity_languages_by_profile in Lingotek Translation 7.7

1 call to lingotek_batch_update_entity_languages_by_profile()
lingotek_admin_profiles_form in ./lingotek.admin.inc

File

./lingotek.batch.inc, line 804
Central location for batch create functions, before control is handed off to individual batch command files.

Code

function lingotek_batch_update_entity_languages_by_profile($profile_id) {

  // don't make any changes if the new profile is the Disabled profile.
  if ($profile_id == LingotekSync::PROFILE_DISABLED) {
    return;
  }
  $profile = LingotekProfile::loadById($profile_id);

  // get the target languages for the given profile
  $available_locales = lingotek_get_target_locales();
  $target_locales_hash = $profile
    ->filterTargetLocales($available_locales);
  $target_locales = array_keys($target_locales_hash);

  // get the current entities assigned to the given profile
  $entities = $profile
    ->getEntities();
  $operations = array();

  // for each entity with a document ID:
  foreach ($entities as $e) {
    if (!empty($e['document_id'])) {

      // compare the target languages set with the profile's target languages
      $entity_locales = lingotek_get_current_locales($e['type'], $e['id']);

      // for each target language in the profile that is not set, add it
      $locales_to_add = array_diff($target_locales, $entity_locales);
      $operations[] = array(
        'lingotek_add_target_locales',
        array(
          $e,
          $locales_to_add,
          $profile,
        ),
      );

      // for each target language set that is not in the profile anymore, remove it
      $locales_to_remove = array_diff($entity_locales, $target_locales);
      $operations[] = array(
        'lingotek_remove_target_locales',
        array(
          $e,
          $locales_to_remove,
        ),
      );
    }
  }

  // run the batch operation, return the result
  if (!empty($operations)) {
    $batch = array(
      'title' => t('Updating target locales for entities in profile @profile_name', array(
        '@profile_name' => $profile
          ->getName(),
      )),
      'operations' => $operations,
      'finished' => 'lingotek_batch_update_entity_languages_by_profile_finished',
      'file' => 'lingotek.batch.inc',
    );
    batch_set($batch);
    batch_process('admin/settings/lingotek/settings');
  }
}