public function postLiveSearchProductTags(Request $request) { AlbertHeijn::setApiKey(env('API_KEY_ALBERTHEIJN')); // Require a query string in order to continue if (!$request->has('query')) { return response()->json(['error' => 'Missing query input']); } // Require at least 3 character inputs if (strlen($request->input('query')) < 3) { return response()->json(['products' => []]); } $products = []; $tags = ProductTag::where('query', $request->input('query'))->take(env('LIVESEARCH_PRODUCT_TAGS_LIMIT', 10))->get(); if (count($tags) > 0) { foreach ($tags as $tag) { $products[] = $tag->tag; } } else { // Fetch products $ahProducts = AlbertHeijn::searchProducts($request->input('query')); // Dont search if there arent any products available if (count($ahProducts) === 0) { return response()->json(['products' => $products]); } foreach ($ahProducts as $product) { // Limit to 10 rows if (count($products) > 50) { break; } // Skip products which doesnt contains any joule's if (!isset($product->joule)) { continue; } // Find the product name $productname = ''; foreach (explode(' ', $product->productomschrijving) as $name) { if (strpos($name, $request->input('query')) !== false && strpos($name, '-') === false && strpos($name, '/') === false && strpos($name, '&') === false && strpos($name, '.') === false) { $productname = strtolower($name); } } // Skip already existing items if (in_array($productname, $products)) { continue; } // Skip empty names if (strlen($productname) === 0) { continue; } // Save it in the database ProductTag::create(['query' => $request->input('query'), 'tag' => $productname]); if (count($products) <= env('LIVESEARCH_PRODUCT_TAGS_LIMIT', 10)) { $products[] = $productname; } } } return response()->json(['products' => $products]); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $product = Product::findOrFail($id); $product->title = $request->title; $product->abstract = $request->abstract; $product->content = $request->content; $product->prix = $request->prix; $product->status = $request->status; $product->category_id = $request->category_id; if (!empty($request->image)) { $image = new Image(); $img_name = $request->file('image')->getClientOriginalName(); $image->name = $img_name; $request->file('image')->move(public_path('images'), $img_name); $image->uri = '/images/' . $img_name; if ($image->save()) { $product->image_id = $image->id; $product->save(); //gestion des tags if (isset($request->Etoile)) { $match = ['tag_id' => 1, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Etoile; $product_tag->save(); } } else { $match = ['tag_id' => 1, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Espace)) { $match = ['tag_id' => 2, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Espace; $product_tag->save(); } } else { $match = ['tag_id' => 2, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Galaxie)) { $match = ['tag_id' => 3, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Galaxie; $product_tag->save(); } } else { $match = ['tag_id' => 3, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Empire)) { $match = ['tag_id' => 4, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Empire; $product_tag->save(); } } else { $match = ['tag_id' => 4, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Alliance)) { $match = ['tag_id' => 5, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Alliance; $product_tag->save(); } } else { $match = ['tag_id' => 5, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } \Session::flash('message', 'Produit bien modifié en BDD.'); } else { \Session::flash('message', 'Probleme lors de l\'acces à la BDD. Merci de réessayer.'); } return redirect('admin/dashboard'); } else { if ($product->save()) { //gestion des tags if (isset($request->Etoile)) { $match = ['tag_id' => 1, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Etoile; $product_tag->save(); } } else { $match = ['tag_id' => 1, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Espace)) { $match = ['tag_id' => 2, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Espace; $product_tag->save(); } } else { $match = ['tag_id' => 2, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Galaxie)) { $match = ['tag_id' => 3, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Galaxie; $product_tag->save(); } } else { $match = ['tag_id' => 3, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Empire)) { $match = ['tag_id' => 4, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Empire; $product_tag->save(); } } else { $match = ['tag_id' => 4, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } if (isset($request->Alliance)) { $match = ['tag_id' => 5, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if ($pt->isEmpty()) { $product_tag = new ProductTag(); $product_tag->product_id = $product->id; $product_tag->tag_id = $request->Alliance; $product_tag->save(); } } else { $match = ['tag_id' => 5, 'product_id' => $product->id]; $pt = ProductTag::where($match)->get(); if (!$pt->isEmpty()) { ProductTag::where($match)->delete(); } } \Session::flash('message', 'Produit bien modifié en BDD.'); } else { \Session::flash('message', 'Probleme lors de l\'acces à la BDD. Merci de réessayer.'); } return redirect('admin/dashboard'); } }