You are here

function _sheetnode_google_batch_import in Sheetnode 7

Same name and namespace in other branches
  1. 6 modules/sheetnode_google/sheetnode_google.import.inc \_sheetnode_google_batch_import()
  2. 7.2 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 125
Extension to sheetnode for importing from Google sheets.

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;
      }
      $spreadsheetService = new Zend_Gdata_Spreadsheets($client);
      $context['sandbox']['spreadsheetService'] = serialize($spreadsheetService);
      $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
      $query
        ->setSpreadsheetKey($key);
      $spreadsheet = $spreadsheetService
        ->getSpreadsheetEntry($query);
      $context['sandbox']['spreadsheet'] = serialize($spreadsheet);
      $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
      $query
        ->setSpreadsheetKey($key);
      $worksheets = $spreadsheetService
        ->getWorksheetFeed($query);
      $context['sandbox']['worksheets'] = serialize($worksheets);
      $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 = unserialize($context['sandbox']['spreadsheetService']);
    $spreadsheet = unserialize($context['sandbox']['spreadsheet']);
    $worksheets = unserialize($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->language = LANGUAGE_NONE;
    $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']++;
  }
}