1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Registry; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Plugin\ListFiles; |
6
|
|
|
use SilverStripe\Assets\Storage\GeneratedAssetHandler; |
7
|
|
|
use SilverStripe\Control\RSS\RSSFeed; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
10
|
|
|
use SilverStripe\ORM\ArrayList; |
11
|
|
|
use SilverStripe\ORM\DataObject; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
13
|
|
|
|
14
|
|
|
class RegistryImportFeed |
15
|
|
|
{ |
16
|
|
|
use Configurable; |
17
|
|
|
use Injectable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The path format to store imported record files in (inside the assets directory) |
21
|
|
|
* |
22
|
|
|
* @config |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private static $storage_path = '_imports/{model}'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The filename to use for storing imported record files. Used by RegistryImportFeedController to save files to. |
29
|
|
|
* |
30
|
|
|
* @config |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $storage_filename = 'import-{date}.csv'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
protected $modelClass; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The class used to manipulate imported feed files on the filesystem |
39
|
|
|
* |
40
|
|
|
* @var GeneratedAssetHandler |
41
|
|
|
*/ |
42
|
|
|
protected $assetHandler; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The "assets" folder name |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $assetsDir; |
50
|
|
|
|
51
|
|
|
public function setModelClass($class) |
52
|
|
|
{ |
53
|
|
|
$this->modelClass = $class; |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getLatest() |
58
|
|
|
{ |
59
|
|
|
$registryPage = RegistryPage::get()->filter(['DataClass' => $this->modelClass])->first(); |
60
|
|
|
if ($registryPage && $registryPage->exists()) { |
61
|
|
|
$files = $this->getImportFiles(); |
62
|
|
|
} else { |
63
|
|
|
// Always return an empty list of the model isn't associated to any RegistryPages |
64
|
|
|
$files = ArrayList::create(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return RSSFeed::create( |
68
|
|
|
$files, |
|
|
|
|
69
|
|
|
'registry-feed/latest/' . $this->sanitiseClassName($this->modelClass), |
|
|
|
|
70
|
|
|
singleton($this->modelClass)->singular_name() . ' data import history' |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the handler used to manipulate the filesystem, and add the ListFiles plugin from Flysystem to inspect |
76
|
|
|
* the contents of a directory |
77
|
|
|
* |
78
|
|
|
* @param GeneratedAssetHandler $handler |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setAssetHandler(GeneratedAssetHandler $handler) |
82
|
|
|
{ |
83
|
|
|
$handler->getFilesystem()->addPlugin(new ListFiles); |
84
|
|
|
|
85
|
|
|
$this->assetHandler = $handler; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the handler used to manipulate the filesystem |
92
|
|
|
* |
93
|
|
|
* @return GeneratedAssetHandler |
94
|
|
|
*/ |
95
|
|
|
public function getAssetHandler() |
96
|
|
|
{ |
97
|
|
|
return $this->assetHandler; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the path that import files will be stored for this model |
102
|
|
|
* |
103
|
|
|
* @param string $modelClass If null, the current model class will be used |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getStoragePath($modelClass = null) |
107
|
|
|
{ |
108
|
|
|
$sanitisedClassName = $this->sanitiseClassName($modelClass ?: $this->modelClass); |
109
|
|
|
return str_replace('{model}', $sanitisedClassName, $this->config()->get('storage_path')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Loop import files in the storage path and push them into an {@link ArrayList} |
114
|
|
|
* |
115
|
|
|
* @return ArrayList |
116
|
|
|
*/ |
117
|
|
|
public function getImportFiles() |
118
|
|
|
{ |
119
|
|
|
$path = $this->getStoragePath(); |
120
|
|
|
$importFiles = $this->getAssetHandler()->getFilesystem()->listFiles($path); |
121
|
|
|
|
122
|
|
|
$files = ArrayList::create(); |
123
|
|
|
|
124
|
|
|
foreach ($importFiles as $importFile) { |
125
|
|
|
$files->push(RegistryImportFeedEntry::create( |
126
|
|
|
$importFile['basename'], |
127
|
|
|
'', |
|
|
|
|
128
|
|
|
DBDatetime::create()->setValue($importFile['timestamp'])->Format(DBDatetime::ISO_DATETIME), |
129
|
|
|
$this->getAssetsDir() . '/' . $importFile['path'] |
130
|
|
|
)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $files; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns a relatively unique filename to storage imported data feeds as |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getImportFilename() |
142
|
|
|
{ |
143
|
|
|
// Note: CLDR date format see DBDatetime |
144
|
|
|
$datetime = DBDatetime::now()->Format('y-MM-dd-HHmmss'); |
145
|
|
|
return str_replace('{date}', $datetime, $this->config()->get('storage_filename')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the assets directory name |
150
|
|
|
* |
151
|
|
|
* @param string $assetsDir |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setAssetsDir($assetsDir) |
155
|
|
|
{ |
156
|
|
|
$this->assetsDir = $assetsDir; |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the assets directory name |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getAssetsDir() |
166
|
|
|
{ |
167
|
|
|
return $this->assetsDir; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* See {@link \SilverStripe\Admin\ModelAdmin::sanitiseClassName} |
172
|
|
|
* |
173
|
|
|
* @param string $class |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function sanitiseClassName($class) |
177
|
|
|
{ |
178
|
|
|
return str_replace('\\', '-', $class); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|