|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Craft; |
|
6
|
|
|
use craft\base\Model; |
|
7
|
|
|
use craft\models\Section; |
|
8
|
|
|
use craft\models\EntryType; |
|
9
|
|
|
use craft\models\Section_SiteSettings; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Schematic Sections. |
|
13
|
|
|
* |
|
14
|
|
|
* Sync Craft Setups. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Nerds & Company |
|
17
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
18
|
|
|
* @license MIT |
|
19
|
|
|
* |
|
20
|
|
|
* @see http://www.nerds.company |
|
21
|
|
|
*/ |
|
22
|
|
|
class Sections extends Base |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Get all section records |
|
26
|
|
|
* |
|
27
|
|
|
* @return Section[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function getRecords() |
|
30
|
|
|
{ |
|
31
|
|
|
return Craft::$app->sections->getAllSections(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get section definition. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Model $record |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getRecordDefinition(Model $record) |
|
42
|
|
|
{ |
|
43
|
|
|
$definition = parent::getRecordDefinition($record); |
|
44
|
|
|
if ($record instanceof Section) { |
|
45
|
|
|
$definition['entryTypes'] = $this->export($record->getEntryTypes()); |
|
46
|
|
|
$definition['siteSettings'] = []; |
|
47
|
|
|
foreach ($record->getSiteSettings() as $siteSetting) { |
|
48
|
|
|
$attributes = $siteSetting->attributes; |
|
49
|
|
|
unset($attributes['sectionId']); |
|
50
|
|
|
unset($attributes['id']); |
|
51
|
|
|
$definition['siteSettings'][] = $attributes; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
if ($record instanceof EntryType) { |
|
55
|
|
|
unset($definition['attributes']['sectionId']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $definition; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Save a record |
|
63
|
|
|
* |
|
64
|
|
|
* @param Model $record |
|
65
|
|
|
* @param array $definition |
|
66
|
|
|
* @return boolean |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function saveRecord(Model $record, array $definition) |
|
69
|
|
|
{ |
|
70
|
|
|
$record->setAttributes($definition['attributes']); |
|
71
|
|
|
if ($record instanceof Section) { |
|
72
|
|
|
$siteSettings = []; |
|
73
|
|
|
foreach ($definition['siteSettings'] as $siteSettingDefinition) { |
|
74
|
|
|
$siteSettings[] = new Section_SiteSettings($siteSettingDefinition); |
|
75
|
|
|
} |
|
76
|
|
|
$record->setSiteSettings($siteSettings); |
|
77
|
|
|
if (Craft::$app->sections->saveSection($record)) { |
|
78
|
|
|
parent::import($definition['entryTypes'], $record->getEntryTypes(), ['sectionId' => $record->id]); |
|
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($record instanceof EntryType) { |
|
84
|
|
|
if ($definition['fieldLayout']) { |
|
85
|
|
|
$record->setFieldLayout($this->getFieldLayout($definition['fieldLayout'])); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
return Craft::$app->sections->saveEntryType($record); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Delete a record |
|
95
|
|
|
* |
|
96
|
|
|
* @param Model $record |
|
97
|
|
|
* @return boolean |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function deleteRecord(Model $record) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($record instanceof Section) { |
|
102
|
|
|
return Craft::$app->sections->deleteSection($record); |
|
103
|
|
|
} |
|
104
|
|
|
if ($record instanceof EntryType) { |
|
105
|
|
|
return Craft::$app->sections->deleteEntryType($record); |
|
106
|
|
|
} |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.