project_edit.php 6.0 KB

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