* @package taoMediaManager */ class FileImporter implements \tao_models_classes_import_ImportHandler, ServiceLocatorAwareInterface, TaskParameterProviderInterface, TaoLoggerAwareInterface { use ImportHandlerHelperTrait { getTaskParameters as getDefaultTaskParameters; } use LoggerAwareTrait; private $instanceUri; public function __construct($instanceUri = null) { $this->instanceUri = $instanceUri; } /** * Returns a textual description of the import format * * @return string */ public function getLabel() { return __('File'); } /** * Returns a form in order to prepare the import * if the import is from a file, the form should include the file element * * @return Form */ public function getForm() { return (new FileImportForm($this->instanceUri)) ->getForm(); } /** * @param \core_kernel_classes_Class $class * @param Form|array $form * @param string|null $userId owner of the resource * @return Report * @throws \common_exception_Error */ public function import($class, $form, $userId = null) { $uploadedFile = $this->fetchUploadedFile($form); try { $service = MediaService::singleton(); $classUri = $class->getUri(); if (!$form instanceof Form && !is_array($form)) { throw new \InvalidArgumentException('Import form should be either a Form object or an array.'); } $instanceUri = $form instanceof Form ? $form->getValue('instanceUri') : (isset($form['instanceUri']) ? $form['instanceUri'] : null); $fileInfo = $form instanceof Form ? $form->getValue('source') : $form['source']; // importing new media if (!$instanceUri || $instanceUri === $classUri) { //if the file is a zip do a zip import if (!\helpers_File::isZipMimeType($fileInfo['type'])) { $mediaResourceUri = $service->createMediaInstance( $uploadedFile, $classUri, \tao_helpers_Uri::decode($form instanceof Form ? $form->getValue('lang') : $form['lang']), $fileInfo['name'], null, $userId ); if (!$mediaResourceUri) { $report = Report::createFailure(__('Fail to import media')); $report->setData(['uriResource' => '']); } else { $report = Report::createSuccess(__('Media imported successfully')); $report->add(Report::createSuccess( __('Imported %s', $fileInfo['name']), ['uriResource' => $mediaResourceUri] // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl )); } } else { $report = $this->getZipImporter()->import($class, $form, $userId); } } else { // editing existing media if (!\helpers_File::isZipMimeType($fileInfo['type'])) { $service->editMediaInstance( $uploadedFile, $instanceUri, \tao_helpers_Uri::decode($form instanceof Form ? $form->getValue('lang') : $form['lang']), $userId ); $report = Report::createSuccess(__('Media imported successfully')); $report->add(Report::createSuccess( __('Edited %s', $fileInfo['name']), ['uriResource' => $instanceUri] // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl )); } else { $report = Report::createFailure(__('You can\'t upload a zip file as a media')); $report->setData(['uriResource' => $instanceUri]); } } } catch (\Exception $e) { $message = $e instanceof \common_exception_UserReadableException ? $e->getUserMessage() : __('An error has occurred. Please contact your administrator.'); $report = Report::createFailure($message); $this->logError($e->getMessage()); $report->setData(['uriResource' => '']); } $this->getUploadService()->remove($uploadedFile); return $report; } /** * Defines the task parameters to be stored for later use. * * @param Form $form * @return array */ public function getTaskParameters(Form $form) { return array_merge( $form->getValues(), $this->getDefaultTaskParameters($form) ); } /** * Get the zip importer for shared stimulus * * @return SharedStimulusPackageImporter */ protected function getZipImporter() { $zipImporter = new SharedStimulusPackageImporter(); $zipImporter->setServiceLocator($this->getServiceLocator()); return $zipImporter; } }