/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $feedid, $id) { if (Input::has('action')) { // Filter 作業 switch ($request->input('action')) { case 'enable': $enable_checked = Input::get('enable'); if (is_array($enable_checked)) { // $products = Product::onlyTrashed()->whereIn('id', $enable_checked)->get(); foreach ($products as $product) { $product->restore(); } // redirect return Redirect::back()->with('message', 'Products Activate successfully'); //return redirect()->action('FeedProductController@index', [$id])->with('message', 'Error !: KEY 值重複 Feed_id:[ '.$id.' ]已有產品ID[ '.$item->product_id.' ]'); } break; case 'disable': $disable_checked = Input::get('disable'); if (is_array($disable_checked)) { // $products = Product::whereIn('id', $disable_checked)->get(); foreach ($products as $product) { $product->delete(); } // redirect //Session::flash('message', 'Successfully deleted the platform!'); return Redirect::back()->with('message', 'Product Disable successfully'); //return redirect()->action('FeedProductController@index', [$id])->with('message', 'Error !: KEY 值重複 Feed_id:[ '.$id.' ]已有產品ID[ '.$item->product_id.' ]'); } break; } } else { // 單一產品資料更新 $validator = Validator::make($request->all(), ['name' => 'required|max:255', 'description' => 'required|max:3000', 'url' => 'required|max:300|url', 'image' => 'required|max:300|url', 'price' => 'required|integer', 'retail_price' => 'required|integer', 'category' => 'required|max:100', 'google_category' => 'required|max:100', 'brand' => 'required|max:20', 'condition' => "required|in:new,used,refurbished", 'availability' => 'required|in:in stock,out of stock,preorder']); if ($validator->fails()) { //return redirect('admin/feeds/'.$feedid.'/product/'.$id) return Redirect::route('admin.feeds.product.edit', array($feedid, $id))->withInput()->withErrors($validator); } try { $product = Product::where('feed_id', $feedid)->where('id', $id)->first(); $product->name = $request->input('name'); $product->description = $request->input('description'); $product->url = $request->input('url'); $product->image = $request->input('image'); $product->price = $request->input('price'); $product->retail_price = $request->input('retail_price'); $product->category = $request->input('category'); $product->google_category = $request->input('google_category'); $product->brand = $request->input('brand'); $product->condition = $request->input('condition'); $product->availability = $request->input('availability'); $product->save(); } catch (Exception $e) { } return Redirect::route('admin.feeds.product.index', array($feedid))->with('message', 'Save - 產品內容更新完成!'); } }
/** * Execute the console command. * * @return mixed */ public function handle() { // $storage = Storage::disk('ftp'); $files = Storage::disk('ftp')->files('/public_html/feed/complete'); //$directories = Storage::disk('ftp')->directories('public_html'); if (count($files) > 0) { //echo explode("_",basename($files[0]))[0]; $contents = Storage::disk('ftp')->get($files[0]); // ip xml $dom = simplexml_load_string($contents) or die('feed file not loading'); //echo count($dom->channel->item); for ($i = 0; $i < count($dom->channel->item); ++$i) { $products[] = array('id' => $dom->channel->item[$i]->product_id, 'name' => $dom->channel->item[$i]->product_name, 'description' => $dom->channel->item[$i]->product_description, 'url' => urldecode($dom->channel->item[$i]->product_url), 'image' => urldecode($dom->channel->item[$i]->product_image), 'category' => $dom->channel->item[$i]->product_category, 'brand' => $dom->channel->item[$i]->product_brand, 'condition' => $dom->channel->item[$i]->product_condition, 'availability' => $dom->channel->item[$i]->product_availability, 'price' => trim($dom->channel->item[$i]->product_price, ' TWD'), 'retail_price' => trim($dom->channel->item[$i]->product_retail_price, ' TWD')); } $feed = Feed::where('location', explode("_", basename($files[0]))[0])->firstOrFail(); //echo explode("_",basename($files[0]))[0]; //echo $feed->id; //var_dump($feed); $feedId = $feed->id; DB::beginTransaction(); try { // 將已有的資料delete $affectedRows = Product::where('feed_id', $feedId)->delete(); // foreach ($products as $item) { // 已存在 $item['feed_id'] = $feedId; if (Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // restore Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->restore(); // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('DB update:' . $item['id']); } else { // insert $insert = Product::create($item); $this->info('DB insert:' . $item['id']); } } DB::commit(); // log $this->info(date('Y-m-d h:i:s') . ' DB commit:' . $feedId); } catch (\Exception $e) { DB::rollback(); switch ($e->getCode()) { case 23000: //log $this->error('DB 23000:' . $e->getLine() . $e->getCode() . $e->getMessage()); break; default: //log $this->error('DB error:' . $e->getLine() . $e->getCode() . $e->getMessage()); } } // 複製到完成資料夾 Storage::disk('ftp')->copy($files[0], str_replace('complete', 'done/complete/' . date("Ymd"), $files[0])); //刪除原檔案 Storage::disk('ftp')->delete($files[0]); } else { $files = Storage::disk('ftp')->files('/public_html/feed/modify'); //$directories = Storage::disk('ftp')->directories('public_html'); if (count($files) > 0) { //echo explode("_",basename($files[0]))[0]; $contents = Storage::disk('ftp')->get($files[0]); // ip xml $dom = simplexml_load_string($contents) or die('feed file not loading'); //echo count($dom->channel->item); $feed = Feed::where('location', explode("_", basename($files[0]))[0])->firstOrFail(); $feedId = $feed->id; DB::beginTransaction(); try { // for ($i = 0; $i < count($dom->channel->item); ++$i) { // action $action = $dom->channel->item[$i]->attributes()->action; // product data $products[] = array('feed_id' => $feedId, 'id' => $dom->channel->item[$i]->product_id, 'name' => $dom->channel->item[$i]->product_name, 'description' => $dom->channel->item[$i]->product_description, 'url' => urldecode($dom->channel->item[$i]->product_url), 'image' => urldecode($dom->channel->item[$i]->product_image), 'category' => $dom->channel->item[$i]->product_category, 'brand' => $dom->channel->item[$i]->product_brand, 'condition' => $dom->channel->item[$i]->product_condition, 'availability' => $dom->channel->item[$i]->product_availability, 'price' => trim($dom->channel->item[$i]->product_price, ' TWD'), 'retail_price' => trim($dom->channel->item[$i]->product_retail_price, ' TWD')); // process switch ($action) { case "add": if (Product::withTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { if (Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // restore Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->restore(); // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('Product restore and update:' . $item['id']); } else { // add 的產品已存在 $this->error('Product to add existed! ' . $item['id']); } } else { // insert $insert = Product::create($item); $this->info('Product insert:' . $item['id']); } break; case "delete": if (Product::where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // delete Product::where('feed_id', $feedId)->where('id', $item['id'])->delete(); $this->info('Product delete:' . $item['id']); } break; case "modify": if (Product::withTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { if (Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // restore Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->restore(); // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('Product restore and update by modify :' . $item['id']); } else { // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('Product restore and update by modify :' . $item['id']); } } break; } } // //foreach ($products as $item){ // 已存在 //$item['feed_id'] = $feedId; //if(Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()){ // restore //Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->restore(); // update //$affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); //$this->info('DB update:'. $item['id']); //} //else{ // insert //$insert = Product::create($item); //$this->info('DB insert:'. $item['id']); //} //} DB::commit(); // log $this->info(date('Y-m-d h:i:s') . ' Product commit:' . $feedId); } catch (\Exception $e) { DB::rollback(); switch ($e->getCode()) { case 23000: //log $this->error('DB 23000:' . $e->getLine() . $e->getCode() . $e->getMessage()); break; default: //log $this->error('DB error:' . $e->getLine() . $e->getCode() . $e->getMessage()); } } // 複製到完成資料夾 Storage::disk('ftp')->copy($files[0], str_replace('modify', 'done/modify/' . date("Ymd"), $files[0])); //刪除原檔案 Storage::disk('ftp')->delete($files[0]); } } }
/** * Execute the console command. * * @return mixed */ public function handle() { // $feedId = $this->argument('id'); $feed = Feed::findOrFail($feedId); //$parser = new Parser(); //$xml = $parser->xml(''); //$item = array(); $dom = simplexml_load_file($feed->location) or die('feed not loading'); echo count($dom->channel->item); for ($i = 0; $i < count($dom->channel->item); ++$i) { $products[] = array('id' => $dom->channel->item[$i]->product_id, 'name' => $dom->channel->item[$i]->product_name, 'description' => $dom->channel->item[$i]->product_description, 'url' => urldecode($dom->channel->item[$i]->product_url), 'image' => urldecode($dom->channel->item[$i]->product_image), 'category' => $dom->channel->item[$i]->product_category, 'brand' => $dom->channel->item[$i]->product_brand, 'condition' => $dom->channel->item[$i]->product_condition, 'availability' => $dom->channel->item[$i]->product_availability, 'price' => trim($dom->channel->item[$i]->product_price, ' TWD'), 'retail_price' => trim($dom->channel->item[$i]->product_retail_price, ' TWD')); } DB::beginTransaction(); try { switch ($feed->content) { case "complete": // 將已有的資料delete $affectedRows = Product::where('feed_id', $feedId)->delete(); // foreach ($products as $item) { // 已存在 $item['feed_id'] = $feedId; if (Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // restore Product::onlyTrashed()->where('feed_id', $feedId)->where('id', $item['id'])->restore(); // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('DB update:' . $item['id']); } else { // insert $insert = Product::create($item); $this->info('DB insert:' . $item['id']); } } break; case "modify": // foreach ($products as $item) { // 已存在 $item['feed_id'] = $feedId; if (Product::where('feed_id', $feedId)->where('id', $item['id'])->exists()) { // update $affectedRows = Product::where('feed_id', $feedId)->where('id', $item['id'])->update($item); $this->info('DB update:' . $item['id']); } else { // insert $insert = Product::create($item); $this->info('DB insert:' . $item['id']); } } break; } DB::commit(); // log $this->info(date('Y-m-d h:i:s') . ' DB commit:' . $feedId); } catch (\Exception $e) { DB::rollback(); switch ($e->getCode()) { case 23000: //log $this->error('DB 23000:' . $e->getLine() . $e->getCode() . $e->getMessage()); break; default: //log $this->error('DB error:' . $e->getLine() . $e->getCode() . $e->getMessage()); } } //var_dump($products); //$this->info('XXX:'. $feed->location); }
public function google(Request $request, $id) { // Generate Google FeedContent $feed = Feed::where('id', $id)->get(); $total = Product::where('feed_id', $id)->count(); if ($feed->isEmpty()) { return view(404); } if (Input::has('start')) { if (Input::has('limit')) { $products = Product::where('feed_id', $id)->skip($request->input('start'))->take($request->input('limit'))->get(); } else { // 預設兩萬筆 $products = Product::where('feed_id', $id)->skip($request->input('start'))->take(5000)->get(); } } else { if (Input::has('limit')) { $products = Product::where('feed_id', $id)->take($request->input('limit'))->get(); } else { // 預設兩萬筆 $products = Product::where('feed_id', $id)->take(5000)->get(); } } //$products = Product::where('feed_id', $id)->get(); if ($request->input('action') == '1') { return response()->view('media.google', compact('products', 'feed', 'total'))->header('Content-Description', 'File Transfer')->header('Content-Disposition', 'attachment; filename=' . $feed->first()->description . '.xml')->header('Content-Transfer-Encoding', 'binary'); } else { $outofstock = Product::onlyTrashed()->where('feed_id', $id)->get(); return view('media.google', compact('products', 'feed', 'total', 'outofstock')); } }