| | 58 | case 'tags': |
| | 59 | switch (@$_GET['action']) { |
| | 60 | case 'edit': |
| | 61 | if (isset($_POST['tags'])) { |
| | 62 | $sql = "BEGIN; |
| | 63 | UPDATE tags SET count = count - 1 WHERE ROWID IN (SELECT tag FROM imagetags WHERE image = '" . $row['id'] . "'); |
| | 64 | DELETE FROM imagetags WHERE image = '" . $row['id'] . "';\n"; |
| | 65 | |
| | 66 | $tags = explode(',', $_POST['tags']); |
| | 67 | for ($i = 0; $i < count($tags); $i++) { |
| | 68 | $tags[$i] = trim($tags[$i]); |
| | 69 | } |
| | 70 | $tags = array_unique($tags); |
| | 71 | foreach ($tags as $tag) { |
| | 72 | if (empty($tag)) continue; |
| | 73 | // check if the taga already exists |
| | 74 | $row = $db->fetch($db->query("SELECT ROWID as id FROM tags WHERE tag = '" . $db->escape(strtolower($tag)) . "'")); |
| | 75 | if (!$row) { |
| | 76 | $db->exec("INSERT INTO tags (tag, text) VALUES ('" . $db->escape(strtolower($tag)) . "', '" . $db->escape($tag) . "');"); |
| | 77 | $row = $db->fetch($db->query("SELECT last_insert_rowid() as id;")); |
| | 78 | } |
| | 79 | // Save the tag for this image and update tag counter |
| | 80 | $sql .= "INSERT INTO imagetags (image, tag) VALUES('" . $id . "', '" . $row['id'] . "');\n"; |
| | 81 | $sql .= "UPDATE tags SET count = count + 1 WHERE ROWID = '" . $row['id'] . "';\n"; |
| | 82 | } |
| | 83 | $sql .= "DELETE FROM tags WHERE count < 1;\n"; |
| | 84 | $sql .= "COMMIT;"; |
| | 85 | $db->exec($sql); |
| | 86 | } |
| | 87 | default: |
| | 88 | errorMsg('No action set.'); |
| | 89 | break; |
| | 90 | break; |