You are here

function formassembly_batch_get_forms in FormAssembly 7

Same name and namespace in other branches
  1. 8 formassembly.module \formassembly_batch_get_forms()

Implementation of callback_batch_operation()

Callback function: first batch operation to sync with FormAssembly

Parameters

bool $use_admin: Is the admin index in use?

$context: Required batch context array

2 string references to 'formassembly_batch_get_forms'
drush_formassembly_fasync in ./formassembly.drush.inc
Callback function for drush command to sync with FormAssembly
formassembly_settings_form_submit in ./formassembly.admin.inc
Submit handler for the FormAssembly settings form.

File

./formassembly.module, line 322
Contains hooks implementations and callbacks to non-admin pages.

Code

function formassembly_batch_get_forms($use_admin = false, &$context) {
  if (empty($context['sandbox'])) {

    // Batch has not run before.  Initialize
    $client_id = variable_get('formassembly_oauth_cid', '');
    $client_secret = variable_get('formassembly_oauth_secret', '');
    $request = new FormAssemblyRequest($client_id, $client_secret);
    $token = $request
      ->getToken();
    if ($token) {
      $request
        ->setToken($token);
    }
    else {
      watchdog('formassembly', 'Could not initialize batch.', array(), WATCHDOG_ERROR);
    }
  }
  else {

    // Batch has started processing.  Get the request back from context.
    $request = $context['sandbox']['request'];
  }
  try {

    //$use_admin = variable_get('formassembly_admin_index', FALSE);
    if ($use_admin) {
      $context['message'] = t('Now requesting admin index page %page', array(
        '%page' => $request
          ->getPage(),
      ));
      if ($request
        ->getForms('admin/api_v1/forms/index.json', $use_admin)) {

        //returns true when finished
        $context['finished'] = 1;
      }
      else {
        $context['finished'] = 0.66;
        $context['sandbox']['request'] = $request;
      }
    }
    else {
      if ($request
        ->getForms()) {
        $context['finished'] = 1;
      }
    }
    $context['results']['request'] = $request;
  } catch (Exception $caught) {
    watchdog('formassembly', 'Form form batch request failed. Error message: @message', array(
      '@message' => $caught
        ->getMessage(),
    ), WATCHDOG_ERROR);
  }
}