1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SaaS Link plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* @link https://workingconcept.com |
6
|
|
|
* @copyright Copyright (c) 2018 Working Concept Inc. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace workingconcept\saaslink\services; |
10
|
|
|
|
11
|
|
|
use workingconcept\saaslink\models\trello\TrelloBoard; |
12
|
|
|
use workingconcept\saaslink\models\trello\TrelloOrganization; |
13
|
|
|
use workingconcept\saaslink\SaasLink; |
14
|
|
|
use Craft; |
15
|
|
|
|
16
|
|
|
class TrelloService extends SaasLinkService |
17
|
|
|
{ |
18
|
|
|
// Constants |
19
|
|
|
// ========================================================================= |
20
|
|
|
|
21
|
|
|
const CACHE_ENABLED = true; |
22
|
|
|
const CACHE_SECONDS = 60; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
// Properties |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $apiBaseUrl = 'https://api.trello.com/1/'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $serviceName = 'Trello'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $serviceSlug = 'trello'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
// Public Methods |
45
|
|
|
// ========================================================================= |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function isConfigured(): bool |
51
|
|
|
{ |
52
|
|
|
return ! empty($this->settings->trelloApiKey) && |
53
|
|
|
! empty($this->settings->trelloApiToken) && |
54
|
|
|
! empty($this->settings->trelloOrganizationId); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function configureClient() |
61
|
|
|
{ |
62
|
|
|
$this->client = new \GuzzleHttp\Client([ |
63
|
|
|
'base_uri' => $this->apiBaseUrl, |
64
|
|
|
'query' => [ |
65
|
|
|
'key' => $this->settings->trelloApiKey, |
66
|
|
|
'token' => $this->settings->trelloApiToken |
67
|
|
|
], |
68
|
|
|
'headers' => [], |
69
|
|
|
'verify' => false, |
70
|
|
|
'debug' => false |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function getAvailableRelationshipTypes(): array |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
[ |
81
|
|
|
'label' => Craft::t('saas-link', 'Board'), |
82
|
|
|
'value' => 'board' |
83
|
|
|
], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function getOptions($relationshipType): array |
91
|
|
|
{ |
92
|
|
|
$options = []; |
93
|
|
|
|
94
|
|
|
if ($relationshipType === 'board') |
95
|
|
|
{ |
96
|
|
|
$boards = SaasLink::$plugin->trello->getBoards(); |
97
|
|
|
|
98
|
|
|
foreach ($boards as $board) |
99
|
|
|
{ |
100
|
|
|
$options[] = [ |
101
|
|
|
'label' => $board->name, |
102
|
|
|
'value' => $board->id, |
103
|
|
|
'link' => $board->url, |
104
|
|
|
'default' => null |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// alphabetize |
109
|
|
|
usort($options, function($a, $b) { |
110
|
|
|
return strtolower($a['label']) <=> strtolower($b['label']); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $options; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get boards. |
119
|
|
|
* |
120
|
|
|
* @param string $filter `all` or a comma-separated list of: `open`, `closed`, `members`, `organization`, `public` |
121
|
|
|
* @param string $fields `all` or comma-separated list of board [fields](https://developers.trello.com/reference#board-object) |
122
|
|
|
* |
123
|
|
|
* @return TrelloBoard[] |
124
|
|
|
*/ |
125
|
|
|
|
126
|
|
|
public function getBoards($filter = 'all', $fields = 'all'): array |
127
|
|
|
{ |
128
|
|
|
$boards = []; |
129
|
|
|
$cacheKey = 'trello_boards_' . $filter . $fields; |
130
|
|
|
|
131
|
|
|
if (self::CACHE_ENABLED && $cachedResponse = Craft::$app->cache->get($cacheKey)) |
132
|
|
|
{ |
133
|
|
|
$responseData = $cachedResponse; |
134
|
|
|
} |
135
|
|
|
else |
136
|
|
|
{ |
137
|
|
|
$response = $this->client->get(sprintf( |
138
|
|
|
'organizations/%s/boards?filter=%s&fields=%s', |
139
|
|
|
$this->settings->trelloOrganizationId, |
140
|
|
|
$filter, |
141
|
|
|
$fields |
142
|
|
|
)); |
143
|
|
|
|
144
|
|
|
$responseData = json_decode($response->getBody()); |
145
|
|
|
|
146
|
|
|
if (self::CACHE_ENABLED) |
147
|
|
|
{ |
148
|
|
|
Craft::$app->cache->set($cacheKey, $responseData, self::CACHE_SECONDS); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach ($responseData as $responseItem) |
153
|
|
|
{ |
154
|
|
|
$boards[] = new TrelloBoard($responseItem); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $boards; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get Organizations to which the relevant human (per API credentials) belongs. |
163
|
|
|
* |
164
|
|
|
* @return TrelloOrganization[] |
165
|
|
|
*/ |
166
|
|
|
public function getMemberOrganizations(): array |
167
|
|
|
{ |
168
|
|
|
$organizations = []; |
169
|
|
|
|
170
|
|
|
// be extra sure we've got credentials since this is called during plugin setup when Settings aren't populated |
171
|
|
|
if ($this->isConfigured()) |
172
|
|
|
{ |
173
|
|
|
$response = $this->client->get('members/me/organizations'); |
174
|
|
|
$responseData = json_decode($response->getBody()); |
175
|
|
|
|
176
|
|
|
foreach ($responseData as $responseItem) |
177
|
|
|
{ |
178
|
|
|
$organizations[] = new TrelloOrganization($responseItem); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $organizations; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
// Private Methods |
187
|
|
|
// ========================================================================= |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|