You are here

function _sheetnode_phpexcel_batch_import_file in Sheetnode 7

Same name and namespace in other branches
  1. 6 modules/sheetnode_phpexcel/sheetnode_phpexcel.import.inc \_sheetnode_phpexcel_batch_import_file()
  2. 7.2 modules/sheetnode_phpexcel/sheetnode_phpexcel.import.inc \_sheetnode_phpexcel_batch_import_file()
1 string reference to '_sheetnode_phpexcel_batch_import_file'
sheetnode_phpexcel_batch_import in modules/sheetnode_phpexcel/sheetnode_phpexcel.module
API function to batch import several files using Batch API.

File

modules/sheetnode_phpexcel/sheetnode_phpexcel.import.inc, line 106
Import sheetnodes through PHPExcel for xls or xlsx spreadsheets.

Code

function _sheetnode_phpexcel_batch_import_file($filename, $filepath, $callback, $params, &$context) {
  module_load_include('inc', 'node', 'node.pages');
  set_time_limit(0);
  global $user;
  if (empty($context['sandbox']['workbook'])) {

    // Load workbook and get number of worksheets.
    $workbook = PHPExcel_IOFactory::load($filepath);
    $context['sandbox']['workbook'] = serialize($workbook);
    $context['sandbox']['total'] = $workbook
      ->getSheetCount();
    $context['sandbox']['current'] = 0;
    $context['sandbox']['filename'] = $filename;
  }
  else {

    // Create sheetnode out of current sheet.
    $workbook = unserialize($context['sandbox']['workbook']);
    $sheet = $workbook
      ->getSheet($context['sandbox']['current'] - 1);
    $node = new StdClass();
    $node->type = 'sheetnode';
    node_object_prepare($node);
    $node->title = $sheet
      ->getTitle();
    $node->name = $user->name;
    $node->language = LANGUAGE_NONE;
    $node->sheetnode['value'] = _sheetnode_phpexcel_import_do($workbook, $sheet);
    $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']) {
    $sheet = $workbook
      ->getSheet($context['sandbox']['current']);
    $context['message'] = t('Now processing sheet %sheet.', array(
      '%sheet' => $sheet
        ->getTitle(),
    ));
    $context['finished'] = $context['sandbox']['current'] / $context['sandbox']['total'];
    $context['sandbox']['current']++;
  }
}