You are here

function drush_acquia_lift_campaign_sync in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 acquia_lift.drush.inc \drush_acquia_lift_campaign_sync()

Syncs the Acquia Lift Campaigns.

This command is an alternative to the UI's "Sync with Lift" buttons.

File

./acquia_lift.drush.inc, line 45
acquia_lift.drush.inc Provides Acquia Lift drush commands.

Code

function drush_acquia_lift_campaign_sync() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.batch');

  // Step 1. Exit if there is no campaign to be synchronized.
  $agents = array();
  foreach (acquia_lift_get_agent_types() as $type => $info) {
    if ($type == 'acquia_lift_target') {
      continue;
    }
    $agents = array_merge($agents, personalize_agent_load_by_type($type));
  }
  if (empty($agents)) {
    drush_log(dt('There is no personalization to be synchronized.'), 'success');
    return;
  }

  // Step 2. Select sync type.
  $options = array(
    'all' => dt('Sync all personalizations'),
    'one' => dt('Sync one personalization'),
  );
  $type_selection = drush_choice($options, dt('Select sync type:'));
  if (!$type_selection) {
    return;
  }

  // Step 3. Based on type, do further selections.
  $agent_names = array_keys($agents);
  switch ($type_selection) {

    // Step 3a. Select all campaigns.
    case 'all':
      $options = array(
        'yes' => dt('Confirm'),
      );
      $options_message = "\n" . dt('Confirm syncing all personalizations:') . "\n- " . implode("\n- ", $agent_names);
      $confirm_selection = drush_choice($options, $options_message);
      if (!$confirm_selection) {
        return;
      }
      break;

    // Step 3b. Select one campaign.
    case 'one':
      $options_message = "\n" . dt('Select a personalization machine name to sync:');
      $name_selection = drush_choice($agent_names, $options_message);
      if (!$name_selection) {
        return;
      }
      $agent_name = $agent_names[$name_selection];
      $agent = $agents[$agent_name];
      $agents = array(
        $agent_name => $agent,
      );
      break;
  }

  // Step 4. Sync selected campaign(s)
  // For each agent, set up a batch job including all the required save operations,
  // then process immediately.
  foreach ($agents as $agent_name => $agent) {
    $message = "\n" . dt('Synchronizing personalization ') . $agent_name . " ...";
    drush_print($message);
    $operations = acquia_lift_get_sync_operations_for_agent($agent);
    $batch_operations = array();
    foreach ($operations as $operation) {
      $batch_operations[] = array(
        'acquia_lift_batch_process_item',
        array(
          $operation,
        ),
      );
    }
    $batch = array(
      'operations' => $batch_operations,
      'finished' => 'acquia_lift_drush_batch_finished',
      'file' => drupal_get_path('module', 'acquia_lift') . '/acquia_lift.batch.inc',
    );

    // Set and process.
    batch_set($batch);
    $batch =& batch_get();
    $batch['progressive'] = false;
    drush_backend_batch_process();
  }
}