|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SaaS Link plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Craft Field Types for linking Entries to Harvest, Trello, and Capsule. |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://workingconcept.com |
|
8
|
|
|
* @copyright Copyright (c) 2018 Working Concept Inc. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace workingconcept\saaslink; |
|
12
|
|
|
|
|
13
|
|
|
use workingconcept\saaslink\services\HarvestService; |
|
14
|
|
|
use workingconcept\saaslink\services\CapsuleService; |
|
15
|
|
|
use workingconcept\saaslink\services\TrelloService; |
|
16
|
|
|
use workingconcept\saaslink\variables\HarvestVariable; |
|
17
|
|
|
use workingconcept\saaslink\variables\TrelloVariable; |
|
18
|
|
|
use workingconcept\saaslink\variables\CapsuleVariable; |
|
19
|
|
|
use workingconcept\saaslink\models\Settings; |
|
20
|
|
|
use workingconcept\saaslink\fields\SaasLinkField; |
|
21
|
|
|
|
|
22
|
|
|
use Craft; |
|
23
|
|
|
use craft\services\Fields; |
|
24
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
25
|
|
|
use craft\events\RegisterComponentTypesEvent; |
|
26
|
|
|
|
|
27
|
|
|
use yii\base\Event; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class SaasLink |
|
31
|
|
|
* |
|
32
|
|
|
* @author Working Concept |
|
33
|
|
|
* @package SaaS Link |
|
34
|
|
|
* @since 1.0.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @property HarvestService $harvest |
|
37
|
|
|
* @property TrelloService $trello |
|
38
|
|
|
* @property CapsuleService $capsule |
|
39
|
|
|
*/ |
|
40
|
|
|
class SaasLink extends craft\base\Plugin |
|
41
|
|
|
{ |
|
42
|
|
|
// Static Properties |
|
43
|
|
|
// ========================================================================= |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var SaasLink |
|
47
|
|
|
*/ |
|
48
|
|
|
public static $plugin; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public $hasCpSettings = true; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
public $schemaVersion = '1.0.1'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
public $t9nCategory = 'saas-link'; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
// Public Methods |
|
67
|
|
|
// ========================================================================= |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
|
|
public function init() |
|
73
|
|
|
{ |
|
74
|
|
|
parent::init(); |
|
75
|
|
|
self::$plugin = $this; |
|
76
|
|
|
|
|
77
|
|
|
$this->setComponents([ |
|
78
|
|
|
'harvest' => HarvestService::class, |
|
79
|
|
|
'trello' => TrelloService::class, |
|
80
|
|
|
'capsule' => CapsuleService::class, |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
Event::on( |
|
84
|
|
|
CraftVariable::class, |
|
85
|
|
|
CraftVariable::EVENT_INIT, |
|
86
|
|
|
function (Event $event) |
|
87
|
|
|
{ |
|
88
|
|
|
$variable = $event->sender; |
|
89
|
|
|
$variable->set('harvest', HarvestVariable::class); |
|
90
|
|
|
$variable->set('trello', TrelloVariable::class); |
|
91
|
|
|
$variable->set('capsule', CapsuleVariable::class); |
|
92
|
|
|
} |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
Event::on( |
|
96
|
|
|
Fields::class, |
|
97
|
|
|
Fields::EVENT_REGISTER_FIELD_TYPES, |
|
98
|
|
|
function (RegisterComponentTypesEvent $event) |
|
99
|
|
|
{ |
|
100
|
|
|
$event->types[] = SaasLinkField::class; |
|
101
|
|
|
} |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
Craft::info( |
|
105
|
|
|
Craft::t( |
|
106
|
|
|
'saas-link', |
|
107
|
|
|
'{name} plugin loaded', |
|
108
|
|
|
['name' => $this->name] |
|
109
|
|
|
), |
|
110
|
|
|
__METHOD__ |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get an array of configured and enabled services. |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getEnabledServices(): array |
|
120
|
|
|
{ |
|
121
|
|
|
$enabled = []; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($this->getSettings()->enabledServices as $service) |
|
124
|
|
|
{ |
|
125
|
|
|
$instance = new $service; |
|
126
|
|
|
|
|
127
|
|
|
if ($instance->isConfigured()) |
|
128
|
|
|
{ |
|
129
|
|
|
$enabled[] = $instance; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $enabled; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Fetch and save base account URLs if they're not already set. |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function beforeSaveSettings(): bool |
|
142
|
|
|
{ |
|
143
|
|
|
$enabled = []; |
|
144
|
|
|
|
|
145
|
|
|
foreach (Settings::SUPPORTED_SERVICES as $service) |
|
146
|
|
|
{ |
|
147
|
|
|
$instance = new $service; |
|
148
|
|
|
|
|
149
|
|
|
if ($instance->isConfigured()) |
|
150
|
|
|
{ |
|
151
|
|
|
$enabled[] = $service; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->getSettings()->enabledServices = $enabled; |
|
156
|
|
|
|
|
157
|
|
|
if ($this->capsule->isConfigured() && empty($this->getSettings()->capsuleBaseUrl)) |
|
158
|
|
|
{ |
|
159
|
|
|
// have the service update the URL so we can save it |
|
160
|
|
|
$this->capsule->setCapsuleBaseUrlSetting(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if ($this->harvest->isConfigured() && empty($this->getSettings()->harvestBaseUrl)) |
|
164
|
|
|
{ |
|
165
|
|
|
// have the service update the URL so we can save it |
|
166
|
|
|
$this->harvest->setHarvestBaseUrlSetting(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
// Protected Methods |
|
174
|
|
|
// ========================================================================= |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @inheritdoc |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function createSettingsModel() |
|
180
|
|
|
{ |
|
181
|
|
|
return new Settings(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @inheritdoc |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function settingsHtml(): string |
|
188
|
|
|
{ |
|
189
|
|
|
$organizationOptions = []; |
|
190
|
|
|
|
|
191
|
|
|
foreach($this->trello->getMemberOrganizations() as $organization) |
|
192
|
|
|
{ |
|
193
|
|
|
$organizationOptions[] = [ |
|
194
|
|
|
'label' => $organization->displayName, |
|
195
|
|
|
'value' => $organization->id, |
|
196
|
|
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return Craft::$app->view->renderTemplate( |
|
200
|
|
|
'saas-link/settings', |
|
201
|
|
|
[ |
|
202
|
|
|
'organizationOptions' => $organizationOptions, |
|
203
|
|
|
'settings' => $this->getSettings() |
|
204
|
|
|
] |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|