public static function newRecordByTranslation($data) { $rules = self::$rules; $messages = self::messages(); $validator = Validator::make($data, $rules, $messages); if ($validator->fails()) { return ['code' => 400, 'type' => 'InvalidData', 'messages' => $validator]; } try { $newCategory = new ProductsCategory(); $newCategory->save(); } catch (\Exception $exception) { return ['code' => 400, 'type' => 'SavingException', 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), 'redirect' => 'back']; } $translationResponse = ProductCategoryTranslation::createNew(['category_id' => $newCategory->id, 'locale' => $data['locale'], 'title' => $data['title']]); return $translationResponse; }
/** * Execute the job. * * @return void */ public function handle() { $filepath = 'data_upload/product_upload/' . $this->product_upload->id . '/' . $this->product_upload->filename; $contents = Storage::get($filepath); $modelnames = ['Product', 'ProductCategory', 'ProductCollection', 'ProductVariant', 'ProductColor', 'ProductMaterial']; $checked = array(); $ok = false; foreach ($modelnames as $modelname) { $checked[$modelname] = []; } $success = 0; $message = ''; $lines = explode("\n", $contents); //debug('Lines: '.count($lines)); $first_line_ok = true; $products_saved = 0; if (count($lines) < 2) { $message = trans('data_upload.line_count_too_low'); } else { foreach ($lines as $n => $line) { $line = trim(utf8_encode($line)); $fields = explode(';', $line); if ($n == 0) { if (count($fields) != config('data_upload.products.count_fields')) { $message = trans('data_upload.column_count_not_matching'); $first_line_ok = false; } } else { if ($first_line_ok) { if (count($fields) == config('data_upload.products.count_fields')) { $ok = true; $german_product_category = TextHelp::unQuote($fields[5]); $english_product_category = TextHelp::unQuote($fields[6]); $slug = trim(strtolower(str_slug($english_product_category))); if (strlen($slug) > 2) { $german_slug = strtolower(str_slug($german_product_category)); $product_category_id = 0; $dontsave_cat = false; if (array_key_exists($slug, $checked['ProductCategory'])) { $product_category_id = $checked['ProductCategory'][$slug]; $dontsave_cat = true; } else { $product_category_translation = ProductCategoryTranslation::where('slug', $slug)->first(); if ($product_category_translation) { $product_category_id = $product_category_translation->product_category_id; } } if (!$product_category_id) { $product_category = ProductCategory::create(); $product_category_id = $product_category->id; } else { $product_category = ProductCategory::find($product_category_id); } if (!$dontsave_cat) { $product_category->translateOrNew('de')->slug = $german_slug; $product_category->translateOrNew('de')->name = $german_product_category; $product_category->translateOrNew('en')->slug = $slug; $product_category->translateOrNew('en')->name = $english_product_category; $product_category->save(); } if (!array_key_exists($slug, $checked['ProductCategory'])) { $checked['ProductCategory'][$slug] = $product_category_id; } $product_collection_name = TextHelp::unQuote($fields[1]); $slug = strtolower(str_slug($product_collection_name)); $german_text = TextHelp::unQuote($fields[7]); $english_text = TextHelp::unQuote($fields[8]); $german_short_text = TextHelp::unQuote($fields[9]); $english_short_text = TextHelp::unQuote($fields[10]); $product_collection_id = 0; $dontsave_col = false; if (array_key_exists($slug . '_' . $product_category_id, $checked['ProductCollection'])) { $product_collection_id = $checked['ProductCollection'][$slug . '_' . $product_category_id]; $dontsave_col = true; } else { $product_collection = ProductCollection::whereSlug($slug)->first(); if ($product_collection) { $product_collection_id = $product_collection->id; } } if (!$product_collection_id) { $product_collection = ProductCollection::create(['product_category_id' => $product_category_id]); $product_collection_id = $product_collection->id; } else { $product_collection = ProductCollection::find($product_collection_id); } if (!$dontsave_col) { $product_collection->slug = $slug; $product_collection->translateOrNew('de')->name = $product_collection_name; $product_collection->translateOrNew('de')->description = $german_text; $product_collection->translateOrNew('de')->teaser_text = $german_short_text; $product_collection->translateOrNew('en')->name = $product_collection_name; $product_collection->translateOrNew('en')->description = $english_text; $product_collection->translateOrNew('en')->teaser_text = $english_short_text; $product_collection->save(); } if (!array_key_exists($slug . '_' . $product_category_id, $checked['ProductCollection'])) { $checked['ProductCollection'][$slug . '_' . $product_category_id] = $product_collection_id; } $product_collection_images = ProductCollectionImage::whereProductCollectionSlug($slug)->get(); if ($product_collection_images) { foreach ($product_collection_images as $product_collection_image) { $product_collection_image->product_collection_id = $product_collection_id; $product_collection_image->save(); } } $product_variant_name = TextHelp::unQuote($fields[2]); $slug = strtolower(str_slug($product_variant_name)); $dimensions = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[13])); $german_text = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[11])); $english_text = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[12])); $german_features = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[14])); $english_features = TextHelp::cleanFilemakerImport(TextHelp::unQuote($fields[15])); $design_number = TextHelp::unQuote($fields[19]); if ($design_number) { //$design_number = substr($design_number, 0, strlen($design_number)-1); //$design_number = trim($design_number); } $product_variant_id = 0; $dontsave_var = false; if (array_key_exists($slug . '_' . $product_collection_id, $checked['ProductVariant'])) { $product_variant_id = $checked['ProductVariant'][$slug . '_' . $product_collection_id]; $dontsave_var = true; } else { $product_variant = ProductVariant::whereSlug($slug)->first(); if ($product_variant) { $product_variant_id = $product_variant->id; } } if (!$product_variant_id) { $product_variant = ProductVariant::create(['product_collection_id' => $product_collection_id]); $product_variant_id = $product_variant->id; } else { $product_variant = ProductVariant::find($product_variant_id); } if (!$dontsave_var) { $product_variant->design_number = $design_number; $product_variant->dimensions = $dimensions; $product_variant->slug = $slug; $product_variant->translateOrNew('de')->name = $product_variant_name; $product_variant->translateOrNew('de')->description = $german_text; $product_variant->translateOrNew('de')->features = $german_features; $product_variant->translateOrNew('en')->name = $product_variant_name; $product_variant->translateOrNew('en')->description = $english_text; $product_variant->translateOrNew('en')->features = $english_features; $product_variant->save(); } if (!array_key_exists($slug . '_' . $product_collection_id, $checked['ProductVariant'])) { $checked['ProductVariant'][$slug . '_' . $product_collection_id] = $product_variant_id; } $product_variant_images = ProductVariantImage::whereProductVariantSlug($slug)->get(); if ($product_variant_images) { foreach ($product_variant_images as $product_variant_image) { $product_variant_image->product_variant_id = $product_variant_id; $product_variant_image->save(); } } $product_color_name = TextHelp::unQuote($fields[3]); $slug = strtolower(str_slug($product_color_name)); $product_color_id = 0; $dontsave_color = false; if ($slug) { if (array_key_exists($slug, $checked['ProductColor'])) { $product_color_id = $checked['ProductColor'][$slug]; $dontsave_color = true; } else { $product_color = ProductColor::where('slug', $slug)->first(); if ($product_color) { $product_color_id = $product_color->id; } } if (!$product_color_id) { $product_color = ProductColor::create(['slug' => $slug]); $product_color_id = $product_color->id; } else { $product_color = ProductColor::find($product_color_id); } if (!$dontsave_color) { $product_color->slug = $slug; $product_color->translateOrNew('en')->name = $product_color_name; $product_color->translateOrNew('de')->name = $product_color_name; $product_color->save(); } if (!array_key_exists($slug, $checked['ProductColor'])) { $checked['ProductColor'][$slug] = $product_color_id; } } $english_name = TextHelp::unQuote($fields[17]); $german_name = TextHelp::unQuote($fields[16]); $slug = strtolower(str_slug($english_name)); $product_material_id = 0; $dontsave_mat = false; if ($slug) { if (array_key_exists($slug, $checked['ProductMaterial'])) { $product_material_id = $checked['ProductMaterial'][$slug]; $dontsave_mat = true; } else { $product_material_translation = ProductMaterialTranslation::where('name', $english_name)->first(); if ($product_material_translation) { $product_material_id = $product_material_translation->product_material_id; } } if (!$product_material_id) { $product_material = ProductMaterial::create(); $product_material_id = $product_material->id; } else { $product_material = ProductMaterial::find($product_material_id); } if (!$dontsave_mat) { $product_material->translateOrNew('en')->name = $english_name; $product_material->translateOrNew('de')->name = $german_name; $product_material->save(); } if (!array_key_exists($slug, $checked['ProductMaterial'])) { $checked['ProductMaterial'][$slug] = $product_material_id; } } $product_number = strtolower(str_slug(TextHelp::unQuote($fields[0]))); $price_brut = floatval(str_replace(',', '.', TextHelp::unQuote($fields[4]))); $merchant_price = floatval(str_replace(',', '.', TextHelp::unQuote($fields[21]))); $merchant_percentage = floatval(str_replace(',', '.', TextHelp::unQuote($fields[22]))); $available = intval(TextHelp::unQuote($fields[18])); $slug = $product_number; $product_id = 0; if (array_key_exists($slug, $checked['Product'])) { $product_id = $checked['Product'][$slug]; } else { $product = Product::where('product_number', $product_number)->first(); if ($product) { $product_id = $product->id; } } if (!$product_id) { $product = Product::create(['product_variant_id' => $product_variant_id, 'product_color_id' => $product_color_id, 'product_material_id' => $product_material_id, 'product_number' => $product_number]); $product_id = $product->id; } else { $product = Product::find($product_id); } $product->product_variant_id = $product_variant_id; $product->product_color_id = $product_color_id; $product->product_material_id = $product_material_id; $product->product_number = $product_number; $product->price_brut = $price_brut * 100; $product->merchant_price = $merchant_price * 100; $product->merchant_percentage = $merchant_percentage; $product->available = $available; $product->save(); $product_images = ProductImage::whereProductNumber($product_number)->get(); if ($product_images) { foreach ($product_images as $product_image) { $product_image->product_id = $product_id; $product_image->save(); } } $product_top_images = ProductTopImage::whereProductNumber($product_number)->get(); if ($product_top_images) { foreach ($product_top_images as $product_top_image) { $product_top_image->product_id = $product_id; $product_top_image->save(); } } $success = 1; $products_saved++; if (!array_key_exists($slug, $checked['Product'])) { $checked['Product'][$slug] = $product_id; } } } } } } } if ($success) { $product_categories = ProductCategory::all(); foreach ($product_categories as $product_category) { foreach ($product_category->product_collections as $product_collection) { foreach ($product_collection->product_variants as $product_variant) { foreach ($product_variant->products as $product) { $slug = strtolower(str_slug($product->product_number)); if (!array_key_exists($slug, $checked['Product'])) { $product->delete(); } } if (!array_key_exists($product_variant->translateOrNew('en')->slug . '_' . $product_collection->id, $checked['ProductVariant'])) { //$product_variant->delete(); } } if (!array_key_exists($product_collection->translateOrNew('en')->slug . '_' . $product_category->id, $checked['ProductCollection'])) { //$product_collection->delete(); } } if (!array_key_exists($product_category->translateOrNew('en')->slug, $checked['ProductCategory'])) { //$product_category->delete(); } } } if ($message) { $message .= ' '; } $message .= $products_saved . ' products saved.'; $this->product_upload->success = $success; $this->product_upload->message = $message; $this->product_upload->save(); $this->dispatch(new PrepareProductCsv($this->product_upload)); $this->dispatch(new SendProductUploadNotification($this->product_upload)); }