Completed
Push — develop ( fcce06...87d9a9 )
by Nate
02:07
created

src/cp/actions/organization/SwitchType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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_Loader
41
     * @throws \yii\base\Exception
42
     * @throws \yii\web\UnauthorizedHttpException
43
     */
44
    public function run($organization = null)
45
    {
46
        $organization = (null !== $organization) ? $this->find($organization) : $this->create();
47
        return $this->runInternal($organization);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     * @return OrganizationElement
53
     */
54
    protected function find($identifier)
55
    {
56
        $site = $this->resolveSiteFromRequest();
57
        return Organization::findOne([
0 ignored issues
show
Bug Compatibility introduced by
The expression \flipbox\organizations\e...e ? $site->id : null)); of type craft\base\Element|null|craft\base\Element[] adds the type craft\base\Element[] to the return on line 57 which is incompatible with the return type documented by flipbox\organizations\cp...zation\SwitchType::find of type flipbox\organizations\elements\Organization|null.
Loading history...
58
            is_numeric($identifier) ? 'id' : 'slug' => $identifier,
59
            $site ? $site->id : null
60
        ]);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     * @return OrganizationElement
66
     */
67
    protected function create()
68
    {
69
        $site = $this->resolveSiteFromRequest();
70
        return new Organization([
71
            'siteId' => $site ? $site->id : null
72
        ]);
73
    }
74
75
    /**
76
     * @inheritdoc
77
     * @throws \Twig_Error_Loader
78
     * @throws \yii\base\Exception
79
     * @throws \yii\web\UnauthorizedHttpException
80
     */
81
    protected function runInternal(OrganizationElement $element)
82
    {
83
        // Check access
84
        if (($access = $this->checkAccess($element)) !== true) {
85
            return $access;
86
        }
87
88
        return $this->data(
89
            $this->populate($element)
90
        );
91
    }
92
93
    /**
94
     * @param OrganizationElement $element
95
     * @return array
96
     * @throws \Twig_Error_Loader
97
     * @throws \yii\base\Exception
98
     */
99
    protected function data(OrganizationElement $element): array
100
    {
101
        $view = Craft::$app->getView();
102
103
        return [
104
            'tabsHtml' => $view->renderTemplate(
105
                '_includes/tabs',
106
                [
107
                    'tabs' => $this->getTabs($element, false)
108
                ]
109
            ),
110
            'fieldsHtml' => $view->renderTemplate(
111
                'organizations/_cp/organization/__fields',
112
                [
113
                    'element' => $element,
114
                    'fieldLayout' => $element->getFieldLayout()
115
                ]
116
            ),
117
            'sitesHtml' => $view->renderTemplate(
118
                'organizations/_cp/organization/__sites',
119
                [
120
                    'element' => $element,
121
                    'enabledSiteIds' => $this->getEnabledSiteIds($element),
122
                    'siteIds' => $this->getSiteIds(),
123
                    'url' => $this->getSiteUrl($element)
124
                ]
125
            ),
126
            'sidebarHtml' => $view->renderTemplate(
127
                'organizations/_cp/organization/__enabled',
128
                [
129
                    'element' => $element,
130
                    'showSites' => $this->showSites()
131
                ]
132
            ),
133
            'headHtml' => $view->getHeadHtml(),
134
            'footHtml' => $view->getBodyHtml(),
135
        ];
136
    }
137
138
    /**
139
     * @inheritdoc
140
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
141
     */
142
    public function populate(OrganizationElement $element): OrganizationElement
143
    {
144
        $this->parentPopulate($element);
145
        $this->populateFromRequest($element);
146
147
        return $element;
148
    }
149
}
150