project_edit.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_retest_fee = HTTP::value("default_retest_fee", "float");
  37. $project->default_tester_fee = HTTP::value("default_tester_fee", "float");
  38. $project->time_of_day = HTTP::value("time_of_day", "trim");
  39. $project->tester_can_claim = HTTP::value("tester_can_claim", "trim");
  40. $project->browse_time = HTTP::value("browse_time", "int");
  41. $project->play_money = HTTP::value("play_money", "int");
  42. $project->play_time = HTTP::value("play_time", "int");
  43. $date_start = HTTP::value("date_start");
  44. $date_deadline = HTTP::value("date_deadline");
  45. $date_report = HTTP::value("date_report");
  46. $form_exception = new FormException;
  47. if(empty($project->template_id)) $form_exception->addError("Template is required.");
  48. else {
  49. $template = Template::get($project->template_id);
  50. if((int) $template->customer_id !== (int) $customer->id) $form_exception->addError("Invalid template selected.");
  51. }
  52. if(empty($project->name)) $form_exception->addError("Name is required.");
  53. if(empty($project->test_count)) $form_exception->addError("Test count is required.");
  54. if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
  55. if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
  56. if(empty($project->default_customer_fee)) $form_exception->addError("Default customer fee is required.");
  57. if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
  58. if(empty($project->time_of_day)) $form_exception->addError("Time of day is required.");
  59. else if(!in_array($project->time_of_day, ["A", "D", "N"])) $form_exception->addError("Invalid time of day selected.");
  60. if(empty($date_start)) $form_exception->addError("Start Date is required.");
  61. if(empty($date_deadline)) $form_exception->addError("Deadline is required.");
  62. if($project->min_age > $project->max_age) $form_exception->addError("Minimum age cannot be greater than maximum age.");
  63. $dt_start = CarbonImmutable::createFromFormat("Y-m-d", $date_start, "UTC");
  64. if(!$dt_start) $form_exception->addError("Start Date is invalid.");
  65. $dt_deadline = CarbonImmutable::createFromFormat("Y-m-d", $date_deadline, "UTC");
  66. if(!$dt_deadline) $form_exception->addError("Deadline is invalid.");
  67. $dt_report = null;
  68. if($date_report) {
  69. $dt_report = CarbonImmutable::createFromFormat("Y-m-d", $date_report, "UTC");
  70. if(!$dt_report) $form_exception->addError("Report Month is invalid.");
  71. }
  72. if($dt_start && $dt_deadline && $dt_start > $dt_deadline) $form_exception->addError("Start Date cannot be after the Deadline.");
  73. // Verify location IDs
  74. Location::load($location_ids_to_add);
  75. foreach($location_ids_to_add as $location_id) {
  76. $location = Location::get($location_id);
  77. if(!$location) $form_exception->addError("An invalid location was picked.");
  78. if((int) $location->customer_id !== (int) $customer->id) $form_exception->addError("An invalid location was picked.");
  79. }
  80. if($form_exception->hasErrors()) throw $form_exception;
  81. $project->dts_start = (string) $dt_start->setTime(0, 0);
  82. $project->dts_deadline = (string) $dt_deadline->setTime(23, 59, 59);
  83. $project->dts_report = $dt_report ? (string) $dt_report->setTime(12, 0, 0) : null;
  84. $success = $project->commit();
  85. if(!$success) throw new Exception("Project could not be edited at this time. Please try again later.");
  86. // Create test for new location IDs
  87. foreach($location_ids_to_add as $location_id) {
  88. $project->createTestForLocationId($location_id);
  89. }
  90. // Cancel tests for removed location IDs
  91. foreach($location_ids_to_delete as $location_id) {
  92. $test = $project->getActiveTestForLocationId($location_id);
  93. if($test) {
  94. $test->status = Test::STATUS_CANCELLED;
  95. $test->dts_updated = Util::getDateTime();
  96. $test->commit();
  97. }
  98. }
  99. Core::addAlert("success", "Project updated successfully.");
  100. HTTP::redirect("/account/projects/$project->id");
  101. }
  102. catch(FormException $e) {
  103. $e->generateAlerts();
  104. }
  105. catch(Exception $e) {
  106. Core::addAlert("danger", $e->getMessage());
  107. }
  108. }
  109. $project_locations = $project->getLocations();
  110. $project_location_ids = array_column($project_locations, "id");
  111. $locations = $customer->getLocations();
  112. $locations = array_filter($locations, static function(Location $location) use ($project_location_ids) {
  113. if(in_array($location->id, $project_location_ids, false)) return true;
  114. if($location->closed || $location->isArchived()) return false;
  115. return true;
  116. });
  117. View::includeOnce("header", compact("page_title"));
  118. View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
  119. View::includeOnce("footer");