1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Nikaia\TranslationSheet\Client\Api; |
8
|
|
|
use Nikaia\TranslationSheet\Sheet\Styles; |
9
|
|
|
use Nikaia\TranslationSheet\Sheet\TranslationsSheet; |
10
|
|
|
use Nikaia\TranslationSheet\Sheet\TranslationsSheetCoordinates; |
11
|
|
|
|
12
|
|
|
class Spreadsheet |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
protected $locales = []; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $translations = []; |
22
|
|
|
|
23
|
|
|
/** @var Api */ |
24
|
20 |
|
protected $api; |
25
|
|
|
|
26
|
20 |
|
public function __construct($id, $locales, $api = null) |
27
|
20 |
|
{ |
28
|
20 |
|
$this->id = $id; |
29
|
20 |
|
$this->locales = $locales; |
30
|
|
|
$this->api = $api ?: app(Api::class); |
31
|
3 |
|
} |
32
|
|
|
|
33
|
3 |
|
public function setLocales($locales) |
34
|
|
|
{ |
35
|
3 |
|
$this->locales = $locales; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
18 |
|
} |
39
|
|
|
|
40
|
18 |
|
public function setTranslations($translations) |
41
|
|
|
{ |
42
|
18 |
|
$this->translations = $translations; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function getTranslations() |
48
|
|
|
{ |
49
|
|
|
return $this->translations; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
4 |
|
public function getTranslationsCount() |
53
|
|
|
{ |
54
|
|
|
return count($this->translations); |
55
|
5 |
|
} |
56
|
|
|
|
57
|
5 |
|
public function getId() |
58
|
|
|
{ |
59
|
|
|
return $this->id; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function getUrl() |
63
|
|
|
{ |
64
|
|
|
return 'https://docs.google.com/spreadsheets/d/' . $this->id; |
65
|
12 |
|
} |
66
|
|
|
|
67
|
12 |
|
public function getLocales() |
68
|
|
|
{ |
69
|
|
|
return $this->locales; |
70
|
5 |
|
} |
71
|
|
|
|
72
|
5 |
|
public function getLocalesCount() |
73
|
|
|
{ |
74
|
|
|
return count($this->locales); |
75
|
8 |
|
} |
76
|
|
|
|
77
|
8 |
|
public function getHeader() |
78
|
8 |
|
{ |
79
|
8 |
|
return array_merge( |
80
|
|
|
array_merge(['Full key'], $this->getLocales()), |
81
|
|
|
['Namespace', 'Group', 'Key', 'Source file'] |
82
|
|
|
); |
83
|
3 |
|
} |
84
|
|
|
|
85
|
|
|
public function getCamelizedHeader() |
86
|
3 |
|
{ |
87
|
3 |
|
return array_map(function ($item) { |
88
|
|
|
if (in_array($item, Util::asArray($this->getLocales()))) { |
89
|
|
|
return $item; |
90
|
3 |
|
} |
91
|
3 |
|
|
92
|
|
|
return Str::camel($item); |
93
|
|
|
}, $this->getHeader()); |
94
|
5 |
|
} |
95
|
|
|
|
96
|
5 |
|
public function getHeaderColumnsCount() |
97
|
|
|
{ |
98
|
5 |
|
$header = $this->getHeader(); |
99
|
|
|
|
100
|
|
|
return count($header); |
101
|
4 |
|
} |
102
|
|
|
|
103
|
4 |
|
public function getHeaderRowsCount() |
104
|
|
|
{ |
105
|
|
|
return 1; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $sheetId |
110
|
|
|
* @param $sheetTitle |
111
|
|
|
* |
112
|
1 |
|
* @return TranslationsSheetCoordinates |
113
|
|
|
*/ |
114
|
1 |
|
public function translationsEmptySheetCoordinates($sheetId, $sheetTitle) |
115
|
1 |
|
{ |
116
|
1 |
|
return TranslationsSheetCoordinates::emptySheet( |
117
|
1 |
|
$this->getHeaderColumnsCount(), |
118
|
1 |
|
$this->getLocalesCount(), |
119
|
|
|
$this->getHeaderColumnsCount() |
120
|
|
|
)->setSheetId($sheetId)->setSheetTitle($sheetTitle); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $sheetId |
125
|
|
|
* @param $sheetTitle |
126
|
3 |
|
* @return TranslationsSheetCoordinates |
127
|
|
|
*/ |
128
|
3 |
|
public function translationsSheetCoordinates($sheetId, $sheetTitle) |
129
|
3 |
|
{ |
130
|
3 |
|
return TranslationsSheetCoordinates::sheetWithData( |
131
|
3 |
|
$this->getTranslationsCount(), |
132
|
3 |
|
$this->getHeaderColumnsCount(), |
133
|
3 |
|
$this->getLocalesCount(), |
134
|
|
|
$this->getHeaderRowsCount() |
135
|
|
|
)->setSheetId($sheetId)->setSheetTitle($sheetTitle); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
2 |
|
public function sheetStyles() |
139
|
|
|
{ |
140
|
|
|
return new Styles; |
141
|
1 |
|
} |
142
|
|
|
|
143
|
|
|
public function deleteAllSheets() |
144
|
1 |
|
{ |
145
|
1 |
|
// We need to create a black sheet, cause we cannot delete all sheets |
146
|
1 |
|
$this->api() |
147
|
|
|
->addBatchRequests([ |
148
|
1 |
|
$this->api()->addBlankSheet() |
149
|
|
|
]) |
150
|
|
|
->sendBatchRequests(); |
151
|
1 |
|
|
152
|
1 |
|
// Delete all sheet and keep the last created one |
153
|
|
|
$this->api() |
154
|
1 |
|
->addBatchRequests( |
155
|
1 |
|
collect($this->api()->freshSheets())->slice(0, -1)->map(function ($sheet) { |
156
|
1 |
|
return $this->api()->deleteSheetRequest($sheet['properties']['sheetId']); |
157
|
|
|
})->toArray() |
158
|
1 |
|
) |
159
|
1 |
|
->sendBatchRequests(); |
160
|
|
|
|
161
|
|
|
$this->api()->forgetSheets(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return api instance initialized with the spreadsheet ID. |
166
|
3 |
|
* |
167
|
|
|
* @return Api |
168
|
3 |
|
*/ |
169
|
|
|
public function api() |
170
|
|
|
{ |
171
|
|
|
return $this->api->setSpreadsheetId($this->getId()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Returns collection of the spreadsheets represented by TranslationSheet objects. |
176
|
|
|
* |
177
|
|
|
* @return Collection |
178
|
|
|
*/ |
179
|
|
|
public function sheets() |
180
|
|
|
{ |
181
|
|
|
$this->api()->forgetSheets()->getSheets(); |
182
|
|
|
|
183
|
|
|
return $this->configuredSheets()->map(function (TranslationsSheet $translationsSheet) { |
184
|
|
|
if ($sheet = $this->api()->getSheetByTitle($translationsSheet->getTitle())) { |
185
|
|
|
$translationsSheet->setId( |
186
|
|
|
data_get($sheet, 'properties.sheetId') |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $translationsSheet; |
191
|
|
|
}); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function configuredSheets() |
195
|
|
|
{ |
196
|
|
|
return collect([static::primaryTranslationSheet()]) |
197
|
|
|
->merge( |
198
|
|
|
collect(config('translation_sheet.extra_sheets')) |
199
|
|
|
->map(function ($sheetConfig) { |
200
|
|
|
/** @var TranslationsSheet $instance */ |
201
|
|
|
$instance = resolve(TranslationsSheet::class); |
202
|
|
|
|
203
|
|
|
return $instance |
204
|
|
|
->markAsExtraSheet() |
205
|
|
|
->setTitle($sheetConfig['name']) |
206
|
|
|
->setPath($sheetConfig['path']) |
207
|
|
|
->setTabColor($sheetConfig['tabColor']); |
208
|
|
|
}) |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function configuredExtraSheets() |
213
|
|
|
{ |
214
|
|
|
// Remove the first one. (aka. the translations required default sheet). |
215
|
|
|
return $this->configuredSheets()->slice(1); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function hasConfiguredExtaSheets() |
219
|
|
|
{ |
220
|
|
|
return $this->configuredExtraSheets()->count() > 0; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function ensureConfiguredSheetsAreCreated() |
224
|
|
|
{ |
225
|
|
|
$googleSheets = collect($this->api()->forgetSheets()->getSheets()); |
226
|
|
|
|
227
|
|
|
// By default, spreadsheet has 1 required sheet. This will match translation-sheet |
228
|
|
|
// default translations sheet. |
229
|
|
|
$requests = [ |
230
|
|
|
$this->api()->setSheetTitle( |
231
|
|
|
$firstSheetId = data_get(collect($googleSheets)->first(), 'properties.sheetId'), |
232
|
|
|
config('translation_sheet.primary_sheet.name', 'Translations') |
233
|
|
|
) |
234
|
|
|
]; |
235
|
|
|
|
236
|
|
|
// Then, we need to create any extra sheets before we can use them. |
237
|
|
|
$this->configuredExtraSheets()->each(function (TranslationsSheet $translationsSheet) use (&$requests) { |
238
|
|
|
if ($this->api()->getSheetByTitle($translationsSheet->getTitle())) { |
239
|
|
|
return; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$requests[] = $this->api()->addBlankSheet($translationsSheet->getTitle()); |
243
|
|
|
}); |
244
|
|
|
|
245
|
|
|
$this->api()->addBatchRequests($requests)->sendBatchRequests(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public static function primaryTranslationSheet() |
249
|
|
|
{ |
250
|
|
|
/** @var TranslationsSheet $instance */ |
251
|
|
|
$instance = resolve(TranslationsSheet::class); |
252
|
|
|
|
253
|
|
|
return $instance |
254
|
|
|
->markAsPrimarySheet() |
255
|
|
|
->setTitle(config('translation_sheet.primary_sheet.name', 'Translations')); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|