1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Mappers; |
4
|
|
|
|
5
|
|
|
use Craft; |
6
|
|
|
use craft\elements\User; |
7
|
|
|
use NerdsAndCompany\Schematic\Behaviors\FieldLayoutBehavior; |
8
|
|
|
use NerdsAndCompany\Schematic\Schematic; |
9
|
|
|
use NerdsAndCompany\Schematic\Interfaces\MapperInterface; |
10
|
|
|
use yii\base\Component as BaseComponent; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Schematic User Mapper. |
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
|
|
|
class UserSettingsMapper extends BaseComponent implements MapperInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Load fieldlayout behavior. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function behaviors(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
FieldLayoutBehavior::className(), |
|
|
|
|
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function export(array $settings = []): array |
41
|
|
|
{ |
42
|
|
|
$settings = Craft::$app->systemSettings->getSettings('users'); |
43
|
|
|
$photoVolumeId = (int) $settings['photoVolumeId']; |
44
|
|
|
$volume = Craft::$app->volumes->getVolumeById($photoVolumeId); |
45
|
|
|
unset($settings['photoVolumeId']); |
46
|
|
|
$settings['photoVolume'] = $volume ? $volume->handle : null; |
47
|
|
|
|
48
|
|
|
$fieldLayout = Craft::$app->fields->getLayoutByType(User::class); |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
'settings' => $settings, |
52
|
|
|
'fieldLayout' => $this->getFieldLayoutDefinition($fieldLayout), |
|
|
|
|
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function import(array $userSettings, array $settings = []): array |
60
|
|
|
{ |
61
|
|
|
$photoVolumeId = null; |
62
|
|
|
if (array_key_exists('photoVolume', $userSettings['settings']) && null != $userSettings['settings']['photoVolume']) { |
63
|
|
|
$volume = Craft::$app->volumes->getVolumeByHandle($userSettings['settings']['photoVolume']); |
64
|
|
|
$photoVolumeId = $volume ? $volume->id : null; |
65
|
|
|
} |
66
|
|
|
unset($userSettings['settings']['photoVolume']); |
67
|
|
|
|
68
|
|
|
if (array_key_exists('settings', $userSettings)) { |
69
|
|
|
Schematic::info('- Saving user settings'); |
70
|
|
|
$userSettings['settings']['photoVolumeId'] = $photoVolumeId; |
71
|
|
|
if (!Craft::$app->systemSettings->saveSettings('users', $userSettings['settings'])) { |
72
|
|
|
Schematic::warning('- Couldn’t save user settings.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (array_key_exists('fieldLayout', $userSettings)) { |
77
|
|
|
Schematic::info('- Saving user field layout'); |
78
|
|
|
$fieldLayout = $this->getFieldLayout($userSettings['fieldLayout']); |
|
|
|
|
79
|
|
|
$fieldLayout->type = User::class; |
80
|
|
|
|
81
|
|
|
Craft::$app->fields->deleteLayoutsByType(User::class); |
82
|
|
|
if (!Craft::$app->fields->saveLayout($fieldLayout)) { |
83
|
|
|
Schematic::warning('- Couldn’t save user field layout.'); |
84
|
|
|
|
85
|
|
|
Schematic::importError($fieldLayout, 'users'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.