You are here

function _commerce_discount_rebuild_rules_config in Commerce Discount 7

Actually rebuild the defaults of a given entity.

Parameters

array $discounts: An array of discount entities.

See also

entity_defaults_rebuild()

2 calls to _commerce_discount_rebuild_rules_config()
commerce_discount_commerce_discount_insert in ./commerce_discount.module
Implements hook_commerce_discount_insert().
commerce_discount_commerce_discount_update in ./commerce_discount.module
Implements hook_commerce_discount_update().

File

./commerce_discount.module, line 418
Defines the discount and discount offer entities, bundles and functionality.

Code

function _commerce_discount_rebuild_rules_config(array $discounts) {

  // Return early if we can't acquire a lock on the rules config.
  if (!lock_acquire('entity_rebuild_rules_config')) {
    return;
  }
  $existing_rules_config = FALSE;
  $rules_config_names = array();
  foreach ($discounts as $discount) {
    $rules_config_names[] = commerce_discount_build_rule_machine_name($discount->name);
  }
  $info = entity_get_info('rules_config');
  $hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_rules_config';
  $keys = $info['entity keys'] + array(
    'module' => 'module',
    'status' => 'status',
    'name' => $info['entity keys']['id'],
  );

  // Check for the existence of the module and status columns.
  if (!in_array($keys['status'], $info['schema_fields_sql']['base table']) || !in_array($keys['module'], $info['schema_fields_sql']['base table'])) {
    trigger_error("Missing database columns for the exportable entity 'rules_config' as defined by entity_exportable_schema_fields(). Update the according module and run update.php!", E_USER_WARNING);
    return;
  }

  // Rebuild the discount rules for the given discounts.
  $entities = array();
  foreach (commerce_discount_build_discount_rules($discounts) as $name => $entity) {
    if (!in_array($name, $rules_config_names)) {
      continue;
    }
    $entity->{$keys['name']} = $name;
    $entity->{$keys['module']} = 'commerce_discount';
    $entities[$name] = $entity;
    $existing_rules_config = TRUE;
  }
  drupal_alter($hook, $entities);

  // Check for defaults that disappeared or overridden?
  if (!$existing_rules_config) {
    $statuses = array(
      $keys['status'] => array(
        ENTITY_OVERRIDDEN,
        ENTITY_IN_CODE,
        ENTITY_FIXED,
      ),
    );
    $existing_defaults = entity_load_multiple_by_name('rules_config', FALSE, $statuses);
    foreach ($existing_defaults as $name => $entity) {
      if (empty($entities[$name]) && in_array($name, $rules_config_names)) {
        $entity->is_rebuild = TRUE;
        if (entity_has_status('rules_config', $entity, ENTITY_OVERRIDDEN)) {
          $entity->{$keys['status']} = ENTITY_CUSTOM;
          entity_save('rules_config', $entity);
        }
        else {
          entity_delete('rules_config', $name);
        }
        unset($entity->is_rebuild);
      }
    }
  }

  // Load all existing entities.
  $existing_entities = entity_load_multiple_by_name('rules_config', array_keys($entities));
  foreach ($existing_entities as $name => $entity) {
    if (entity_has_status('rules_config', $entity, ENTITY_CUSTOM)) {

      // If the entity already exists but is not yet marked as overridden, we
      // have to update the status.
      if (!entity_has_status('rules_config', $entity, ENTITY_OVERRIDDEN)) {
        $entity->{$keys['status']} |= ENTITY_OVERRIDDEN;
        $entity->{$keys['module']} = $entities[$name]->{$keys['module']};
        $entity->is_rebuild = TRUE;
        entity_save('rules_config', $entity);
        unset($entity->is_rebuild);
      }

      // The entity is overridden, so we do not need to save the default.
      unset($entities[$name]);
    }
  }

  // Save defaults.
  $originals = array();
  foreach ($entities as $name => $entity) {
    if (!empty($existing_entities[$name])) {

      // Make sure we are updating the existing default.
      $entity->{$keys['id']} = $existing_entities[$name]->{$keys['id']};
      unset($entity->is_new);
    }

    // Pre-populate $entity->original as we already have it. So we avoid
    // loading it again.
    $entity->original = !empty($existing_entities[$name]) ? $existing_entities[$name] : FALSE;

    // Keep original entities for hook_{entity_type}_defaults_rebuild()
    // implementations.
    $originals[$name] = $entity->original;
    if (!isset($entity->{$keys['status']})) {
      $entity->{$keys['status']} = ENTITY_IN_CODE;
    }
    else {
      $entity->{$keys['status']} |= ENTITY_IN_CODE;
    }
    $entity->is_rebuild = TRUE;
    entity_save('rules_config', $entity);
    unset($entity->is_rebuild);
  }

  // Invoke an entity type-specific hook so modules may apply changes, e.g.
  // efficiently rebuild caches.
  module_invoke_all('rules_config_defaults_rebuild', $entities, $originals);
  lock_release('entity_rebuild_rules_config');
}