Completed
Push — develop ( 856b14...45ab37 )
by Nate
17:44
created

AbstractViewController::baseVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\view\providers;
4
5
use Craft;
6
use craft\helpers\UrlHelper;
7
use craft\web\Controller;
8
use flipbox\patron\cp\Cp;
9
use flipbox\patron\Patron;
10
use flipbox\patron\records\Provider;
11
12
/**
13
 * @property Cp $module
14
 */
15
abstract class AbstractViewController extends \flipbox\patron\cp\controllers\view\AbstractViewController
16
{
17
    /**
18
     * The index view template path
19
     */
20
    const TEMPLATE_BASE = 'patron/_cp/provider';
21
22
    /*******************************************
23
     * ENVIRONMENTS
24
     *******************************************/
25
26
    /**
27
     * @param Provider $provider
28
     * @return array
29
     */
30
    protected function availableEnvironments(Provider $provider): array
31
    {
32
        $usedEnvironments = array_keys($provider->environments);
33
        $allEnvironments = Patron::getInstance()->getSettings()->getEnvironments();
34
35
        return array_diff($allEnvironments, $usedEnvironments);
36
    }
37
38
    /*******************************************
39
     * TABS
40
     *******************************************/
41
42
    /**
43
     * @param Provider $provider
44
     * @return array
45
     */
46
    protected function getTabs(Provider $provider): array
47
    {
48
        if ($provider->getId() === null) {
49
            return [];
50
        }
51
52
        $baseUrl = Craft::$app->getRequest()->getSegment(1) . '/' .
53
            Craft::$app->getRequest()->getSegment(2) . '/' .
54
            Craft::$app->getRequest()->getSegment(3);
55
56
        return [
57
            'general' => [
58
                'label' => Craft::t('patron', 'General'),
59
                'url' => UrlHelper::url($baseUrl . '/')
60
            ],
61
            'tokens' => [
62
                'label' => Craft::t('patron', 'Tokens'),
63
                'url' => UrlHelper::url($baseUrl . '/tokens')
64
            ]
65
        ];
66
    }
67
68
    /*******************************************
69
     * PATHS
70
     *******************************************/
71
72
    /**
73
     * @return string
74
     */
75
    protected function getBaseActionPath(): string
76
    {
77
        return parent::getBaseActionPath() . '/providers';
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    protected function getBaseCpPath(): string
84
    {
85
        return parent::getBaseCpPath() . '/providers';
86
    }
87
88
    /*******************************************
89
     * VARIABLES
90
     *******************************************/
91
92
    /**
93
     * Set base variables used to generate template views
94
     *
95
     * @param array $variables
96
     */
97
    protected function baseVariables(array &$variables = [])
98
    {
99
        parent::baseVariables($variables);
100
101
        // Page title
102
        $variables['title'] .= ': ' . Craft::t('patron', "Providers");
103
104
        // Breadcrumbs
105
        $variables['crumbs'][] = [
106
            'label' => Craft::t('patron', "Providers"),
107
            'url' => UrlHelper::url(Patron::getInstance()->getUniqueId() . '/providers')
108
        ];
109
    }
110
111
    /**
112
     * @param array $variables
113
     */
114
    protected function insertVariables(array &$variables)
115
    {
116
        // apply base view variables
117
        $this->baseVariables($variables);
118
119
        // Set the "Continue Editing" URL
120
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/{id}');
121
122
        // Append title
123
        $variables['title'] .= ' - ' . Craft::t('patron', 'New');
124
125
        // Breadcrumbs
126
        $variables['crumbs'][] = [
127
            'label' => Craft::t('patron', 'New'),
128
            'url' => UrlHelper::url($variables['baseCpPath'] . '/new')
129
        ];
130
    }
131
132
    /*******************************************
133
     * UPDATE VARIABLES
134
     *******************************************/
135
136
    /**
137
     * @param array $variables
138
     * @param Provider $provider
139
     */
140
    protected function updateVariables(array &$variables, Provider $provider)
141
    {
142
        // apply base view variables
143
        $this->baseVariables($variables);
144
145
        // Set the "Continue Editing" URL
146
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $provider->getId());
147
148
        // Append title
149
        $variables['title'] .= ' - ' . $provider->getDisplayName();
150
151
        // Breadcrumbs
152
        $variables['crumbs'][] = [
153
            'label' => $provider->getDisplayName(),
154
            'url' => UrlHelper::url(
155
                Patron::getInstance()->getUniqueId() . '/providers/' . $provider->getId()
156
            )
157
        ];
158
    }
159
}
160