project_export.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. $user = Core::$user;
  3. if(!isset($id, $type)) {
  4. $ids = http::value("ids", "int");
  5. $type = http::value("type");
  6. }
  7. else {
  8. $ids = [$id];
  9. }
  10. $projects = Project::query(["id" => $ids]);
  11. if(!$projects) {
  12. throw new RuntimeException("No projects to export.");
  13. }
  14. $template_processing = false;
  15. switch($type) {
  16. case "admin":
  17. case "customer":
  18. $template_processing = true;
  19. break;
  20. }
  21. if($template_processing) {
  22. $template_ids = array_column($projects, "template_id");
  23. $template_ids = array_unique($template_ids);
  24. if(count($template_ids) !== 1) {
  25. throw new RuntimeException("Cannot create export with different templates.");
  26. }
  27. $template = Template::get($template_ids[0]);
  28. if(!$template) {
  29. throw new RuntimeException("Template cannot be found.");
  30. }
  31. }
  32. $tests = Test::query([
  33. "project_id" => $ids,
  34. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  35. ]);
  36. switch($type) {
  37. case "admin":
  38. if(!$user->canExportProjectCsv()) {
  39. throw new RuntimeException("You do not have permission to perform this action.");
  40. }
  41. $template->exportTestsCSV("project_csv.php", $tests);
  42. break;
  43. case "customer":
  44. $template->exportTestsCSV("project_customer_csv.php", $tests);
  45. break;
  46. case "quickbooks-customer":
  47. Project::exportCSV("project_quickbooks_customer_csv.php", $projects);
  48. break;
  49. case "quickbooks-tester":
  50. Test::exportCSV("project_quickbooks_tester_csv.php", $tests);
  51. break;
  52. case "kml":
  53. // TODO
  54. break;
  55. }