1
0

project_edit.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $project->createTestForLocationId($location_id);
  85. }
  86. // Cancel tests for removed location IDs
  87. foreach($location_ids_to_delete as $location_id) {
  88. $test = $project->getActiveTestForLocationId($location_id);
  89. if($test) {
  90. $test->status = Test::STATUS_CANCELLED;
  91. $test->dts_updated = Util::getDateTime();
  92. $test->commit();
  93. }
  94. }
  95. Core::addAlert("success", "Project updated successfully.");
  96. HTTP::redirect("/account/projects/$project->id");
  97. }
  98. catch(FormException $e) {
  99. $e->generateAlerts();
  100. }
  101. catch(Exception $e) {
  102. Core::addAlert("danger", $e->getMessage());
  103. }
  104. }
  105. $project_locations = $project->getLocations();
  106. $project_location_ids = array_column($project_locations, "id");
  107. $locations = $customer->getLocations();
  108. $locations = array_filter($locations, static function(Location $location) use ($project_location_ids) {
  109. if(in_array($location->id, $project_location_ids, false)) return true;
  110. if($location->closed || $location->isArchived()) return false;
  111. return true;
  112. });
  113. View::includeOnce("header", compact("page_title"));
  114. View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
  115. View::includeOnce("footer");