Explorar o código

Added sorting.

Alan Lam %!s(int64=8) %!d(string=hai) anos
pai
achega
abc3de6976
Modificáronse 2 ficheiros con 44 adicións e 3 borrados
  1. 14 3
      controllers/locations.php
  2. 30 0
      views/locations.php

+ 14 - 3
controllers/locations.php

@@ -8,6 +8,12 @@ Core::addBreadcrumb("/account/locations", "Locations");
 $customer = Customer::getForAdmin();
 if(!$customer) throw new Exception("You do not have permission to access this page.");
 
+$sorts = [
+	"name" => "Name",
+	"last-added" => "Last Added",
+	"internal-id" => "Internal ID",
+];
+
 $page_title = "$customer->name Locations";
 
 Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
@@ -24,7 +30,7 @@ if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($nam
 
 $sort = http::value("sort");
 switch($sort) {
-	case "last_added":
+	case "last-added":
 		$query_sort = "id";
 		$order = "desc";
 		break;
@@ -34,13 +40,18 @@ switch($sort) {
 		break;
 }
 
-
 $locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
 
+if($sort == "internal-id") {
+	usort($locations, function($a, $b) {
+		return strnatcasecmp($a->internal_id, $b->internal_id);
+	});
+}
+
 $filter_count = count($nvp) - 1;
 
 View::includeOnce("header", compact("page_title"));
-View::include("locations", compact("customer", "filter_count", "locations"));
+View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
 View::includeOnce("footer");
 
 ?>

+ 30 - 0
views/locations.php

@@ -1,3 +1,9 @@
+<?php
+$query = $_GET;
+unset($query["path"]);
+$query["sort"] = "SORT";
+?>
+
 <script>
 var customerId = <?=$customer->id?>;
 </script>
@@ -83,6 +89,30 @@ var customerId = <?=$customer->id?>;
 	}
 	?>
 
+	<div class="row mb-3">
+		<div class="col-sm">
+		</div>
+		<div class="col-sm-auto">
+			<div class="input-group">
+				<div class="input-group-prepend">
+					<span class="input-group-text">
+						Sort
+					</span>
+				</div>
+				<select class="form-control" id="sort-dropdown" data-url-template="/account/locations?<?=htmlspecialchars(http_build_query($query))?>">
+				<?php
+				foreach($sorts as $value => $display) {
+					$selected = ($value == $sort) ? "selected" : "";
+					?>
+					<option value="<?=$value?>" <?=$selected?>><?=$display?></option>
+					<?php
+				}
+				?>
+				</select>
+			</div>
+		</div>
+	</div>
+
 	<?php
 	View::include("location_list", compact("locations"));
 	?>