/** * Does EVERYTHING * @todo cleanup interface */ public function index() { $formData = array(); if ($this->hasRequestParameter('classUri')) { if (trim($this->getRequestParameter('classUri')) != '') { $formData['class'] = new core_kernel_classes_Class(tao_helpers_Uri::decode($this->getRequestParameter('classUri'))); } } if ($this->hasRequestParameter('uri') && $this->hasRequestParameter('classUri')) { if (trim($this->getRequestParameter('uri')) != '') { $formData['instance'] = new core_kernel_classes_Resource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); } } $formData['id'] = $this->getRequestParameter('id'); $handlers = $this->getAvailableExportHandlers(); $exporter = $this->getCurrentExporter(); $selectedResource = isset($formData['instance']) ? $formData['instance'] : $formData['class']; $formFactory = new tao_actions_form_Export($handlers, $exporter->getExportForm($selectedResource), $formData); $myForm = $formFactory->getForm(); if (!is_null($exporter)) { $myForm->setValues(array('exportHandler' => get_class($exporter))); } $this->setData('myForm', $myForm->render()); if ($this->hasRequestParameter('exportChooser_sent') && $this->getRequestParameter('exportChooser_sent') == 1) { //use method GET to allow direct file download (not ajax compatible) $exportData = $_GET; if (isset($exportData['instances'])) { $instanceCount = count($exportData['instances']); for ($i = 0; $i < $instanceCount; $i++) { $exportData['instances'][$i] = tao_helpers_Uri::decode($exportData['instances'][$i]); } } elseif (isset($exportData['exportInstance'])) { $exportData['exportInstance'] = tao_helpers_Uri::decode($exportData['exportInstance']); } $file = $exporter->export($exportData, tao_helpers_Export::getExportPath()); if (!is_null($file) && file_exists($file)) { return tao_helpers_Export::outputFile(tao_helpers_Export::getRelativPath($file)); } } $context = Context::getInstance(); $this->setData('export_extension', $context->getExtensionName()); $this->setData('export_module', $context->getModuleName()); $this->setData('export_action', $context->getActionName()); $this->setData('formTitle', __('Export ')); $this->setView('form/export.tpl', 'tao'); }
/** * export a compiled delivery into an archive * * @param core_kernel_classes_Resource $compiledDelivery * @throws Exception * @return string */ public static function exportCompiledDelivery(core_kernel_classes_Resource $compiledDelivery) { $fileName = tao_helpers_Display::textCleaner($compiledDelivery->getLabel()) . '.zip'; $path = tao_helpers_File::concat(array(tao_helpers_Export::getExportPath(), $fileName)); if (!tao_helpers_File::securityCheck($path, true)) { throw new Exception('Unauthorized file name'); } $zipArchive = new ZipArchive(); if ($zipArchive->open($path, ZipArchive::CREATE) !== true) { throw new Exception('Unable to create archive at ' . $path); } $taoDeliveryVersion = common_ext_ExtensionsManager::singleton()->getInstalledVersion('taoDelivery'); $data = array('dir' => array(), 'label' => $compiledDelivery->getLabel(), 'version' => $taoDeliveryVersion); $directories = $compiledDelivery->getPropertyValues(new core_kernel_classes_Property(PROPERTY_COMPILEDDELIVERY_DIRECTORY)); foreach ($directories as $id) { $directory = tao_models_classes_service_FileStorage::singleton()->getDirectoryById($id); tao_helpers_File::addFilesToZip($zipArchive, $directory->getPath(), $directory->getRelativePath()); $data['dir'][$id] = $directory->getRelativePath(); } $runtime = $compiledDelivery->getUniquePropertyValue(new core_kernel_classes_Property(PROPERTY_COMPILEDDELIVERY_RUNTIME)); $serviceCall = tao_models_classes_service_ServiceCall::fromResource($runtime); $data['runtime'] = base64_encode($serviceCall->serializeToString()); $rdfExporter = new tao_models_classes_export_RdfExporter(); $rdfdata = $rdfExporter->getRdfString(array($compiledDelivery)); if (!$zipArchive->addFromString('delivery.rdf', $rdfdata)) { throw common_Exception('Unable to add metadata to exported delivery assembly'); } $data['meta'] = 'delivery.rdf'; $content = json_encode($data); //'<?php return '.common_Utils::toPHPVariableString($data).";"; if (!$zipArchive->addFromString(self::MANIFEST_FILE, $content)) { $zipArchive->close(); unlink($path); throw common_Exception('Unable to add manifest to exported delivery assembly'); } $zipArchive->close(); return $path; }
/** * Create an temp export tree and return path * * @param PortableElementObject $object * @return string */ protected function getZipLocation(PortableElementObject $object) { return \tao_helpers_Export::getExportPath() . DIRECTORY_SEPARATOR . 'pciPackage_' . $object->getTypeIdentifier() . '.zip'; }
/** * Output file with direct download with tao_helpers_export * Filename is extracted from form data * * @param File $file * @throws \common_Exception */ protected function output(File $file) { $tmpFile = \tao_helpers_Export::getExportPath() . DIRECTORY_SEPARATOR . $file->getBasename(); if (($resource = fopen($tmpFile, 'w')) === false) { throw new \common_Exception('Unable to write "' . $file->getPrefix() . '" into tmp folder("' . $tmpFile . '").'); } stream_copy_to_stream($file->readStream(), $resource); fclose($resource); $filename = $this->hasRequestParameter('filename') ? $this->getRequestParameter('filename') . '.csv' : ''; \tao_helpers_Export::outputFile($file->getBasename(), $filename); return; }
private function createZipFile($filename, $exportClasses = array(), $exportFiles = array()) { $zip = new \ZipArchive(); $baseDir = \tao_helpers_Export::getExportPath(); $path = $baseDir . '/' . $filename . '.zip'; if ($zip->open($path, \ZipArchive::CREATE) !== TRUE) { throw new \common_Exception('Unable to create zipfile ' . $path); } if ($zip->numFiles === 0) { $nbFiles = 0; foreach ($exportFiles as $label => $files) { $archivePath = ''; /** @var $class \core_kernel_classes_Class */ if (array_key_exists($label, $exportClasses)) { $archivePath = $exportClasses[$label] . '/'; $zip->addEmptyDir($archivePath); $nbFiles++; } $nbFiles += count($files); //create the directory foreach ($files as $file) { //add each file in the correct directory $link = $file->getUniquePropertyValue(new \core_kernel_classes_Property(MEDIA_LINK)); if ($link instanceof \core_kernel_classes_Literal) { $link = $link->literal; } /** @var FileManagement $fileManagement */ $fileManagement = $this->getServiceManager()->get(FileManagement::SERVICE_ID); $zip->addFromString($archivePath . $file->getLabel(), $fileManagement->getFileStream($link)->getContents()); } } \common_Logger::i("Number of file : " . $zip->numFiles . " / " . $nbFiles); } $zip->close(); return $path; }
/** * render an item as a Qti zip package * @author christophe GARCIA <*****@*****.**> */ public function export() { try { if ($this->getRequestMethod() != Request::HTTP_GET) { throw new \common_exception_NotImplemented('Only GET method is accepted to export QIT Item.'); } if (!$this->hasRequestParameter('id')) { $this->returnFailure(new \common_exception_MissingParameter('required parameter `id` is missing')); } $id = $this->getRequestParameter('id'); $item = new \core_kernel_classes_Resource($id); $itemService = \taoItems_models_classes_ItemsService::singleton(); if ($itemService->hasItemModel($item, array(ItemModel::MODEL_URI))) { $path = \tao_helpers_Export::getExportFile(); $tmpZip = new \ZipArchive(); $tmpZip->open($path, \ZipArchive::CREATE); $exporter = new QTIPackedItemExporter($item, $tmpZip); $exporter->export(array('apip' => false)); $exporter->getZip()->close(); header('Content-Type: application/zip'); \tao_helpers_Http::returnFile($path, false); return; } else { $this->returnFailure(new \common_exception_NotFound('item can\'t be found')); } } catch (\Exception $e) { $this->returnFailure($e); } }
/** * @param $file */ protected function sendFileToClient($file, $test) { setcookie("fileDownload", "true", 0, "/"); tao_helpers_Export::outputFile(tao_helpers_Export::getRelativPath($file)); return; }