project_add.php 4.0 KB

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