You are here

public function LingotekProfile::getEntities in Lingotek Translation 7.7

1 call to LingotekProfile::getEntities()
LingotekProfile::delete in lib/Drupal/lingotek/LingotekProfile.php

File

lib/Drupal/lingotek/LingotekProfile.php, line 248
Defines LingotekProfile

Class

LingotekProfile
A class wrapper for Lingotek Profiles

Code

public function getEntities($entity_type = NULL) {
  if (!empty($entity_type)) {

    // get all bundles that belong to the given profile
    $all_bundles = $this
      ->getBundles();
    $bundles = array();
    $entities = array();
    if (isset($all_bundles[$entity_type])) {
      $bundles = array(
        $entity_type => $all_bundles[$entity_type],
      );
    }

    // get all entities that belond to those bundles
    foreach ($bundles as $entity_type => $entity_bundles) {
      if ($entity_type == 'comment') {
        $ref_tables = array();
        foreach (array_keys($entity_bundles) as $key) {
          $tmp_array = explode('_', $key);
          $key = implode('_', array_slice($tmp_array, 2));
          $ref_tables[] = $key;
        }
        $query = db_select('' . $entity_type . '', 'e')
          ->fields('e', array(
          'cid',
        ));
        $query
          ->join('node', 'n', "n.nid = e.nid AND n.type IN ('" . implode("','", $ref_tables) . "')");
        $results = $query
          ->execute()
          ->fetchCol();
        foreach ($results as $id) {
          $entities[] = array(
            'id' => $id,
            'type' => $entity_type,
          );
        }
      }
      else {
        $query = new EntityFieldQuery();
        $query
          ->entityCondition('entity_type', $entity_type)
          ->entityCondition('bundle', array_keys($entity_bundles), 'IN');
        $result = $query
          ->execute();
        unset($query);
        if (isset($result[$entity_type])) {
          foreach ($result[$entity_type] as $id => $entity_data) {
            $entities[] = array(
              'id' => $id,
              'type' => $entity_type,
            );
          }
        }
      }

      // END OPTIMIZED WAY
    }

    // subtract all entities specifically *not* set to the given profile
    $query = db_select('lingotek_entity_metadata', 'lem')
      ->fields('lem', array(
      'entity_id',
      'entity_type',
    ))
      ->condition('lem.entity_key', 'profile')
      ->condition('lem.value', $this
      ->getId(), '!=')
      ->condition('lem.entity_type', $entity_type);
    $result = $query
      ->execute();
    $subtract_entity_ids = $result
      ->fetchAll();
    $doc_ids = lingotek_get_document_id_tree();
    $subtractions = array();
    foreach ($subtract_entity_ids as $sei) {
      if (!isset($subtractions[$sei->entity_type])) {
        $subtractions[$sei->entity_type] = array();
      }
      $subtractions[$sei->entity_type][$sei->entity_id] = TRUE;
    }
    $filtered_entities = array();
    foreach ($entities as $e) {
      if (!isset($subtractions[$e['type']][$e['id']])) {
        if (isset($doc_ids[$e['type']][$e['id']])) {
          $e['document_id'] = $doc_ids[$e['type']][$e['id']];
        }
        $filtered_entities[$e['id']] = $e;
      }
    }

    // add all entities specifically set to the given profile
    $query = db_select('lingotek_entity_metadata', 'lem')
      ->fields('lem', array(
      'entity_id',
      'entity_type',
    ))
      ->condition('lem.entity_key', 'profile')
      ->condition('lem.value', $this
      ->getId());
    if ($entity_type != 'all') {
      $query
        ->condition('lem.entity_type', $entity_type);
    }
    $result = $query
      ->execute();
    $add_entity_ids = $result
      ->fetchAll();
    foreach ($add_entity_ids as $aei) {
      $addition = array(
        'id' => $aei->entity_id,
        'type' => $aei->entity_type,
      );
      if (isset($doc_ids[$aei->entity_type][$aei->entity_id])) {
        $addition['document_id'] = $doc_ids[$aei->entity_type][$aei->entity_id];
      }
      $filtered_entities[$aei->entity_id] = $addition;
    }
    return $filtered_entities;
  }
  else {

    // GATHER LIST OF ENTITIES AND RECURSE
    // gather all bundles for searching, as some entities may be one-offs
    // even though Lingotek is not enabled for the entire bundle.
    self::$profiles[LingotekSync::PROFILE_DISABLED] = TRUE;

    // TODO: CREATE SOME KIND OF FOREACH LOOP
    $all_bundles = lingotek_get_bundles_by_profile_id(array_keys(self::$profiles));
    $all_entities = array();

    // aggregate all entity-type-specific results into a single numbered array
    foreach (array_keys($all_bundles) as $entity_type) {
      $entities = $this
        ->getEntities($entity_type);
      foreach ($entities as $e) {
        $all_entities[] = $e;
      }
    }
    return $all_entities;
  }
}