project_edit.php 4.9 KB

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