Browse Source

Added support for editing projects.

Alan Lam 8 years ago
parent
commit
76289986cc
5 changed files with 198 additions and 13 deletions
  1. 5 6
      controllers/project_add.php
  2. 123 0
      controllers/project_edit.php
  3. 1 1
      routes.php
  4. 14 1
      views/project.php
  5. 55 5
      views/project_form.php

+ 5 - 6
controllers/project_add.php

@@ -20,14 +20,13 @@ $project = new Project;
 $project->customer_id = $customer->id;
 $project->customer_id = $customer->id;
 $project->default_tester_fee = 10;
 $project->default_tester_fee = 10;
 
 
+$location_ids = http::value("location_ids");
+$location_ids = explode(", ", $location_ids);
+$location_ids = array_filter($location_ids);
+
 $submit = http::value("submit");
 $submit = http::value("submit");
 if($submit) {
 if($submit) {
 	try {
 	try {
-
-		$location_ids = http::value("location_ids");
-		$location_ids = explode(", ", $location_ids);
-		$location_ids = array_filter($location_ids);
-
 		Core::validateCSRFToken($submit);
 		Core::validateCSRFToken($submit);
 
 
 		$project->template_id = http::value("template_id", "int");
 		$project->template_id = http::value("template_id", "int");
@@ -102,7 +101,7 @@ if($submit) {
 $locations = $customer->getLocations();
 $locations = $customer->getLocations();
 
 
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
-View::include("project_add_form", compact("project", "templates", "locations"));
+View::include("project_form", compact("project", "templates", "locations", "location_ids"));
 View::includeOnce("footer");
 View::includeOnce("footer");
 
 
 ?>
 ?>

+ 123 - 0
controllers/project_edit.php

@@ -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");
+
+?>

+ 1 - 1
routes.php

@@ -44,7 +44,7 @@ $routes = [
 	"/^account\/projects$/" => ["controller" => "projects", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects$/" => ["controller" => "projects", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/add$/" => ["controller" => "project_add", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/add$/" => ["controller" => "project_add", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern$/" => ["controller" => "project", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/$id_pattern$/" => ["controller" => "project", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
-	"/^account\/projects\/$id_pattern\/edit$/" => ["controller" => "project_edit", "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/edit$/" => ["controller" => "project_edit", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 
 
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 
 

+ 14 - 1
views/project.php

@@ -5,7 +5,20 @@ var projectID = <?=$project->id?>;
 <input type="hidden" id="submit" value="<?=$_SESSION["csrf_token"]?>" />
 <input type="hidden" id="submit" value="<?=$_SESSION["csrf_token"]?>" />
 
 
 <div class="container pt-3 my-4">
 <div class="container pt-3 my-4">
-	<h2><?=htmlspecialchars($project->name)?></h2>
+	<div class="row">
+		<div class="col-auto">
+			<h2><?=htmlspecialchars($project->name)?></h2>
+		</div>
+		<div class="col text-right">
+		<?php
+		if(Core::$user->level >= User::LEVEL_ADMIN) {
+			?>
+			<a class="btn btn-outline-primary" href="/account/projects/<?=$project->id?>/edit">Edit</a>
+			<?php
+		}
+		?>
+		</div>
+	</div>
 
 
 	<div class="card mb-3">
 	<div class="card mb-3">
 		<div class="card-body">
 		<div class="card-body">

+ 55 - 5
views/project_add_form.php → views/project_form.php

@@ -1,5 +1,14 @@
+<?php
+
+$date_start = explode(" ", $project->dts_start)[0];
+$date_deadline = explode(" ", $project->dts_deadline)[0];
+
+?>
 <div class="container mt-3 py-4">
 <div class="container mt-3 py-4">
-	<h2>Add Project</h2>
+	<h2>
+		<?=($project->id ? "Edit" : "Add")?>
+		Project
+	</h2>
 
 
 	<form method="post">
 	<form method="post">
 
 
@@ -50,25 +59,63 @@
 			<div class="col">
 			<div class="col">
 				<div class="form-group">
 				<div class="form-group">
 					<label for="date-start">Start Date</label>
 					<label for="date-start">Start Date</label>
-					<input class="form-control" type="date" name="date_start" id="date-start" placeholder="Enter start date" value="<?=htmlspecialchars($project->dts_start)?>" required />
+					<input class="form-control" type="date" name="date_start" id="date-start" placeholder="Enter start date" value="<?=htmlspecialchars($date_start)?>" required />
 				</div>
 				</div>
 			</div>
 			</div>
 			<div class="col">
 			<div class="col">
 				<div class="form-group">
 				<div class="form-group">
 					<label for="date-deadline">Deadline</label>
 					<label for="date-deadline">Deadline</label>
-					<input class="form-control" type="date" name="date_deadline" id="date-deadline" placeholder="Enter deadline" value="<?=htmlspecialchars($project->dts_deadline)?>" required />
+					<input class="form-control" type="date" name="date_deadline" id="date-deadline" placeholder="Enter deadline" value="<?=htmlspecialchars($date_deadline)?>" required />
 				</div>
 				</div>
 			</div>
 			</div>
 		</div>
 		</div>
 
 
-		<input type="hidden" name="location_ids" id="location-ids" />
+		<hr />
+
+		<input type="hidden" name="location_ids" id="location-ids" value="<?=implode(", ", $location_ids)?>" />
 
 
 		<h3>Project Locations</h3>
 		<h3>Project Locations</h3>
 
 
+		<?php
+		if($project->id) {
+			?>
+			<div class="alert alert-warning">
+				<div class="row">
+					<div class="col-auto">
+						<i class="fal fa-exclamation-triangle"></i>
+					</div>
+					<div class="col pl-0">
+						Please be aware that deselecting a location will cancel the test for the project no matter its current status.<br />
+						<strong>This action cannot be undone, you have been warned!</strong>
+					</div>
+				</div>
+			</div>
+			<?php
+		}
+		?>
+
 		<div class="row">
 		<div class="row">
 			<div class="col-md mb-3">
 			<div class="col-md mb-3">
 				<h4>Selected</h4>
 				<h4>Selected</h4>
 				<div class="list-group" id="selected-locations-list-group">
 				<div class="list-group" id="selected-locations-list-group">
+				<?php
+				foreach($locations as $index => $location) {
+					if(!in_array($location->id, $location_ids)) continue;
+					?>
+					<div class="list-group-item" data-location-id="<?=$location->id?>" data-name="<?=htmlspecialchars($location->name)?>">
+						<div class="row">
+							<div class="col-auto">
+								<?=htmlspecialchars($location->name)?><br />
+								<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]))?></small>
+							</div>
+							<div class="col align-self-center text-right">
+								<button class="btn btn-outline-primary" type="button">Deselect</button>
+							</div>
+						</div>
+					</div>
+					<?php
+				}
+				?>
 				</div>
 				</div>
 				<div class="alert alert-info" id="selected-locations-alert">
 				<div class="alert alert-info" id="selected-locations-alert">
 					No locations selected for testing.
 					No locations selected for testing.
@@ -79,6 +126,7 @@
 				<div class="list-group" id="not-selected-locations-list-group">
 				<div class="list-group" id="not-selected-locations-list-group">
 				<?php
 				<?php
 				foreach($locations as $index => $location) {
 				foreach($locations as $index => $location) {
+					if(in_array($location->id, $location_ids)) continue;
 					?>
 					?>
 					<div class="list-group-item" data-location-id="<?=$location->id?>" data-name="<?=htmlspecialchars($location->name)?>">
 					<div class="list-group-item" data-location-id="<?=$location->id?>" data-name="<?=htmlspecialchars($location->name)?>">
 						<div class="row">
 						<div class="row">
@@ -101,6 +149,8 @@
 			</div>
 			</div>
 		</div>
 		</div>
 
 
-		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Add</button>
+		<hr />
+
+		<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Submit</button>
 	</form>
 	</form>
 </div>
 </div>