|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\models\MatrixBlockType as MatrixBlockTypeModel; |
|
8
|
|
|
use yii\base\Component as BaseComponent; |
|
9
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
10
|
|
|
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior; |
|
11
|
|
|
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior; |
|
12
|
|
|
use NerdsAndCompany\Schematic\Interfaces\ConverterInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Schematic Base Converter. |
|
16
|
|
|
* |
|
17
|
|
|
* Sync Craft Setups. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Nerds & Company |
|
20
|
|
|
* @copyright Copyright (c) 2015-2019, Nerds & Company |
|
21
|
|
|
* @license MIT |
|
22
|
|
|
* |
|
23
|
|
|
* @see http://www.nerds.company |
|
24
|
|
|
* |
|
25
|
|
|
* @method getSources(string $fieldType, $sources, string $indexFrom, string $indexTo) |
|
26
|
|
|
* @method getSource(string $fieldType, string $source, string $indexFrom, string $indexTo) |
|
27
|
|
|
* @method getFieldLayoutDefinition(FieldLayout $fieldLayout): array |
|
28
|
|
|
* @method getFieldLayout(array $fieldLayoutDef): FieldLayout |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class Base extends BaseComponent implements ConverterInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Load fieldlayout and sources behaviors. |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
33 |
|
public function behaviors() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
33 |
|
FieldLayoutBehavior::class, |
|
41
|
|
|
SourcesBehavior::class, |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract public function saveRecord(Model $record, array $definition): bool; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
abstract public function deleteRecord(Model $record): bool; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getRecordIndex(Model $record): string |
|
59
|
|
|
{ |
|
60
|
|
|
return $record->handle; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
23 |
|
public function getRecordDefinition(Model $record): array |
|
67
|
|
|
{ |
|
68
|
|
|
$definition = [ |
|
69
|
23 |
|
'class' => get_class($record), |
|
70
|
23 |
|
'attributes' => $record->getAttributes(), |
|
71
|
|
|
]; |
|
72
|
23 |
|
unset($definition['attributes']['id']); |
|
73
|
23 |
|
unset($definition['attributes']['structureId']); |
|
74
|
23 |
|
unset($definition['attributes']['dateCreated']); |
|
75
|
23 |
|
unset($definition['attributes']['dateUpdated']); |
|
76
|
|
|
|
|
77
|
|
|
// Define sources |
|
78
|
23 |
|
$definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'id', 'handle'); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// Define field layout |
|
81
|
23 |
|
if (isset($definition['attributes']['fieldLayoutId'])) { |
|
82
|
7 |
|
if (!$record instanceof MatrixBlockTypeModel) { |
|
83
|
6 |
|
$definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
23 |
|
unset($definition['attributes']['fieldLayoutId']); |
|
87
|
|
|
|
|
88
|
|
|
// Define site settings |
|
89
|
23 |
|
if (isset($record->siteSettings)) { |
|
90
|
4 |
|
$definition['siteSettings'] = []; |
|
91
|
4 |
|
foreach ($record->getSiteSettings() as $siteSetting) { |
|
92
|
4 |
|
$definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
23 |
|
return $definition; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
9 |
|
public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes) |
|
103
|
|
|
{ |
|
104
|
|
|
// Set sources |
|
105
|
9 |
|
$definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'handle', 'id'); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
9 |
|
$attributes = array_merge($definition['attributes'], $defaultAttributes); |
|
108
|
9 |
|
$record->setAttributes($attributes, false); |
|
109
|
|
|
|
|
110
|
|
|
// Set field layout |
|
111
|
9 |
|
if (array_key_exists('fieldLayout', $definition)) { |
|
112
|
3 |
|
$fieldLayout = $this->getFieldLayout($definition['fieldLayout']); |
|
113
|
3 |
|
$fieldLayout->id = $record->fieldLayoutId; |
|
114
|
3 |
|
$record->setFieldLayout($fieldLayout); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Set site settings |
|
118
|
9 |
|
if (array_key_exists('siteSettings', $definition)) { |
|
119
|
2 |
|
$siteSettings = []; |
|
120
|
2 |
|
foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) { |
|
121
|
2 |
|
$siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']); |
|
122
|
2 |
|
$site = Craft::$app->sites->getSiteByHandle($handle); |
|
123
|
2 |
|
if ($site) { |
|
124
|
1 |
|
$siteSetting->siteId = $site->id; |
|
125
|
1 |
|
$siteSettings[$site->id] = $siteSetting; |
|
126
|
|
|
} else { |
|
127
|
2 |
|
Schematic::warning(' - Site '.$handle.' could not be found'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
2 |
|
$record->setSiteSettings($siteSettings); |
|
131
|
|
|
} |
|
132
|
9 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: