Răsfoiți Sursa

Add ability to add notes to a location

Alan Lam 6 ani în urmă
părinte
comite
58353e433a

+ 42 - 0
controllers/location_add_note.php

@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @var int $id
+ */
+
+$user = Core::$user;
+
+$location = Location::get($id);
+if(!$location) throw new Exception("Location not found.");
+if(!Core::$user->hasPermission($location)) throw new Exception("You do not have have permission to access this page.");
+
+$submit = http::value("submit");
+if($submit) {
+	try {
+		Core::validateCSRFToken($submit);
+
+		$form_exception = new FormException;
+
+		$note = new LocationNote;
+		$note->location_id = $location->id;
+		$note->user_id = $user->id;
+		$note->content = HTTP::value("content", "trim");
+		$note->dts_created = DB::dts();
+
+		if(empty($note->content)) $form_exception->addError("Note is required.");
+
+		if($form_exception->hasErrors()) throw $form_exception;
+		$success = $note->commit();
+		if(!$success) throw new Exception("Failed to add note at this time. Please try again later.");
+
+		Core::addAlert("success", "Note added successfully.");
+	}
+	catch(FormException $e) {
+		$e->generateAlerts();
+	}
+	catch(Exception $e) {
+		Core::addAlert("danger", $e->getMessage());
+	}
+
+	HTTP::redirect("/account/locations/$location->id");
+}

+ 21 - 0
db/migrations/20201125172227_add_location_note_table.php

@@ -0,0 +1,21 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddLocationNoteTable extends AbstractMigration {
+	public function change() {
+
+		$fk_options = ["update" => "CASCADE", "delete" => "CASCADE"];
+
+		$this->table("location_note", ["signed" => false])
+			->addColumn("location_id", "integer", ["signed" => false])
+			->addColumn("user_id", "integer", ["signed" => false])
+			->addColumn("content", "text")
+			->addColumn("dts_created", "datetime")
+			->addColumn("dts_updated", "datetime", ["null" => true])
+			->addColumn("dts_deleted", "datetime", ["null" => true])
+			->addForeignKey("location_id", "location", "id", $fk_options)
+			->addForeignKey("user_id", "user", "id", $fk_options)
+			->create();
+	}
+}

+ 4 - 0
engine/db.php

@@ -286,4 +286,8 @@ class db {
 	public static function thread_id(): int {
 	public static function thread_id(): int {
 		return mysqli_thread_id(db::$DB);
 		return mysqli_thread_id(db::$DB);
 	}
 	}
+
+	public static function dts(): string {
+		return gmdate("Y-m-d H:i:s");
+	}
 }
 }

+ 14 - 0
engine/location.php

@@ -203,4 +203,18 @@ class Location extends Model {
     	return (bool) $this->archived;
     	return (bool) $this->archived;
 	}
 	}
 
 
+	/**
+	 * @return LocationNote[]
+	 * @throws Exception
+	 */
+	public function getNotes(): array {
+    	return LocationNote::query([
+    		"location_id" => $this->id,
+			"dts_deleted" => null,
+		], [
+			"sort" => "id",
+			"order" => "desc",
+		]);
+	}
+
 }
 }

+ 35 - 0
engine/location_note.php

@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * Class LocationNote
+ * @property int $id
+ * @property int $location_id
+ * @property int $user_id
+ * @property string $content
+ * @property string $dts_created
+ * @property string $dts_updated
+ * @property string $dts_deleted
+ */
+class LocationNote extends Model {
+
+	protected static $table = "location_note";
+	protected static $id_field = "id";
+	protected static $fillable_fields = ["location_id", "user_id", "content", "dts_created", "dts_updated", "dts_deleted"];
+
+	public function getUser(): User {
+		return User::get($this->user_id);
+	}
+
+	public function getLocation(): Location {
+		return Location::get($this->location_id);
+	}
+
+	public function getDateTimeCreated(): DateTime {
+		return new DateTime($this->dts_created, new DateTimeZone("UTC"));
+	}
+
+	public function getContentHTML(): string {
+		return nl2br(htmlspecialchars($this->content));
+	}
+
+}

+ 1 - 0
routes.php

@@ -120,6 +120,7 @@ $routes = [
 	// Locations
 	// Locations
 	"/^account\/locations$/" => ["controller" => "locations", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations$/" => ["controller" => "locations", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/add$/" => ["controller" => "location_add", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/add$/" => ["controller" => "location_add", "require_user_level" => User::LEVEL_CUSTOMER],
+	"/^account\/locations\/$id_pattern\/add-note$/" => ["controller" => "location_add_note", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern$/" => ["controller" => "location_view", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern$/" => ["controller" => "location_view", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern\/edit$/" => ["controller" => "location_edit", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern\/edit$/" => ["controller" => "location_edit", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern\/photo$/" => ["controller" => "location_photo", "params" => ["location_id"]],
 	"/^account\/locations\/$id_pattern\/photo$/" => ["controller" => "location_photo", "params" => ["location_id"]],

+ 4 - 0
views/location.php

@@ -9,6 +9,7 @@ $tabs[] = ["details", __("Details")];
 if(Core::$user->isAdmin()) $tabs[] = ["photos", __("Photos")];
 if(Core::$user->isAdmin()) $tabs[] = ["photos", __("Photos")];
 $tabs[] = ["tests", __("Tests")];
 $tabs[] = ["tests", __("Tests")];
 if(Core::$user->isAdmin()) $tabs[] = ["log", __("Log")];
 if(Core::$user->isAdmin()) $tabs[] = ["log", __("Log")];
+$tabs[] = ["notes", __("Notes")];
 
 
 $details = [
 $details = [
 	"ID" => $location->id,
 	"ID" => $location->id,
@@ -107,6 +108,9 @@ if($venue_types) $details["Types"] = $type_names_csv;
 		if(Core::$user->isAdmin()) {
 		if(Core::$user->isAdmin()) {
 			View::include("location_log_tab", compact("location"));
 			View::include("location_log_tab", compact("location"));
 		}
 		}
+
+		View::include("location_notes_tab", compact("location"));
+
 		?>
 		?>
 
 
 	</div>
 	</div>

+ 67 - 0
views/location_notes_tab.php

@@ -0,0 +1,67 @@
+<?php
+/**
+ * @var Location $location
+ */
+
+$user = Core::$user;
+$action = sprintf("/account/locations/%d/add-note", $location->id);
+$notes = $location->getNotes();
+
+?>
+
+<div class="tab-pane fade" id="notes" role="tabpanel" aria-labelledby="notes-tab">
+
+	<form class="card mb-3" action="<?=htmlspecialchars($action)?>" method="post">
+		<div class="card-header">
+			<?=htmlspecialchars(__("Add Note"))?>
+		</div>
+		<div class="card-body">
+			<label class="sr-only" for="note-content">
+				<?=htmlspecialchars(__("Note"))?>
+			</label>
+			<textarea name="content" id="note-content" rows="4" class="form-control" placeholder="<?=htmlspecialchars(__("Type note"))?>"></textarea>
+		</div>
+		<div class="card-footer">
+			<button class="btn btn-success" type="submit" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>">
+				<?=htmlspecialchars(__("Add Note"))?>
+			</button>
+		</div>
+	</form>
+
+	<?php
+
+	if(empty($notes)) {
+		?>
+		<div class="alert alert-info">
+			<i class="fa fa-fw fa-info-circle"></i>
+			<?=htmlspecialchars(__("No notes have been added to this location."))?>
+		</div>
+		<?php
+	}
+	else {
+		?>
+		<div class="list-group">
+			<?php
+			foreach($notes as $note) {
+				$user = $note->getUser();
+				$dt = $note->getDateTimeCreated();
+				$dt->setTimezone(new DateTimeZone(TIMEZONE));
+				?>
+				<div class="list-group-item">
+					<div>
+						<strong><?=htmlspecialchars($user->getDisplayName())?></strong>
+						<span class="float-right text-muted"><?=htmlspecialchars($dt->format("d/m/Y H:i"))?></span>
+					</div>
+					<div>
+						<?=$note->getContentHTML()?>
+					</div>
+				</div>
+				<?php
+			}
+			?>
+		</div>
+		<?php
+	}
+	?>
+
+</div>