class AjaxController extends CubistFrontController
{
// Shared validation logic for dynamic CMS forms
- protected function _validate_form(Request $request) {
+ protected function _validate_form(Request $request)
+ {
$data = $request->all();
/** @var PageData $page */
$page = Page::find($data['page'])->getPageData();
});
}
- public function cart(Request $request) {
+ public function cart(Request $request)
+ {
$request->validate([
'action' => 'required|string', // add/update/delete
// Get existing session or an empty array
$cart_items = $request->session()->get('cart_items', []);
- switch($request->input('action')) {
+ switch ($request->input('action')) {
case 'add':
// If the item already exists in the cart, increment the quantity
return Product::getCartData();
}
- public function request_quote(Request $request) {
+ public function request_quote(Request $request)
+ {
// Array of product IDs => quantity
- $cartData = (array) json_decode($request->input('cart_data'));
+ $cartData = (array)json_decode($request->input('cart_data'));
// Validated form fields
$validatedData = $this->_validate_form($request);
return [];
}
+
+ public function search(Request $request)
+ {
+ $limit = $request->limit ?? 50;
+ $index = config('cubist.internal_search_index');
+
+ // Set weight of each field
+ $fields = ['short_title' => 4, 'long_title' => 1, 'keywords' => 5, 'description' => 1, 'main' => 2];
+
+ $queryfields = [];
+ foreach ($fields as $field => $weight) {
+ $queryfields[] = $field . '^' . $weight;
+ $queryfields[] = $field . '.stemmed' . '^' . $weight;
+ }
+
+ $res = \Elasticsearch::search(['index' => $index,
+ 'body' => [
+ //'explain' => true,
+ 'from' => 0,
+ 'size' => $limit,
+ 'query' => [
+ 'multi_match' => [
+ 'query' => $request->q,
+ 'fields' => $queryfields,
+ ]
+ ]
+ ]
+ ]);
+
+ $hits = [];
+ foreach ($res['hits']['hits'] as $hit) {
+ $hits[] = ['url' => $hit['_id'],
+ 'title' => $hit['_source']['short_title']
+ ];
+ }
+
+ return $hits;
+ }
}