525d9955f45dbba49a3e953b2f575ea8e9acad90
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Classes / Utility / ImportExportUtility.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Utility;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Utility for import / export
21 * Can be used for API access for simple importing of files
22 *
23 */
24 class ImportExportUtility {
25
26 /**
27 * Import a T3D file directly
28 *
29 * @param string $file The full absolute path to the file
30 * @param int $pid The pid under which the t3d file should be imported
31 * @throws \ErrorException
32 * @throws \InvalidArgumentException
33 * @return array
34 */
35 public function importT3DFile($file, $pid) {
36 $importResponse = array();
37 if (!is_string($file)) {
38 throw new \InvalidArgumentException('Input parameter $file has to be of type string', 1377625645);
39 }
40 if (!is_int($pid)) {
41 throw new \InvalidArgumentException('Input parameter $int has to be of type integer', 1377625646);
42 }
43 /** @var $import \TYPO3\CMS\Impexp\ImportExport */
44 $import = GeneralUtility::makeInstance('TYPO3\\CMS\\Impexp\\ImportExport');
45 $import->init(0, 'import');
46
47 $this->emitAfterImportExportInitialisationSignal($import);
48
49 if ($file && @is_file($file)) {
50 if ($import->loadFile($file, 1)) {
51 // Import to root page:
52 $import->importData($pid);
53 // Get id of container page:
54 $newPages = $import->import_mapId['pages'];
55 reset($newPages);
56 $importResponse = current($newPages);
57 }
58 }
59
60 // Check for errors during the import process:
61 if (empty($importResponse) && $errors = $import->printErrorLog()) {
62 throw new \ErrorException($errors, 1377625537);
63 } else {
64 return $importResponse;
65 }
66 }
67
68 /**
69 * Get the SignalSlot dispatcher
70 *
71 * @return \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
72 */
73 protected function getSignalSlotDispatcher() {
74 return GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
75 }
76
77 /**
78 * Emits a signal after initialization
79 *
80 * @param \TYPO3\CMS\Impexp\ImportExport $import
81 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
82 * @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
83 */
84 protected function emitAfterImportExportInitialisationSignal(\TYPO3\CMS\Impexp\ImportExport $import) {
85 $this->getSignalSlotDispatcher()->dispatch(__CLASS__, 'afterImportExportInitialisation', array($import));
86 }
87 }