1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Chatlas BEdita plugin |
6
|
|
|
* |
7
|
|
|
* Copyright 2023 Atlas Srl |
8
|
|
|
*/ |
9
|
|
|
namespace BEdita\Chatlas\Command; |
10
|
|
|
|
11
|
|
|
use BEdita\Chatlas\Client\ChatlasClient; |
12
|
|
|
use BEdita\Chatlas\Utility\ReadCSVTrait; |
13
|
|
|
use BEdita\Core\Utility\LoggedUser; |
14
|
|
|
use Cake\Command\Command; |
15
|
|
|
use Cake\Console\Arguments; |
16
|
|
|
use Cake\Console\ConsoleIo; |
17
|
|
|
use Cake\Console\ConsoleOptionParser; |
18
|
|
|
use Cake\Log\LogTrait; |
19
|
|
|
use Cake\Utility\Hash; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Import CSV and create questions or documents |
23
|
|
|
* |
24
|
|
|
* @property \BEdita\Core\Model\Table\ObjectsTable $Collections |
25
|
|
|
*/ |
26
|
|
|
class ImportCsvCommand extends Command |
27
|
|
|
{ |
28
|
|
|
use LogTrait; |
29
|
|
|
use ReadCSVTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Chatlas API client |
33
|
|
|
* |
34
|
|
|
* @var \BEdita\Chatlas\Client\ChatlasClient |
35
|
|
|
*/ |
36
|
|
|
protected ChatlasClient $chatlas; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public $defaultTable = 'Collections'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
|
|
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser |
47
|
|
|
{ |
48
|
|
|
return $parser->addOption('file', [ |
49
|
|
|
'help' => 'Path of CSV file to import', |
50
|
|
|
'short' => 'f', |
51
|
|
|
'required' => true, |
52
|
|
|
]) |
53
|
|
|
->addOption('type', [ |
54
|
|
|
'help' => 'Type of object to import: documents or questions', |
55
|
|
|
'short' => 't', |
56
|
|
|
'default' => 'documents', |
57
|
|
|
'choices' => ['documents', 'questions'], |
58
|
|
|
'required' => true, |
59
|
|
|
]) |
60
|
|
|
->addOption('collection', [ |
61
|
|
|
'help' => 'Collection used to index (use the unique collection name)', |
62
|
|
|
'short' => 'c', |
63
|
|
|
'required' => true, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
*/ |
70
|
|
|
public function initialize(): void |
71
|
|
|
{ |
72
|
|
|
$this->chatlas = new ChatlasClient(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
*/ |
78
|
|
|
public function execute(Arguments $args, ConsoleIo $io) |
79
|
|
|
{ |
80
|
|
|
$filepath = $args->getOption('file'); |
81
|
|
|
if (!file_exists($filepath)) { |
82
|
|
|
$io->abort(sprintf('File not found: %s', $filepath)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$name = $args->getOption('collection'); |
86
|
|
|
$response = $this->chatlas->get('/collections', compact('name')); |
87
|
|
|
$collectionId = Hash::get($response->getJson(), '0.cmetadata.id'); |
88
|
|
|
if (empty($collectionId)) { |
89
|
|
|
$io->abort(sprintf('Collection not found: %s', $name)); |
90
|
|
|
} |
91
|
|
|
$collection = $this->Collections->get($collectionId); |
92
|
|
|
$this->readCSVFile($filepath); |
93
|
|
|
$Table = $this->fetchTable($args->getOption('type')); |
94
|
|
|
$entities = []; |
95
|
|
|
LoggedUser::setUserAdmin(); |
96
|
|
|
foreach ($this->csvData as $item) { |
97
|
|
|
$item['status'] = 'on'; |
98
|
|
|
$entity = $Table->newEntity($item); |
99
|
|
|
$entities[] = $Table->saveOrFail($entity); |
100
|
|
|
} |
101
|
|
|
/** @phpstan-ignore-next-line */ |
102
|
|
|
$this->Collections->addRelated($collection, 'has_documents', $entities); |
103
|
|
|
|
104
|
|
|
$io->out('Done'); |
105
|
|
|
|
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|