-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRegistryImportFeed.php
176 lines (153 loc) · 4.61 KB
/
RegistryImportFeed.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
<?php
namespace SilverStripe\Registry;
use SilverStripe\Assets\Storage\GeneratedAssetHandler;
use SilverStripe\Control\RSS\RSSFeed;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBDatetime;
class RegistryImportFeed
{
use Configurable;
use Injectable;
/**
* The path format to store imported record files in (inside the assets directory)
*
* @config
* @var string
*/
private static $storage_path = '_imports/{model}';
/**
* The filename to use for storing imported record files. Used by RegistryImportFeedController to save files to.
*
* @config
* @var string
*/
private static $storage_filename = 'import-{date}.csv';
protected $modelClass;
/**
* The class used to manipulate imported feed files on the filesystem
*
* @var GeneratedAssetHandler
*/
protected $assetHandler;
/**
* The "assets" folder name
*
* @var string
*/
protected $assetsDir;
public function setModelClass($class)
{
$this->modelClass = $class;
return $this;
}
public function getLatest()
{
$registryPage = RegistryPage::get()->filter(['DataClass' => $this->modelClass])->first();
if ($registryPage && $registryPage->exists()) {
$files = $this->getImportFiles();
} else {
// Always return an empty list of the model isn't associated to any RegistryPages
$files = ArrayList::create();
}
return RSSFeed::create(
$files,
'registry-feed/latest/' . $this->sanitiseClassName($this->modelClass),
singleton($this->modelClass)->singular_name() . ' data import history'
);
}
/**
* Set the handler used to manipulate the filesystem, and add the ListFiles plugin from Flysystem to inspect
* the contents of a directory
*
* @param GeneratedAssetHandler $handler
* @return $this
*/
public function setAssetHandler(GeneratedAssetHandler $handler)
{
$this->assetHandler = $handler;
return $this;
}
/**
* Get the handler used to manipulate the filesystem
*
* @return GeneratedAssetHandler
*/
public function getAssetHandler()
{
return $this->assetHandler;
}
/**
* Get the path that import files will be stored for this model
*
* @param string $modelClass If null, the current model class will be used
* @return string
*/
public function getStoragePath($modelClass = null)
{
$sanitisedClassName = $this->sanitiseClassName($modelClass ?: $this->modelClass);
return str_replace('{model}', $sanitisedClassName ?? '', $this->config()->get('storage_path') ?? '');
}
/**
* Loop import files in the storage path and push them into an {@link ArrayList}
*
* @return ArrayList<RegistryImportFeedEntry>
*/
public function getImportFiles()
{
$path = $this->getStoragePath();
$importFiles = $this->getAssetHandler()->getFilesystem()->listContents($path)->toArray();
$files = ArrayList::create();
foreach ($importFiles as $importFile) {
$files->push(RegistryImportFeedEntry::create(
basename($importFile->path()),
'',
DBDatetime::create()->setValue($importFile->lastModified())->Format(DBDatetime::ISO_DATETIME),
$this->getAssetsDir() . '/' . $importFile->path()
));
}
return $files;
}
/**
* Returns a relatively unique filename to storage imported data feeds as
*
* @return string
*/
public function getImportFilename()
{
// Note: CLDR date format see DBDatetime
$datetime = DBDatetime::now()->Format('y-MM-dd-HHmmss');
return str_replace('{date}', $datetime ?? '', $this->config()->get('storage_filename') ?? '');
}
/**
* Set the assets directory name
*
* @param string $assetsDir
* @return $this
*/
public function setAssetsDir($assetsDir)
{
$this->assetsDir = $assetsDir;
return $this;
}
/**
* Get the assets directory name
*
* @return string
*/
public function getAssetsDir()
{
return $this->assetsDir;
}
/**
* See {@link \SilverStripe\Admin\ModelAdmin::sanitiseClassName}
*
* @param string $class
* @return string
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class ?? '');
}
}