/** * Generate export file of current result */ protected function _genExport($po_result, $ps_output_type, $ps_output_filename, $ps_title = null) { $this->view->setVar('criteria_summary', $vs_criteria_summary = $this->getCriteriaForDisplay()); // add displayable description of current search/browse parameters $this->view->setVar('criteria_summary_truncated', mb_substr($vs_criteria_summary, 0, 60) . (mb_strlen($vs_criteria_summary) > 60 ? '...' : '')); $po_result->seek(0); // reset result before exporting anything $this->opo_result_context->setParameter('last_export_type', $ps_output_type); $this->opo_result_context->saveContext(); if (substr($ps_output_type, 0, 4) !== '_pdf') { switch ($ps_output_type) { case '_xlsx': require_once __CA_LIB_DIR__ . "/core/Parsers/PHPExcel/PHPExcel.php"; require_once __CA_LIB_DIR__ . "/core/Parsers/PHPExcel/PHPExcel/Writer/Excel2007.php"; $this->render('Results/xlsx_results.php'); return; case '_docx': require_once __CA_LIB_DIR__ . "/core/Parsers/PHPWord/Autoloader.php"; \PhpOffice\PhpWord\Autoloader::register(); $this->render('Results/docx_results.php'); return; case '_csv': $vs_delimiter = ","; $vs_output_file_name = mb_substr(preg_replace("/[^A-Za-z0-9\\-]+/", '_', $ps_output_filename . '_csv'), 0, 30); $vs_file_extension = 'txt'; $vs_mimetype = "text/plain"; break; case '_tab': $vs_delimiter = "\t"; $vs_output_file_name = mb_substr(preg_replace("/[^A-Za-z0-9\\-]+/", '_', $ps_output_filename . '_tab'), 0, 30); $vs_file_extension = 'txt'; $vs_mimetype = "text/plain"; default: break; } header("Content-Disposition: attachment; filename=export_" . $vs_output_file_name . "." . $vs_file_extension); header("Content-type: " . $vs_mimetype); // get display list self::Index(null, null); $va_display_list = $this->view->getVar('display_list'); $va_rows = array(); // output header $va_row = array(); foreach ($va_display_list as $va_display_item) { $va_row[] = $va_display_item['display']; } $va_rows[] = join($vs_delimiter, $va_row); $po_result->seek(0); $t_display = $this->view->getVar('t_display'); while ($po_result->nextHit()) { $va_row = array(); foreach ($va_display_list as $vn_placement_id => $va_display_item) { $vs_value = html_entity_decode($t_display->getDisplayValue($po_result, $vn_placement_id, array('convert_codes_to_display_text' => true, 'convertLineBreaks' => false)), ENT_QUOTES, 'UTF-8'); $vs_value = preg_replace("![\r\n\t]+!", " ", $vs_value); // quote values as required if (preg_match("![^A-Za-z0-9 .;]+!", $vs_value)) { $vs_value = '"' . str_replace('"', '""', $vs_value) . '"'; } $va_row[] = $vs_value; } $va_rows[] = join($vs_delimiter, $va_row); } $this->opo_response->addContent(join("\n", $va_rows), 'view'); } else { // // PDF output // $va_template_info = caGetPrintTemplateDetails('results', substr($ps_output_type, 5)); if (!is_array($va_template_info)) { $this->postError(3110, _t("Could not find view for PDF"), "BaseFindController->PrintSummary()"); return; } try { $this->view->setVar('base_path', $vs_base_path = pathinfo($va_template_info['path'], PATHINFO_DIRNAME) . '/'); $this->view->addViewPath(array($vs_base_path, "{$vs_base_path}/local")); $o_pdf = new PDFRenderer(); $va_page_size = PDFRenderer::getPageSize(caGetOption('pageSize', $va_template_info, 'letter'), 'mm', caGetOption('pageOrientation', $va_template_info, 'portrait')); $vn_page_width = $va_page_size['width']; $vn_page_height = $va_page_size['height']; $this->view->setVar('pageWidth', "{$vn_page_width}mm"); $this->view->setVar('pageHeight', "{$vn_page_height}mm"); $this->view->setVar('marginTop', caGetOption('marginTop', $va_template_info, '0mm')); $this->view->setVar('marginRight', caGetOption('marginRight', $va_template_info, '0mm')); $this->view->setVar('marginBottom', caGetOption('marginBottom', $va_template_info, '0mm')); $this->view->setVar('marginLeft', caGetOption('marginLeft', $va_template_info, '0mm')); $this->view->setVar('PDFRenderer', $o_pdf->getCurrentRendererCode()); $vs_content = $this->render($va_template_info['path']); $o_pdf->setPage(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait'), caGetOption('marginTop', $va_template_info, '0mm'), caGetOption('marginRight', $va_template_info, '0mm'), caGetOption('marginBottom', $va_template_info, '0mm'), caGetOption('marginLeft', $va_template_info, '0mm')); $o_pdf->render($vs_content, array('stream' => true, 'filename' => caGetOption('filename', $va_template_info, 'export_results.pdf'))); } catch (Exception $e) { $this->postError(3100, _t("Could not generate PDF"), "BaseFindController->PrintSummary()"); } return; } }
/** * Generates display for specific bundle or (optionally) a specific repetition in a bundle * ** Right now only attribute bundles are supported for printing ** * * @param array $pa_options Array of options passed through to _initView */ public function PrintBundle($pa_options = null) { list($vn_subject_id, $t_subject) = $this->_initView($pa_options); if (!$this->_checkAccess($t_subject)) { return false; } // // PDF output // $vs_template = substr($this->request->getParameter('template', pString), 5); // get rid of _pdf_ prefix if (!is_array($va_template_info = caGetPrintTemplateDetails('bundles', $vs_template))) { $this->postError(3110, _t("Could not find view for PDF"), "BaseEditorController->PrintBundle()"); return; } // Element code to display $vs_element = $this->request->getParameter('element_code', pString); $vn_attribute_id = $this->request->getParameter('attribute_id', pString); // Does user have access to this element? if ($this->request->user->getBundleAccessLevel($t_subject->tableName(), $vs_element) == __CA_BUNDLE_ACCESS_NONE__) { $this->postError(2320, _t("No access to element"), "BaseEditorController->PrintBundle()"); return; } // Add raw array of values to view if ($vn_attribute_id > 0) { $o_attr = $t_subject->getAttributeByID($vn_attribute_id); if ((int) $o_attr->getRowID() !== (int) $vn_subject_id || (int) $o_attr->getTableNum() !== (int) $t_subject->tableNum()) { $this->postError(2320, _t("Element is not part of current item"), "BaseEditorController->PrintBundle()"); return; } $this->view->setVar('valuesAsAttributeInstances', $va_values = array($o_attr)); } else { $this->view->setVar('valuesAsAttributeInstances', $va_values = $t_subject->getAttributesByElement($vs_element)); } // Extract values into array for easier view processing $va_extracted_values = array(); foreach ($va_values as $o_value) { $va_extracted_values[] = $o_value->getDisplayValues(); } $this->view->setVar('valuesAsElementCodeArrays', $va_extracted_values); $va_barcode_files_to_delete = array(); try { $this->view->setVar('base_path', $vs_base_path = pathinfo($va_template_info['path'], PATHINFO_DIRNAME)); $this->view->addViewPath(array($vs_base_path, "{$vs_base_path}/local")); $va_barcode_files_to_delete += caDoPrintViewTagSubstitution($this->view, $t_subject, $va_template_info['path'], array('checkAccess' => $this->opa_access_values)); $o_pdf = new PDFRenderer(); $this->view->setVar('PDFRenderer', $o_pdf->getCurrentRendererCode()); $va_page_size = PDFRenderer::getPageSize(caGetOption('pageSize', $va_template_info, 'letter'), 'mm', caGetOption('pageOrientation', $va_template_info, 'portrait')); $vn_page_width = $va_page_size['width']; $vn_page_height = $va_page_size['height']; $this->view->setVar('pageWidth', "{$vn_page_width}mm"); $this->view->setVar('pageHeight', "{$vn_page_height}mm"); $this->view->setVar('marginTop', caGetOption('marginTop', $va_template_info, '0mm')); $this->view->setVar('marginRight', caGetOption('marginRight', $va_template_info, '0mm')); $this->view->setVar('marginBottom', caGetOption('marginBottom', $va_template_info, '0mm')); $this->view->setVar('marginLeft', caGetOption('marginLeft', $va_template_info, '0mm')); $vs_content = $this->render($va_template_info['path']); $o_pdf->setPage(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait'), caGetOption('marginTop', $va_template_info, '0mm'), caGetOption('marginRight', $va_template_info, '0mm'), caGetOption('marginBottom', $va_template_info, '0mm'), caGetOption('marginLeft', $va_template_info, '0mm')); $o_pdf->render($vs_content, array('stream' => true, 'filename' => caGetOption('filename', $va_template_info, 'print_bundles.pdf'))); $vb_printed_properly = true; foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); @unlink("{$vs_tmp}.png"); } } catch (Exception $e) { foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); @unlink("{$vs_tmp}.png"); } $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "BaseEditorController->PrintBundle()"); } }