project_edit.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. /**
  3. * @var int $id
  4. */
  5. use Carbon\CarbonImmutable;
  6. $project = Project::get($id);
  7. if(!$project) throw new Exception("Project not found.");
  8. Core::addScript("public/js/app-project-form.js");
  9. $page_title = "Edit Project";
  10. $customer = $project->getCustomer();
  11. Core::addBreadcrumb("/account", "Account");
  12. Core::addBreadcrumb("/account/projects", "Projects");
  13. Core::addBreadcrumb("/account/projects?customer_id=$customer->id", "$customer->name Projects");
  14. Core::addBreadcrumb("/account/projects/$id", $project->name);
  15. Core::addBreadcrumb("/account/projects/$id/edit", $page_title);
  16. // Check templates
  17. $templates = $customer->getTemplates();
  18. $tests = $project->getTests();
  19. $location_ids = array_column($tests, "location_id");
  20. $submit = http::value("submit");
  21. if($submit) {
  22. try {
  23. Core::validateCSRFToken($submit);
  24. $new_location_ids = HTTP::value("location_ids");
  25. $new_location_ids = explode(", ", $new_location_ids);
  26. $new_location_ids = array_filter($new_location_ids);
  27. $location_ids_to_add = array_diff($new_location_ids, $location_ids);
  28. $location_ids_to_delete = array_diff($location_ids, $new_location_ids);
  29. $project->template_id = HTTP::value("template_id", "int");
  30. $project->setTemplateKeys(HTTP::value("template_keys"));
  31. $project->name = HTTP::value("name", "trim");
  32. $project->test_count = HTTP::value("test_count", "int");
  33. $project->min_age = HTTP::value("min_age", "int");
  34. $project->max_age = HTTP::value("max_age", "int");
  35. $project->default_customer_fee = HTTP::value("default_customer_fee", "float");
  36. $project->default_tester_fee = HTTP::value("default_tester_fee", "float");
  37. $project->time_of_day = HTTP::value("time_of_day", "trim");
  38. $project->tester_can_claim = HTTP::value("tester_can_claim", "trim");
  39. $date_start = HTTP::value("date_start");
  40. $date_deadline = HTTP::value("date_deadline");
  41. $date_report = HTTP::value("date_report");
  42. $form_exception = new FormException;
  43. if(empty($project->template_id)) $form_exception->addError("Template is required.");
  44. else {
  45. $template = Template::get($project->template_id);
  46. if((int) $template->customer_id !== (int) $customer->id) $form_exception->addError("Invalid template selected.");
  47. }
  48. if(empty($project->name)) $form_exception->addError("Name is required.");
  49. if(empty($project->test_count)) $form_exception->addError("Test count is required.");
  50. if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
  51. if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
  52. if(empty($project->default_customer_fee)) $form_exception->addError("Default customer fee is required.");
  53. if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
  54. if(empty($project->time_of_day)) $form_exception->addError("Time of day is required.");
  55. else if(!in_array($project->time_of_day, ["A", "D", "N"])) $form_exception->addError("Invalid time of day selected.");
  56. if(empty($date_start)) $form_exception->addError("Start Date is required.");
  57. if(empty($date_deadline)) $form_exception->addError("Deadline is required.");
  58. if($project->min_age > $project->max_age) $form_exception->addError("Minimum age cannot be greater than maximum age.");
  59. $dt_start = CarbonImmutable::createFromFormat("Y-m-d", $date_start, "UTC");
  60. if(!$dt_start) $form_exception->addError("Start Date is invalid.");
  61. $dt_deadline = CarbonImmutable::createFromFormat("Y-m-d", $date_deadline, "UTC");
  62. if(!$dt_deadline) $form_exception->addError("Deadline is invalid.");
  63. $dt_report = null;
  64. if($date_report) {
  65. $dt_report = CarbonImmutable::createFromFormat("Y-m-d", $date_report, "UTC");
  66. if(!$dt_report) $form_exception->addError("Report Month is invalid.");
  67. }
  68. if($dt_start && $dt_deadline && $dt_start > $dt_deadline) $form_exception->addError("Start Date cannot be after the Deadline.");
  69. // Verify location IDs
  70. Location::load($location_ids_to_add);
  71. foreach($location_ids_to_add as $location_id) {
  72. $location = Location::get($location_id);
  73. if(!$location) $form_exception->addError("An invalid location was picked.");
  74. if((int) $location->customer_id !== (int) $customer->id) $form_exception->addError("An invalid location was picked.");
  75. }
  76. if($form_exception->hasErrors()) throw $form_exception;
  77. $project->dts_start = (string) $dt_start->setTime(0, 0);
  78. $project->dts_deadline = (string) $dt_deadline->setTime(23, 59, 59);
  79. $project->dts_report = $dt_report ? (string) $dt_report->setTime(12, 0, 0) : null;
  80. $success = $project->commit();
  81. if(!$success) throw new Exception("Project could not be edited at this time. Please try again later.");
  82. // Create test for new location IDs
  83. foreach($location_ids_to_add as $location_id) {
  84. $test = new Test;
  85. $test->project_id = $project->id;
  86. $test->location_id = $location_id;
  87. $test->status = Test::STATUS_UNASSIGNED;
  88. $test->dts_created = Util::getDateTime();
  89. $success = $test->commit();
  90. if(!$success) throw new Exception("Test cannot be created, please try again later.");
  91. }
  92. // Cancel tests for removed location IDs
  93. foreach($location_ids_to_delete as $location_id) {
  94. $test = Test::queryOne([
  95. "project_id" => $project->id,
  96. "location_id" => $location_id,
  97. "status" => (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED)
  98. ]);
  99. if($test) {
  100. $test->status = Test::STATUS_CANCELLED;
  101. $test->dts_updated = Util::getDateTime();
  102. $test->commit();
  103. }
  104. }
  105. Core::addAlert("success", "Project updated successfully.");
  106. HTTP::redirect("/account/projects/$project->id");
  107. }
  108. catch(FormException $e) {
  109. $e->generateAlerts();
  110. }
  111. catch(Exception $e) {
  112. Core::addAlert("danger", $e->getMessage());
  113. }
  114. }
  115. $project_locations = $project->getLocations();
  116. $project_location_ids = array_column($project_locations, "id");
  117. $locations = $customer->getLocations();
  118. $locations = array_filter($locations, static function(Location $location) use ($project_location_ids) {
  119. if(in_array($location->id, $project_location_ids, false)) return true;
  120. if($location->closed || $location->isArchived()) return false;
  121. return true;
  122. });
  123. View::includeOnce("header", compact("page_title"));
  124. View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
  125. View::includeOnce("footer");