You are here

function _sheetnode_google_batch_import in Sheetnode 6

Same name and namespace in other branches
  1. 7.2 modules/sheetnode_google/sheetnode_google.import.inc \_sheetnode_google_batch_import()
  2. 7 modules/sheetnode_google/sheetnode_google.import.inc \_sheetnode_google_batch_import()
1 string reference to '_sheetnode_google_batch_import'
sheetnode_google_batch_import in modules/sheetnode_google/sheetnode_google.module
API function to batch import several files using Batch API.

File

modules/sheetnode_google/sheetnode_google.import.inc, line 130

Code

function _sheetnode_google_batch_import($username, $password, $key, $callback, $params, &$context) {
  module_load_include('inc', 'node', 'node.pages');
  set_time_limit(0);
  global $user;
  if (empty($context['sandbox']['spreadsheetService'])) {

    // Load workbook and get number of worksheets.
    try {
      if (!empty($username)) {
        $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
        $client = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $service);
      }
      else {
        $client = NULL;
      }
      $context['sandbox']['spreadsheetService'] = $spreadsheetService = new Zend_Gdata_Spreadsheets($client);
      $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
      $query
        ->setSpreadsheetKey($key);
      $context['sandbox']['spreadsheet'] = $spreadsheet = $spreadsheetService
        ->getSpreadsheetEntry($query);
      $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
      $query
        ->setSpreadsheetKey($key);
      $context['sandbox']['worksheets'] = $worksheets = $spreadsheetService
        ->getWorksheetFeed($query);
      $context['sandbox']['total'] = intval($worksheets
        ->getTotalResults()
        ->getText());
      $context['sandbox']['current'] = 0;
    } catch (Exception $e) {

      // TODO: Set the error in Batch API.
      watchdog('sheetnode_google', $e
        ->getMessage(), array(), WATCHDOG_ERROR);
      return;
    }
  }
  else {

    // Create sheetnode out of current sheet.
    $spreadsheetService = $context['sandbox']['spreadsheetService'];
    $spreadsheet = $context['sandbox']['spreadsheet'];
    $worksheets = $context['sandbox']['worksheets'];
    $worksheet = $worksheets->entries[$context['sandbox']['current'] - 1];
    $node = new StdClass();
    $node->type = 'sheetnode';
    node_object_prepare($node);
    $node->title = $worksheet->title->text;
    $node->name = $user->name;
    $node->sheetnode['value'] = _sheetnode_google_import_do($spreadsheetService, $spreadsheet, $worksheet);
    $node->sheetnode['template'] = NULL;

    // Let other modules alter the sheetnode or do other work.
    if (!empty($callback) && function_exists($callback)) {
      $callback($node, $params, $context);
    }
    drupal_alter('sheetnode_import', $node, $params, $context);

    // Save the sheetnode.
    $node = node_submit($node);
    node_save($node);
    if (!empty($node->nid)) {
      $context['results'][] = $node->nid;
    }
  }

  // Update progress information.
  if ($context['sandbox']['current'] < $context['sandbox']['total']) {
    $worksheet = $worksheets->entries[$context['sandbox']['current']];
    $context['message'] = t('Now processing sheet %sheet.', array(
      '%sheet' => $worksheet->title->text,
    ));
    $context['finished'] = $context['sandbox']['current'] / $context['sandbox']['total'];
    $context['sandbox']['current']++;
  }
}