3

Hello I run this in a form_submit function:

<?php
function generate_xls_form_submit($form, &$form_state) {



$data = array();
  $information = '';

  if($form_state['values']['SelectFormstackForm']){
    $data['FormstackForm'] = $form_state['values']['SelectFormstackForm'];
  }
  if($form_state['values']['FromDate']){
    $data['FromDate'] = $form_state['values']['FromDate'];
  }
  if($form_state['values']['ToDate']){
    $data['ToDate'] = $form_state['values']['ToDate'];
  }

  //if we don't have 3 options, que can not retrieve all we need.
  if(count($data) != 3){
    return NULL;
  }

  // we will retrieve all the nid to get them the lead field values.
  $IdList = submission_generate_xls_get_leads_id($data);

  //total de resultados encontrados.
  $cantidadIdList = count($IdList);

  // Get formstack form field labels from database.
  $labels = submission_generate_xls_get_fomrstack_field_labels($data['FormstackForm']);

  //cargamos la clase de PHPExcel.php
  require_once 'Classes/PHPExcel.php';

  date_default_timezone_set('Europe/London');
  // Create new PHPExcel object
  $objPHPExcel = new PHPExcel();

  // Set properties
  $objPHPExcel->getProperties()->setCreator("Captalis")
                 ->setLastModifiedBy("Captalis")
                 ->setTitle("Leads generados del formulario ." . $data['FormstackForm'])
                 ->setSubject("Leads")
                 ->setDescription("")
                 ->setKeywords("office 2007 openxml php")
                 ->setCategory("Customers");

  //montamos el encabezado
  $vocal = 'B';
  foreach ($labels as $key => $value) {
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($vocal . '3', $value);
    ++$vocal;
  }

  // Rename worksheet
  $objPHPExcel->getActiveSheet()->setTitle('Leads');            

  //build batch operations.
  foreach ($IdList as $key => $value) {
    $operations[] = array('batch_generate_xls_row', array($value, $key, $cantidadIdList));
  }

  // configure a batch
  $batch = array(
      'title' => t('Build excell.'),
      'operations' => $operations,
      'init_message' => t('Batch is starting.'),
      'progress_message' => t('Batch is running...'),
      'error_message' => t('Batch has encountered an error.'),
      'finished' => 'submission_generate_xls_finished',
  );

  batch_set($batch);
}
?>

this is my batch functions:

/**
 *
 *
 */
function batch_generate_xls_row($value, $key, $cantidadIdList, &$context) {


  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = $cantidadIdList;
  }
  $values = submission_generate_xls_get_leads_values($value);

  $vocal = 'B';
  $integer = $key+4;
  foreach ($values as $key => $value) {
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($vocal . $integer, $value);
    ++$vocal;
  }

  $context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;

  $context['sandbox']['progress']++;
  $context['sandbox']['current_node'] = $value;
  $context['message'] = 'este es el procesamiento de ' . $value;
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {

    $context['finished'] =  $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}


/**
 *
 *
 */

function submission_generate_xls_finished($success, $results, $operations) {
  if ($success) {
    // Here we could do something meaningful with the results.
    // We just display the number of nodes we processed...

    drupal_set_message(t('The excell is done'));
    drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);

    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="01simple.xlsx"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;

  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
  }
}

And this is the error watchdog give me:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'batch_generate_xls_row' not found or invalid function name in _batch_process() (line 284 of /home/oscar/webapps/www/drupal/drupal7/includes/batch.inc).

Any help?

Thanks

Oskar

9

I presume batch_generate_xls_row is not defined in your .module file. In this case you need to specify the file in the batch definition:

<?php

$batch = array(
    ...
    'file' => drupal_get_path('module', 'mymodule') . '/pages.inc',
);
?>

See batch_set() documentation for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.