|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use ExternalContent; |
|
6
|
|
|
use ExternalContentSource; |
|
7
|
|
|
use PhpTek\Exodus\Model\StaticSiteImportDataObject; |
|
8
|
|
|
use SilverStripe\Core\Extension; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package phptek/silverstripe-exodus |
|
14
|
|
|
* @author Sam Minee <[email protected]> |
|
15
|
|
|
* @author Russell Michell <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class StaticSiteExternalContentAdminExtension extends Extension |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
24
|
|
|
'crawlsite', |
|
25
|
|
|
'clearimports', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Controller method which starts a crawl. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $request |
|
32
|
|
|
* @throws Exception |
|
33
|
|
|
* @return HTTPResponse |
|
34
|
|
|
*/ |
|
35
|
|
|
public function crawlsite(array $request): HTTPResponse |
|
36
|
|
|
{ |
|
37
|
|
|
$selected = $request['ID'] ?? 0; |
|
38
|
|
|
|
|
39
|
|
|
if (!$selected) { |
|
40
|
|
|
$messageType = 'bad'; |
|
41
|
|
|
$message = _t('ExternalContent.NOITEMSELECTED', 'No item selected to crawl.'); |
|
42
|
|
|
} else { |
|
43
|
|
|
$source = ExternalContent::getDataObjectFor($selected); |
|
44
|
|
|
|
|
45
|
|
|
if (!($source instanceof ExternalContentSource)) { |
|
46
|
|
|
throw new \Exception('ExternalContent is not an instance of ExternalContentSource.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$messageType = 'good'; |
|
50
|
|
|
$message = _t('ExternalContent.CONTENTMIGRATED', 'Crawl successful. You can attempt an import now.'); |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
$source->crawl(); |
|
54
|
|
|
} catch (\Exception $e) { |
|
55
|
|
|
$messageType = 'bad'; |
|
56
|
|
|
$message = "Error crawling. Crawler said: " . $e->getMessage(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$owner = $this->getOwner(); |
|
61
|
|
|
$owner->getEditForm()->sessionError($message, $messageType); |
|
62
|
|
|
|
|
63
|
|
|
return $owner->getResponseNegotiator()->respond($owner->getRequest()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* |
|
68
|
|
|
* Delete all StaticSiteImportDataObject's and related logic. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $request |
|
71
|
|
|
* @return HTTPResponse |
|
72
|
|
|
*/ |
|
73
|
|
|
public function clearimports(array $request): HTTPResponse |
|
74
|
|
|
{ |
|
75
|
|
|
if (!empty($request['ClearAllImports'])) { |
|
76
|
|
|
$imports = DataObject::get(StaticSiteImportDataObject::class); |
|
77
|
|
|
} elseif ($selectedImports = $request['ShowImports']) { |
|
78
|
|
|
$imports = DataObject::get(StaticSiteImportDataObject::class)->byIDs($selectedImports); |
|
79
|
|
|
} else { |
|
80
|
|
|
$imports = null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($imports) { |
|
84
|
|
|
$imports->each(function ($item) { |
|
85
|
|
|
$item->delete(); |
|
86
|
|
|
}); |
|
87
|
|
|
$messageType = 'good'; |
|
88
|
|
|
$message = _t('StaticSiteConnector.ImportsDeleted', 'Selected imports were cleared successfully.'); |
|
89
|
|
|
} else { |
|
90
|
|
|
$messageType = 'bad'; |
|
91
|
|
|
$message = _t('StaticSiteConnector.ImportsDeleted', 'No imports were selected to clear.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$owner = $this->getOwner(); |
|
95
|
|
|
$owner->getEditForm()->sessionError($message, $messageType); |
|
96
|
|
|
|
|
97
|
|
|
return $owner->getResponseNegotiator()->respond($owner->getRequest()); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|