You are here

public function SearchApiDenormalizedEntityDataSourceController::trackItemChange in Search API Grouping 7.2

Set the tracking status of the given items to "changed"/"dirty".

Unless $dequeue is set to TRUE, this operation is ignored for items whose status is not "indexed".

Parameters

array $item_ids: Either an array with the IDs (entity id or denormalized item id) of the changed items. Or FALSE to mark all items as changed for the given indexes.

array $indexes: The indexes for which the change should be tracked.

bool $dequeue: If set to TRUE, also change the status of queued items.

Throws

SearchApiDataSourceException If any of the indexes doesn't use the same item type as this controller.

Overrides SearchApiAbstractDataSourceController::trackItemChange

1 call to SearchApiDenormalizedEntityDataSourceController::trackItemChange()
SearchApiDenormalizedEntityDataSourceController::trackItemInsert in includes/datasource_denormalized_entity.inc
Start tracking the index status for the given items on the given indexes.

File

includes/datasource_denormalized_entity.inc, line 253
Contains the SearchApiDenormalizedEntityDataSourceController class.

Class

SearchApiDenormalizedEntityDataSourceController
Data source for all entities known to the Entity API.

Code

public function trackItemChange($item_ids, array $indexes, $dequeue = FALSE) {

  // Marking all items as changed deserves a special handling.
  if ($item_ids === FALSE) {
    return $this
      ->startTracking($indexes);
  }
  $entity_type = $this
    ->getEntityType();

  // Fetch the entity id's to deal with.
  if (!empty($item_ids)) {

    // Ensures the given $item_ids are entity ids.
    foreach ($item_ids as $item_id) {
      if (stristr($item_id, SEARCH_API_GROUPING_ENTITY_FIELD_SEPERATOR) !== FALSE) {
        $parts = explode(SEARCH_API_GROUPING_ENTITY_FIELD_SEPERATOR, $item_id);
        $entity_ids[$parts[1]] = $parts[1];
      }
      else {

        // As mixed array are silly - if the id isn't a denormalized one skip
        // the whole processing.
        $entity_ids = drupal_map_assoc($item_ids);
        break;
      }
    }

    // Load entities to deal with.
    $entities = entity_load($entity_type, $entity_ids);
  }
  else {

    // Load all entities.
    $entities = entity_load($entity_type, FALSE);
    $entity_ids = drupal_map_assoc(array_keys($entities));
  }
  foreach ($indexes as $index) {
    $this
      ->checkIndex($index);

    // Creates the permutations for this entity an registers each permutation
    // as item in the table.
    $this
      ->createPermutationItems($index, $entity_type, $entities);

    // Change only if all items or defined set of items can be adjusted.
    // This checks prevents db errors in cases where a specific set was
    // requested but the set wasn't available.
    if (empty($item_ids) || !empty($entity_ids)) {

      // @TODO Check if db_merge() can be used.
      // Update existing entries.
      $update = db_update($this->table)
        ->fields(array(
        $this->changedColumn => REQUEST_TIME,
      ))
        ->condition($this->indexIdColumn, $index->id)
        ->condition('entity_type', $entity_type)
        ->condition($this->changedColumn, 0, $dequeue ? '<=' : '=');
      if (!empty($item_ids)) {
        $update
          ->condition('etid', $entity_ids, 'IN');
      }
      $affected_rows = $update
        ->execute();
    }
  }
}