|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\models\SiteGroup; |
|
7
|
|
|
use craft\base\Model; |
|
8
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Schematic Sites Converter. |
|
12
|
|
|
* |
|
13
|
|
|
* Sync Craft Setups. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Nerds & Company |
|
16
|
|
|
* @copyright Copyright (c) 2015-2019, Nerds & Company |
|
17
|
|
|
* @license MIT |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://www.nerds.company |
|
20
|
|
|
*/ |
|
21
|
|
|
class Site extends Base |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var number[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $groups; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function getRecordDefinition(Model $record): array |
|
32
|
|
|
{ |
|
33
|
3 |
|
$definition = parent::getRecordDefinition($record); |
|
34
|
|
|
|
|
35
|
3 |
|
if ($record->groupId) { |
|
36
|
3 |
|
$definition['group'] = $record->group->name; |
|
37
|
|
|
} |
|
38
|
3 |
|
unset($definition['attributes']['groupId']); |
|
39
|
|
|
|
|
40
|
3 |
|
return $definition; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function saveRecord(Model $record, array $definition): bool |
|
47
|
|
|
{ |
|
48
|
3 |
|
if ($definition['group']) { |
|
49
|
3 |
|
$record->groupId = $this->getGroupIdByName($definition['group']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return Craft::$app->sites->saveSite($record); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function deleteRecord(Model $record): bool |
|
59
|
|
|
{ |
|
60
|
3 |
|
return Craft::$app->sites->deleteSiteById($record->id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get group id by name. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* |
|
68
|
|
|
* @return int|null |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function getGroupIdByName($name) |
|
71
|
|
|
{ |
|
72
|
3 |
|
if (!isset($this->groups)) { |
|
73
|
3 |
|
$this->resetCraftSitesServiceGroupsCache(); |
|
74
|
|
|
|
|
75
|
3 |
|
$this->groups = []; |
|
76
|
3 |
|
foreach (Craft::$app->sites->getAllGroups() as $group) { |
|
77
|
3 |
|
$this->groups[$group->name] = $group->id; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
3 |
|
if (!array_key_exists($name, $this->groups)) { |
|
81
|
2 |
|
$group = new SiteGroup(['name' => $name]); |
|
82
|
2 |
|
if (Craft::$app->sites->saveGroup($group)) { |
|
83
|
1 |
|
$this->groups[$name] = $group->id; |
|
84
|
|
|
} else { |
|
85
|
1 |
|
Schematic::importError($group, $name); |
|
86
|
|
|
|
|
87
|
1 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
return $this->groups[$name]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Reset craft site service groups cache using reflection. |
|
96
|
|
|
*/ |
|
97
|
3 |
|
private function resetCraftSitesServiceGroupsCache() |
|
98
|
|
|
{ |
|
99
|
3 |
|
$obj = Craft::$app->sites; |
|
100
|
3 |
|
$refObject = new \ReflectionObject($obj); |
|
101
|
3 |
|
if ($refObject->hasProperty('_fetchedAllGroups')) { |
|
102
|
|
|
$refProperty = $refObject->getProperty('_fetchedAllGroups'); |
|
103
|
|
|
$refProperty->setAccessible(true); |
|
104
|
|
|
$refProperty->setValue($obj, false); |
|
105
|
|
|
} |
|
106
|
3 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|