|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\Card; |
|
8
|
|
|
use Application\Model\Export; |
|
9
|
|
|
use Application\Model\User; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @extends AbstractRepository<Export> |
|
13
|
|
|
*/ |
|
14
|
|
|
class ExportRepository extends AbstractRepository |
|
15
|
|
|
{ |
|
16
|
3 |
|
public function updateCards(Export $export, array $collectionIds, array $cardIds): int |
|
17
|
|
|
{ |
|
18
|
|
|
// Inject all collections and cards picked one-by-one |
|
19
|
3 |
|
$this->insertMultiple($export, 'export_collection', 'collection_id', $collectionIds); |
|
20
|
3 |
|
$this->insertMultiple($export, 'export_card', 'card_id', $cardIds); |
|
21
|
|
|
|
|
22
|
3 |
|
$connection = $this->getEntityManager()->getConnection(); |
|
23
|
3 |
|
$params = ['export' => $export->getId()]; |
|
24
|
|
|
|
|
25
|
|
|
// "Expand" collections into cards |
|
26
|
3 |
|
$sql = <<<STRING |
|
27
|
|
|
REPLACE INTO export_card (export_id, card_id) |
|
28
|
|
|
SELECT export_id, card_id FROM card_collection |
|
29
|
|
|
INNER JOIN export_collection |
|
30
|
|
|
ON card_collection.collection_id = export_collection.collection_id |
|
31
|
|
|
AND export_collection.export_id = :export |
|
32
|
3 |
|
STRING; |
|
33
|
3 |
|
$connection->executeStatement($sql, $params); |
|
34
|
|
|
|
|
35
|
|
|
/** @var CardRepository $cardRepository */ |
|
36
|
3 |
|
$cardRepository = $this->getEntityManager()->getRepository(Card::class); |
|
37
|
3 |
|
$cardSubQuery = $cardRepository->getAccessibleSubQuery(User::getCurrent()); |
|
38
|
|
|
|
|
39
|
|
|
// Remove cards to which we have no access |
|
40
|
3 |
|
if ($cardSubQuery) { |
|
41
|
2 |
|
$connection->executeStatement('DELETE FROM export_card WHERE export_id = :export AND card_id NOT IN (' . $cardSubQuery . ')', $params); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
$cardCount = $connection->fetchOne('SELECT COUNT(*) FROM export_card WHERE export_id = :export', $params); |
|
45
|
|
|
|
|
46
|
3 |
|
$params = [ |
|
47
|
3 |
|
'export' => $export->getId(), |
|
48
|
3 |
|
'cardCount' => $cardCount, |
|
49
|
3 |
|
]; |
|
50
|
|
|
|
|
51
|
3 |
|
$connection->executeStatement('UPDATE export SET card_count = :cardCount WHERE id = :export', $params); |
|
52
|
|
|
|
|
53
|
3 |
|
return (int) $cardCount; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
private function insertMultiple(Export $export, string $table, string $field, array $ids): void |
|
57
|
|
|
{ |
|
58
|
3 |
|
$connection = $this->getEntityManager()->getConnection(); |
|
59
|
|
|
|
|
60
|
3 |
|
$cardValues = []; |
|
61
|
3 |
|
$exportId = $export->getId(); |
|
62
|
3 |
|
foreach ($ids as $id) { |
|
63
|
3 |
|
$cardValues[] = '(' . $exportId . ', ' . $id . ')'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Inject all card picked one-by-oneS |
|
67
|
3 |
|
if ($cardValues) { |
|
68
|
3 |
|
$values = implode(', ', $cardValues); |
|
69
|
3 |
|
$connection->executeStatement("REPLACE INTO $table (export_id, $field) VALUES $values"); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|