/
Service.php
194 lines (156 loc) · 6 KB
/
Service.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Concrete\Core\Site\Type\Skeleton;
use Concrete\Core\Application\Application;
use Concrete\Core\Backup\ContentImporter;
use Concrete\Core\Entity\Site\Site;
use Concrete\Core\Entity\Site\SiteTree;
use Concrete\Core\Entity\Site\Type;
use Doctrine\ORM\EntityManagerInterface;
use Concrete\Core\Entity\Site\Skeleton;
use Concrete\Core\Entity\Site\SkeletonLocale;
use Concrete\Core\Entity\Site\SkeletonTree;
use Concrete\Core\Localization\Locale\Service as LocaleService;
use Concrete\Core\Attribute\Category\SiteTypeCategory;
class Service
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var Application
*/
protected $application;
/**
* @var LocaleService
*/
protected $localeService;
/**
* @var SiteTypeCategory
*/
protected $siteTypeCategory;
public function __construct(
Application $application,
EntityManagerInterface $entityManager,
LocaleService $localeService,
SiteTypeCategory $siteTypeCategory
)
{
$this->application = $application;
$this->localeService = $localeService;
$this->entityManager = $entityManager;
$this->siteTypeCategory = $siteTypeCategory;
}
/**
* @return EntityManagerInterface
*/
public function getEntityManager()
{
return $this->entityManager;
}
public function getSkeleton(Type $type)
{
$r = $this->entityManager->getRepository(Skeleton::class);
return $r->findOneByType($type);
}
public function getHomePage(Type $type)
{
/**
* @var $skeleton Skeleton
*/
$skeleton = $this->getSkeleton($type);
$tree = $skeleton->getLocales()[0]->getSiteTree();
if (is_object($tree)) {
return $tree->getSiteHomePageObject();
}
}
public function getAttributeValues(Skeleton $skeleton)
{
return $this->siteTypeCategory->getAttributeValues($skeleton);
}
public function createSkeleton(Type $type, SkeletonLocale $locale)
{
// Create a new site tree for the type
$tree = new SkeletonTree();
$tree->setType($type);
$this->entityManager->persist($tree);
$this->entityManager->flush();
$home = \Page::addHomePage($tree);
// We have to update the site tree immediately because subsequent attempts to Page::isHomePage
// rely on the site home page ID being set in the $tree object
$tree->setSiteHomePageID($home->getCollectionID());
$this->entityManager->persist($tree);
$this->entityManager->flush();
$name = t('Skeleton Home: %s', $type->getSiteTypeName());
$home->update([
'cName' => $name,
'pTemplateID' => $type->getSiteTypeHomePageTemplateID()
]);
// Create skeleton versions of the required root single pages
$importer = new ContentImporter();
$importer->setHomePage($home);
$importer->importContentFile(DIR_BASE_CORE . '/config/install/base/single_pages/root.xml');
$skeleton = new Skeleton();
$skeleton->setType($type);
$this->entityManager->persist($skeleton);
$this->entityManager->flush();
// Now create a default locale:
$locale->setSkeleton($skeleton);
$tree->setLocale($locale);
$locale->setSiteTree($tree);
$locale = $this->localeService->updatePluralSettings($locale);
$this->entityManager->persist($locale);
$this->entityManager->persist($tree);
$this->entityManager->persist($skeleton);
$this->entityManager->flush();
$this->entityManager->refresh($skeleton);
return $skeleton;
}
public function delete(Skeleton $skeleton)
{
$locales = $skeleton->getLocales();
foreach($locales as $locale) {
$this->localeService->delete($locale);
}
$this->entityManager->remove($skeleton);
$this->entityManager->flush();
}
public function publishSkeletonToSite(Skeleton $skeleton, Site $site)
{
// First, create a new site tree
$tree = new SiteTree();
$this->entityManager->persist($tree);
$this->entityManager->flush();
$locale = $site->getDefaultLocale();
$tree->setLocale($locale);
$locale->setSiteTree($tree);
$this->entityManager->persist($locale);
$this->entityManager->persist($tree);
$this->entityManager->flush();
// Duplicate the home page.
$skeletonHome = $skeleton->getLocales()[0]->getSiteTree()->getSiteHomePageObject();
$home = $skeletonHome->duplicateAll(null, true, $site);
$tree->setSiteHomePageID($home->getCollectionID());
$this->entityManager->persist($tree);
$this->entityManager->flush();
// This is stupid, but the handle has been incorrectly set because when we duplicated the page
// we didn't know if this was the home page or not.
$home->update(['cName' => $site->getSiteName(), 'cHandle' => '']);
$home->rescanCollectionPath(); // Have to do this so we clear out the isSystemPage boolean.
// Now duplicate the other top level pages. Duplicate them UNDER home, then move to them root.
// Kind of goofy, but it helps with permission inheritance
// Add copies of all the root-level single pages like stacks, page not found
$db = $this->entityManager->getConnection();
$siteTreeID = $skeleton->getLocales()[0]->getSiteTree()->getSiteTreeID();
$r = $db->executeQuery('select cID from Pages where cIsTemplate = 0 and siteTreeID = ? and cParentID = 0 and cID <> ?', [$siteTreeID, $skeletonHome->getCollectionID()]);
while ($row = $r->FetchRow()) {
$c = \Page::getByID($row['cID']);
$c = $c->duplicateAll($home, false, $site);
$c->moveToRoot();
}
// Finally, delete
$this->entityManager->persist($site);
$this->entityManager->flush();
return $home;
}
}