function RulesEntityController::delete

Overridden to also delete tags and events.

See also

EntityAPIControllerExportable::delete()

File

includes/rules.core.inc, line 265

Class

RulesEntityController
Make sure loaded rule configs are instantiated right.

Code

public function delete($ids, DatabaseTransaction $transaction = NULL) {
  $transaction = isset($transaction) ? $transaction : db_transaction();
  // Use entity-load as ids may be the names as well as the ids.
  $configs = $ids ? entity_load('rules_config', $ids) : array();
  if ($configs) {
    foreach ($configs as $config) {
      db_delete('rules_trigger')->condition('id', $config->id)
        ->execute();
      db_delete('rules_tags')->condition('id', $config->id)
        ->execute();
      db_delete('rules_dependencies')->condition('id', $config->id)
        ->execute();
    }
  }
  $return = parent::delete($ids, $transaction);
  // Stop event dispatchers when deleting the last rule of an event set.
  $processed = array();
  foreach ($configs as $config) {
    if ($config->getPluginName() != 'reaction rule') {
      continue;
    }
    foreach ($config->events() as $event_name) {
      // Only process each event once.
      if (!empty($processed[$event_name])) {
        continue;
      }
      $processed[$event_name] = TRUE;
      // Check if the event handler implements the event dispatcher interface.
      $handler = rules_get_event_handler($event_name, $config->getEventSettings($event_name));
      if (!$handler instanceof RulesEventDispatcherInterface) {
        continue;
      }
      // Only stop an event dispatcher if there are no rules for it left.
      if ($handler->isWatching() && !rules_config_load_multiple(FALSE, array(
        'event' => $event_name,
        'plugin' => 'reaction rule',
        'active' => TRUE,
      ))) {
        $handler->stopWatching();
      }
    }
  }
  return $return;
}