File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/AttributeController.php
<?php
namespace App\Http\Controllers;
use DB;
use Excel;
use DataTables;
use App\Attribute;
use App\AttributeType;
use Box\Spout\Common\Type;
use Illuminate\Http\Request;
use App\Http\Controllers\WoocommerceController;
use App\Http\Controllers\Exports\ReportAttributes;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
class AttributeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($storeType = 'main')
{
return view('attributes.list')->with('storeType', $storeType);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function indexAdd($storeType = 'main')
{
$attribute_types = AttributeType::where('active', 1)->get();
return view('attributes.create', compact('attribute_types', 'storeType'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request,$sync = true)
{
try {
if (!$current_attribute = Attribute::where('name', $request->input('name'))->first()) {
$data_create = [];
if($sync){
$woo = new WoocommerceController();
$data = [
'name' => substr($request->input('name'), 0, 28),
];
$reference_id = $woo->createAttribute($data);
$data_create = array_merge($request->all(),['reference_woocommerce_attribute_id' => $reference_id]);
}else{
$data_create = $request->all();
}
if ($attribute = Attribute::create($data_create)) {
if($sync){
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => null));
}else{
return array('d' => $attribute->id);
}
}
} else {
if($sync){
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}else{
return array('d' => $current_attribute->id);
}
}
} catch (\Throwable $th) {
if($sync){
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
}else{
return;
}
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function indexEdit($storeType = 'main', $id)
{
$attribute = Attribute::findOrFail($id);
$attribute_types = AttributeType::where('active', 1)->get();
if ($attribute) {
return view('attributes.edit', compact('attribute','attribute_types', 'storeType'));
} else {
return redirect()->back();
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
if (!$current = Attribute::where([['id', '!=', $id], ['name', $request->input('name')]])->first()) {
if (Attribute::where('id', $id)->update(array_slice($request->all(), 1))) {
$current = Attribute::where('id', $id)->first();
$woo = new WoocommerceController();
$data = [
'name' => substr($request->input('name'), 0, 28),
];
$woo->updateAttribute($current->reference_woocommerce_attribute_id,$data);
return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
}
} else {
return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('name')]), "data" => null));
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function activate(Request $request){
$id= $request['id'];
$state= $request['state'];
$attribute = Attribute::find($id);
$attribute->active = $state;
$attribute->update();
if ($attribute) {
return array('r' => true, 'd' => null, 'm'=> __('messages.updated_successfully'));
}else{
return array('r' => false,'d' => null, 'm'=> __('messages.error_updating'));
}
}
public function tableFilter($storeType = 'main'){
$obj = $obj = DB::table('attributes')
->select('attributes.id','attributes.name','attributes.display_name','attributes.is_more_one','attributes.required','attributes.active')
->where('attributes.store_type', $storeType);
return DataTables::of($obj)
->addColumn('actions', function ($obj) {
return '
<i class="fa fa-pencil iconMini" onClick="clickEdit('.$obj->id.')" data-id="'.$obj->id.'" data-toggle="tooltip" data-placement="bottom" style="cursor:pointer;"></i>
';
})
->editColumn('active', function ($obj) {
if($obj->active == 0){
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk('.$obj->id.')" data-id="'.$obj->id.'" id="Checkactive'.$obj->id.'" name="Checkactivo" /> <span></span>' . __('messages.yes') . ' </label></div> </label> </div>';
}else{
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> ' . __('messages.no') . ' <input type="checkbox" onChange="chk('.$obj->id.')" data-id="'.$obj->id.'" id="Checkactive'.$obj->id.'" name="Checkactivo" checked="" />
<span></span> ' . __('messages.yes') . ' </label> </div> </label> </div>';
}
})
->editColumn('is_more_one', function ($obj) {
if($obj->is_more_one == 1){
return '<div> '. __('messages.yes') . '</div>';
}else{
return '<div>' . __('messages.no') . '</div>';
}
})
->editColumn('required', function ($obj) {
if($obj->required == 1){
return '<div> '. __('messages.yes') . '</div>';
}else{
return '<div>' . __('messages.no') . '</div>';
}
})
->rawColumns([ 'active','actions','is_more_one','required'])
->make(true);
}
public function export()
{
return Excel::download(new ReportAttributes(request()->storeType), trans('messages.attributes.title_10').'.xlsx');
}
public function import(Request $request)
{
try {
$this->edit = [
'creados' => 0,
'editados' => 0,
];
DB::beginTransaction();
$reader = ReaderEntityFactory::createXLSXReader();
$reader->open($request->importAttributes);
$cnt = 0;
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
if ($cnt > 0) {
$cells = $row->getCells();
if($attribute = Attribute::where('name', trim($this->getValueRow($cells,1)))->first()){
$attribute->name = $this->getValueRow($cells,1);
$attribute->display_name = $this->getValueRow($cells,2);
$attribute->is_more_one = $this->getValueRow($cells,3) == 'Si' ? true : false;
$attribute->required = $this->getValueRow($cells,4) == 'Si' ? true : false;
$attribute->active = $this->getValueRow($cells,5) == 'Si' ? true : false;
$attribute->attribute_type_id = $this->getValueRow($cells,6);
$attribute->store_type = $request->store_type ?? null;
$attribute->update();
$woo = new WoocommerceController();
$data = [
'name' => substr($this->getValueRow($cells,1), 0, 28),
];
$woo->updateAttribute($attribute->reference_woocommerce_attribute_id,$data);
$this->edit['editados'] += 1;
}else{
$woo = new WoocommerceController();
$data = [
'name' => substr($this->getValueRow($cells,1), 0, 28),
];
$reference_id = $woo->createAttribute($data);
Attribute::create([
"name" => $this->getValueRow($cells,1),
"display_name" => $this->getValueRow($cells,2),
"is_more_one" => $this->getValueRow($cells,3) == 'Si' ? true : false,
"required" => $this->getValueRow($cells,4) == 'Si' ? true : false,
"active" => $this->getValueRow($cells,5) == 'Si' ? true : false,
"attribute_type_id" => $this->getValueRow($cells,6),
'reference_woocommerce_attribute_id' => $reference_id,
'store_type' => $this->request->store_type ?? null,
]);
$this->edit['creados'] += 1;
}
}
$cnt++;
}
}
DB::commit();
$reader->close();
return array('r' => true, 'd' => null, 'm' => trans('messages.screen_products_tag45') . $this->edit['creados'] . ' creados, ' . $this->edit['editados'] . ' editados.');
} catch (\Exception $e) {
return array('r' => false, 'd' => null, 'm' => trans('messages.screen_products_tag46') . $e->getMessage());
}
}
}