/
ContentImporter.php
177 lines (158 loc) · 5.79 KB
/
ContentImporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Concrete\Core\Backup;
use Concrete\Core\Backup\ContentImporter\Importer\Routine\SpecifiableHomePageRoutineInterface;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\File\File;
use Concrete\Core\File\Importer;
use Concrete\Core\Page\Type\Composer\FormLayoutSetControl;
use Concrete\Core\Tree\Node\Type\FileFolder;
use Core;
class ContentImporter
{
protected static $mcBlockIDs = array();
protected static $ptComposerOutputControlIDs = array();
protected $home;
public function importContentFile($file)
{
$sx = simplexml_load_file($file);
$this->import($sx);
}
public function setHomePage($c)
{
$this->home = $c;
}
public function importContentString($string)
{
$sx = simplexml_load_string($string);
$this->import($sx);
}
public function importXml(\SimpleXMLElement $xml)
{
$this->import($xml);
}
protected function import(\SimpleXMLElement $element)
{
$manager = Core::make('import/item/manager');
foreach ($manager->getImporterRoutines() as $routine) {
if (isset($this->home) && $routine instanceof SpecifiableHomePageRoutineInterface) {
$home = \Page::getByID($this->home->getCollectionID()); // we always need the most recent version.
$routine->setHomePage($home);
}
$routine->import($element);
if (isset($this->home) && $routine instanceof SpecifiableHomePageRoutineInterface) {
// Unset the home page on each item post import
$routine->setHomePage(null);
}
}
}
public static function getMasterCollectionTemporaryBlockIDs()
{
return self::$mcBlockIDs;
}
public static function addMasterCollectionBlockID($b, $id)
{
self::$mcBlockIDs[$b->getBlockID()] = $id;
}
public static function getMasterCollectionTemporaryBlockID($b)
{
if (isset(self::$mcBlockIDs[$b->getBlockID()])) {
return self::$mcBlockIDs[$b->getBlockID()];
}
}
public static function addPageTypeComposerOutputControlID(FormLayoutSetControl $control, $id)
{
self::$ptComposerOutputControlIDs[$id] = $control->getPageTypeComposerFormLayoutSetControlID();
}
public static function getPageTypeComposerFormLayoutSetControlFromTemporaryID($id)
{
if (isset(self::$ptComposerOutputControlIDs[$id])) {
return self::$ptComposerOutputControlIDs[$id];
}
}
public function importFiles($fromPath, $computeThumbnails = true)
{
$fh = new Importer();
if (!$computeThumbnails) {
$fh->setRescanThumbnailsOnImport(false);
$helper = Core::make('helper/file');
}
$contents = Core::make('helper/file')->getDirectoryContents($fromPath);
foreach ($contents as $filename) {
if (!is_dir($fromPath . '/' . $filename)) {
if (preg_match("/([0-9]{12}]*)\_(.*)/", $filename, $matches)) {
// a prefix is already present in the filename.
$fvPrefix = $matches[1];
$fvFilename = $matches[2];
} else {
$fvPrefix = null;
$fvFilename = $filename;
}
$fv = $fh->import($fromPath . '/' . $filename, $fvFilename, null, $fvPrefix);
if (!is_object($fv)) {
throw new \Exception(Importer::getErrorMessage($fv));
}
if (!$computeThumbnails) {
$types = \Concrete\Core\File\Image\Thumbnail\Type\Type::getVersionList();
foreach ($types as $type) {
// since we provide the thumbnails, we're going to get a list of thumbnail types
// and loop through them, assigning them to all the files.
$thumbnailPath = $fromPath . '/' . $type->getHandle() . '/' . $filename;
if (file_exists($thumbnailPath)) {
$fv->importThumbnail($type, $thumbnailPath);
}
}
}
}
}
}
/**
* Moves files with a particular filename to a particular folder.
*
* @param array $fileNames
* @param string|FileFolder $folderName
*/
public function moveFilesByName(array $fileNames, $folder)
{
$app = app();
if (is_string($folder)) {
$folder = FileFolder::getNodeByName($folder);
}
if ($folder) {
$db = $app->make(Connection::class);
foreach ($fileNames as $name) {
$fID = $db->fetchOne('select fID from FileVersions where fvFilename = ?', [$name]);
if ($fID) {
$file = File::getByID($fID);
if ($file) {
$fileNode = $file->getFileNodeObject();
if ($fileNode) {
$fileNode->move($folder);
}
}
}
}
}
}
/**
* Deletes files with a particular filename.
*
* Note: only use this in highly controlled environments, as in resetting sample content.
*
* @param array $fileNames
* @param string|FileFolder $folderName
*/
public function deleteFilesByName(array $fileNames)
{
$app = app();
$db = $app->make(Connection::class);
foreach ($fileNames as $name) {
$fID = $db->fetchOne('select fID from FileVersions where fvFilename = ?', [$name]);
if ($fID) {
$file = File::getByID($fID);
if ($file) {
$file->delete();
}
}
}
}
}