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
|
16 |
|
public function behaviors() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
16 |
|
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 |
View Code Duplication |
if (isset($definition['attributes']['sources'])) { |
|
|
|
|
71
|
2 |
|
$sources = $this->getSources($definition['class'], $definition['attributes']['sources'], 'id', 'handle'); |
72
|
2 |
|
$definition['attributes']['sources'] = $sources; |
73
|
|
|
} |
74
|
|
|
|
75
|
23 |
View Code Duplication |
if (isset($definition['attributes']['source'])) { |
|
|
|
|
76
|
1 |
|
$source = $this->getSource($definition['class'], $definition['attributes']['source'], 'id', 'handle'); |
77
|
1 |
|
$definition['attributes']['source'] = $source; |
78
|
|
|
} |
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 |
View Code Duplication |
if (isset($definition['attributes']['sources'])) { |
|
|
|
|
106
|
2 |
|
$sources = $this->getSources($definition['class'], $definition['attributes']['sources'], 'handle', 'id'); |
107
|
2 |
|
$definition['attributes']['sources'] = $sources; |
108
|
|
|
} |
109
|
|
|
|
110
|
9 |
View Code Duplication |
if (isset($definition['attributes']['source'])) { |
|
|
|
|
111
|
1 |
|
$source = $this->getSource($definition['class'], $definition['attributes']['source'], 'handle', 'id'); |
112
|
1 |
|
$definition['attributes']['source'] = $source; |
113
|
|
|
} |
114
|
|
|
|
115
|
9 |
|
$attributes = array_merge($definition['attributes'], $defaultAttributes); |
116
|
9 |
|
$record->setAttributes($attributes, false); |
117
|
|
|
|
118
|
|
|
// Set field layout |
119
|
9 |
|
if (array_key_exists('fieldLayout', $definition)) { |
120
|
3 |
|
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout'])); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Set site settings |
124
|
9 |
|
if (array_key_exists('siteSettings', $definition)) { |
125
|
2 |
|
$siteSettings = []; |
126
|
2 |
|
foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) { |
127
|
2 |
|
$siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']); |
128
|
2 |
|
$site = Craft::$app->sites->getSiteByHandle($handle); |
129
|
2 |
|
if ($site) { |
130
|
1 |
|
$siteSetting->siteId = $site->id; |
131
|
1 |
|
$siteSettings[$site->id] = $siteSetting; |
132
|
|
|
} else { |
133
|
2 |
|
Schematic::warning(' - Site '.$handle.' could not be found'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
2 |
|
$record->setSiteSettings($siteSettings); |
137
|
|
|
} |
138
|
9 |
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.