Exemplo n.º 1
0
 /**
  * execute the next step of a donation receipt run
  *
  * @return array of stats:
  */
 public function nextStep()
 {
     // some containers
     $exporter_results = array();
     $files = array();
     $profile = $this->snapshot->getProfile();
     // Synchronize this step
     $lock = CRM_Utils_DonrecHelper::getLock('CRM_Donrec_Logic_Engine', 'nextStep');
     if (!$lock->isAcquired()) {
         // lock timed out
         CRM_Core_Error::debug_log_message("de.systopia.donrec - couldn't acquire lock. Timeout is " . $lock->_timeout);
         // compile and return "state of affairs" report
         $stats = $this->createStats();
         $stats['log'] = array();
         $stats['files'] = $files;
         $stats['chunk_size'] = 0;
         CRM_Donrec_Logic_Exporter::addLogEntry($stats, "Couldn't acquire lock. Parallel processing denied. Lock timeout is {$lock->_timeout}s.");
         return $stats;
     }
     // check status
     $is_bulk = !empty($this->parameters['bulk']);
     $is_test = !empty($this->parameters['test']);
     // initialize stuff
     $chunk = $this->snapshot->getNextChunk($is_bulk, $is_test);
     $exporters = $this->getExporters();
     // loop over receipts
     foreach ($chunk as $chunk_id => $chunk_items) {
         // Setup some parameters
         //**********************************
         // Prepare chunk_items:
         // #FIXME: It is more convenient to have a simalar array-structure for bulk-
         // and single-processing. In future the getNextChunk-method might be
         // refactored and build up the arrays correspondingly.
         $chunk_items = $is_bulk ? $chunk_items : array($chunk_items);
         $contact_id = $chunk_items[0]['contact_id'];
         $line_ids = array();
         foreach ($chunk_items as $chunk_item) {
             $line_ids[] = $chunk_item['id'];
         }
         // create a SnapshotReceipt
         $snapshot_receipt = $this->snapshot->getSnapshotReceipt($line_ids, $is_test);
         // call exporters
         //**********************************
         foreach ($exporters as $exporter) {
             $exporter_id = $exporter->getID();
             if ($is_bulk) {
                 $result = $exporter->exportBulk($snapshot_receipt, $is_test);
             } else {
                 $result = $exporter->exportSingle($snapshot_receipt, $is_test);
             }
             if (!isset($exporter_results[$exporter_id])) {
                 $exporter_results[$exporter_id] = array();
                 $exporter_results[$exporter_id]['success'] = 0;
                 $exporter_results[$exporter_id]['failure'] = 0;
             }
             if ($result) {
                 $exporter_results[$exporter_id]['success']++;
             } else {
                 $exporter_results[$exporter_id]['failure']++;
             }
         }
         // save original pdfs and create receipt for non-test-runs
         //**********************************
         if (!$is_test) {
             $receipt_params = array();
             $receipt_params['type'] = $is_bulk ? 'BULK' : 'SINGLE';
             if ($profile->saveOriginalPDF()) {
                 $pdf_file = $this->getPDF($line_ids);
                 $file = CRM_Donrec_Logic_File::createPermanentFile($pdf_file, basename($pdf_file), $contact_id);
                 if (!empty($file)) {
                     $receipt_params['original_file'] = $file['id'];
                 }
             }
             CRM_Donrec_Logic_Receipt::createFromSnapshotReceipt($snapshot_receipt, $receipt_params);
         }
     }
     // The last chunk is empty.
     // If it is the last do some wrap-up.
     // Otherwise mark the chunk as processed.
     //**********************************
     if (!$chunk) {
         foreach ($exporters as $exporter) {
             $result = $exporter->wrapUp($this->snapshot->getId(), $is_test, $is_bulk);
             if (!empty($result['download_name']) && !empty($result['download_url'])) {
                 $files[$exporter->getID()] = array($result['download_name'], $result['download_url']);
             }
         }
     } else {
         $this->snapshot->markChunkProcessed($chunk, $is_test, $is_bulk);
     }
     // compile stats
     //**********************************
     $stats = $this->createStats();
     // create log-messages
     foreach ($exporter_results as $exporter_id => $result) {
         $msg = sprintf('%s processed %d items - %d succeeded, %d failed', $exporter_id, count($chunk), $result['success'], $result['failure']);
         $type = $result['failure'] ? 'ERROR' : 'INFO';
         CRM_Donrec_Logic_Exporter::addLogEntry($stats, $msg, $type);
     }
     $stats['files'] = $files;
     if ($chunk == NULL) {
         $stats['progress'] = 100.0;
     } else {
         $stats['chunk_size'] = count($chunk);
     }
     // release our lock
     $lock->release();
     return $stats;
 }