project_edit.php 5.8 KB

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