1
0

project_export.php 1.7 KB

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