1
0

project_export.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. case "account":
  19. $template_processing = true;
  20. break;
  21. }
  22. if($template_processing) {
  23. $template_ids = array_column($projects, "template_id");
  24. $template_ids = array_unique($template_ids);
  25. if(count($template_ids) !== 1) {
  26. throw new RuntimeException("Cannot create export with different templates.");
  27. }
  28. $template = Template::get($template_ids[0]);
  29. if(!$template) {
  30. throw new RuntimeException("Template cannot be found.");
  31. }
  32. }
  33. $tests = Test::query([
  34. "project_id" => $ids,
  35. "status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
  36. ]);
  37. switch($type) {
  38. case "admin":
  39. if(!$user->canExportProjectCsv()) {
  40. throw new RuntimeException("You do not have permission to perform this action.");
  41. }
  42. $template->exportTestsCSV("project_csv.php", $tests);
  43. break;
  44. case "customer":
  45. $template->exportTestsCSV("project_customer_csv.php", $tests);
  46. break;
  47. case "account":
  48. $template->exportTestsCSV("project_account_csv.php", $tests);
  49. break;
  50. case "quickbooks-customer":
  51. Project::exportCSV("project_quickbooks_customer_csv.php", $projects);
  52. break;
  53. case "quickbooks-tester":
  54. Test::exportCSV("project_quickbooks_tester_csv.php", $tests);
  55. break;
  56. case "kml":
  57. // TODO
  58. break;
  59. }