Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
csv.php
1<?php
3
8
9
10class Csv
13{
14 use Translate\Controller\ProcessParams;
15
16 const SETTING_ID = 'TRANSLATE_IMPORT';
17
18 const ACTION_IMPORT = 'import';
19 const ACTION_PURGE = 'purge';
20 const ACTION_CANCEL = 'cancel';
21 const ACTION_UPLOAD = 'upload';
22 const ACTION_INDEX = 'index';
23 const ACTION_FINALIZE = 'finalize';
24
25 const METHOD_ADD_UPDATE = 'ADD_UPDATE';
26 const METHOD_UPDATE_ONLY = 'UPDATE_ONLY';
27 const METHOD_ADD_ONLY = 'ADD_ONLY';
28
30 private $tabId = 0;
31
33 private $encodingIn;
34
36 private $updateMethod;
37
39 private $languages;
40
42 private $csvFilePath;
43
45 private $reindex;
46
47
48
54 public function configureActions()
55 {
56 $configureActions = parent::configureActions();
58
59 $configureActions[self::ACTION_UPLOAD] = [
60 '+prefilters' => [
61 new Main\Engine\ActionFilter\HttpMethod([Main\Engine\ActionFilter\HttpMethod::METHOD_POST]),
62 $permission
63 ],
64 ];
65 $configureActions[self::ACTION_IMPORT] = [
66 '+prefilters' => [
67 $permission
68 ],
69 ];
70 $configureActions[self::ACTION_PURGE] = [
71 '+prefilters' => [
72 $permission
73 ],
74 ];
75 $configureActions[self::ACTION_CANCEL] = [
76 '+prefilters' => [
77 $permission
78 ],
79 ];
80 $configureActions[self::ACTION_FINALIZE] = [
81 '+prefilters' => [
82 $permission
83 ],
84 ];
85 $configureActions[self::ACTION_INDEX] = [
86 '+prefilters' => [
88 ],
89 ];
90
91 return $configureActions;
92 }
93
94
101 protected function init()
102 {
103 parent::init();
104
105 $tabId = $this->request->get('tabId');
106 if (empty($tabId) || (int)$tabId <= 0)
107 {
108 throw new Main\ArgumentException("Missing 'tabId' parameter");
109 }
110 $this->tabId = (int)$tabId;
111
112 $this->keepField(['encodingIn', 'updateMethod', 'csvFilePath', 'languages']);
113 $params = $this->getProgressParameters();
114
115 // languages
116 $this->languages = Translate\Config::getEnabledLanguages();
117
118 // encoding
119 $enc = $this->request->get('encodingIn');
120 if ($enc !== null && \in_array(\mb_strtolower($enc), Translate\Config::getAllowedEncodings()))
121 {
122 $this->encodingIn = \mb_strtolower($enc);
123 }
124 elseif (isset($params['encodingIn']) && \in_array($params['encodingIn'], Translate\Config::getAllowedEncodings()))
125 {
126 $this->encodingIn = $params['encodingIn'];
127 }
128
129 // update method
130 $updateMethod = $this->request->get('updateMethod');
131 if ($updateMethod !== null)
132 {
133 if (\in_array($updateMethod, [self::METHOD_ADD_ONLY, self::METHOD_UPDATE_ONLY, self::METHOD_ADD_UPDATE]))
134 {
135 $this->updateMethod = $updateMethod;
136 }
137 }
138 if (empty($this->updateMethod) && isset($params['updateMethod']))
139 {
140 $this->updateMethod = $params['updateMethod'];
141 }
142 if (empty($this->updateMethod))
143 {
144 $this->updateMethod = self::METHOD_ADD_ONLY;
145 }
146
147 // update index
148 $reindex = $this->request->get('reindex');
149 $this->reindex = ($reindex === 'Y');
150
151 // file to import
152 if (isset($params['csvFilePath']))
153 {
154 $this->csvFilePath = $params['csvFilePath'];
155 }
156
157 $this->saveProgressParameters();
158 }
159
160
166 public function importAction(): array
167 {
169 self::ACTION_IMPORT,
170 $this,
171 [
172 'tabId' => $this->tabId,
173 'encodingIn' => $this->encodingIn,
174 'updateMethod' => $this->updateMethod,
175 'csvFilePath' => $this->csvFilePath,
176 ]
177 );
178
179 $result = $action->run(true);
180
181 if (\count($action->getErrors()) > 0)
182 {
183 $this->addErrors($action->getErrors());
184 }
185
186 if ($action instanceof Translate\Controller\ITimeLimit)
187 {
188 if ($action->hasProcessCompleted() && $result['TOTAL_ITEMS'] == 0)
189 {
190 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_VOID');
191 }
192 else
193 {
194 $messagePlaceholders = [
195 '#TOTAL_PHRASES#' => $result['TOTAL_ITEMS'],
196 '#PROCESSED_PHRASES#' => $result['PROCESSED_ITEMS'],
197 ];
198 if ($action->hasProcessCompleted())
199 {
200 $result['SUMMARY'] =
201 Loc::getMessage('TR_IMPORT_COMPLETED')."\n".
202 Loc::getMessage('TR_IMPORT_ACTION_STATS', $messagePlaceholders);
203 }
204 else
205 {
206 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_ACTION_STATS', $messagePlaceholders);
207 }
208 }
209 }
210 else
211 {
212 if ($result['TOTAL_ITEMS'] == 0)
213 {
214 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_VOID');
215 }
216 else
217 {
218 $messagePlaceholders = [
219 '#TOTAL_PHRASES#' => $result['TOTAL_ITEMS'],
220 '#PROCESSED_PHRASES#' => $result['PROCESSED_ITEMS'],
221 ];
222
223 $result['SUMMARY'] =
224 Loc::getMessage('TR_IMPORT_COMPLETED')."\n".
225 Loc::getMessage('TR_IMPORT_ACTION_STATS', $messagePlaceholders);
226 }
227 }
228
229 return $result;
230 }
231
237 public function indexAction(): array
238 {
239 if ($this->reindex !== true)
240 {
241 return [
242 'STATUS' => Translate\Controller\STATUS_COMPLETED,
243 'SUMMARY' => Loc::getMessage('TR_IMPORT_COMPLETED')
244 ];
245 }
246
248 self::ACTION_INDEX,
249 $this,
250 [
251 'tabId' => $this->tabId,
252 'csvFilePath' => $this->csvFilePath,
253 ]
254 );
255
256 $result = $action->run(true);
257
258 if (\count($action->getErrors()) > 0)
259 {
260 $this->addErrors($action->getErrors());
261 }
262
263 if ($action instanceof Translate\Controller\ITimeLimit)
264 {
265 if ($action->hasProcessCompleted())
266 {
267 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_COMPLETED');
268 }
269 else
270 {
271 $messagePlaceholders = [
272 '#TOTAL_FILES#' => $result['TOTAL_ITEMS'],
273 '#PROCESSED_FILES#' => $result['PROCESSED_ITEMS'],
274 ];
275 $result['SUMMARY'] = Loc::getMessage('TR_INDEX_ACTION_STATS', $messagePlaceholders);
276 }
277 }
278 else
279 {
280 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_COMPLETED');
281 }
282
283 return $result;
284 }
285
286
292 public function uploadAction(): array
293 {
294 $result = [];
295 $success = false;
296 if (
297 isset($_FILES['csvFile'], $_FILES['csvFile']['tmp_name'])
298 && ($_FILES['csvFile']['error'] == 0)
299 && \file_exists($_FILES['csvFile']['tmp_name'])
300 )
301 {
302 if (
303 (\filesize($_FILES['csvFile']['tmp_name']) > 0)
304 && (\mb_substr($_FILES['csvFile']['name'], -4) === '.csv')
305 )
306 {
307 if ($this->moveUploadedFile($_FILES['csvFile'], '.csv'))
308 {
309 $this->saveProgressParameters();
310 $success = true;
311 }
312 }
313 else
314 {
315 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_EMPTY_FILE_ERROR')));
316 }
317 }
318 else
319 {
320 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_EMPTY_FILE_ERROR')));
321 }
322
323 if ($success)
324 {
325 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_UPLOAD_OK');
326 }
327
328 $result['STATUS'] = Translate\Controller\STATUS_COMPLETED;
329
330 return $result;
331 }
332
333
343 private function moveUploadedFile($postedFile, $suffix = '.csv', $timeToLive = 3): bool
344 {
345 if (
346 isset($postedFile['tmp_name'])
347 && \file_exists($postedFile['tmp_name'])
348 )
349 {
351 $tmpFile = Translate\IO\CsvFile::generateTemporalFile('translate', $suffix, $timeToLive);
352 if (@\copy($postedFile['tmp_name'], $tmpFile->getPhysicalPath()))
353 {
354 $this->csvFilePath = $tmpFile->getPhysicalPath();
355 return true;
356 }
357 }
358
359 $this->addError(new Main\Error(Loc::getMessage('TR_IMPORT_EMPTY_FILE_ERROR')));
360
361 return false;
362 }
363
364
372 public function cancelAction($tabId): array
373 {
374 $result = $this->purgeAction($tabId);
375 $result['SUMMARY'] = Loc::getMessage('TR_IMPORT_ACTION_CANCEL');
376
377 return $result;
378 }
379
380
389 public function purgeAction($tabId): array
390 {
391 if (empty($tabId) || (int)$tabId <= 0)
392 {
393 throw new Main\ArgumentException("Missing 'tabId' parameter");
394 }
395
396 $settings = $this->getProgressParameters();
397
398 if (!empty($settings['csvFilePath']))
399 {
400 $path = new Main\IO\File($settings['csvFilePath']);
401 if ($path->isExists())
402 {
403 $path->delete();
404 }
405 }
406
408
409 return [
410 'SUMMARY' => Loc::getMessage('TR_IMPORT_FILE_DROPPED'),
411 'STATUS' => Translate\Controller\STATUS_COMPLETED
412 ];
413 }
414
420 public function finalizeAction(): array
421 {
422 $settings = $this->getProgressParameters();
423
424 if (!empty($settings['csvFilePath']))
425 {
426 $path = new Main\IO\File($settings['csvFilePath']);
427 if ($path->isExists())
428 {
429 $path->delete();
430 }
431 }
432
434
435 return [
436 'STATUS' => Translate\Controller\STATUS_COMPLETED
437 ];
438 }
439}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29