summaryrefslogtreecommitdiff
path: root/includes/lookup_component.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/lookup_component.php')
-rw-r--r--includes/lookup_component.php38
1 files changed, 38 insertions, 0 deletions
diff --git a/includes/lookup_component.php b/includes/lookup_component.php
new file mode 100644
index 0000000..18d47a6
--- /dev/null
+++ b/includes/lookup_component.php
@@ -0,0 +1,38 @@
+<?php
+/**
+ * JSON autocomplete endpoint — returns component titles matching ?q=
+ * Used by add_component.tpl datalist.
+ *
+ * @package stock
+ */
+
+namespace Bitweaver\Stock;
+
+require_once '../../kernel/includes/setup_inc.php';
+
+global $gBitDb, $gBitUser;
+
+if( !$gBitUser->hasPermission( 'p_stock_view' ) ) {
+ header( 'Content-Type: application/json' );
+ echo '[]';
+ exit;
+}
+
+$q = trim( $_GET['q'] ?? '' );
+if( strlen( $q ) < 2 ) {
+ header( 'Content-Type: application/json' );
+ echo '[]';
+ exit;
+}
+
+$rows = $gBitDb->getArray(
+ "SELECT FIRST 30 lc.content_id, lc.title
+ FROM liberty_content lc
+ WHERE lc.content_type_guid=? AND LOWER(lc.title) LIKE ?
+ ORDER BY lc.title",
+ [ 'stockcomponent', '%'.strtolower( $q ).'%' ]
+);
+
+header( 'Content-Type: application/json' );
+echo json_encode( array_values( $rows ?? [] ) );
+exit;