/** * Export a single record using the mapping defined by this exporter and return as string * @param string $ps_exporter_code defines the exporter to use * @param int $pn_record_id Primary key of the record to export. Record type is determined by the table_num field for this exporter. * @param array $pa_options * singleRecord = Gives a signal to the export format implementation that this is a single record export. For certain formats * this might trigger different behavior, for instance the XML export format prepends the item-level output with <?xml ... ?> * in those cases. * rdfMode = Signals the implementation that this is an RDF mode export * logDirectory = path to directory where logs should be written * logLevel = KLogger constant for minimum log level to record. Default is KLogger::INFO. Constants are, in descending order of shrillness: * KLogger::EMERG = Emergency messages (system is unusable) * KLogger::ALERT = Alert messages (action must be taken immediately) * KLogger::CRIT = Critical conditions * KLogger::ERR = Error conditions * KLogger::WARN = Warnings * KLogger::NOTICE = Notices (normal but significant conditions) * KLogger::INFO = Informational messages * KLogger::DEBUG = Debugging messages * logger = Optional ready-to-use instance of KLogger to use for logging/debugging * @return string Exported record as string */ public static function exportRecord($ps_exporter_code, $pn_record_id, $pa_options = array()) { // The variable cache is valid for the whole record export. // It's being modified in ca_data_exporters::processExporterItem // and then reset here if we move on to the next record. ca_data_exporters::$s_variables = array(); $o_log = caGetOption('logger', $pa_options); // only set up new logging facilities if no existing one has been passed down if (!$o_log || !$o_log instanceof KLogger) { $vs_log_dir = caGetOption('logDirectory', $pa_options); if (!file_exists($vs_log_dir) || !is_writable($vs_log_dir)) { $vs_log_dir = caGetTempDirPath(); } if (!($vn_log_level = caGetOption('logLevel', $pa_options))) { $vn_log_level = KLogger::INFO; } $o_log = new KLogger($vs_log_dir, $vn_log_level); } // make sure we pass logger to item processor $pa_options['logger'] = $o_log; ca_data_exporters::$s_instance_cache = array(); $t_exporter = ca_data_exporters::loadExporterByCode($ps_exporter_code); if (!$t_exporter) { $o_log->logError(_t("Failed to load exporter with code '%1' for item with ID %2", $ps_exporter_code, $pn_record_id)); return false; } $o_log->logInfo(_t("Successfully loaded exporter with code '%1' for item with ID %2", $ps_exporter_code, $pn_record_id)); $va_export = array(); foreach ($t_exporter->getTopLevelItems() as $va_item) { $va_export = array_merge($va_export, $t_exporter->processExporterItem($va_item['item_id'], $t_exporter->get('table_num'), $pn_record_id, $pa_options)); } $o_log->logInfo(_t("The export tree for exporter code '%1' and item with ID %2 is now ready to be processed by the export format (i.e. transformed to XML, for example).", $ps_exporter_code, $pn_record_id)); $o_log->logDebug(print_r($va_export, true)); // we may wanna auto-load this? switch ($t_exporter->getSetting('exporter_format')) { case 'XML': $o_export = new ExportXML(); break; case 'MARC': $o_export = new ExportMARC(); break; case 'CSV': $o_export = new ExportCSV(); break; default: return; } $o_export->setLogger($o_log); // if someone wants to mangle the whole tree ... well, go right ahead $o_manager = new ApplicationPluginManager(); $o_manager->hookExportRecord(array('exporter_instance' => $t_exporter, 'record_id' => $pn_record_id, 'export' => &$va_export)); $pa_options['settings'] = $t_exporter->getSettings(); $vs_wrap_before = $t_exporter->getSetting('wrap_before_record'); $vs_wrap_after = $t_exporter->getSetting('wrap_after_record'); if ($vs_wrap_before || $vs_wrap_after) { $pa_options['singleRecord'] = false; } $vs_export = $o_export->processExport($va_export, $pa_options); if (strlen($vs_wrap_before) > 0) { $vs_export = $vs_wrap_before . "\n" . $vs_export; } if (strlen($vs_wrap_after) > 0) { $vs_export = $vs_export . "\n" . $vs_wrap_after; } return $vs_export; }
/** * Export a single record using the mapping defined by this exporter and return as string * @param string $ps_exporter_code defines the exporter to use * @param int $pn_record_id Primary key of the record to export. Record type is determined by the table_num field for this exporter. * @param array $pa_options * singleRecord = Gives a signal to the export format implementation that this is a single record export. For certain formats * this might trigger different behavior, for instance the XML export format prepends the item-level output with <?xml ... ?> * in those cases. * @return string Exported record as string */ public static function exportRecord($ps_exporter_code, $pn_record_id, $pa_options = array()) { ca_data_exporters::$s_instance_cache = array(); $t_exporter = ca_data_exporters::loadExporterByCode($ps_exporter_code); if (!$t_exporter) { return false; } $va_export = array(); foreach ($t_exporter->getTopLevelItems() as $va_item) { $va_export = array_merge($va_export, $t_exporter->processExporterItem($va_item['item_id'], $t_exporter->get('table_num'), $pn_record_id, $pa_options)); } // TODO: we may wanna auto-load this switch ($t_exporter->getSetting('exporter_format')) { case 'XML': $o_export = new ExportXML(); break; case 'MARC': $o_export = new ExportMARC(); break; case 'CSV': $o_export = new ExportCSV(); break; default: return; } // if someone wants to mangle the whole tree ... well, go right ahead $o_manager = new ApplicationPluginManager(); //caDebug($va_export, "Export before tree plugin hook"); if (is_null($va_plugin_export = $o_manager->hookExportRecord(array('exporter_instance' => $t_exporter, 'record_id' => $pn_record_id, 'export' => $va_export)))) { return; // skip this record if plugin returns null } else { $va_export = $va_plugin_export['export']; } //caDebug($va_export,"Export after tree plugin hook"); $pa_options['settings'] = $t_exporter->getSettings(); return $o_export->processExport($va_export, $pa_options); }