1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\cp\actions\organization; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\ember\actions\CheckAccessTrait; |
13
|
|
|
use flipbox\craft\ember\actions\LookupTrait; |
14
|
|
|
use flipbox\craft\ember\actions\PopulateTrait; |
15
|
|
|
use flipbox\organizations\actions\organizations\PopulateOrganizationTrait; |
16
|
|
|
use flipbox\organizations\cp\controllers\OrganizationTabsTrait; |
17
|
|
|
use flipbox\organizations\cp\controllers\OrganizationSitesTrait; |
18
|
|
|
use flipbox\organizations\elements\Organization; |
19
|
|
|
use flipbox\organizations\elements\Organization as OrganizationElement; |
20
|
|
|
use yii\base\Action; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Flipbox Factory <[email protected]> |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
*/ |
26
|
|
|
class SwitchType extends Action |
27
|
|
|
{ |
28
|
|
|
use OrganizationSitesTrait, |
29
|
|
|
OrganizationTabsTrait, |
30
|
|
|
PopulateOrganizationTrait, |
31
|
|
|
PopulateTrait, |
32
|
|
|
LookupTrait, |
33
|
|
|
CheckAccessTrait { |
34
|
|
|
populate as parentPopulate; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param null $organization |
39
|
|
|
* @return array|mixed |
40
|
|
|
* @throws \Twig\Error\LoaderError |
41
|
|
|
* @throws \Twig\Error\RuntimeError |
42
|
|
|
* @throws \Twig\Error\SyntaxError |
43
|
|
|
* @throws \craft\errors\SiteNotFoundException |
44
|
|
|
* @throws \flipbox\craft\ember\exceptions\RecordNotFoundException |
45
|
|
|
* @throws \yii\web\UnauthorizedHttpException |
46
|
|
|
*/ |
47
|
|
|
public function run($organization = null) |
48
|
|
|
{ |
49
|
|
|
$organization = (null !== $organization) ? $this->find($organization) : $this->create(); |
50
|
|
|
return $this->runInternal($organization); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
* @return OrganizationElement |
56
|
|
|
*/ |
57
|
|
|
protected function find($identifier) |
58
|
|
|
{ |
59
|
|
|
$site = $this->resolveSiteFromRequest(); |
60
|
|
|
return Organization::findOne([ |
|
|
|
|
61
|
|
|
is_numeric($identifier) ? 'id' : 'slug' => $identifier, |
62
|
|
|
$site ? $site->id : null |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
* @return OrganizationElement |
69
|
|
|
*/ |
70
|
|
|
protected function create() |
71
|
|
|
{ |
72
|
|
|
$site = $this->resolveSiteFromRequest(); |
73
|
|
|
return new Organization([ |
74
|
|
|
'siteId' => $site ? $site->id : null |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param OrganizationElement $element |
80
|
|
|
* @return array|mixed |
81
|
|
|
* @throws \Twig\Error\LoaderError |
82
|
|
|
* @throws \Twig\Error\RuntimeError |
83
|
|
|
* @throws \Twig\Error\SyntaxError |
84
|
|
|
* @throws \craft\errors\SiteNotFoundException |
85
|
|
|
* @throws \yii\web\UnauthorizedHttpException |
86
|
|
|
*/ |
87
|
|
|
protected function runInternal(OrganizationElement $element) |
88
|
|
|
{ |
89
|
|
|
// Check access |
90
|
|
|
if (($access = $this->checkAccess($element)) !== true) { |
91
|
|
|
return $access; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->data( |
95
|
|
|
$this->populate($element) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param OrganizationElement $element |
101
|
|
|
* @return array |
102
|
|
|
* @throws \Twig\Error\LoaderError |
103
|
|
|
* @throws \Twig\Error\RuntimeError |
104
|
|
|
* @throws \Twig\Error\SyntaxError |
105
|
|
|
* @throws \craft\errors\SiteNotFoundException |
106
|
|
|
*/ |
107
|
|
|
protected function data(OrganizationElement $element): array |
108
|
|
|
{ |
109
|
|
|
$view = Craft::$app->getView(); |
110
|
|
|
|
111
|
|
|
return [ |
112
|
|
|
'tabsHtml' => $view->renderTemplate( |
113
|
|
|
'_includes/tabs', |
114
|
|
|
[ |
115
|
|
|
'tabs' => $this->getTabs($element, true) |
116
|
|
|
] |
117
|
|
|
), |
118
|
|
|
'fieldsHtml' => $view->renderTemplate( |
119
|
|
|
'organizations/_cp/organization/__fields', |
120
|
|
|
[ |
121
|
|
|
'element' => $element, |
122
|
|
|
'fieldLayout' => $element->getFieldLayout() |
123
|
|
|
] |
124
|
|
|
), |
125
|
|
|
'sitesHtml' => $view->renderTemplate( |
126
|
|
|
'organizations/_cp/organization/__sites', |
127
|
|
|
[ |
128
|
|
|
'element' => $element, |
129
|
|
|
'enabledSiteIds' => $this->getEnabledSiteIds($element), |
130
|
|
|
'siteIds' => $this->getSiteIds(), |
131
|
|
|
'url' => $this->getSiteUrl($element) |
132
|
|
|
] |
133
|
|
|
), |
134
|
|
|
'sidebarHtml' => $view->renderTemplate( |
135
|
|
|
'organizations/_cp/organization/__enabled', |
136
|
|
|
[ |
137
|
|
|
'element' => $element, |
138
|
|
|
'showSites' => $this->showSites() |
139
|
|
|
] |
140
|
|
|
), |
141
|
|
|
'headHtml' => $view->getHeadHtml(), |
142
|
|
|
'footHtml' => $view->getBodyHtml(), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param OrganizationElement $element |
148
|
|
|
* @return OrganizationElement |
149
|
|
|
* @throws \Exception |
150
|
|
|
*/ |
151
|
|
|
public function populate(OrganizationElement $element): OrganizationElement |
152
|
|
|
{ |
153
|
|
|
$this->parentPopulate($element); |
154
|
|
|
$this->populateFromRequest($element); |
155
|
|
|
|
156
|
|
|
return $element; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|