/** * 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; }
/** * 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; }
/** * View Receipts */ function civicrm_api3_donation_receipt_view($params) { // check for missing receipt id parameter if (empty($params['rid'])) { return civicrm_api3_create_error(ts("No 'rid' parameter given.", array('domain' => 'de.systopia.donrec'))); } $receipt = CRM_Donrec_Logic_Receipt::get($params['rid']); if (empty($receipt)) { return civicrm_api3_create_error(sprintf(ts("Receipt with id %d does not exist.", array('domain' => 'de.systopia.donrec')), $params['rid'])); } if (empty($params['name'])) { $name = 'View.pdf'; } else { $name = $params['name']; } $values = $receipt->getAllProperties(); $profile = $receipt->getProfile(); // mark this as DRAFT id ORIGINAL if (empty($values['watermark'])) { $values['status'] = 'DRAFT'; $values['watermark'] = $profile->get('draft_text'); } $pdf = $profile->getTemplate()->generatePDF($values, $parameter); $url = CRM_Donrec_Logic_File::createTemporaryFile($pdf, $name); // and return the result return civicrm_api3_create_success($url); }
/** * 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; }