|
| 1 | +<?php |
| 2 | + |
| 3 | +class FormatPreview |
| 4 | +{ |
| 5 | + /** |
| 6 | + * Save a Format Preview event. |
| 7 | + */ |
| 8 | + public static function save_event(string $projectid, string $round, string $page, string $proofer): void |
| 9 | + { |
| 10 | + $setters = join(", ", [ |
| 11 | + set_col_str("projectid", $projectid), |
| 12 | + set_col_str("image", $page), |
| 13 | + set_col_str("round_id", $round), |
| 14 | + set_col_str("username", $proofer), |
| 15 | + set_col_num("timestamp", time()), |
| 16 | + ]); |
| 17 | + $sql = sprintf( |
| 18 | + " |
| 19 | + INSERT INTO format_preview_events |
| 20 | + SET $setters |
| 21 | + ON DUPLICATE KEY UPDATE |
| 22 | + timestamp = %d |
| 23 | + ", |
| 24 | + time() |
| 25 | + ); |
| 26 | + DPDatabase::query($sql); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Delete any format_preview_events for this project. |
| 31 | + * |
| 32 | + * This is called when deleting a project. |
| 33 | + */ |
| 34 | + public static function delete_project_events(string $projectid): void |
| 35 | + { |
| 36 | + $sql = sprintf( |
| 37 | + " |
| 38 | + DELETE FROM format_preview_events |
| 39 | + WHERE projectid='%s' |
| 40 | + ", |
| 41 | + DPDatabase::escape($projectid) |
| 42 | + ); |
| 43 | + DPDatabase::query($sql); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Copy format_preview_events from one project to another. |
| 48 | + * |
| 49 | + * This is called when copying pages into a project. |
| 50 | + * |
| 51 | + * @param string[] $images |
| 52 | + */ |
| 53 | + public static function copy_project_events(string $src_projectid, string $dest_projectid, array $images = []): void |
| 54 | + { |
| 55 | + if ($images) { |
| 56 | + $images_where = " AND image in (" . surround_and_join(array_map("DPDatabase::escape", $images), '"', '"', ",") . ")"; |
| 57 | + } else { |
| 58 | + $images_where = ""; |
| 59 | + } |
| 60 | + |
| 61 | + $sql = sprintf( |
| 62 | + " |
| 63 | + INSERT INTO format_preview_events |
| 64 | + (projectid, image, round_id, username, timestamp) |
| 65 | + SELECT '%s', image, round_id, username, timestamp |
| 66 | + FROM format_preview_events |
| 67 | + WHERE projectid = '%s' $images_where |
| 68 | + ", |
| 69 | + DPDatabase::escape($dest_projectid), |
| 70 | + DPDatabase::escape($src_projectid) |
| 71 | + ); |
| 72 | + DPDatabase::query($sql); |
| 73 | + } |
| 74 | +} |
0 commit comments