|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nikaia\TranslationSheet\Sheet; |
|
4
|
|
|
|
|
5
|
|
|
class TranslationsSheet extends AbstractSheet |
|
6
|
|
|
{ |
|
7
|
11 |
|
protected $title = null; |
|
8
|
|
|
protected $id = null; |
|
9
|
|
|
protected $path = null; |
|
10
|
11 |
|
protected $tabColor = null; |
|
11
|
|
|
protected $primary = true; |
|
12
|
10 |
|
|
|
13
|
10 |
|
public function getId() |
|
14
|
|
|
{ |
|
15
|
|
|
return $this->id; |
|
16
|
|
|
} |
|
17
|
9 |
|
|
|
18
|
|
|
public function setId($id) |
|
19
|
9 |
|
{ |
|
20
|
|
|
$this->id = $id; |
|
21
|
|
|
|
|
22
|
7 |
|
return $this; |
|
23
|
|
|
} |
|
24
|
7 |
|
|
|
25
|
|
|
public function getTitle() |
|
26
|
|
|
{ |
|
27
|
3 |
|
return $this->title; |
|
28
|
|
|
} |
|
29
|
3 |
|
|
|
30
|
|
|
public function setTitle($title) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->title = $title; |
|
33
|
|
|
|
|
34
|
2 |
|
return $this; |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function setPath($path) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->path = $path; |
|
40
|
2 |
|
|
|
41
|
|
|
return $this; |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function getPath() |
|
45
|
|
|
{ |
|
46
|
2 |
|
return $this->path ?? app()->make('path.lang'); |
|
47
|
2 |
|
} |
|
48
|
2 |
|
|
|
49
|
|
|
public function setTabColor($tabColor) |
|
50
|
1 |
|
{ |
|
51
|
|
|
$this->tabColor = $tabColor; |
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function getTabColor() |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->tabColor ?? $this->styles()->translationSheetTabColor(); |
|
58
|
2 |
|
} |
|
59
|
2 |
|
|
|
60
|
|
|
public function markAsPrimarySheet() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->primary = true; |
|
63
|
2 |
|
|
|
64
|
2 |
|
return $this; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function markAsExtraSheet() |
|
68
|
2 |
|
{ |
|
69
|
2 |
|
$this->primary = false; |
|
70
|
2 |
|
|
|
71
|
2 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function isPrimarySheet() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->primary; |
|
77
|
2 |
|
} |
|
78
|
2 |
|
|
|
79
|
2 |
|
public function isExtraSheet() |
|
80
|
2 |
|
{ |
|
81
|
2 |
|
return !$this->primary; |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function coordinates() |
|
85
|
|
|
{ |
|
86
|
2 |
|
return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle()); |
|
87
|
2 |
|
} |
|
88
|
2 |
|
|
|
89
|
2 |
|
public function emptyCoordinates() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle()); |
|
92
|
|
|
} |
|
93
|
2 |
|
|
|
94
|
|
|
public function setup() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->spreadsheet->api() |
|
97
|
2 |
|
->addBatchRequests([ |
|
98
|
2 |
|
// Set properties |
|
99
|
2 |
|
$this->spreadsheet->api()->setSheetPropertiesRequest( |
|
100
|
2 |
|
$this->getId(), |
|
101
|
1 |
|
$this->getTitle(), |
|
102
|
|
|
$this->styles()->translationSheetTabColor() |
|
103
|
|
|
), |
|
104
|
|
|
]) |
|
105
|
2 |
|
->sendBatchRequests(); |
|
106
|
|
|
} |
|
107
|
2 |
|
|
|
108
|
|
|
public function writeTranslations($translations) |
|
109
|
|
|
{ |
|
110
|
|
|
$translations = array_values($translations); |
|
111
|
2 |
|
|
|
112
|
2 |
|
$this->spreadsheet->setTranslations($translations); |
|
113
|
|
|
|
|
114
|
1 |
|
$this->spreadsheet->api() |
|
115
|
|
|
->writeCells($this->coordinates()->dataShortRange(), $translations); |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
public function readTranslations() |
|
119
|
1 |
|
{ |
|
120
|
1 |
|
return $this->spreadsheet->api()->readCells($this->getId(), $this->coordinates()->dataShortRange(2, true)); |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function styleDocument() |
|
124
|
1 |
|
{ |
|
125
|
|
|
$fullkeyRange = $this->coordinates()->fullKeyColumnRange(); |
|
126
|
1 |
|
$translationsRange = $this->coordinates()->translationsRange(); |
|
127
|
|
|
$metaRange = $this->coordinates()->metaColumnsRange(); |
|
128
|
1 |
|
|
|
129
|
1 |
|
$requests = [ |
|
130
|
|
|
// Header row |
|
131
|
1 |
|
$this->spreadsheet->api()->frozenRowRequest($this->getId()), |
|
132
|
1 |
|
$this->spreadsheet->api()->styleArea($this->emptyCoordinates()->headerRange(), $this->styles()->translationsHeader()), |
|
133
|
1 |
|
$this->spreadsheet->api()->protectRangeRequest($this->emptyCoordinates()->headerRange(), 'HEADER.'), |
|
134
|
|
|
|
|
135
|
|
|
// Full key column |
|
136
|
1 |
|
$this->spreadsheet->api()->frozenColumnRequest($this->getId()), |
|
137
|
|
|
$this->spreadsheet->api()->protectRangeRequest($fullkeyRange, 'FULL_KEY'), |
|
138
|
1 |
|
$this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), 0, 1, 450), |
|
139
|
1 |
|
$this->spreadsheet->api()->styleArea($fullkeyRange, $this->styles()->fullKeyColumn()), |
|
140
|
|
|
|
|
141
|
1 |
|
// Translations columns |
|
142
|
|
|
$this->spreadsheet->api()->styleArea($translationsRange, $this->styles()->translationsColumns()), |
|
143
|
1 |
|
|
|
144
|
|
|
// Meta columns |
|
145
|
1 |
|
$this->spreadsheet->api()->protectRangeRequest($metaRange, 'META'), |
|
146
|
|
|
$this->spreadsheet->api()->styleArea($metaRange, $this->styles()->metaColumns()), |
|
147
|
|
|
$this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->namespaceColumnIndex(), $this->coordinates()->namespaceColumnIndex() + 1, 145), |
|
148
|
2 |
|
$this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->groupColumnIndex(), $this->coordinates()->groupColumnIndex() + 1, 80), |
|
149
|
|
|
$this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->keyColumnIndex(), $this->coordinates()->keyColumnIndex() + 1, 240), |
|
150
|
2 |
|
$this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->sourceFileColumnIndex(), $this->coordinates()->sourceFileColumnIndex() + 1, 360), |
|
151
|
|
|
]; |
|
152
|
2 |
|
|
|
153
|
2 |
|
// Fixed locales translations column width |
|
154
|
1 |
|
$beginAt = 1; |
|
155
|
|
|
foreach ($this->spreadsheet->getLocales() as $locale) { |
|
156
|
|
|
$requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350); |
|
157
|
2 |
|
$beginAt++; |
|
158
|
1 |
|
} |
|
159
|
|
|
|
|
160
|
2 |
|
// Send requests |
|
161
|
|
|
$this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); |
|
162
|
1 |
|
|
|
163
|
|
|
// Delete extra columns and rows if any |
|
164
|
1 |
|
try { |
|
165
|
1 |
|
$this->spreadsheet->api()->addBatchRequests([ |
|
166
|
1 |
|
$this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()), |
|
167
|
|
|
$this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()), |
|
168
|
1 |
|
])->sendBatchRequests(); |
|
169
|
|
|
} catch (\Google_Service_Exception $e) { |
|
170
|
|
|
// Avoid exception failing the command |
|
171
|
|
|
// ... If there is no extra columns or rows Google api will raise an exception : |
|
172
|
|
|
// ... Invalid requests[xxx].deleteDimension: Cannot delete a column that doesn't exist ... |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function prepareForWrite() |
|
177
|
|
|
{ |
|
178
|
|
|
// We need to remove all protected ranges, to avoid any duplicates that may lead to |
|
179
|
|
|
// some weird behaviours |
|
180
|
|
|
$this->removeAllProtectedRanges(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function lockTranslations() |
|
184
|
|
|
{ |
|
185
|
|
|
$range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId())); |
|
186
|
|
|
|
|
187
|
|
|
$this->api() |
|
188
|
|
|
->addBatchRequests([ |
|
189
|
|
|
$this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'), |
|
190
|
|
|
$this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()), |
|
191
|
|
|
]) |
|
192
|
|
|
->sendBatchRequests(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function unlockTranslations() |
|
196
|
|
|
{ |
|
197
|
|
|
$requests = []; |
|
198
|
|
|
$range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId())); |
|
199
|
|
|
|
|
200
|
|
|
$protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS'); |
|
201
|
|
|
foreach ($protectedRanges as $protectedRange) { |
|
202
|
|
|
$requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns()); |
|
206
|
|
|
|
|
207
|
|
|
$this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function isTranslationsLocked() |
|
211
|
|
|
{ |
|
212
|
|
|
$protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS'); |
|
213
|
|
|
|
|
214
|
|
|
return !empty($protectedRanges) && count($protectedRanges) > 0; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function removeAllProtectedRanges() |
|
218
|
|
|
{ |
|
219
|
|
|
$protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId()); |
|
220
|
|
|
|
|
221
|
|
|
$requests = []; |
|
222
|
|
|
foreach ($protectedRanges as $protectedRange) { |
|
223
|
|
|
$requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if (empty($requests)) { |
|
227
|
|
|
return; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function updateHeaderRow() |
|
234
|
|
|
{ |
|
235
|
|
|
$this->api()->writeCells( |
|
236
|
|
|
$this->emptyCoordinates()->headerShortRange(), |
|
237
|
|
|
[$this->spreadsheet->getHeader()] |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|