public function import($class, $form, $userId = null)
    {
        try {
            $uploadedFile = $this->fetchUploadedFile($form);
            $resource = new core_kernel_classes_Class($form instanceof Form ? $form->getValue('classUri') : $form['classUri']);

            // unzip the file
            try {
                \helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::LONG);
                $directory = $this->extractArchive($uploadedFile);
                \helpers_TimeOutHelper::reset();
            } catch (\Exception $e) {
                $report = Report::createFailure(__('Unable to extract the archive'));
                $report->setData(['uriResource' => '']);

                return $report;
            }

            // get list of directory in order to create classes
            $iterator = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS),
                \RecursiveIteratorIterator::LEAVES_ONLY
            );

            $service = MediaService::singleton();
            $language = $form instanceof Form ? $form->getValue('lang') : $form['lang'];

            $this->directoryMap = [
                rtrim($directory, DIRECTORY_SEPARATOR) => $resource->getUri()
            ];
            $report = Report::createSuccess(__('Media imported successfully'));

            /** @var $file \SplFileInfo */
            foreach ($iterator as $file) {
                if ($file->isFile()) {
                    $this->logInfo('File ' . $file->getPathname());
                    if (isset($this->directoryMap[$file->getPath()])) {
                        $classUri = $this->directoryMap[$file->getPath()];
                    } else {
                        $classUri = $this->createClass($file->getPath());
                    }

                    $mediaResourceUri = $service->createMediaInstance($file->getRealPath(), $classUri, $language, $file->getFilename(), null, $userId);
                    $report->add(Report::createSuccess(
                        __('Imported %s', substr($file->getRealPath(), strlen($directory))),
                        ['uriResource' => $mediaResourceUri] // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl
                    ));
                }
            }
        } 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' => '']);
        }

        return $report;
    }
public function import($class, $form, $userId = null)
    {
        try {
            $uploadedFile = $this->fetchUploadedFile($form);
            $resource = new core_kernel_classes_Class($form instanceof Form ? $form->getValue('classUri') : $form['classUri']);

            // unzip the file
            try {
                \helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::LONG);
                $directory = $this->extractArchive($uploadedFile);
                \helpers_TimeOutHelper::reset();
            } catch (\Exception $e) {
                $report = Report::createFailure(__('Unable to extract the archive'));
                $report->setData(['uriResource' => '']);

                return $report;
            }

            // get list of directory in order to create classes
            $iterator = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS),
                \RecursiveIteratorIterator::LEAVES_ONLY
            );

            $service = MediaService::singleton();
            $language = $form instanceof Form ? $form->getValue('lang') : $form['lang'];

            $this->directoryMap = [
                rtrim($directory, DIRECTORY_SEPARATOR) => $resource->getUri()
            ];
            $report = Report::createSuccess(__('Media imported successfully'));

            /** @var $file \SplFileInfo */
            foreach ($iterator as $file) {
                if ($file->isFile()) {
                    $this->logInfo('File ' . $file->getPathname());
                    if (isset($this->directoryMap[$file->getPath()])) {
                        $classUri = $this->directoryMap[$file->getPath()];
                    } else {
                        $classUri = $this->createClass($file->getPath());
                    }

                    $mediaResourceUri = $service->createMediaInstance($file->getRealPath(), $classUri, $language, $file->getFilename(), null, $userId);
                    $report->add(Report::createSuccess(
                        __('Imported %s', substr($file->getRealPath(), strlen($directory))),
                        ['uriResource' => $mediaResourceUri] // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl
                    ));
                }
            }
        } 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' => '']);
        }

        return $report;
    }
public function import ( $ class , $ form , $ userId = null ) { try { $ uploadedFile = $ this -> fetchUploadedFile ( $ form ) ; $ resource = new core_kernel_classes_Class ( $ form instanceof Form ? $ form -> getValue ( 'classUri' ) : $ form [ 'classUri' ] ) ; // unzip the file try { \ helpers_TimeOutHelper :: setTimeOutLimit ( \ helpers_TimeOutHelper :: LONG ) ; $ directory = $ this -> extractArchive ( $ uploadedFile ) ; \ helpers_TimeOutHelper :: reset ( ) ; } catch ( \ Exception $ e ) { $ report = Report :: createFailure ( __ ( 'Unable to extract the archive' ) ) ; $ report -> setData ( [ 'uriResource' => '' ] ) ; return $ report ; } // get list of directory in order to create classes $ iterator = new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ directory , \ RecursiveDirectoryIterator :: CURRENT_AS_FILEINFO | \ RecursiveDirectoryIterator :: SKIP_DOTS ) , \ RecursiveIteratorIterator :: LEAVES_ONLY ) ; $ service = MediaService :: singleton ( ) ; $ language = $ form instanceof Form ? $ form -> getValue ( 'lang' ) : $ form [ 'lang' ] ; $ this -> directoryMap = [ rtrim ( $ directory , DIRECTORY_SEPARATOR ) => $ resource -> getUri ( ) ] ; $ report = Report :: createSuccess ( __ ( 'Media imported successfully' ) ) ; /** @var $file \SplFileInfo */ foreach ( $ iterator as $ file ) { if ( $ file -> isFile ( ) ) { $ this -> logInfo ( 'File ' . $ file -> getPathname ( ) ) ; if ( isset ( $ this -> directoryMap [ $ file -> getPath ( ) ] ) ) { $ classUri = $ this -> directoryMap [ $ file -> getPath ( ) ] ; } else { $ classUri = $ this -> createClass ( $ file -> getPath ( ) ) ; } $ mediaResourceUri = $ service -> createMediaInstance ( $ file -> getRealPath ( ) , $ classUri , $ language , $ file -> getFilename ( ) , null , $ userId ) ; $ report -> add ( Report :: createSuccess ( __ ( 'Imported %s' , substr ( $ file -> getRealPath ( ) , strlen ( $ directory ) ) ) , [ 'uriResource' => $ mediaResourceUri ] // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl ) ) ; } } } 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' => '' ] ) ; } return $ report ; }
Starts the import based on the form values

@param \core_kernel_classes_Class   $class
@param \tao_helpers_form_Form|array $form
@param string|null $userId owner of the resource
@return \common_report_Report
Starts the import based on the form values