/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, ['product_title' => 'required']); $product = new Product(); $product->is_published = $request->get('is_published') == 'on' ? 1 : 0; $product->title = $request->get('product_title'); $product->description = $request->get('product_description'); $product->min_player = $request->get('product_input-players-min'); $product->max_player = $request->get('product_input-players-max'); $product->min_age = $request->get('product_input-age-min'); $product->cost_price = $request->get('product_costprice'); $product->sale_price = $request->get('product_saleprice'); $product->save(); if ($request->get('product_categories') != null) { foreach ($request->get('product_categories') as $category_id) { $product->categories()->attach($category_id); } } $files = $request->file('product_images'); $count = 0; foreach ($files as $file) { if ($file == null) { continue; } $product->addMedia($file)->usingFileName($product->id . "_" . $count . "." . $file->getClientOriginalExtension())->toCollection('images'); $count++; } Session::flash('flash_message', 'Product successfully added!'); return view('dashboard/create_product')->with('categories', Category::get())->with('product', new Product()); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Requests\Product $request) { try { DB::beginTransaction(); $product = new Product(); $product->name = $request->input('name'); $product->model = $request->input('model'); if ($request->hasFile('photo')) { $extension = strtolower($request->file('photo')->getClientOriginalExtension()); $validExtensions = ['jpg', 'jpeg', 'png']; $photoName = uniqid() . '.' . $extension; if (in_array($extension, $validExtensions)) { Image::make($request->file('photo'))->resize(260, 180, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save('uploads/products/' . $photoName); $product->photo = $photoName; } else { flash()->error('Image must be of extension jpg/png'); return redirect()->back(); } } $product->added_by = auth()->user()->id; $product->save(); if ($request->has('category_id') && count($request->input('category_id')) > 0) { $product->categories()->attach($request->input('category_id')); } DB::commit(); flash()->success('Product information added successfully.'); return redirect()->back(); } catch (\Exception $e) { DB::rollback(); flash()->error($e->getMessage()); return redirect()->back(); } }
public function doLoad(Request $request) { $input = $request->all(); $rules = ['productList' => 'required']; $messages = ['productList.required' => 'Es necesario seleccionar un archivo']; $validator = Validator::make($input, $rules, $messages); if ($validator->passes()) { Excel::load($request->file('productList'), function ($reader) { $rows = $reader->get(); foreach ($rows as $row) { $product = new Product(); $product->nombre = !empty($row->nombre) ? $row->nombre : ""; $product->barcode = !empty($row->barcode) ? $row->barcode : ""; $product->marca = !empty($row->marca) ? $row->marca : ""; $product->fuente = !empty($row->fuente) ? $row->fuente : ""; $product->denominacion_legal = !empty($row->denominacion_legal) ? $row->denominacion_legal : ""; $product->alimento = !empty($row->alimento) ? $row->alimento : ""; $product->unidad = !empty($row->unidad) ? $row->unidad : ""; $product->cantidad = !empty($row->cantidad) ? $row->cantidad : ""; $product->calorias = !empty($row->calorias) ? $row->calorias : ""; $product->calorias_porcentaje = !empty($row->calorias_porcentaje) ? $row->calorias_porcentaje : ""; $product->hidratos = !empty($row->hidratos) ? $row->hidratos : ""; $product->azucares = !empty($row->azucares) ? $row->azucares : ""; $product->sacarosa = !empty($row->sacarosa) ? $row->sacarosa : ""; $product->polidextro = !empty($row->polidextro) ? $row->polidextro : ""; $product->almidon = !empty($row->almidon) ? $row->almidon : ""; $product->polialcoholes = !empty($row->polialcoholes) ? $row->polialcoholes : ""; $product->fibra = !empty($row->fibra) ? $row->fibra : ""; $product->proteinas = !empty($row->proteinas) ? $row->proteinas : ""; $product->grasas = !empty($row->grasas) ? $row->grasas : ""; $product->ags = !empty($row->ags) ? $row->ags : ""; $product->agmi = !empty($row->agmi) ? $row->agmi : ""; $product->agpi = !empty($row->agpi) ? $row->agpi : ""; $product->trans = !empty($row->trans) ? $row->trans : ""; $product->colesterol = !empty($row->colesterol) ? $row->colesterol : ""; $product->epa = !empty($row->epa) ? $row->epa : ""; $product->dha = !empty($row->dha) ? $row->dha : ""; $product->omega_3 = !empty($row->omega_3) ? $row->omega_3 : ""; $product->omega_6 = !empty($row->omega_6) ? $row->omega_6 : ""; $product->omega_9 = !empty($row->omega_9) ? $row->omega_9 : ""; $product->sodio = !empty($row->sodio) ? $row->sodio : ""; $product->calcio = !empty($row->calcio) ? $row->calcio : ""; $product->hierro = !empty($row->hierro) ? $row->hierro : ""; $product->fosforo = !empty($row->fosforo) ? $row->fosforo : ""; $product->potasio = !empty($row->potasio) ? $row->potasio : ""; $product->magnesio = !empty($row->magnesio) ? $row->magnesio : ""; $product->selenio = !empty($row->selenio) ? $row->selenio : ""; $product->zinc = !empty($row->zinc) ? $row->zinc : ""; $product->vit_a_mc = !empty($row->vit_a_mc) ? $row->vit_a_mc : ""; $product->vit_a_ui = !empty($row->vit_a_ui) ? $row->vit_a_ui : ""; $product->vit_e_mc = !empty($row->vit_e_mc) ? $row->vit_e_mc : ""; $product->vit_e_ui = !empty($row->vit_e_ui) ? $row->vit_e_ui : ""; $product->vit_d = !empty($row->vit_d) ? $row->vit_d : ""; $product->b1 = !empty($row->b1) ? $row->b1 : ""; $product->b2 = !empty($row->b2) ? $row->b2 : ""; $product->niacina = !empty($row->niacina) ? $row->niacina : ""; $product->b6 = !empty($row->b6) ? $row->b6 : ""; $product->b9 = !empty($row->b9) ? $row->b9 : ""; $product->b12 = !empty($row->b12) ? $row->b12 : ""; $product->vit_c = !empty($row->vit_c) ? $row->vit_c : ""; $product->indice_glucemico = !empty($row->indice_glucemico) ? $row->indice_glucemico : ""; $product->alcohol = !empty($row->alcohol) ? $row->alcohol : ""; $product->calorias_alcohol = !empty($row->calorias_alcohol) ? $row->calorias_alcohol : ""; $product->descripcion = !empty($row->descripcion) ? $row->descripcion : ""; $product->ingredientes = !empty($row->ingredientes) ? $row->ingredientes : ""; $product->nombre_cientifico = !empty($row->nombre_cientifico) ? $row->nombre_cientifico : ""; $product->tags = !empty($row->tags) ? $row->tags : ""; $product->otros_nombres = !empty($row->otros_nombres) ? $row->otros_nombres : ""; $product->rnpa = !empty($row->rnpa) ? $row->rnpa : ""; $product->rne = !empty($row->rne) ? $row->rne : ""; $product->user_id = session('user')->id; $product->save(); $equivalences = !empty($row->equivalencias) ? array_map('trim', explode(',', $row->equivalencias)) : []; foreach ($equivalences as $equivalence) { $equivalence = explode(':', $equivalence); Equivalence::create(['product_id' => $product->id, 'name' => $equivalence[0], 'amount' => $equivalence[1]]); } $categories = array_map('trim', explode(',', $row->categorias)); $product->categories()->attach($categories); if (!empty($row->foto)) { $foto = file_get_contents($row->foto); $filename = basename($row->foto); $ext = explode('.', $filename)[1]; $filename = $product->id . '.' . $ext; \Storage::put('products/' . $filename, $foto); $product->foto = $filename; $product->save(); } else { $product->foto = ""; } } }); return response()->json(['status' => 'ok', 'url' => url('product/index')]); } else { return response()->json(['status' => 'error', 'message' => $validator->messages()->all()]); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate request $validateProduct = Validator::make($request->get('Product'), Product::$rules); $validationMessages = []; foreach ($request->get('ProductTranslation') as $key => $value) { $validateProductTranslation = Validator::make($value, ProductTranslation::$rules); if ($validateProductTranslation->fails()) { $validationMessages = array_merge_recursive($validationMessages, $validateProductTranslation->messages()->toArray()); } } if ($validateProduct->fails() or count($validationMessages) > 0) { $validationMessages = array_merge_recursive($validateProduct->messages()->toArray(), $validationMessages); return redirect()->back()->withErrors($validationMessages)->withInput(); } // get all languages $languages = Language::all(); // find language default $languageDefault = $languages->where('is_key_language', 1)->first(); if (is_null($languageDefault)) { $languageDefault = $languages->first(); } // sure execute success, if not success rollback DB::transaction(function () use($request, $languageDefault) { $user = $request->user(); // insert Product $product = new Product(); $product->key = Common::createKeyURL($request->input('ProductTranslation.' . $languageDefault->code . '.name')); $product->code = $request->input('Product.code'); $product->model = $request->input('Product.model'); $product->producer_id = $request->input('Product.producer_id'); $product->origin = $request->input('Product.origin'); $product->unit = $request->input('Product.unit'); $product->price = $request->input('Product.price'); $product->discount = $request->input('Product.discount'); $product->priority = $request->input('Product.priority'); $product->is_publish = $request->input('Product.is_publish'); $product->created_by = $user->name; $product->updated_by = $user->name; $product->save(); // sync categories if ($request->input('Product.categories') != "") { $categories = explode(",", $request->input('Product.categories')); if (count($categories) > 0) { $product->categories()->attach($categories); } } // sync colors if ($request->input('Product.colors') != "") { $colors = explode(",", $request->input('Product.colors')); if (count($colors) > 0) { $product->colors()->attach($colors); } } // save attachments if ($request->input('Product.attachments') != "") { $requestAttachments = explode(',', $request->input('Product.attachments')); $attachments = []; foreach ($requestAttachments as $key => $value) { array_push($attachments, new Attachment(['entry_id' => $product->id, 'table_name' => 'products', 'path' => $value, 'priority' => 0, 'is_publish' => 1])); } if (count($attachments) > 0) { $product->attachments()->saveMany($attachments); } } // save data languages foreach ($request->get('ProductTranslation') as $locale => $value) { $product->translateOrNew($locale)->name = $request->input('ProductTranslation.' . $locale . '.name'); $product->translateOrNew($locale)->summary = $request->input('ProductTranslation.' . $locale . '.summary'); $product->translateOrNew($locale)->content = $request->input('ProductTranslation.' . $locale . '.content'); $product->translateOrNew($locale)->meta_description = $request->input('ProductTranslation.' . $locale . '.meta_description'); $product->translateOrNew($locale)->meta_keywords = $request->input('ProductTranslation.' . $locale . '.meta_keywords'); } $product->save(); }); return redirect()->route('admin.products.index'); }
public function subscriptioncallback(Request $request) { if ($request->has('hub_mode')) { if ($request->get('hub_mode') == 'subscribe') { print $request->get('hub_challenge'); exit(1); } } else { $objects = $request->json()->all(); foreach ($objects as $object) { $instagram = new InstagramAPI(); $instagramAccount = InstagramAccount::where('instagram_id', $object['object_id'])->first(); if ($instagramAccount->isSupplier()) { $instagram->setAccessToken($instagramAccount->access_token); $media = $instagram->getUserMedia($instagramAccount->instagram_id, 1); if ($media->meta->code == 200) { foreach ($media->data as $singleMedia) { $isKoalaProduct = false; foreach ($singleMedia->tags as $tag) { if ($tag == 'koalabazaar') { $isKoalaProduct = true; } } if ($isKoalaProduct && $singleMedia->type == 'image' && ProductsInstagram::where('id', '=', $singleMedia->id)->first() == null) { $caption = null; if (isset($singleMedia->caption)) { $caption = $singleMedia->caption->text; } $product = new Product(); $product->supplier_id = $instagramAccount->instagramable->id; $product->title = $caption; $product->description = $caption; $file = new FileEntry(); $status = $file->storeFromUrl($singleMedia->images->standard_resolution->url, $instagramAccount->instagramable->id, 'product'); if ($status) { $product->image = $file->filename; } else { $product->image = null; } if ($caption == null) { $product->price = null; $product->currency_unit_id = null; } else { $text = mb_strtolower($caption, 'UTF-8'); $units = CurrencyUnit::all(); $estimatedPrice = null; $currencyUnit = null; foreach ($units as $unit) { $firstOccurence = stripos($text, $unit->unit_short_name); if ($firstOccurence) { for ($i = $firstOccurence - 1; $i >= 0; $i--) { $charAt = substr($text, $i, 1); if (is_numeric($charAt) || $charAt == '.') { $estimatedPrice = $charAt . $estimatedPrice; } else { $i = 0; } } $currencyUnit = $unit->id; break; } } if ($estimatedPrice) { $product->price = $estimatedPrice; $product->currency_unit_id = $currencyUnit; } else { $product->price = null; $product->currency_unit_id = null; } } if ($product->price == null || $product->currency_unit_id == null) { $product->is_active = false; } else { $product->is_active = true; } $product->save(); foreach ($singleMedia->tags as $tag) { $relatedCategories = Category::where('keywords', 'LIKE', '%' . mb_strtolower($tag, 'UTF-8') . '%')->get(); foreach ($relatedCategories as $relatedCategory) { if ($product->categories()->where('id', $relatedCategory->id)->first()) { } else { $product->categories()->attach($relatedCategory); } } } $productInstagram = new ProductsInstagram(); $productInstagram->product_id = $product->id; $productInstagram->url = $singleMedia->link; $productInstagram->id = $singleMedia->id; $productInstagram->image_url = $singleMedia->images->standard_resolution->url; $productInstagram->caption = $caption; $productInstagram->created_on_instagram = date('Y-m-d h:i:sa', $singleMedia->created_time); $productInstagram->save(); } } } } } } }