summaryrefslogtreecommitdiff
path: root/mtg-pdf.php
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-07-21 11:35:54 +0200
committerDaniel Weipert <git@mail.dweipert.de>2024-07-21 11:35:54 +0200
commita50f4c0a4b8b87c36d02d0636e58aa60a6b92b39 (patch)
tree5388769f028d4b29345e9db5f29aff7a0fc7d42a /mtg-pdf.php
parent4a44b8c9f6f8379aabcc66d2bb562fef68ec8c17 (diff)
optimize error messages and use name for sql select
Diffstat (limited to 'mtg-pdf.php')
-rw-r--r--mtg-pdf.php27
1 files changed, 23 insertions, 4 deletions
diff --git a/mtg-pdf.php b/mtg-pdf.php
index b5e9fb2..068c91e 100644
--- a/mtg-pdf.php
+++ b/mtg-pdf.php
@@ -25,6 +25,10 @@ for ($idx = 0; $idx < count($matches[0]); $idx++) {
];
}
+if (empty($cards)) {
+ die("Couldn't find any cards matching the pattern.");
+}
+
/*
* Select data from DB
*/
@@ -33,21 +37,32 @@ $db = new \PDO('sqlite:AllPrintings.sqlite');
$images = [];
foreach ($cards as $card) {
- $query = 'SELECT scryfallId,layout from cardIdentifiers JOIN cards on cardIdentifiers.uuid=cards.uuid WHERE cards.setCode=:setCode AND cards.number=:number';
+ $query = <<<SQL
+ SELECT cardIdentifiers.scryfallId, cards.layout
+ FROM cardIdentifiers
+ JOIN cards ON cardIdentifiers.uuid=cards.uuid
+ WHERE (cards.name=:name OR cards.faceName=:name) AND cards.setCode=:setCode AND cards.number=:number
+ SQL;
$statement = $db->prepare($query);
- $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]);
+ $statement->execute(['name' => $card['name'], 'setCode' => $card['set'], 'number' => $card['number']]);
$result = $statement->fetch();
$id = $result['scryfallId'] ?? '';
if (empty($id)) {
- $query = 'SELECT scryfallId,layout from tokenIdentifiers JOIN tokens on tokenIdentifiers.uuid=tokens.uuid WHERE tokens.setCode=:setCode AND tokens.number=:number';
+ $query = <<<SQL
+ SELECT tokenIdentifiers.scryfallId, tokens.layout
+ FROM tokenIdentifiers
+ JOIN tokens ON tokenIdentifiers.uuid=tokens.uuid
+ WHERE (tokens.name=:name OR tokens.faceName=:name) AND tokens.setCode=:setCode AND tokens.number=:number
+ SQL;
$statement = $db->prepare($query);
- $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]);
+ $statement->execute(['name' => $card['name'], 'setCode' => $card['set'], 'number' => $card['number']]);
$result = $statement->fetch();
$id = $result['scryfallId'] ?? '';
if (empty($id)) {
echo "$card[name] ($card[set]) $card[number] not found.";
+ echo php_sapi_name() == 'cli' ? "\n" : '<br>';
continue;
}
}
@@ -67,6 +82,10 @@ foreach ($cards as $card) {
}
}
+if (empty($images)) {
+ die('No cards to print.');
+}
+
/*
* Build HTML
*/