|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Converters\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use yii\base\Component as BaseComponent; |
|
8
|
|
|
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior; |
|
9
|
|
|
use NerdsAndCompany\Schematic\Behaviors\SourcesBehavior; |
|
10
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Schematic Base Service. |
|
14
|
|
|
* |
|
15
|
|
|
* Sync Craft Setups. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Nerds & Company |
|
18
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
19
|
|
|
* @license MIT |
|
20
|
|
|
* |
|
21
|
|
|
* @see http://www.nerds.company |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract class Base extends BaseComponent |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Load fieldlayout and sources behaviors. |
|
27
|
|
|
* |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function behaviors() |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
FieldLayoutBehavior::className(), |
|
|
|
|
|
|
34
|
|
|
SourcesBehavior::className(), |
|
|
|
|
|
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Save a record. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Model $record |
|
42
|
|
|
* @param array $definition |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract public function saveRecord(Model $record, array $definition); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Delete a record. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Model $record |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract public function deleteRecord(Model $record); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get single record definition. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Model $record |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getRecordDefinition(Model $record) |
|
65
|
|
|
{ |
|
66
|
|
|
$definition = [ |
|
67
|
|
|
'class' => get_class($record), |
|
68
|
|
|
'attributes' => $record->getAttributes(), |
|
69
|
|
|
]; |
|
70
|
|
|
unset($definition['attributes']['id']); |
|
71
|
|
|
unset($definition['attributes']['structureId']); |
|
72
|
|
|
unset($definition['attributes']['dateCreated']); |
|
73
|
|
|
unset($definition['attributes']['dateUpdated']); |
|
74
|
|
|
|
|
75
|
|
|
// Define sources |
|
76
|
|
|
if (isset($definition['attributes']['sources'])) { |
|
77
|
|
|
$definition['sources'] = $this->getSources($definition['class'], $definition['attributes']['sources'], 'id', 'handle'); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (isset($definition['attributes']['source'])) { |
|
81
|
|
|
$definition['source'] = $this->getSource($definition['class'], $definition['attributes']['sources'], 'id', 'handle'); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Define field layout |
|
85
|
|
|
if (isset($definition['attributes']['fieldLayoutId'])) { |
|
86
|
|
|
$definition['fieldLayout'] = $this->getFieldLayoutDefinition($record->getFieldLayout()); |
|
|
|
|
|
|
87
|
|
|
unset($definition['attributes']['fieldLayoutId']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Define site settings |
|
91
|
|
|
if (isset($record->siteSettings)) { |
|
92
|
|
|
$definition['siteSettings'] = []; |
|
93
|
|
|
foreach ($record->getSiteSettings() as $siteSetting) { |
|
94
|
|
|
$definition['siteSettings'][$siteSetting->site->handle] = $this->getRecordDefinition($siteSetting); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $definition; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set record attributes from definition. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Model $record |
|
105
|
|
|
* @param array $definition |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setRecordAttributes(Model &$record, array $definition, array $defaultAttributes) |
|
108
|
|
|
{ |
|
109
|
|
|
$attributes = array_merge($definition['attributes'], $defaultAttributes); |
|
110
|
|
|
$record->setAttributes($attributes); |
|
111
|
|
|
|
|
112
|
|
|
// Set field layout |
|
113
|
|
|
if (array_key_exists('fieldLayout', $definition)) { |
|
114
|
|
|
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout'])); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// Set site settings |
|
118
|
|
|
if (array_key_exists('siteSettings', $definition)) { |
|
119
|
|
|
$siteSettings = []; |
|
120
|
|
|
foreach ($definition['siteSettings'] as $handle => $siteSettingDefinition) { |
|
121
|
|
|
$siteSetting = new $siteSettingDefinition['class']($siteSettingDefinition['attributes']); |
|
122
|
|
|
$site = Craft::$app->sites->getSiteByHandle($handle); |
|
123
|
|
|
if ($site) { |
|
124
|
|
|
$siteSetting->siteId = $site->id; |
|
125
|
|
|
$siteSettings[$site->id] = $siteSetting; |
|
126
|
|
|
} else { |
|
127
|
|
|
Schematic::warning(' - Site '.$handle.' could not be found'); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
$record->setSiteSettings($siteSettings); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.