/** * Generate export file of current result */ protected function _genExport($pt_subject, $ps_template, $ps_output_filename, $ps_title = null) { $this->view->setVar('t_subject', $pt_subject); if (substr($ps_template, 0, 5) === '_pdf_') { $va_template_info = caGetPrintTemplateDetails('summary', substr($ps_template, 5)); } elseif (substr($ps_template, 0, 9) === '_display_') { $vn_display_id = substr($ps_template, 9); $t_display = new ca_bundle_displays($vn_display_id); if ($vn_display_id && $t_display->haveAccessToDisplay($this->request->getUserID(), __CA_BUNDLE_DISPLAY_READ_ACCESS__)) { $this->view->setVar('t_display', $t_display); $this->view->setVar('display_id', $vn_display_id); $va_display_list = array(); $va_placements = $t_display->getPlacements(array('settingsOnly' => true)); foreach ($va_placements as $vn_placement_id => $va_display_item) { $va_settings = caUnserializeForDatabase($va_display_item['settings']); // get column header text $vs_header = $va_display_item['display']; if (isset($va_settings['label']) && is_array($va_settings['label'])) { $va_tmp = caExtractValuesByUserLocale(array($va_settings['label'])); if ($vs_tmp = array_shift($va_tmp)) { $vs_header = $vs_tmp; } } $va_display_list[$vn_placement_id] = array('placement_id' => $vn_placement_id, 'bundle_name' => $va_display_item['bundle_name'], 'display' => $vs_header, 'settings' => $va_settings); } $this->view->setVar('placements', $va_display_list); } else { $this->postError(3100, _t("Invalid format %1", $ps_template), "DetailController->_genExport()"); return; } $va_template_info = caGetPrintTemplateDetails('summary', 'summary'); } else { $this->postError(3100, _t("Invalid format %1", $ps_template), "DetailController->_genExport()"); return; } // // PDF output // if (!is_array($va_template_info)) { $this->postError(3110, _t("Could not find view for PDF"), "DetailController->_genExport()"); return; } // // Tag substitution // // Views can contain tags in the form {{{tagname}}}. Some tags, such as "itemType" and "detailType" are defined by // the detail controller. More usefully, you can pull data from the item being detailed by using a valid "get" expression // as a tag (Eg. {{{ca_objects.idno}}}. Even more usefully for some, you can also use a valid bundle display template // (see http://docs.collectiveaccess.org/wiki/Bundle_Display_Templates) as a tag. The template will be evaluated in the // context of the item being detailed. // $va_defined_vars = array_keys($this->view->getAllVars()); // get list defined vars (we don't want to copy over them) $va_tag_list = $this->getTagListForView($va_template_info['path']); // get list of tags in view foreach ($va_tag_list as $vs_tag) { if (in_array($vs_tag, $va_defined_vars)) { continue; } if (strpos($vs_tag, "^") !== false || strpos($vs_tag, "<") !== false) { $this->view->setVar($vs_tag, $pt_subject->getWithTemplate($vs_tag, array('checkAccess' => $this->opa_access_values))); } elseif (strpos($vs_tag, ".") !== false) { $this->view->setVar($vs_tag, $pt_subject->get($vs_tag, array('checkAccess' => $this->opa_access_values))); } else { $this->view->setVar($vs_tag, "?{$vs_tag}"); } } 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")); $vs_content = $this->render($va_template_info['path']); $o_dompdf = new DOMPDF(); $o_dompdf->load_html($vs_content); $o_dompdf->set_paper(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait')); $o_dompdf->set_base_path(caGetPrintTemplateDirectoryPath('summary')); $o_dompdf->render(); $o_dompdf->stream(caGetOption('filename', $va_template_info, 'export_results.pdf')); $vb_printed_properly = true; } catch (Exception $e) { $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "DetailController->_genExport()"); } return; }
/** * 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 ? '...' : '')); $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"; $vs_content = $this->render('Results/xlsx_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: // TODO add exporter code here 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'); // 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")); $vs_content = $this->render($va_template_info['path']); $o_dompdf = new DOMPDF(); $o_dompdf->load_html($vs_content); $o_dompdf->set_paper(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait')); $o_dompdf->set_base_path(caGetPrintTemplateDirectoryPath('results')); $o_dompdf->render(); $o_dompdf->stream(caGetOption('filename', $va_template_info, 'export_results.pdf')); $vb_printed_properly = true; } catch (Exception $e) { $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "BaseFindController->PrintSummary()"); } return; } }
/** * @param $ps_type * @param $ps_template * @param null $pa_options * @return array|bool|false|mixed */ function caGetPrintTemplateDetails($ps_type, $ps_template, $pa_options = null) { $vs_template_path = caGetPrintTemplateDirectoryPath($ps_type); if (file_exists("{$vs_template_path}/local/{$ps_template}.php")) { $vs_template_path = "{$vs_template_path}/local/{$ps_template}.php"; } elseif (file_exists("{$vs_template_path}/{$ps_template}.php")) { $vs_template_path = "{$vs_template_path}/{$ps_template}.php"; } else { return false; } $vs_cache_key = caMakeCacheKeyFromOptions($pa_options, $ps_type . '/' . $vs_template_path); if (ExternalCache::contains($vs_cache_key, 'PrintTemplateDetails')) { $va_list = ExternalCache::fetch($vs_cache_key, 'PrintTemplateDetails'); if (ExternalCache::fetch("{$vs_cache_key}_mtime", 'PrintTemplateDetails') >= filemtime($vs_template_path)) { //Debug::msg('[caGetPrintTemplateDetails] cache hit'); return $va_list; } } //Debug::msg('[caGetPrintTemplateDetails] cache miss'); $vs_template = file_get_contents($vs_template_path); $va_info = array(); foreach (array("@name", "@type", "@pageSize", "@pageOrientation", "@tables", "@marginLeft", "@marginRight", "@marginTop", "@marginBottom", "@horizontalGutter", "@verticalGutter", "@labelWidth", "@labelHeight", "@elementCode") as $vs_tag) { if (preg_match("!{$vs_tag}([^\n\n]+)!", $vs_template, $va_matches)) { $va_info[str_replace("@", "", $vs_tag)] = trim($va_matches[1]); } else { $va_info[str_replace("@", "", $vs_tag)] = null; } } $va_info['tables'] = preg_split("![,;]{1}!", $va_info['tables']); $va_info['path'] = $vs_template_path; ExternalCache::save($vs_cache_key, $va_info, 'PrintTemplateDetails'); ExternalCache::save("{$vs_cache_key}_mtime", filemtime($vs_template_path), 'PrintTemplateDetails'); return $va_info; }
/** * Generate export file of current result */ protected function _genExport($po_result, $ps_template, $ps_output_filename, $ps_title = null) { if ($this->opo_result_context) { $this->opo_result_context->setParameter('last_export_type', $ps_output_type); $this->opo_result_context->saveContext(); } if (substr($ps_template, 0, 5) === '_pdf_') { $va_template_info = caGetPrintTemplateDetails('results', substr($ps_template, 5)); } elseif (substr($ps_template, 0, 9) === '_display_') { $vn_display_id = substr($ps_template, 9); $t_display = new ca_bundle_displays($vn_display_id); if ($vn_display_id && $t_display->haveAccessToDisplay($this->request->getUserID(), __CA_BUNDLE_DISPLAY_READ_ACCESS__)) { $this->view->setVar('display', $t_display); $va_placements = $t_display->getPlacements(array('settingsOnly' => true)); foreach ($va_placements as $vn_placement_id => $va_display_item) { $va_settings = caUnserializeForDatabase($va_display_item['settings']); // get column header text $vs_header = $va_display_item['display']; if (isset($va_settings['label']) && is_array($va_settings['label'])) { $va_tmp = caExtractValuesByUserLocale(array($va_settings['label'])); if ($vs_tmp = array_shift($va_tmp)) { $vs_header = $vs_tmp; } } $va_display_list[$vn_placement_id] = array('placement_id' => $vn_placement_id, 'bundle_name' => $va_display_item['bundle_name'], 'display' => $vs_header, 'settings' => $va_settings); } $this->view->setVar('display_list', $va_display_list); } else { $this->postError(3100, _t("Invalid format %1", $ps_template), "FindController->_genExport()"); return; } $va_template_info = caGetPrintTemplateDetails('results', 'display'); } else { $this->postError(3100, _t("Invalid format %1", $ps_template), "FindController->_genExport()"); return; } // // PDF output // if (!is_array($va_template_info)) { $this->postError(3110, _t("Could not find view for PDF"), "FindController->_genExport()"); 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")); set_time_limit(600); $vs_content = $this->render($va_template_info['path']); $o_dompdf = new DOMPDF(); $o_dompdf->load_html($vs_content); $o_dompdf->set_paper(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait')); $o_dompdf->set_base_path(caGetPrintTemplateDirectoryPath('results')); $o_dompdf->render(); $o_dompdf->stream(caGetOption('filename', $va_template_info, 'export_results.pdf')); $vb_printed_properly = true; } catch (Exception $e) { $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "FindController->_genExport()"); } 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)); $vs_content = $this->render($va_template_info['path']); $o_dompdf = new DOMPDF(); $o_dompdf->load_html($vs_content); $o_dompdf->set_paper(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait')); $o_dompdf->set_base_path(caGetPrintTemplateDirectoryPath('summary')); $o_dompdf->render(); $o_dompdf->stream(caGetOption('filename', $va_template_info, 'print_bundles.pdf')); $vb_printed_properly = true; foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); } } catch (Exception $e) { foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); } $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "BaseEditorController->PrintBundle()"); } }
/** * * * @return array */ function caGetPrintTemplateDetails($ps_type, $ps_template, $pa_options = null) { $vs_template_path = caGetPrintTemplateDirectoryPath($ps_type); if (file_exists("{$vs_template_path}/local/{$ps_template}.php")) { $vs_template_path = "{$vs_template_path}/local/{$ps_template}.php"; } elseif (file_exists("{$vs_template_path}/{$ps_template}.php")) { $vs_template_path = "{$vs_template_path}/{$ps_template}.php"; } else { return false; } if ($o_cache = caGetCacheObject('caPrintTemplatesList_' . $ps_type)) { $vs_cache_key = caMakeCacheKeyFromOptions($pa_options, $ps_type . '/' . $vs_template_path); if (($va_info = $o_cache->load($vs_cache_key)) && ($vn_mtime = $o_cache->load("{$vs_cache_key}_mtime")) >= filemtime($vs_template_path)) { return $va_info; } } $vs_template = file_get_contents($vs_template_path); $va_info = array(); foreach (array("@name", "@type", "@pageSize", "@pageOrientation", "@tables", "@marginLeft", "@marginRight", "@marginTop", "@marginBottom", "@horizontalGutter", "@verticalGutter", "@labelWidth", "@labelHeight") as $vs_tag) { if (preg_match("!{$vs_tag}([^\n\n]+)!", $vs_template, $va_matches)) { $va_info[str_replace("@", "", $vs_tag)] = trim($va_matches[1]); } else { $va_info[str_replace("@", "", $vs_tag)] = null; } } $va_info['tables'] = preg_split("![,;]{1}!", $va_info['tables']); $va_info['path'] = $vs_template_path; if ($o_cache) { $o_cache->save($va_info, $vs_cache_key); $o_cache->save(filemtime($vs_template_path), "{$vs_cache_key}_mtime"); } return $va_info; }
/** * Generates display summary of record data based upon a bundle display for print (PDF) * * @param array $pa_options Array of options passed through to _initView */ public function PrintSummary($pa_options = null) { AssetLoadManager::register('tableList'); list($vn_subject_id, $t_subject) = $this->_initView($pa_options); if (!$this->_checkAccess($t_subject)) { return false; } $t_display = new ca_bundle_displays(); $va_displays = $t_display->getBundleDisplays(array('table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'restrictToTypes' => array($t_subject->getTypeID()))); if (!($vn_display_id = $this->request->getParameter('display_id', pInteger)) || !isset($va_displays[$vn_display_id])) { if (!($vn_display_id = $this->request->user->getVar($t_subject->tableName() . '_summary_display_id')) || !isset($va_displays[$vn_display_id])) { $va_tmp = array_keys($va_displays); $vn_display_id = $va_tmp[0]; } } $this->view->setVar('t_display', $t_display); $this->view->setVar('bundle_displays', $va_displays); // Check validity and access of specified display if ($t_display->load($vn_display_id) && $t_display->haveAccessToDisplay($this->request->getUserID(), __CA_BUNDLE_DISPLAY_READ_ACCESS__)) { $this->view->setVar('display_id', $vn_display_id); $va_placements = $t_display->getPlacements(array('returnAllAvailableIfEmpty' => true, 'table' => $t_subject->tableNum(), 'user_id' => $this->request->getUserID(), 'access' => __CA_BUNDLE_DISPLAY_READ_ACCESS__, 'no_tooltips' => true, 'format' => 'simple', 'settingsOnly' => true)); $va_display_list = array(); foreach ($va_placements as $vn_placement_id => $va_display_item) { $va_settings = caUnserializeForDatabase($va_display_item['settings']); // get column header text $vs_header = $va_display_item['display']; if (isset($va_settings['label']) && is_array($va_settings['label'])) { if ($vs_tmp = array_shift(caExtractValuesByUserLocale(array($va_settings['label'])))) { $vs_header = $vs_tmp; } } $va_display_list[$vn_placement_id] = array('placement_id' => $vn_placement_id, 'bundle_name' => $va_display_item['bundle_name'], 'display' => $vs_header, 'settings' => $va_settings); } $this->view->setVar('placements', $va_display_list); $this->request->user->setVar($t_subject->tableName() . '_summary_display_id', $vn_display_id); $vs_format = $this->request->config->get("summary_print_format"); } else { $this->view->setVar('display_id', null); $this->view->setVar('placements', array()); } // // PDF output // if (!is_array($va_template_info = caGetPrintTemplateDetails('summary', "{$this->ops_table_name}_summary"))) { if (!is_array($va_template_info = caGetPrintTemplateDetails('summary', "summary"))) { $this->postError(3110, _t("Could not find view for PDF"), "BaseEditorController->PrintSummary()"); return; } } $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)); $vs_content = $this->render($va_template_info['path']); $o_dompdf = new DOMPDF(); $o_dompdf->load_html($vs_content); $o_dompdf->set_paper(caGetOption('pageSize', $va_template_info, 'letter'), caGetOption('pageOrientation', $va_template_info, 'portrait')); $o_dompdf->set_base_path(caGetPrintTemplateDirectoryPath('summary')); $o_dompdf->render(); $o_dompdf->stream(caGetOption('filename', $va_template_info, 'print_summary.pdf')); $vb_printed_properly = true; foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); } } catch (Exception $e) { foreach ($va_barcode_files_to_delete as $vs_tmp) { @unlink($vs_tmp); } $vb_printed_properly = false; $this->postError(3100, _t("Could not generate PDF"), "BaseEditorController->PrintSummary()"); } }