1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Reasons plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Adds conditionals to field layouts. |
6
|
|
|
* |
7
|
|
|
* @link https://vaersaagod.no |
8
|
|
|
* @copyright Copyright (c) 2020 Mats Mikkel Rummelhoff |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace mmikkel\reasons\assetbundles\reasons; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Element; |
15
|
|
|
use craft\base\Volume; |
16
|
|
|
use craft\elements\Asset; |
17
|
|
|
use craft\elements\Category; |
18
|
|
|
use craft\elements\Entry; |
19
|
|
|
use craft\elements\GlobalSet; |
20
|
|
|
use craft\elements\Tag; |
21
|
|
|
use craft\elements\User; |
22
|
|
|
use craft\helpers\Json; |
23
|
|
|
use craft\web\AssetBundle; |
24
|
|
|
use craft\web\assets\cp\CpAsset; |
25
|
|
|
use craft\web\View; |
26
|
|
|
|
27
|
|
|
use mmikkel\reasons\Reasons; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @author Mats Mikkel Rummelhoff |
31
|
|
|
* @package Reasons |
32
|
|
|
* @since 2.0.0 |
33
|
|
|
*/ |
34
|
|
|
class ReasonsAssetBundle extends AssetBundle |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
// Public Methods |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function init() |
44
|
|
|
{ |
45
|
|
|
$this->sourcePath = '@mmikkel/reasons/assetbundles/reasons/dist'; |
46
|
|
|
|
47
|
|
|
$this->depends = [ |
48
|
|
|
CpAsset::class, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$this->js = [ |
52
|
|
|
'js/reasons.js', |
53
|
|
|
'js/builder.js', |
54
|
|
|
'js/fld.js', |
55
|
|
|
'js/render.js', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$this->css = [ |
59
|
|
|
'css/reasons.css', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
parent::init(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function registerAssetFiles($view) |
69
|
|
|
{ |
70
|
|
|
parent::registerAssetFiles($view); |
71
|
|
|
|
72
|
|
|
$data = Reasons::getInstance()->reasons->getData(); |
73
|
|
|
|
74
|
|
|
// Check if there's a revisionId in the GET params |
75
|
|
|
// We have to do this to account for Craft's new draft/revision system, and the new DraftEditor JS component |
76
|
|
|
// I'm just gonna add all element types to this, even if it only applies to Entries right now |
77
|
|
|
// If I'm lucky, this'll turn out to be future proof |
78
|
|
|
$revisionId = (int)Craft::$app->getRequest()->getParam('revisionId'); |
79
|
|
|
if ($revisionId) { |
80
|
|
|
$sourceElementId = (int)Element::find()->revisionId($revisionId)->scalar(); |
81
|
|
|
if ($sourceElementId && $element = Craft::$app->getElements()->getElementById($sourceElementId)) { |
82
|
|
|
$elementType = \get_class($element); |
83
|
|
|
$renderContext = null; |
84
|
|
|
if ($elementType === Entry::class) { |
85
|
|
|
/** @var Entry $element */ |
86
|
|
|
$renderContext = "entryType:{$element->typeId}"; |
87
|
|
|
} else if ($elementType === Category::class) { |
88
|
|
|
/** @var Category $element */ |
89
|
|
|
$renderContext = "categoryGroup:{$element->groupId}"; |
90
|
|
|
} else if ($elementType === Tag::class) { |
91
|
|
|
/** @var Tag $element */ |
92
|
|
|
$renderContext = "tagGroup:{$element->groupId}"; |
93
|
|
|
} else if ($elementType === GlobalSet::class) { |
94
|
|
|
/** @var GlobalSet $element */ |
95
|
|
|
$renderContext = "globalSet:{$element->id}"; |
96
|
|
|
} else if ($elementType === User::class) { |
97
|
|
|
$renderContext = 'users'; |
98
|
|
|
} else if ($elementType === Asset::class) { |
99
|
|
|
/** @var Asset $element */ |
100
|
|
|
$renderContext = "assetSource:{$element->volumeId}"; |
101
|
|
|
} |
102
|
|
|
$data['renderContext'] = $renderContext; |
103
|
|
|
} |
104
|
|
|
} else { |
105
|
|
|
// Sadly, the new Asset edit view doesn't have `<input type="hidden" name="volumeId"/>` input – so we'll need to work around that, too |
106
|
|
|
$segments = Craft::$app->getRequest()->getSegments(); |
107
|
|
|
if (\count($segments) === 3 && $segments[0] === 'assets') { |
108
|
|
|
$volumeHandle = $segments[1]; |
109
|
|
|
/** @var Volume $volume */ |
110
|
|
|
$volume = Craft::$app->getVolumes()->getVolumeByHandle($volumeHandle); |
111
|
|
|
if ($volume !== null) { |
112
|
|
|
$data['renderContext'] = "assetSource:{$volume->id}"; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$json = Json::encode($data, JSON_UNESCAPED_UNICODE); |
118
|
|
|
$js = <<<JS |
119
|
|
|
Craft.ReasonsPlugin.init({$json}); |
120
|
|
|
JS; |
121
|
|
|
$view->registerJs($js, View::POS_END); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|