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-2018, 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
|
23 |
|
public function getRecordDefinition(Model $record): array |
59
|
|
|
{ |
60
|
|
|
$definition = [ |
61
|
23 |
|
'class' => get_class($record), |
62
|
23 |
|
'attributes' => $record->getAttributes(), |
63
|
|
|
]; |
64
|
23 |
|
unset($definition['attributes']['id']); |
65
|
23 |
|
unset($definition['attributes']['structureId']); |
66
|
23 |
|
unset($definition['attributes']['dateCreated']); |
67
|
23 |
|
unset($definition['attributes']['dateUpdated']); |
68
|
|
|
|
69
|
|
|
// Define sources |
70
|
23 |
|
$definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'id', 'handle'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// Define field layout |
73
|
23 |
|
if (isset($definition['attributes']['fieldLayoutId'])) { |
74
|
7 |
|
if (!$record instanceof MatrixBlockTypeModel) { |
75
|
6 |
|
$definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
23 |
|
unset($definition['attributes']['fieldLayoutId']); |
79
|
|
|
|
80
|
|
|
// Define site settings |
81
|
23 |
|
if (isset($record->siteSettings)) { |
82
|
4 |
|
$definition['siteSettings'] = []; |
83
|
4 |
|
foreach ($record->getSiteSettings() as $siteSetting) { |
84
|
4 |
|
$definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
23 |
|
return $definition; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
9 |
|
public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes) |
95
|
|
|
{ |
96
|
|
|
// Set sources |
97
|
9 |
|
$definition['attributes'] = $this->findSources($definition['class'], $definition['attributes'], 'handle', 'id'); |
|
|
|
|
98
|
|
|
|
99
|
9 |
|
$attributes = array_merge($definition['attributes'], $defaultAttributes); |
100
|
9 |
|
$record->setAttributes($attributes, false); |
101
|
|
|
|
102
|
|
|
// Set field layout |
103
|
9 |
|
if (array_key_exists('fieldLayout', $definition)) { |
104
|
3 |
|
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout'])); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Set site settings |
108
|
9 |
|
if (array_key_exists('siteSettings', $definition)) { |
109
|
2 |
|
$siteSettings = []; |
110
|
2 |
|
foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) { |
111
|
2 |
|
$siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']); |
112
|
2 |
|
$site = Craft::$app->sites->getSiteByHandle($handle); |
113
|
2 |
|
if ($site) { |
114
|
1 |
|
$siteSetting->siteId = $site->id; |
115
|
1 |
|
$siteSettings[$site->id] = $siteSetting; |
116
|
|
|
} else { |
117
|
2 |
|
Schematic::warning(' - Site '.$handle.' could not be found'); |
118
|
|
|
} |
119
|
|
|
} |
120
|
2 |
|
$record->setSiteSettings($siteSettings); |
121
|
|
|
} |
122
|
9 |
|
} |
123
|
|
|
} |
124
|
|
|
|
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: