WebOrbiton
v1.0.0.1

StocketBase

37 lines · 1.2 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. if (count(get_included_files()) === 1) {
  6. http_response_code(403);
  7. exit;
  8. }
  9. ​
  10. $productId = (int) ($_POST['product_id'] ?? 0);
  11. $versionId = (int) ($_POST['version_id'] ?? 0);
  12. ​
  13. if ($productId <= 0 || $versionId <= 0) {
  14. return ['We couldn\'t find that version.', 'error', null];
  15. }
  16. ​
  17. $statement = Database::site()->prepare('SELECT created_by, deleted_at FROM products WHERE id = :id LIMIT 1');
  18. $statement->execute(['id' => $productId]);
  19. $product = $statement->fetch();
  20. ​
  21. if (!$product || $product['deleted_at'] !== null) {
  22. return ['We couldn\'t find that product.', 'error', null];
  23. }
  24. ​
  25. $isOwner = (int) $product['created_by'] === (int) $currentUser['id'];
  26. if (!$isOwner && !Auth::hasRoleAtLeast(Auth::ROLE_STORE_OWNER)) {
  27. return ['You don\'t have access to this product\'s history.', 'error', null];
  28. }
  29. ​
  30. if (!ProductVersions::restore($productId, $versionId, (int) $currentUser['id'], (string) $currentUser['display_name'])) {
  31. return ['We couldn\'t find that version.', 'error', null];
  32. }
  33. ​
  34. ActivityLog::record('product.restore_version', 'product', $productId, 'version #' . $versionId);
  35. ​
  36. return ['Earlier version restored.', 'success', $productId];
  37. ​