Просмотр исходного кода

Add location update logging support

Alan Lam 7 лет назад
Родитель
Сommit
18422bde74

+ 7 - 1
controllers/location_edit.php

@@ -6,6 +6,8 @@ $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.");
 
+$original_data = $location->getModelData();
+
 $customer = $location->getCustomer();
 
 $submit = http::value("submit");
@@ -39,7 +41,7 @@ if($submit) {
 		$location->area_manager = http::value("area_manager", "trim");
 		$location->area_manager_email = http::value("area_manager_email", "email");
 
-		$location->closed = http::value("closed", "int");
+		$location->closed = http::value("closed", "int") ? 1 : 0;
 
 		if(empty($location->name)) $form_exception->addError("Name is required.");
 		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
@@ -99,10 +101,14 @@ if($submit) {
 			}
 		}
 
+		$modified_fields = $location->getModifiedFields();
+
 		if($form_exception->hasErrors()) throw $form_exception;
 		$success = $location->commit();
 		if(!$success) throw new Exception("Failed to edit location at this time. Please try again later.");
 
+		$location->logUpdate($original_data, $modified_fields);
+
 		// Venue types
 		$venue_type_ids = http::value("venue_type_ids", "int");
 		$venue_types = VenueType::query(["id" => $venue_type_ids]);

+ 44 - 0
engine/location.php

@@ -97,4 +97,48 @@ class Location extends Model {
 		]);
 	}
 
+
+    public function logUpdate($old_data, $modified_fields) {
+
+		if(empty($modified_fields)) return true;
+
+        $entry = new LocationUpdateLogEntry;
+        $entry->location_id = $this->id;
+        $entry->user_id = Core::$user->id;
+        $entry->modifications_json = json_encode($this->getModifiedArray($old_data, $modified_fields));
+        $entry->dts = Util::getDateTime();
+        return $entry->commit();
+	}
+
+	/**
+	 * @return LocationUpdateLogEntry[]
+	 */
+	public function getUpdateLogEntries() {
+
+		return LocationUpdateLogEntry::query([
+			"location_id" => $this->id,
+		], [
+			"sort" => "dts",
+			"order" => "desc",
+		]);
+
+	}
+
+    private function getModifiedArray($old_data, $modified_fields): array {
+
+		$new_data = $this->getModelData();
+
+        $modified = [];
+
+        foreach($modified_fields as $field) {
+            $modified[] = [
+				"field" => $field,
+                "before" => $old_data->$field,
+                "after" => $new_data->$field,
+            ];
+        }
+
+        return $modified;
+    }
+
 }

+ 38 - 0
engine/location_update_log_entry.php

@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @property int $id
+ * @property int $location_id
+ * @property int $user_id
+ * @property string $modifications_json
+ * @property string $dts
+ *
+ * @method LocationUpdateLogEntry get(int $id)
+ */
+
+class LocationUpdateLogEntry extends Model {
+
+	protected static $table = "location_update_log_entry";
+	protected static $id_field = "id";
+	protected static $fillable_fields = ["location_id", "user_id", "modifications_json", "dts"];
+
+	// Object methods
+	/**
+	 * @return Location
+	 */
+	public function getLocation() {
+		return Location::get($this->location_id);
+	}
+
+	/**
+	 * @return User
+	 */
+	public function getUser() {
+		return User::get($this->user_id);
+	}
+
+	public function getModifications(): array {
+		return json_decode($this->modifications_json) ?: [];
+	}
+
+}

+ 5 - 1
engine/model.php

@@ -322,7 +322,7 @@ abstract class Model {
 		$id = $this->getID();
 
 		$nvp = [];
-		if(is_array($id)) {
+		if(is_array($id) && is_array($id_field)) {
 			$nvp = array_combine($id_field, $id);
 		}
 		else {
@@ -394,6 +394,10 @@ abstract class Model {
 			$this->in_db = true;
 		}
 
+		if($result) {
+			$this->modified_fields = [];
+		}
+
 		return !empty($result);
 	}
 

+ 5 - 0
views/location.php

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

+ 57 - 0
views/location_log_tab.php

@@ -0,0 +1,57 @@
+
+<?php
+/**
+ * @var Location $location
+ */
+
+$entries = $location->getUpdateLogEntries();
+
+?>
+<div class="tab-pane fade" id="log" role="tabpanel" aria-labelledby="log-tab">
+<?php
+if(empty($entries)) {
+    ?>
+    <div class="alert alert-info">
+        <i class="fal fa-fw fa-info-circle"></i> No log entries to display for this location.
+    </div>
+    <?php
+}
+else {
+    ?>
+    <table class="table table-sm table-bordered">
+        <thead>
+            <tr>
+                <th>Date/Time</th>
+                <th>User</th>
+                <th>Field</th>
+                <th>Before</th>
+                <th>After</th>
+            </tr>
+        </thead>
+        <tbody>
+            <?php
+            foreach($entries as $entry) {
+                $user = $entry->getUser();
+                $dts = Util::getDisplayDateTimeFromUTC($entry->dts);
+                $modifications = $entry->getModifications();
+                foreach($modifications as $modification) {
+                    ?>
+                    <tr>
+                        <td><?=htmlspecialchars($dts)?></td>
+                        <td><a href="<?=htmlspecialchars($user->getURL())?>"><?=htmlspecialchars($user->email)?></a></td>
+                        <td class="text-monospace"><?=htmlspecialchars($modification->field)?></td>
+                        <td class="text-monospace"><?=htmlspecialchars($modification->before)?></td>
+                        <td class="text-monospace"><?=htmlspecialchars($modification->after)?></td>
+                    </tr>
+                    <?php
+                }
+                ?>
+                <?php
+            }
+            ?>
+        </tbody>
+    </table>
+    <?php
+}
+?>
+</div>