|
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-2018, 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
|
|
|
public function getRecordDefinition(Model $record): array |
|
32
|
|
|
{ |
|
33
|
|
|
$definition = parent::getRecordDefinition($record); |
|
34
|
|
|
|
|
35
|
|
|
$definition['group'] = $record->group->name; |
|
36
|
|
|
unset($definition['attributes']['groupId']); |
|
37
|
|
|
|
|
38
|
|
|
return $definition; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function saveRecord(Model $record, array $definition): bool |
|
45
|
|
|
{ |
|
46
|
|
|
if ($definition['group']) { |
|
47
|
|
|
$record->groupId = $this->getGroupIdByName($definition['group']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return Craft::$app->sites->saveSite($record); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function deleteRecord(Model $record): bool |
|
57
|
|
|
{ |
|
58
|
|
|
return Craft::$app->sites->deleteSiteById($record->id); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getGroupIdByName($name) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!isset($this->groups)) { |
|
67
|
|
|
$this->groups = []; |
|
68
|
|
|
foreach (Craft::$app->sites->getAllGroups() as $group) { |
|
69
|
|
|
$this->groups[$group->name] = $group->id; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
if (!array_key_exists($name, $this->groups)) { |
|
73
|
|
|
$group = new SiteGroup(['name' => $name]); |
|
74
|
|
|
if (Craft::$app->sites->saveGroup($group)) { |
|
75
|
|
|
$this->groups[$name] = $group->id; |
|
76
|
|
|
} else { |
|
77
|
|
|
Schematic::importError($group, $name); |
|
78
|
|
|
|
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->groups[$name]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|