|
|
@@ -0,0 +1,123 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+$project = Project::get($id);
|
|
|
+if(!$project) throw new Exception("Project not found.");
|
|
|
+
|
|
|
+Core::addScript("public/js/app-project-form.js");
|
|
|
+
|
|
|
+$page_title = "Edit Project";
|
|
|
+
|
|
|
+$customer = $project->getCustomer();
|
|
|
+
|
|
|
+Core::addBreadcrumb("/account", "Account");
|
|
|
+Core::addBreadcrumb("/account/projects", "Projects");
|
|
|
+Core::addBreadcrumb("/account/projects/$id", $project->name);
|
|
|
+Core::addBreadcrumb("/account/projects/$id/edit", $page_title);
|
|
|
+
|
|
|
+// Check templates
|
|
|
+$templates = $customer->getTemplates();
|
|
|
+
|
|
|
+$tests = $project->getTests();
|
|
|
+$location_ids = array_column($tests, "location_id");
|
|
|
+
|
|
|
+$submit = http::value("submit");
|
|
|
+if($submit) {
|
|
|
+ try {
|
|
|
+ Core::validateCSRFToken($submit);
|
|
|
+
|
|
|
+ $new_location_ids = http::value("location_ids");
|
|
|
+ $new_location_ids = explode(", ", $new_location_ids);
|
|
|
+ $new_location_ids = array_filter($new_location_ids);
|
|
|
+
|
|
|
+ $location_ids_to_add = array_diff($new_location_ids, $location_ids);
|
|
|
+ $location_ids_to_delete = array_diff($location_ids, $new_location_ids);
|
|
|
+
|
|
|
+ $project->template_id = http::value("template_id", "int");
|
|
|
+ $project->name = http::value("name", "trim");
|
|
|
+ $project->test_count = http::value("test_count", "int");
|
|
|
+ $project->min_age = http::value("min_age", "int");
|
|
|
+ $project->max_age = http::value("max_age", "int");
|
|
|
+ $project->default_tester_fee = http::value("default_tester_fee", "int");
|
|
|
+ $project->dts_start = http::value("date_start");
|
|
|
+ $project->dts_deadline = http::value("date_deadline");
|
|
|
+
|
|
|
+ $form_exception = new FormException;
|
|
|
+
|
|
|
+ if(empty($project->template_id)) $form_exception->addError("Template is required.");
|
|
|
+ else {
|
|
|
+ $template = Template::get($project->template_id);
|
|
|
+ if($template->customer_id != $customer->id) $form_exception->addError("Invalid template selected.");
|
|
|
+ }
|
|
|
+ if(empty($project->name)) $form_exception->addError("Name is required.");
|
|
|
+ if(empty($project->test_count)) $form_exception->addError("Test count is required.");
|
|
|
+ if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
|
|
|
+ if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
|
|
|
+ if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
|
|
|
+ if(empty($project->dts_start)) $form_exception->addError("Start Date is required.");
|
|
|
+ if(empty($project->dts_deadline)) $form_exception->addError("Deadline is required.");
|
|
|
+ if($project->min_age > $project->max_age) $form_exception->addError("Minimum age cannot be greater than maximum age.");
|
|
|
+
|
|
|
+ $project->dts_start .= " 00:00:00";
|
|
|
+ $project->dts_deadline .= " 23:59:59";
|
|
|
+ $project->dts_created = Util::getDateTime();
|
|
|
+
|
|
|
+ $unix_start = strtotime($project->dts_start);
|
|
|
+ $unix_end = strtotime($project->dts_deadline);
|
|
|
+
|
|
|
+ if($unix_start > $unix_end) $form_exception->addError("Start Date cannot be after the Deadline.");
|
|
|
+
|
|
|
+ // Verify location IDs
|
|
|
+ Location::load($location_ids_to_add);
|
|
|
+ foreach($location_ids_to_add as $location_id) {
|
|
|
+ $location = Location::get($location_id);
|
|
|
+ if(!$location) $form_exception->addError("An invalid location was picked.");
|
|
|
+ if($location->customer_id != $customer->id) $form_exception->addError("An invalid location was picked.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if($form_exception->hasErrors()) throw $form_exception;
|
|
|
+
|
|
|
+ $success = $project->commit();
|
|
|
+ if(!$success) throw new Exception("Project could not be added at this time. Please try again later.");
|
|
|
+
|
|
|
+ // Create test for new location IDs
|
|
|
+ foreach($location_ids_to_add as $location_id) {
|
|
|
+ $test = new Test;
|
|
|
+ $test->project_id = $project->id;
|
|
|
+ $test->location_id = $location_id;
|
|
|
+ $test->status = Test::STATUS_UNASSIGNED;
|
|
|
+ $test->dts_created = Util::getDateTime();
|
|
|
+ $success = $test->commit();
|
|
|
+ if(!$success) throw new Exception("Test cannot be created, please try again later.");
|
|
|
+ }
|
|
|
+
|
|
|
+ // Cancel tests for removed location IDs
|
|
|
+ foreach($location_ids_to_delete as $location_id) {
|
|
|
+ $test = Test::queryOne([
|
|
|
+ "project_id" => $project->id,
|
|
|
+ "location_id" => $location_id,
|
|
|
+ "status" => (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED)
|
|
|
+ ]);
|
|
|
+ if($test) {
|
|
|
+ $test->status = Test::STATUS_CANCELLED;
|
|
|
+ $test->commit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Core::addAlert("success", "Project updated successfully.");
|
|
|
+ HTTP::redirect("/account/projects/$project->id");
|
|
|
+ }
|
|
|
+ catch(FormException $e) {
|
|
|
+ $e->generateAlerts();
|
|
|
+ }
|
|
|
+ catch(Exception $e) {
|
|
|
+ Core::addAlert("danger", $e->getMessage());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+$locations = $customer->getLocations();
|
|
|
+
|
|
|
+View::includeOnce("header", compact("page_title"));
|
|
|
+View::include("project_form", compact("project", "templates", "locations", "location_ids"));
|
|
|
+View::includeOnce("footer");
|
|
|
+
|
|
|
+?>
|