Exemplo n.º 1
0
 /**
  * generate the final result
  *
  * @return array:
  *          'is_error': set if there is a fatal error
  *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
  *          'download_url: URL to download the result
  *          'download_name: suggested file name for the download
  */
 public function wrapUp($chunk, $is_test, $is_bulk)
 {
     $reply = array();
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'Dummy process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO);
     return $reply;
 }
Exemplo n.º 2
0
 /**
  * generate the final result
  *
  * @return array:
  *          'is_error': set if there is a fatal error
  *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
  *          'download_url: URL to download the result
  *          'download_name: suggested file name for the download
  */
 public function wrapUp($snapshot_id, $is_test, $is_bulk)
 {
     $reply = array();
     // create the zip file
     $config = CRM_Core_Config::singleton();
     $preferredFileName = ts("donation_receipts", array('domain' => 'de.systopia.donrec'));
     $preferredSuffix = ts('.zip', array('domain' => 'de.systopia.donrec'));
     $archiveFileName = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredSuffix);
     $fileURL = $archiveFileName;
     $outerArchive = new ZipArchive();
     $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
     $ids = $snapshot->getIds();
     $toRemove = array();
     // Sort array by page count
     $pageCountArr = array();
     foreach ($ids as $id) {
         $proc_info = $snapshot->getProcessInformation($id);
         if (!empty($proc_info)) {
             $pageCount = isset($proc_info['PDF']['pdf_pagecount']) ? $proc_info['PDF']['pdf_pagecount'] : FALSE;
             $filename = isset($proc_info['PDF']['pdf_file']) ? $proc_info['PDF']['pdf_file'] : FALSE;
             if ($pageCount) {
                 $pageCountArr[$pageCount][] = array($pageCount, $id, $filename);
             }
         }
     }
     // add files to sub-archives
     // open main archive and add sub-archives
     if ($outerArchive->open($fileURL, ZIPARCHIVE::CREATE) === TRUE) {
         foreach ($pageCountArr as $entry) {
             foreach ($entry as $item) {
                 if ($item[0] && $item[2]) {
                     // if page count and file name exists
                     $folder = sprintf(ts('%d-page', array('domain' => 'de.systopia.donrec')), $item[0]) . DIRECTORY_SEPARATOR;
                     $opResult = $outerArchive->addFile($item[2], $folder . basename($item[2]));
                     CRM_Donrec_Logic_Exporter::addLogEntry($reply, "adding <span title='{$item[2]}'>created {$item[0]}-page PDF file</span> ({$opResult})", CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG);
                 }
             }
         }
         if (!$outerArchive->close()) {
             CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'zip->close() returned false!', CRM_Donrec_Logic_Exporter::LOG_TYPE_ERROR);
         }
     } else {
         CRM_Donrec_Logic_Exporter::addLogEntry($reply, sprintf('PDF processing failed: Could not open zip file '), CRM_Donrec_Logic_Exporter::FATAL);
         return $reply;
     }
     $file = CRM_Donrec_Logic_File::createTemporaryFile($fileURL, $preferredFileName . $preferredSuffix);
     CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting ZIP file URL is '{$file}'.");
     if (!empty($file)) {
         $reply['download_name'] = $preferredFileName . $preferredSuffix;
         $reply['download_url'] = $file;
     }
     // remove loose pdf files or store them
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'Removing temporary files.', CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG);
     foreach ($toRemove as $file) {
         unlink($file);
     }
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'PDF generation process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO);
     return $reply;
 }
Exemplo n.º 3
0
 /**
  * generate the final result
  *
  * @return array:
  *          'is_error': set if there is a fatal error
  *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
  *          'download_url: URL to download the result
  *          'download_name: suggested file name for the download
  */
 public function wrapUp($snapshot_id, $is_test, $is_bulk)
 {
     $reply = array();
     // create the zip file
     $config = CRM_Core_Config::singleton();
     $preferredFileName = ts("donation_receipts.zip", array('domain' => 'de.systopia.donrec'));
     $archiveFileName = CRM_Donrec_Logic_File::makeFileName(ts("donation_receipts", array('domain' => 'de.systopia.donrec')), ".zip");
     $zip = new ZipArchive();
     $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
     $ids = $snapshot->getIds();
     $toRemove = array();
     if ($zip->open($archiveFileName, ZIPARCHIVE::CREATE) === TRUE) {
         foreach ($ids as $id) {
             $proc_info = $snapshot->getProcessInformation($id);
             if (!empty($proc_info)) {
                 $filename = isset($proc_info['PDF']['pdf_file']) ? $proc_info['PDF']['pdf_file'] : FALSE;
                 if ($filename) {
                     $toRemove[$id] = $filename;
                     $opResult = $zip->addFile($filename, basename($filename));
                     CRM_Donrec_Logic_Exporter::addLogEntry($reply, "adding <span title='{$filename}'>created PDF file</span> to <span title='{$archiveFileName}'>ZIP archive</span> ({$opResult})", CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG);
                 }
             }
         }
         if (!$zip->close()) {
             CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'zip->close() returned false!', CRM_Donrec_Logic_Exporter::LOG_TYPE_ERROR);
         }
     } else {
         CRM_Donrec_Logic_Exporter::addLogEntry($reply, sprintf('PDF processing failed: Could not open zip file '), CRM_Donrec_Logic_Exporter::LOG_TYPE_FATAL);
         return $reply;
     }
     $file = CRM_Donrec_Logic_File::createTemporaryFile($archiveFileName, $preferredFileName);
     CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting ZIP file URL is '{$file}'.");
     if (!empty($file)) {
         $reply['download_name'] = $preferredFileName;
         $reply['download_url'] = $file;
     }
     // remove loose pdf files or store them
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'Removing temporary PDF files.', CRM_Donrec_Logic_Exporter::LOG_TYPE_DEBUG);
     foreach ($toRemove as $file) {
         unlink($file);
     }
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'PDF generation process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO);
     return $reply;
 }
Exemplo n.º 4
0
 /**
  * generate the final result
  *
  * @return array:
  *          'is_error': set if there is a fatal error
  *          'log': array with keys: 'type', 'level', 'timestamp', 'message'
  *          'download_url: URL to download the result
  *          'download_name: suggested file name for the download
  */
 public function wrapUp($snapshot_id, $is_test, $is_bulk)
 {
     $snapshot = CRM_Donrec_Logic_Snapshot::get($snapshot_id);
     $reply = array();
     // open file
     $preferredFileName = ts('donation_receipts');
     $preferredFileSuffix = ts('.csv', array('domain' => 'de.systopia.donrec'));
     $temp_file = CRM_Donrec_Logic_File::makeFileName($preferredFileName, $preferredFileSuffix);
     $handle = fopen($temp_file, 'w');
     // get headers
     $headers = CRM_Donrec_Logic_ReceiptTokens::getFullTokenList();
     $headers = $this->flattenTokenData($headers);
     $headers = array_keys($headers);
     $header_written = false;
     // write them all into the file
     $ids = $snapshot->getIds();
     foreach ($ids as $id) {
         $proc_info = $snapshot->getProcessInformation($id);
         $csv_data = $proc_info['CSV']['csv_data'];
         if (!empty($csv_data)) {
             if (!$header_written) {
                 // extend header by extra fields
                 $headers = array_merge($headers, array_keys($csv_data));
                 $headers = array_unique($headers);
                 // write header
                 fputcsv($handle, $headers, ';', '"');
                 $header_written = true;
             }
             // create and write a line
             $line = array();
             foreach ($headers as $field) {
                 if (isset($csv_data[$field])) {
                     $line[$field] = $csv_data[$field];
                 } else {
                     $line[$field] = '';
                 }
             }
             fputcsv($handle, $line, ';', '"');
         }
     }
     // get process info iterator
     fclose($handle);
     // create the file
     $file = CRM_Donrec_Logic_File::createTemporaryFile($temp_file, $preferredFileName . $preferredFileSuffix);
     CRM_Core_Error::debug_log_message("de.systopia.donrec: resulting CSV file URL is '{$file}'.");
     if (!empty($file)) {
         $reply['download_name'] = $preferredFileName;
         $reply['download_url'] = $file;
     }
     CRM_Donrec_Logic_Exporter::addLogEntry($reply, 'CSV process ended.', CRM_Donrec_Logic_Exporter::LOG_TYPE_INFO);
     return $reply;
 }
Exemplo n.º 5
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;
 }