MCPcopy Create free account
hub / github.com/Rello/analytics / importClipboard

Method importClipboard

lib/Service/DataloadService.php:571–653  ·  view source on GitHub ↗

* Import clipboard data * * @NoAdminRequired * @param int $objectId * @param $import * @param bool $isDataset * @return array|false * @throws \OCP\DB\Exception */

($objectId, $import, bool $isDataset, $storageMapping = null, $header = null, $delimiter = null)

Source from the content-addressed store, hash-verified

569 * @throws \OCP\DB\Exception
570 */
571 public function importClipboard($objectId, $import, bool $isDataset, $storageMapping = null, $header = null, $delimiter = null)
572 {
573 $datasetId = $this->getDatasetId($objectId, $isDataset);
574 if ($datasetId != '') {
575 $dataset = $this->DatasetService->read((int)$datasetId);
576 if (is_array($dataset) && ($dataset['storageMode'] ?? 'legacy') === 'flexible_shared') {
577 if ($import === '') {
578 return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [[
579 'code' => 'invalid_clipboard_import',
580 'message' => 'No data was provided.',
581 'details' => [],
582 ]]];
583 }
584 try {
585 if ($storageMapping === null) {
586 $delimiter = $this->detectDelimiter($import);
587 $rows = array_map(static fn (string $row): array => str_getcsv($row, $delimiter), str_getcsv($import, "\n"));
588 $columns = $this->FlexibleStorageService->getDescriptor((int)$datasetId)['columns'];
589 $header = array_column($columns, 'name');
590 if ($rows !== [] && array_map('trim', $rows[0]) === $header) {
591 array_shift($rows);
592 }
593 $storageMapping = $this->positionalImportMapping($columns, $header, $rows);
594 } else {
595 if (!is_array($header) || !is_string($delimiter) || $delimiter === '') {
596 throw new FlexibleStorageException('invalid_clipboard_import', 'Flexible clipboard imports require explicit header and delimiter values.');
597 }
598 $rows = array_map(static fn (string $row): array => str_getcsv($row, $delimiter), str_getcsv($import, "\n"));
599 }
600 $result = $this->FlexibleStorageService->executeMappedLoad((int)$datasetId, $storageMapping, $header, $rows, false);
601 $result['delimiter'] = $delimiter;
602 $this->DatasetService->provider((int)$datasetId);
603 return $result;
604 } catch (FlexibleStorageException $e) {
605 return ['insert' => 0, 'update' => 0, 'delete' => 0, 'error' => 1, 'validationErrors' => [$e->toResponse()['error']]];
606 }
607 }
608 $insert = $update = $errorMessage = $errorCounter = 0;
609 $delimiter = '';
610
611 if ($import === '') {
612 $errorMessage = $this->l10n->t('No data');
613 } else {
614 $delimiter = $this->detectDelimiter($import);
615 $rows = str_getcsv($import, "\n");
616
617 foreach ($rows as &$row) {
618 $row = str_getcsv($row, $delimiter);
619 $numberOfColumns = count($row);
620 // last column needs to be a float
621 $row[2] = $this->floatvalue($row[$numberOfColumns - 1]);
622 if ($row[2] === false) {
623 $errorCounter++;
624 } else {
625 if ($numberOfColumns < 3) $row[1] = null;
626 $action = $this->StorageService->update($datasetId, $row[0], $row[1], $row[2]);
627 $insert = $insert + $action['insert'];
628 $update = $update + $action['update'];

Callers

nothing calls this directly

Calls 12

getDatasetIdMethod · 0.95
detectDelimiterMethod · 0.95
floatvalueMethod · 0.95
getDescriptorMethod · 0.80
executeMappedLoadMethod · 0.80
toResponseMethod · 0.80
countFunction · 0.50
readMethod · 0.45
providerMethod · 0.45
tMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected