project_add.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. Core::addScript("public/js/app-project-form.js");
  3. $page_title = "Add Project";
  4. $customer = Customer::getForAdmin();
  5. if(empty($customer)) throw new Exception("Customer not found.");
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/projects", "Projects");
  8. Core::addBreadcrumb("/account/projects?customer_id=$customer->id", "$customer->name Projects");
  9. Core::addBreadcrumb("/account/projects/add", $page_title);
  10. // Check templates
  11. $templates = $customer->getTemplates();
  12. if(empty($templates)) throw new Exception("A template must exist for $customer->name before a project can be created.");
  13. $project = new Project;
  14. $project->dts_start = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1));
  15. $project->dts_deadline = date("Y-m-d H:i:s", strtotime("last day of " . date("M")));
  16. $project->customer_id = $customer->id;
  17. $project->default_tester_fee = 10;
  18. $location_ids = http::value("location_ids");
  19. $location_ids = explode(", ", $location_ids);
  20. $location_ids = array_filter($location_ids);
  21. $submit = http::value("submit");
  22. if($submit) {
  23. try {
  24. Core::validateCSRFToken($submit);
  25. $project->template_id = http::value("template_id", "int");
  26. $project->setTemplateKeys(HTTP::value("template_keys"));
  27. $project->name = http::value("name", "trim");
  28. $project->test_count = http::value("test_count", "int");
  29. $project->min_age = http::value("min_age", "int");
  30. $project->max_age = http::value("max_age", "int");
  31. $project->default_tester_fee = http::value("default_tester_fee", "float");
  32. $project->time_of_day = http::value("time_of_day", "trim");
  33. $project->tester_can_claim = http::value("tester_can_claim", "trim");
  34. $date_start = http::getDateFromFields("date_start");
  35. $date_deadline = http::getDateFromFields("date_deadline");
  36. $date_report = http::getDateFromFields("date_report");
  37. $form_exception = new FormException;
  38. if(empty($project->template_id)) $form_exception->addError("Template is required.");
  39. else {
  40. $template = Template::get($project->template_id);
  41. if($template->customer_id != $customer->id) $form_exception->addError("Invalid template selected.");
  42. }
  43. if(empty($project->name)) $form_exception->addError("Name is required.");
  44. if(empty($project->test_count)) $form_exception->addError("Test count is required.");
  45. if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
  46. if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
  47. if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
  48. if(empty($project->time_of_day)) $form_exception->addError("Time of day is required.");
  49. else if(!in_array($project->time_of_day, ["A", "D", "N"])) $form_exception->addError("Invalid time of day selected.");
  50. if(empty($date_start)) $form_exception->addError("Start Date is required.");
  51. if(empty($date_deadline)) $form_exception->addError("Deadline is required.");
  52. if($project->min_age > $project->max_age) $form_exception->addError("Minimum age cannot be greater than maximum age.");
  53. $unix_start = strtotime($date_start . " 00:00:00");
  54. $unix_deadline = strtotime($date_deadline . " 23:59:59");
  55. $unix_report = null;
  56. if(!empty($date_report)) {
  57. $unix_report = strtotime($date_report . " 12:00:00");
  58. }
  59. $project->dts_start = Util::getDateTime($unix_start);
  60. $project->dts_deadline = Util::getDateTime($unix_deadline);
  61. $project->dts_created = Util::getDateTime();
  62. $project->dts_report = Util::getDateTime($unix_report);
  63. if($unix_start > $unix_deadline) $form_exception->addError("Start Date cannot be after the Deadline.");
  64. // Verify location IDs
  65. Location::load($location_ids);
  66. foreach($location_ids as $location_id) {
  67. $location = Location::get($location_id);
  68. if(!$location) $form_exception->addError("An invalid location was picked.");
  69. if($location->customer_id != $customer->id) $form_exception->addError("An invalid location was picked.");
  70. }
  71. if($form_exception->hasErrors()) throw $form_exception;
  72. $success = $project->commit();
  73. if(!$success) throw new Exception("Project could not be added at this time. Please try again later.");
  74. // Create test for location IDs
  75. foreach($location_ids as $location_id) {
  76. $test = new Test;
  77. $test->project_id = $project->id;
  78. $test->location_id = $location_id;
  79. $test->status = Test::STATUS_UNASSIGNED;
  80. $test->dts_created = Util::getDateTime();
  81. $success = $test->commit();
  82. if(!$success) throw new Exception("Test cannot be created, please try again later.");
  83. }
  84. Core::addAlert("success", "Project added 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(["closed" => 0, "archived" => 0]);
  95. View::includeOnce("header", compact("page_title"));
  96. View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
  97. View::includeOnce("footer");