You are here

public static function LingotekSync::getEntitySourceCount in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.5 lib/Drupal/lingotek/LingotekSync.php \LingotekSync::getEntitySourceCount()
  2. 7.6 lib/Drupal/lingotek/LingotekSync.php \LingotekSync::getEntitySourceCount()
1 call to LingotekSync::getEntitySourceCount()
LingotekSync::getSourceCounts in lib/Drupal/lingotek/LingotekSync.php

File

lib/Drupal/lingotek/LingotekSync.php, line 432
LingotekSync

Class

LingotekSync
A utility class for Lingotek Syncing.

Code

public static function getEntitySourceCount($lingotek_locale, $entity_type = NULL) {
  $managed_entities = lingotek_managed_entity_types();
  $drupal_language_code = Lingotek::convertLingotek2Drupal($lingotek_locale);
  $q = array();
  $total_count = 0;
  foreach ($managed_entities as $m_entity_type => $properties) {
    if (!is_null($entity_type) && $entity_type != $m_entity_type) {
      continue;
    }
    $entity_base_table = $properties['base table'];
    $query = db_select($entity_base_table, 't')
      ->condition('t.language', $drupal_language_code);

    // exclude translation sets (only for nodes)
    if ($entity_base_table == 'node') {
      $tnid_query = db_or();
      $tnid_query
        ->condition('t.tnid', 0);
      $tnid_query
        ->where('t.tnid = t.nid');
      $query
        ->condition($tnid_query);
    }

    // Exclude translation sets for menu_links
    if ($entity_base_table == 'menu_links') {
      $min_query = db_select('menu_links', 'ml')
        ->condition('ml.i18n_tsid', 0, '!=')
        ->groupBy('i18n_tsid');
      $min_query
        ->addExpression('MIN(mlid)', 'minimum');
      $ml_or = db_or();
      $ml_or
        ->condition('t.i18n_tsid', 0);
      $ml_or
        ->condition('t.mlid', $min_query, 'IN');
      $query
        ->condition('t.language', LANGUAGE_NONE, '!=');
      $query
        ->condition($ml_or);
    }

    // exclude disabled entities (including those that have disabled bundles)
    $disabled_profile = LingotekProfile::loadById(LingotekSync::PROFILE_DISABLED);
    $disabled_entities = $disabled_profile
      ->getEntities($entity_type);
    if (count($disabled_entities)) {
      $disabled_entity_ids = array();
      array_walk($disabled_entities, function ($a) use (&$disabled_entity_ids) {
        $disabled_entity_ids[] = $a['id'];
      });
      $enabled_entity_ids = lingotek_get_enabled_entities_by_type($entity_type);
      if (count($disabled_entity_ids) < count($enabled_entity_ids)) {
        $query
          ->condition($properties['entity keys']['id'], array_merge(array(
          -1,
        ), $disabled_entity_ids), "NOT IN");

        //exclude disabled entities
      }
      else {
        $query
          ->condition($properties['entity keys']['id'], array_merge(array(
          -1,
        ), $enabled_entity_ids), "IN");

        //include only eabled entities
      }
    }
    $count = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $total_count += $count;
  }
  return $total_count;
}