project_edit.php 4.5 KB

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