/
RescanMultilingualPageCommandHandler.php
113 lines (101 loc) · 4.13 KB
/
RescanMultilingualPageCommandHandler.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
<?php
namespace Concrete\Core\Page\Command;
use Concrete\Core\Multilingual\Page\Section\Processor\ReplaceBlockPageRelationsTask;
use Concrete\Core\Multilingual\Page\Section\Processor\ReplaceContentLinksTask;
use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Page\Page;
class RescanMultilingualPageCommandHandler
{
protected function replaceBlockPageRelations(Page $c, Page $section)
{
$db = \Database::connection();
$blocks = $c->getBlocks();
$nvc = $c->getVersionToModify();
$isApproved = $c->getVersionObject()->isApproved();
foreach ($blocks as $b) {
$controller = $b->getController();
$pageColumns = $controller->getBlockTypeExportPageColumns();
if (count($pageColumns)) {
$columns = $db->MetaColumnNames($controller->getBlockTypeDatabaseTable());
$data = array();
$record = $controller->getBlockControllerData();
foreach ($columns as $key) {
$data[$key] = $record->{$key};
}
foreach ($pageColumns as $column) {
$cID = $data[$column];
if ($cID > 0) {
$link = Page::getByID($cID, 'ACTIVE');
if (is_object($section)) {
$relatedID = $section->getTranslatedPageID($link);
if ($relatedID) {
$data[$column] = $relatedID;
}
}
}
}
unset($data['bID']);
$ob = $b;
// replace the block with the version of the block in the later version (if applicable)
$b2 = \Block::getByID($b->getBlockID(), $nvc, $b->getAreaHandle());
if ($b2->isAlias()) {
$nb = $ob->duplicate($nvc);
$b2->deleteBlock();
$b2 = clone $nb;
}
$b2->update($data);
}
}
if ($isApproved) {
$nvc->getVersionObject()->approve();
}
}
public function replaceContentLinks(Page $c, Page $section)
{
$blocks = $c->getBlocks();
$nvc = $c->getVersionToModify();
$isApproved = $c->getVersionObject()->isApproved();
foreach ($blocks as $b) {
if ($b->getBlockTypeHandle() == 'content') {
$content = $b->getController()->content;
$content = preg_replace_callback(
'/{CCM:CID_([0-9]+)}/i',
function ($matches) {
$cID = $matches[1];
if ($cID > 0) {
$link = Page::getByID($cID, 'ACTIVE');
if (is_object($section)) {
$relatedID = $section->getTranslatedPageID($link);
if ($relatedID) {
return sprintf('{CCM:CID_%s}', $relatedID);
}
}
}
},
$content);
$ob = $b;
// replace the block with the version of the block in the later version (if applicable)
$b2 = \Block::getByID($b->getBlockID(), $nvc, $b->getAreaHandle());
if ($b2->isAlias()) {
$nb = $ob->duplicate($nvc);
$b2->deleteBlock();
$b2 = clone $nb;
}
$data = array('content' => $content);
$b2->update($data);
}
}
if ($isApproved) {
$nvc->getVersionObject()->approve();
}
}
public function __invoke(RescanMultilingualPageCommand $command)
{
$page = Page::getByID($command->getPageID());
if ($page && !$page->isError()) {
$section = Section::getBySectionOfSite($page);
$this->replaceBlockPageRelations($page, $section);
$this->replaceContentLinks($page, $section);
}
}
}