1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/force/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/force/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\salesforce\cp\controllers\view; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\web\Controller; |
13
|
|
|
use flipbox\craft\ember\helpers\UrlHelper; |
14
|
|
|
use flipbox\craft\salesforce\cp\Cp as CpModule; |
15
|
|
|
use flipbox\craft\salesforce\Force; |
16
|
|
|
use flipbox\craft\salesforce\records\Connection; |
17
|
|
|
use yii\base\DynamicModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
* |
23
|
|
|
* @property CpModule $module |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractController extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The index view template path |
29
|
|
|
*/ |
30
|
|
|
const TEMPLATE_BASE = 'salesforce' . DIRECTORY_SEPARATOR . '_cp'; |
31
|
|
|
|
32
|
|
|
/******************************************* |
33
|
|
|
* BASE PATHS |
34
|
|
|
*******************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
protected function getBaseActionPath(): string |
40
|
|
|
{ |
41
|
|
|
return Force::getInstance()->getUniqueId() . '/cp'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function getBaseCpPath(): string |
48
|
|
|
{ |
49
|
|
|
return Force::getInstance()->getUniqueId(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $endpoint |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function getBaseContinueEditingUrl(string $endpoint = ''): string |
57
|
|
|
{ |
58
|
|
|
return $this->getBaseCpPath() . $endpoint; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/******************************************* |
63
|
|
|
* CONNECTIONS |
64
|
|
|
*******************************************/ |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function getConnections(): array |
70
|
|
|
{ |
71
|
|
|
return Connection::findAll(['enabled' => true]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Connection|null |
76
|
|
|
*/ |
77
|
|
|
protected function findDefaultConnection() |
78
|
|
|
{ |
79
|
|
|
return Connection::findOne([ |
80
|
|
|
'enabled' => true, |
81
|
|
|
'handle' => Force::getInstance()->getSettings()->getDefaultConnection() |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Connection|null |
87
|
|
|
*/ |
88
|
|
|
protected function findActiveConnection() |
89
|
|
|
{ |
90
|
|
|
$selectedConnection = Craft::$app->getRequest()->getParam( |
91
|
|
|
'connection', |
92
|
|
|
Force::getInstance()->getSettings()->getDefaultConnection() |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$connection = Connection::findOne([ |
96
|
|
|
'enabled' => true, |
97
|
|
|
'handle' => $selectedConnection |
98
|
|
|
]) ?: $this->findDefaultConnection(); |
99
|
|
|
|
100
|
|
|
if ($connection === null) { |
101
|
|
|
$connections = $this->getConnections(); |
102
|
|
|
|
103
|
|
|
if (count($connections) === 1) { |
104
|
|
|
return reset($connections); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $connection; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return DynamicModel |
113
|
|
|
*/ |
114
|
|
|
protected function invalidConnectionModel(): DynamicModel |
115
|
|
|
{ |
116
|
|
|
$model = new DynamicModel(); |
117
|
|
|
$model->addError( |
118
|
|
|
'connection', |
119
|
|
|
'Invalid connection. ' . |
120
|
|
|
'<a href="' . UrlHelper::cpUrl('salesforce/settings/connections') . '">' . |
121
|
|
|
'Manage connections to Salesforce' . |
122
|
|
|
'</a>.' |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $model; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/******************************************* |
129
|
|
|
* VARIABLES |
130
|
|
|
*******************************************/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
|
|
protected function baseVariables(array &$variables = []) |
136
|
|
|
{ |
137
|
|
|
$module = Force::getInstance(); |
138
|
|
|
|
139
|
|
|
$title = Force::t("Salesforce"); |
140
|
|
|
|
141
|
|
|
// Settings |
142
|
|
|
$variables['settings'] = $module->getSettings(); |
143
|
|
|
$variables['title'] = $title; |
144
|
|
|
|
145
|
|
|
// Connections |
146
|
|
|
$variables['availableConnections'] = $this->getConnections(); |
147
|
|
|
$variables['defaultConnection'] = $this->findDefaultConnection(); |
148
|
|
|
$variables['activeConnection'] = $this->findActiveConnection(); |
149
|
|
|
|
150
|
|
|
// Path to controller actions |
151
|
|
|
$variables['baseActionPath'] = $this->getBaseActionPath(); |
152
|
|
|
|
153
|
|
|
// Path to CP |
154
|
|
|
$variables['baseCpPath'] = $this->getBaseCpPath(); |
155
|
|
|
|
156
|
|
|
// Set the "Continue Editing" URL |
157
|
|
|
$variables['continueEditingUrl'] = $this->getBaseCpPath(); |
158
|
|
|
|
159
|
|
|
// Select our sub-nav |
160
|
|
|
if (!$activeSubNav = Craft::$app->getRequest()->getSegment(2)) { |
161
|
|
|
$activeSubNav = 'queries'; |
162
|
|
|
} |
163
|
|
|
$variables['selectedSubnavItem'] = 'salesforce.' . $activeSubNav; |
164
|
|
|
|
165
|
|
|
// Breadcrumbs |
166
|
|
|
$variables['crumbs'][] = [ |
167
|
|
|
'label' => $title, |
168
|
|
|
'url' => UrlHelper::url(Force::getInstance()->getUniqueId()) |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/******************************************* |
173
|
|
|
* UPSERT VARIABLES |
174
|
|
|
*******************************************/ |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $variables |
178
|
|
|
*/ |
179
|
|
|
protected function insertVariables(array &$variables) |
180
|
|
|
{ |
181
|
|
|
// apply base view variables |
182
|
|
|
$this->baseVariables($variables); |
183
|
|
|
|
184
|
|
|
// Set the "Continue Editing" URL |
185
|
|
|
$variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/{id}'); |
186
|
|
|
|
187
|
|
|
// Append title |
188
|
|
|
$variables['title'] .= ' - ' . Force::t('New'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|