Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php namespace App\Http\Controllers\Admin; use App\Models\Product; use App\Models\..
Decoded Output download
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Product;
use App\Models\ProductItem;
use App\Models\Category;
use App\Models\Brand;
use App\Models\ItemAttribute;
use App\Models\Attribute;
use App\Models\Text;
use App\Models\AgeWiseCategory;
use App\Models\ItemAge;
use App\Models\ItemImage;
use App\Models\AdditionalFilter;
use App\Models\ProductFilter;
use App\Models\Tag;
use App\Models\ItemAttributeValueImage;
use App\Models\OrderDetails;
use App\Models\Settings;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Auth;
class ProductController extends Controller
{
function __construct()
{
$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);
$this->middleware('permission:product-create', ['only' => ['create','store']]);
$this->middleware('permission:product-edit', ['only' => ['edit','update']]);
$this->middleware('permission:product-delete', ['only' => ['destroy']]);
}
private function validate_input($request) {
$rules=[
'category_id'=>'required',
'brand_id'=>'required',
'videos' => 'array|min:1',
'videos.*' => 'required|mimes:mp4|max:5120',
'sps'=>'required|array|min:1',
'sps.*'=>'required',
'mrps'=>'required|array|min:1',
'mrps.*'=>'required',
'qtys'=>'required|array|min:1',
'qtys.*'=>'required',
// 'age_wise_category_ids'=>'required|array|min:1',
// 'age_wise_category_ids.*'=>'required',
'status'=>'nullable'
];
if (Settings::first()->attribute_wise_upload == 0)
{
$rules=[
'images' => 'required_without:old_images_ids|array|min:1',
'images.*.*' => 'required|mimes:jpeg,png,jpg,gif,webp|max:2048'
];
}
foreach(getLanguages() as $language)
{
$rules[$language.'_name']="required|string|max:255";
$rules[$language.'_description']='nullable';
$rules[$language.'_detailDesc']='nullable';
}
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return ["status"=>0,
"message"=>$validator->errors()->first(),
"error_list"=>["name"=>$validator->errors()->first('name')]
];
}
$data= $validator->valid();
$data['status']=$data['status']??0;
$data['attribute_ids']=$data['attribute_ids']??[];
if($request->image)
{
$data['image']=str_replace('public/product',"",$data['image']);
}
$data['user_id']= Auth::user()->id;
return ["status"=>1,
"data"=>$data];
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.products.product');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$languages=getLanguages();
$categories=Category::get();
$agewisecategory = AgeWiseCategory::get();
$brands=Brand::get();
$attributes=Attribute::get();
$product=new Product();
$additional_filters=AdditionalFilter::with('Options')->get();
$sort_order = Product::max('sort_order');
$sort_order=$sort_order??0;
$sort_order++;
$product->sort_order=$sort_order;
// dd($sort_order);
$select_attributes=collect([]);
return view('admin.products.product_add',compact('sort_order','agewisecategory','languages','categories','brands','product','attributes','additional_filters','select_attributes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$valid= $this->validate_input($request);
if($valid['status'])
{
try{
DB::transaction(function() use($request,$valid)
{
$data=$valid['data'];
$product=new Product();
$text = new Text();
foreach(getLanguages() as $language){
$text->$language=$data[$language.'_name'];
}
$text->save();
$product->name_id=$text->id;
if(isset($data['english_name']))
{
$product->canonical_name=\Str::slug($data['english_name']);
}
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_description'];
}
$Text=$text->save();
$product->description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_detailDesc'];
}
$Text=$text->save();
$product->detail_description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_specification'];
}
$Text=$text->save();
$product->specification_id=$text->id;
$product->hide_product=$request->hide_product ?? 0;
$product->shipping_cost=$request->shipping_cost ?? 0;
$product->model=$request->model ?? 0;
$product->return_days=$request->return_days ?? 0;
$product->category_id=$valid['data']['category_id'];
$product->brand_id=$valid['data']['brand_id']??0;
$product->low_qty=$valid['data']['low_qty']?? 0;
$product->shipping_days=$valid['data']['shipping_days']?? 0;
$product->tax=$valid['data']['tax']?? 0;
$product->tax_type=$valid['data']['tax_type']?? 0;
$product->sku=$request->sku??0;
$product->sort_order=$request->sort_order;
$product->delivery_charge=$request->delivery_charge;
$product->youtube=$request->youtube_link;
$product->popular_dep=$request->popular_dep;
$product->variation=$request->add_item??0;
$product->status=$valid['data']['status'];
$product->attribute_ids=implode(",",$valid['data']['attribute_ids']);
$product->user_id=$valid['data']['user_id'];
// dd($product);
$product->save();
if($request->age_wise_category_ids)
{
foreach($request->age_wise_category_ids as $age_wise_category_id)
{
$item_age= new ItemAge();
$item_age->age_wise_category_id = $age_wise_category_id;
$item_age->product_id =$product->id;
$item_age->save();
}
}
if($request->tags)
{
foreach($request->tags as $tag)
{
$Tag=new Tag();
$Tag->product_id=$product->id;
$Tag->name=$tag;
$Tag->save();
}
}
for($x = 0; $x < count($request->qtys); $x++) {
$product_details= new ProductItem();
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->wsp = $request->wsps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name= $request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->save();
$row_count=$request->row_counts[$x];
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
if($request->attribute_ids)
{
foreach($request->attribute_ids as $attribute_id)
{
$item_attribute= new ItemAttribute();
$item_attribute->attribute_id = $attribute_id;
$item_attribute->name = $request->attribute_values[$attribute_id][$x];
$item_attribute->product_item_id =$product_details->id;
$item_attribute->save();
}
}
$additional_filters=AdditionalFilter::get();
foreach($additional_filters as $additional_filter)
{
if(isset($request->additional_filters[$additional_filter->id]))
{
$option_ids=$request->additional_filters[$additional_filter->id];
foreach($option_ids as $option_id)
{
$productFilter=new ProductFilter();
$productFilter->option_id=$option_id;
$productFilter->product_id=$product->id;
$productFilter->save();
}
}
}
}
});
return response()->json(["status"=>1,
"message"=>"Inserted Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
$languages=getLanguages();
$categories=Category::get();
$brands=Brand::get();
$agewisecategory = AgeWiseCategory::get();
$attributes=Attribute::get();
$additional_filters=AdditionalFilter::with('Options')->get();
$select_attributes=$product->Listattributes();
$sort_order = ($product && $product->sort_order == 0) ? Product::select('sort_order')->latest()->first()->sort_order+1 : ($product ? $product->sort_order+1 : 0);
$select_attributes->map(function($attribute) use($product){
$attribute->values= ItemAttribute::select('name')
->where('attribute_id',$attribute->id)
->whereIn('product_item_id',$product->ProductItems->pluck('id'))
->groupBy('name')
->get()
->map(function($itemAttribute) use($product,$attribute){
$itemAttribute->images=ItemAttributeValueImage::select('id','file')->where('product_id', $product->id)->where('attribute_id', $attribute->id)->where('attribute_value', $itemAttribute->name)->get();
return $itemAttribute;
});
return $attribute;
});
$selected_image_attribute_ids=ItemAttributeValueImage::select('attribute_id')->where('product_id',$product->id)->groupBy('attribute_id')->pluck('attribute_id')->toArray();
$item_attribute_value_grouped=ItemAttributeValueImage::select('attribute_id','attribute_value','file')->where('product_id',$product->id)->groupBy('attribute_id','attribute_value','file')->get();
return view('admin.products.product_add',compact('sort_order','agewisecategory','languages','categories','brands','product','attributes','additional_filters','select_attributes','selected_image_attribute_ids','item_attribute_value_grouped'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
// dd($request->all());
$valid= $this->validate_input($request);
if($valid['status'])
{
if($request->attribute_ids)
{
for($x = 0; $x < count($request->qtys); $x++)
{
foreach($request->attribute_ids as $attribute_id)
{
if(!isset($request->attribute_values[$attribute_id]))
{
return response()->json(["status"=>0,
"message"=>"Please check varient attributes"]);
}
if(!isset($request->attribute_values[$attribute_id][$x]))
{
return response()->json(["status"=>0,
"message"=>"Please check varient attributes"]);
}
}
}
}
try{
DB::transaction(function() use($valid,$product,$request)
{
$product->Name()->delete();
$product->Description()->delete();
$product->DetailDescription()->delete();
$data=$valid['data'];
$text = new Text();
foreach(getLanguages() as $language){
$text->$language=$data[$language.'_name'];
}
$text->save();
$product->name_id=$text->id;
if(isset($data['english_name']))
{
$product->canonical_name=\Str::slug($data['english_name']);
}
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_description'];
}
$Text=$text->save();
$product->description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_detailDesc'];
}
$Text=$text->save();
$product->detail_description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_specification'];
}
$Text=$text->save();
$product->specification_id=$text->id;
$product->shipping_cost=$request->shipping_cost;
$product->return_days=$request->return_days??0;
$product->model=$request->model ?? 0;
$product->hide_product=$request->hide_product ?? 0;
$product->category_id=$valid['data']['category_id'];
$product->brand_id=$valid['data']['brand_id'];
$product->low_qty=$valid['data']['low_qty'];
$product->shipping_days=$valid['data']['shipping_days'];
$product->tax=$valid['data']['tax'];
$product->tax_type=$valid['data']['tax_type'];
$product->sku=$request->sku;
$product->sort_order=$request->sort_order;
$product->delivery_charge=$request->delivery_charge;
$product->youtube=$request->youtube_link;
$product->variation=$request->add_item??0;
$product->popular_dep=$request->popular_dep;
$product->status=$valid['data']['status'];
$product->user_id=$valid['data']['user_id'];
$product->attribute_ids=implode(",",$valid['data']['attribute_ids']);
$product->update();
$product->ItemAges()->delete();
if($request->age_wise_category_ids)
{
foreach($request->age_wise_category_ids as $age_wise_category_id)
{
$item_age= new ItemAge();
$item_age->age_wise_category_id = $age_wise_category_id;
$item_age->product_id =$product->id;
$item_age->save();
}
}
$product->Tags()->delete();
if($request->tags)
{
foreach($request->tags as $tag)
{
$Tag=new Tag();
$Tag->product_id=$product->id;
$Tag->name=$tag;
$Tag->save();
}
}
foreach($product->ProductItems as $item)
{
$item->ItemAttributes()->delete();
}
for($x = 0; $x < count($request->qtys); $x++) {
if($request->product_item_ids[$x]>0)
{
$product_details= ProductItem::find($request->product_item_ids[$x]);
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->wsp = $request->wsps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name=$request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->update();
$old_item_image_exist_ids=[];
if(isset($request->old_images_ids[$product_details->id]))
$old_item_image_exist_ids=$request->old_images_ids[$product_details->id];
ItemImage::where('product_item_id',$product_details->id)->whereNotIn('id',$old_item_image_exist_ids)->delete();
$row_count=$request->row_counts[$x];
if($request->images)
{
if(isset($request->images[$row_count]))
{
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
}
}
}else{
$product_details= new ProductItem();
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name=$request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->save();
$row_count=$request->row_counts[$x];
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
}
if($request->attribute_ids)
{
foreach($request->attribute_ids as $attribute_id)
{
$item_attribute= new ItemAttribute();
$item_attribute->attribute_id = $attribute_id;
$item_attribute->name = $request->attribute_values[$attribute_id][$x];
$item_attribute->product_item_id =$product_details->id;
$item_attribute->save();
}
}
}
$product->ProductFilters()->delete();
$additional_filters=AdditionalFilter::get();
foreach($additional_filters as $additional_filter)
{
if(isset($request->additional_filters[$additional_filter->id]))
{
$option_ids=$request->additional_filters[$additional_filter->id];
foreach($option_ids as $option_id)
{
$productFilter=new ProductFilter();
$productFilter->option_id=$option_id;
$productFilter->product_id=$product->id;
$productFilter->save();
}
}
}
if (Settings::first()->attribute_wise_upload == 1)
{
$old_images=ItemAttributeValueImage::where('product_id',$product->id)->get()->pluck('id')->toArray();
$old_attribute_images_ids=$request->old_attribute_images_ids??[];
$image_need_to_remove=array_diff($old_images,$old_attribute_images_ids);
$item_attribute_value_image_ids=ItemAttributeValueImage::whereIn('id',$image_need_to_remove)->get()->pluck('id');
ItemImage::whereIn('item_attribute_value_image_id',$item_attribute_value_image_ids)->delete();
ItemAttributeValueImage::whereIn('id',$image_need_to_remove)->delete();
$i=0;
$image_attribute_names=$request->image_attribute_names??[];
foreach($image_attribute_names as $image_attribute_name)
{
$attribute_id= $request->image_attribute_ids[$i];
if(isset($request->attribute_images[$image_attribute_name]))
{
foreach($request->attribute_images[$image_attribute_name] as $attribute_image)
{
$image_name=$attribute_image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$ItemAttributeValueImage= ItemAttributeValueImage::create(["product_id"=>$product->id,
"attribute_id"=>$attribute_id,
"attribute_value"=>$image_attribute_name,
"file"=>$image_name
]);
$item_attributes= ItemAttribute::where('name',$image_attribute_name)->whereIn('product_item_id',$product->ProductItems->pluck('id'))->get();
foreach($item_attributes as $item_attribute)
{
$item_attribute->ProductItem->ItemImages()->create(["name"=>$image_name,'item_attribute_value_image_id'=>$ItemAttributeValueImage->id]);
}
}
}
$i++;
}
}
});
return response()->json(["status"=>1,
"message"=>"Updated Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
try{
DB::transaction(function() use($product)
{
Text::where('id',$product->name_id)->delete();
Text::where('id',$product->description_id )->delete();
foreach($product->ProductItems as $item)
{
$item->ItemImages()->delete();
$item->ItemAttributes()->delete();
$item->StockAdjustments()->delete();
}
ProductItem::where('product_id',$product->id)->delete();
$product->ProductFilters()->delete();
$product->ItemAges()->delete();
$product->delete();
});
return response()->json(["status"=>1,
"message"=>"Removed Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
public function tableData()
{
$data = DB::select("SELECT products.id, products.name_id, products.low_qty, products.status, names.english AS name, IF(products.status = 1, 'active', 'deactive') AS status, category_nm.english AS categories, SUM(product_items.qty) AS total_quantity FROM products INNER JOIN product_items ON products.id = product_items.product_id INNER JOIN texts AS names ON products.name_id = names.id LEFT JOIN categories ON products.category_id = categories.id LEFT JOIN texts AS category_nm ON categories.name_id = category_nm.id WHERE products.deleted_at IS NULL GROUP BY products.id, products.name_id, names.english, products.status, category_nm.english, products.low_qty");
$lowQtySetting = Settings::first()->low_qty;
if ($lowQtySetting !== null) {
foreach ($data as $row) {
$totalQuantity = $row->total_quantity;
$lowQty = $row->low_qty;
if ($lowQty > $totalQuantity) {
$row->total_quantity = '<span style="color: red;">'.$totalQuantity.'</span>';
} else {
$row->total_quantity = '<span>'.$totalQuantity.'</span>';
}
}
}else{
foreach ($data as $row) {
$totalQuantity = $row->total_quantity;
$lowQty = $row->low_qty;
$row->total_quantity = '<span>'.$totalQuantity.'</span>';
}
}
return DataTables::of($data)
->addIndexColumn()
->addColumn('action', function ($row) {
$actions = '';
if (\Gate::allows('product-edit')) {
$actions .= '<a href="'.route('products.edit', [$row->id]).'"><i class="material-icons">edit</i></a>';
}
if (
\Gate::allows('product-delete') &&
!OrderDetails::whereIn('product_item_id', function ($qry) use ($row) {
$qry->select('id')->from('product_items')->where('product_id', $row->id);
})->exists()
) {
$actions .= '<a style="cursor: pointer;" onclick="deleteData('.$row->id.')"><i class="material-icons">delete</i></a>';
}
return $actions;
})
->rawColumns(['action' => 'action', 'total_quantity'])
->make(true);
}
private function validate_category_input($request) {
$rules=[ ];
foreach(getLanguages() as $language)
{
$rules[$language.'_name']="required|string|max:255";
$rules[$language.'_description']='nullable';
}
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return ["status"=>0,
"message"=>$validator->errors()->first(),
"error_list"=>["name"=>$validator->errors()->first('name')]
];
}
$data= $validator->valid();
return ["status"=>1,
"data"=>$data];
}
/**
* Store a newly created without resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function addCatgory(Request $request)
{
$valid= $this->validate_category_input($request);
$GLOBALS['category']=[];
if($valid['status']) {
try {
DB::transaction(function () use ($request) {
$category = new Category();
$text = new Text();
$text->english = $request->english_name;
// $text->arabic = $request->arabic_name;
$text->save();
$category->name_id = $text->id;
$category->parent_id = $request->parent_id ?? 0;
$category->user_id = Auth::user()->id;
$category->save();
$GLOBALS['category'] = ["id" => $category->id, "name" => $category->Name->english];
});
return response()->json(["status" => 1,
"message" => "Inserted Successfully",
"category" => $GLOBALS['category']]);
} catch (Exception $e) {
return response()->json(["status" => 0,
"message" => "Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Store a newly created without resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function addBrand(Request $request)
{
// dd($request->all());
$valid= $this->validate_category_input($request);
if($valid['status']) {
$GLOBALS['brand'] = [];
try {
DB::transaction(function () use ($request) {
$brand = new Brand();
$text = new Text();
$text->english = $request->english_name;
// $text->arabic = $request->arabic_name;
$text->save();
if($request->brand_image)
{
$brand_image=$request->file('brand_image')->store('public/brand');
$brand_image=str_replace('public/brand',"",$brand_image);
// $path = $request->file('brand_image')->store('public/brand');
$brand->image=$brand_image;
}
$brand->name_id = $text->id;
$brand->user_id = Auth::user()->id;
$brand->save();
$GLOBALS['brand'] = ["id" => $brand->id, "name" => $brand->Name->english];
});
return response()->json(["status" => 1,
"message" => "Inserted Successfully",
"brand" => $GLOBALS['brand']]);
} catch (Exception $e) {
return response()->json(["status" => 0,
"message" => "Process Failed"]);
}
}
return response()->json($valid);
}
}
?>
Did this file decode correctly?
Original Code
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Product;
use App\Models\ProductItem;
use App\Models\Category;
use App\Models\Brand;
use App\Models\ItemAttribute;
use App\Models\Attribute;
use App\Models\Text;
use App\Models\AgeWiseCategory;
use App\Models\ItemAge;
use App\Models\ItemImage;
use App\Models\AdditionalFilter;
use App\Models\ProductFilter;
use App\Models\Tag;
use App\Models\ItemAttributeValueImage;
use App\Models\OrderDetails;
use App\Models\Settings;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Auth;
class ProductController extends Controller
{
function __construct()
{
$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);
$this->middleware('permission:product-create', ['only' => ['create','store']]);
$this->middleware('permission:product-edit', ['only' => ['edit','update']]);
$this->middleware('permission:product-delete', ['only' => ['destroy']]);
}
private function validate_input($request) {
$rules=[
'category_id'=>'required',
'brand_id'=>'required',
'videos' => 'array|min:1',
'videos.*' => 'required|mimes:mp4|max:5120',
'sps'=>'required|array|min:1',
'sps.*'=>'required',
'mrps'=>'required|array|min:1',
'mrps.*'=>'required',
'qtys'=>'required|array|min:1',
'qtys.*'=>'required',
// 'age_wise_category_ids'=>'required|array|min:1',
// 'age_wise_category_ids.*'=>'required',
'status'=>'nullable'
];
if (Settings::first()->attribute_wise_upload == 0)
{
$rules=[
'images' => 'required_without:old_images_ids|array|min:1',
'images.*.*' => 'required|mimes:jpeg,png,jpg,gif,webp|max:2048'
];
}
foreach(getLanguages() as $language)
{
$rules[$language.'_name']="required|string|max:255";
$rules[$language.'_description']='nullable';
$rules[$language.'_detailDesc']='nullable';
}
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return ["status"=>0,
"message"=>$validator->errors()->first(),
"error_list"=>["name"=>$validator->errors()->first('name')]
];
}
$data= $validator->valid();
$data['status']=$data['status']??0;
$data['attribute_ids']=$data['attribute_ids']??[];
if($request->image)
{
$data['image']=str_replace('public/product',"",$data['image']);
}
$data['user_id']= Auth::user()->id;
return ["status"=>1,
"data"=>$data];
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.products.product');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$languages=getLanguages();
$categories=Category::get();
$agewisecategory = AgeWiseCategory::get();
$brands=Brand::get();
$attributes=Attribute::get();
$product=new Product();
$additional_filters=AdditionalFilter::with('Options')->get();
$sort_order = Product::max('sort_order');
$sort_order=$sort_order??0;
$sort_order++;
$product->sort_order=$sort_order;
// dd($sort_order);
$select_attributes=collect([]);
return view('admin.products.product_add',compact('sort_order','agewisecategory','languages','categories','brands','product','attributes','additional_filters','select_attributes'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$valid= $this->validate_input($request);
if($valid['status'])
{
try{
DB::transaction(function() use($request,$valid)
{
$data=$valid['data'];
$product=new Product();
$text = new Text();
foreach(getLanguages() as $language){
$text->$language=$data[$language.'_name'];
}
$text->save();
$product->name_id=$text->id;
if(isset($data['english_name']))
{
$product->canonical_name=\Str::slug($data['english_name']);
}
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_description'];
}
$Text=$text->save();
$product->description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_detailDesc'];
}
$Text=$text->save();
$product->detail_description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_specification'];
}
$Text=$text->save();
$product->specification_id=$text->id;
$product->hide_product=$request->hide_product ?? 0;
$product->shipping_cost=$request->shipping_cost ?? 0;
$product->model=$request->model ?? 0;
$product->return_days=$request->return_days ?? 0;
$product->category_id=$valid['data']['category_id'];
$product->brand_id=$valid['data']['brand_id']??0;
$product->low_qty=$valid['data']['low_qty']?? 0;
$product->shipping_days=$valid['data']['shipping_days']?? 0;
$product->tax=$valid['data']['tax']?? 0;
$product->tax_type=$valid['data']['tax_type']?? 0;
$product->sku=$request->sku??0;
$product->sort_order=$request->sort_order;
$product->delivery_charge=$request->delivery_charge;
$product->youtube=$request->youtube_link;
$product->popular_dep=$request->popular_dep;
$product->variation=$request->add_item??0;
$product->status=$valid['data']['status'];
$product->attribute_ids=implode(",",$valid['data']['attribute_ids']);
$product->user_id=$valid['data']['user_id'];
// dd($product);
$product->save();
if($request->age_wise_category_ids)
{
foreach($request->age_wise_category_ids as $age_wise_category_id)
{
$item_age= new ItemAge();
$item_age->age_wise_category_id = $age_wise_category_id;
$item_age->product_id =$product->id;
$item_age->save();
}
}
if($request->tags)
{
foreach($request->tags as $tag)
{
$Tag=new Tag();
$Tag->product_id=$product->id;
$Tag->name=$tag;
$Tag->save();
}
}
for($x = 0; $x < count($request->qtys); $x++) {
$product_details= new ProductItem();
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->wsp = $request->wsps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name= $request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->save();
$row_count=$request->row_counts[$x];
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
if($request->attribute_ids)
{
foreach($request->attribute_ids as $attribute_id)
{
$item_attribute= new ItemAttribute();
$item_attribute->attribute_id = $attribute_id;
$item_attribute->name = $request->attribute_values[$attribute_id][$x];
$item_attribute->product_item_id =$product_details->id;
$item_attribute->save();
}
}
$additional_filters=AdditionalFilter::get();
foreach($additional_filters as $additional_filter)
{
if(isset($request->additional_filters[$additional_filter->id]))
{
$option_ids=$request->additional_filters[$additional_filter->id];
foreach($option_ids as $option_id)
{
$productFilter=new ProductFilter();
$productFilter->option_id=$option_id;
$productFilter->product_id=$product->id;
$productFilter->save();
}
}
}
}
});
return response()->json(["status"=>1,
"message"=>"Inserted Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Product $product)
{
$languages=getLanguages();
$categories=Category::get();
$brands=Brand::get();
$agewisecategory = AgeWiseCategory::get();
$attributes=Attribute::get();
$additional_filters=AdditionalFilter::with('Options')->get();
$select_attributes=$product->Listattributes();
$sort_order = ($product && $product->sort_order == 0) ? Product::select('sort_order')->latest()->first()->sort_order+1 : ($product ? $product->sort_order+1 : 0);
$select_attributes->map(function($attribute) use($product){
$attribute->values= ItemAttribute::select('name')
->where('attribute_id',$attribute->id)
->whereIn('product_item_id',$product->ProductItems->pluck('id'))
->groupBy('name')
->get()
->map(function($itemAttribute) use($product,$attribute){
$itemAttribute->images=ItemAttributeValueImage::select('id','file')->where('product_id', $product->id)->where('attribute_id', $attribute->id)->where('attribute_value', $itemAttribute->name)->get();
return $itemAttribute;
});
return $attribute;
});
$selected_image_attribute_ids=ItemAttributeValueImage::select('attribute_id')->where('product_id',$product->id)->groupBy('attribute_id')->pluck('attribute_id')->toArray();
$item_attribute_value_grouped=ItemAttributeValueImage::select('attribute_id','attribute_value','file')->where('product_id',$product->id)->groupBy('attribute_id','attribute_value','file')->get();
return view('admin.products.product_add',compact('sort_order','agewisecategory','languages','categories','brands','product','attributes','additional_filters','select_attributes','selected_image_attribute_ids','item_attribute_value_grouped'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
// dd($request->all());
$valid= $this->validate_input($request);
if($valid['status'])
{
if($request->attribute_ids)
{
for($x = 0; $x < count($request->qtys); $x++)
{
foreach($request->attribute_ids as $attribute_id)
{
if(!isset($request->attribute_values[$attribute_id]))
{
return response()->json(["status"=>0,
"message"=>"Please check varient attributes"]);
}
if(!isset($request->attribute_values[$attribute_id][$x]))
{
return response()->json(["status"=>0,
"message"=>"Please check varient attributes"]);
}
}
}
}
try{
DB::transaction(function() use($valid,$product,$request)
{
$product->Name()->delete();
$product->Description()->delete();
$product->DetailDescription()->delete();
$data=$valid['data'];
$text = new Text();
foreach(getLanguages() as $language){
$text->$language=$data[$language.'_name'];
}
$text->save();
$product->name_id=$text->id;
if(isset($data['english_name']))
{
$product->canonical_name=\Str::slug($data['english_name']);
}
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_description'];
}
$Text=$text->save();
$product->description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_detailDesc'];
}
$Text=$text->save();
$product->detail_description_id=$text->id;
$text = new Text();
foreach(getLanguages() as $language){
$text->$language = $data[$language.'_specification'];
}
$Text=$text->save();
$product->specification_id=$text->id;
$product->shipping_cost=$request->shipping_cost;
$product->return_days=$request->return_days??0;
$product->model=$request->model ?? 0;
$product->hide_product=$request->hide_product ?? 0;
$product->category_id=$valid['data']['category_id'];
$product->brand_id=$valid['data']['brand_id'];
$product->low_qty=$valid['data']['low_qty'];
$product->shipping_days=$valid['data']['shipping_days'];
$product->tax=$valid['data']['tax'];
$product->tax_type=$valid['data']['tax_type'];
$product->sku=$request->sku;
$product->sort_order=$request->sort_order;
$product->delivery_charge=$request->delivery_charge;
$product->youtube=$request->youtube_link;
$product->variation=$request->add_item??0;
$product->popular_dep=$request->popular_dep;
$product->status=$valid['data']['status'];
$product->user_id=$valid['data']['user_id'];
$product->attribute_ids=implode(",",$valid['data']['attribute_ids']);
$product->update();
$product->ItemAges()->delete();
if($request->age_wise_category_ids)
{
foreach($request->age_wise_category_ids as $age_wise_category_id)
{
$item_age= new ItemAge();
$item_age->age_wise_category_id = $age_wise_category_id;
$item_age->product_id =$product->id;
$item_age->save();
}
}
$product->Tags()->delete();
if($request->tags)
{
foreach($request->tags as $tag)
{
$Tag=new Tag();
$Tag->product_id=$product->id;
$Tag->name=$tag;
$Tag->save();
}
}
foreach($product->ProductItems as $item)
{
$item->ItemAttributes()->delete();
}
for($x = 0; $x < count($request->qtys); $x++) {
if($request->product_item_ids[$x]>0)
{
$product_details= ProductItem::find($request->product_item_ids[$x]);
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->wsp = $request->wsps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name=$request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->update();
$old_item_image_exist_ids=[];
if(isset($request->old_images_ids[$product_details->id]))
$old_item_image_exist_ids=$request->old_images_ids[$product_details->id];
ItemImage::where('product_item_id',$product_details->id)->whereNotIn('id',$old_item_image_exist_ids)->delete();
$row_count=$request->row_counts[$x];
if($request->images)
{
if(isset($request->images[$row_count]))
{
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
}
}
}else{
$product_details= new ProductItem();
$product_details->qty = $request->qtys[$x];
$product_details->mrp = $request->mrps[$x];
$product_details->sp = $request->sps[$x];
$product_details->product_id =$product->id;
if($request->hasFile("videos.$x"))
{
$video_name=$request->videos[$x]->store('public/product_item');
$video_name=str_replace('public/product_item',"",$video_name);
$product_details->video=$video_name;
}
$product_details->save();
$row_count=$request->row_counts[$x];
foreach($request->images[$row_count] as $image)
{
$image_name=$image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$product_details->ItemImages()->create(["name"=>$image_name]);
}
}
if($request->attribute_ids)
{
foreach($request->attribute_ids as $attribute_id)
{
$item_attribute= new ItemAttribute();
$item_attribute->attribute_id = $attribute_id;
$item_attribute->name = $request->attribute_values[$attribute_id][$x];
$item_attribute->product_item_id =$product_details->id;
$item_attribute->save();
}
}
}
$product->ProductFilters()->delete();
$additional_filters=AdditionalFilter::get();
foreach($additional_filters as $additional_filter)
{
if(isset($request->additional_filters[$additional_filter->id]))
{
$option_ids=$request->additional_filters[$additional_filter->id];
foreach($option_ids as $option_id)
{
$productFilter=new ProductFilter();
$productFilter->option_id=$option_id;
$productFilter->product_id=$product->id;
$productFilter->save();
}
}
}
if (Settings::first()->attribute_wise_upload == 1)
{
$old_images=ItemAttributeValueImage::where('product_id',$product->id)->get()->pluck('id')->toArray();
$old_attribute_images_ids=$request->old_attribute_images_ids??[];
$image_need_to_remove=array_diff($old_images,$old_attribute_images_ids);
$item_attribute_value_image_ids=ItemAttributeValueImage::whereIn('id',$image_need_to_remove)->get()->pluck('id');
ItemImage::whereIn('item_attribute_value_image_id',$item_attribute_value_image_ids)->delete();
ItemAttributeValueImage::whereIn('id',$image_need_to_remove)->delete();
$i=0;
$image_attribute_names=$request->image_attribute_names??[];
foreach($image_attribute_names as $image_attribute_name)
{
$attribute_id= $request->image_attribute_ids[$i];
if(isset($request->attribute_images[$image_attribute_name]))
{
foreach($request->attribute_images[$image_attribute_name] as $attribute_image)
{
$image_name=$attribute_image->store('public/product_item');
$image_name=str_replace('public/product_item',"",$image_name);
$ItemAttributeValueImage= ItemAttributeValueImage::create(["product_id"=>$product->id,
"attribute_id"=>$attribute_id,
"attribute_value"=>$image_attribute_name,
"file"=>$image_name
]);
$item_attributes= ItemAttribute::where('name',$image_attribute_name)->whereIn('product_item_id',$product->ProductItems->pluck('id'))->get();
foreach($item_attributes as $item_attribute)
{
$item_attribute->ProductItem->ItemImages()->create(["name"=>$image_name,'item_attribute_value_image_id'=>$ItemAttributeValueImage->id]);
}
}
}
$i++;
}
}
});
return response()->json(["status"=>1,
"message"=>"Updated Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
try{
DB::transaction(function() use($product)
{
Text::where('id',$product->name_id)->delete();
Text::where('id',$product->description_id )->delete();
foreach($product->ProductItems as $item)
{
$item->ItemImages()->delete();
$item->ItemAttributes()->delete();
$item->StockAdjustments()->delete();
}
ProductItem::where('product_id',$product->id)->delete();
$product->ProductFilters()->delete();
$product->ItemAges()->delete();
$product->delete();
});
return response()->json(["status"=>1,
"message"=>"Removed Successfully"]);
}catch(Exception $e){
return response()->json(["status"=>0,
"message"=>"Process Failed"]);
}
}
public function tableData()
{
$data = DB::select("SELECT products.id, products.name_id, products.low_qty, products.status, names.english AS name, IF(products.status = 1, 'active', 'deactive') AS status, category_nm.english AS categories, SUM(product_items.qty) AS total_quantity FROM products INNER JOIN product_items ON products.id = product_items.product_id INNER JOIN texts AS names ON products.name_id = names.id LEFT JOIN categories ON products.category_id = categories.id LEFT JOIN texts AS category_nm ON categories.name_id = category_nm.id WHERE products.deleted_at IS NULL GROUP BY products.id, products.name_id, names.english, products.status, category_nm.english, products.low_qty");
$lowQtySetting = Settings::first()->low_qty;
if ($lowQtySetting !== null) {
foreach ($data as $row) {
$totalQuantity = $row->total_quantity;
$lowQty = $row->low_qty;
if ($lowQty > $totalQuantity) {
$row->total_quantity = '<span style="color: red;">'.$totalQuantity.'</span>';
} else {
$row->total_quantity = '<span>'.$totalQuantity.'</span>';
}
}
}else{
foreach ($data as $row) {
$totalQuantity = $row->total_quantity;
$lowQty = $row->low_qty;
$row->total_quantity = '<span>'.$totalQuantity.'</span>';
}
}
return DataTables::of($data)
->addIndexColumn()
->addColumn('action', function ($row) {
$actions = '';
if (\Gate::allows('product-edit')) {
$actions .= '<a href="'.route('products.edit', [$row->id]).'"><i class="material-icons">edit</i></a>';
}
if (
\Gate::allows('product-delete') &&
!OrderDetails::whereIn('product_item_id', function ($qry) use ($row) {
$qry->select('id')->from('product_items')->where('product_id', $row->id);
})->exists()
) {
$actions .= '<a style="cursor: pointer;" onclick="deleteData('.$row->id.')"><i class="material-icons">delete</i></a>';
}
return $actions;
})
->rawColumns(['action' => 'action', 'total_quantity'])
->make(true);
}
private function validate_category_input($request) {
$rules=[ ];
foreach(getLanguages() as $language)
{
$rules[$language.'_name']="required|string|max:255";
$rules[$language.'_description']='nullable';
}
$validator = Validator::make($request->all(),$rules);
if ($validator->fails()) {
return ["status"=>0,
"message"=>$validator->errors()->first(),
"error_list"=>["name"=>$validator->errors()->first('name')]
];
}
$data= $validator->valid();
return ["status"=>1,
"data"=>$data];
}
/**
* Store a newly created without resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function addCatgory(Request $request)
{
$valid= $this->validate_category_input($request);
$GLOBALS['category']=[];
if($valid['status']) {
try {
DB::transaction(function () use ($request) {
$category = new Category();
$text = new Text();
$text->english = $request->english_name;
// $text->arabic = $request->arabic_name;
$text->save();
$category->name_id = $text->id;
$category->parent_id = $request->parent_id ?? 0;
$category->user_id = Auth::user()->id;
$category->save();
$GLOBALS['category'] = ["id" => $category->id, "name" => $category->Name->english];
});
return response()->json(["status" => 1,
"message" => "Inserted Successfully",
"category" => $GLOBALS['category']]);
} catch (Exception $e) {
return response()->json(["status" => 0,
"message" => "Process Failed"]);
}
}
return response()->json($valid);
}
/**
* Store a newly created without resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function addBrand(Request $request)
{
// dd($request->all());
$valid= $this->validate_category_input($request);
if($valid['status']) {
$GLOBALS['brand'] = [];
try {
DB::transaction(function () use ($request) {
$brand = new Brand();
$text = new Text();
$text->english = $request->english_name;
// $text->arabic = $request->arabic_name;
$text->save();
if($request->brand_image)
{
$brand_image=$request->file('brand_image')->store('public/brand');
$brand_image=str_replace('public/brand',"",$brand_image);
// $path = $request->file('brand_image')->store('public/brand');
$brand->image=$brand_image;
}
$brand->name_id = $text->id;
$brand->user_id = Auth::user()->id;
$brand->save();
$GLOBALS['brand'] = ["id" => $brand->id, "name" => $brand->Name->english];
});
return response()->json(["status" => 1,
"message" => "Inserted Successfully",
"brand" => $GLOBALS['brand']]);
} catch (Exception $e) {
return response()->json(["status" => 0,
"message" => "Process Failed"]);
}
}
return response()->json($valid);
}
}
Function Calls
None |
Stats
MD5 | bc9478e9c06f1993fa3c48f0917400a4 |
Eval Count | 0 |
Decode Time | 69 ms |