File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/BrandsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Brand;
use Datatables;
use DB;
class BrandsController extends Controller
{
public function index($storeType = 'main')
{
$brands = Brand::orderBy('name', 'ASC')->get();
return view('brands.brands')->with('brands', $brands)->with('storeType', $storeType);
}
public function indexEdit($id)
{
$brand = Brand::find($id);
return view('brands.editBrands')->with('brand', $brand);
}
public function create(Request $request)
{
$r = Brand::where('name', $request["name"])->first();
if ($r) {
return array('r' => false, 'm' => trans('messages.controller_brand_tag1'), 'd' => $r);
}
$brand = new Brand();
$brand->name = $request["name"];
$brand->store_type = $request["store_type"];
$brand->save();
$logObj = $brand;
if (Auth::user()) {
$this->registerLog(Auth::user()->id, 'Creó marca', json_encode($logObj), "Create", 5);
}
return array('r' => true, 'm' => trans('messages.controller_brand_tag2'), 'd' => $brand);
}
public function update(Request $request)
{
$id = $request["id"];
$brand = Brand::find($id);
$logObj = $brand;
$r = Brand::where('name', $request["name"])->first();
if ($r) {
if ($brand->name != $request["name"]) {
return array('r' => false, 'm' => trans('messages.controller_brand_tag1'));
}
}
$brand->name = $request["name"];
$brand->update();
$this->registerLog(Auth::user()->id, 'Actualizó marca ', json_encode($logObj), "Update", 5);
return array('r' => true, 'm' => trans('messages.controller_brand_tag3'));
}
public function activate(Request $request)
{
$id = $request['id'];
$state = $request['state'];
$brand = Brand::find($id);
$logObj = $brand;
$brand->active = $state;
$brand->update();
$this->registerLog(
Auth::user()->id,
'Actualizó marca',
json_encode($logObj),
"Update",
5
);
return array('r' => true);
}
public function delete(Request $request)
{
$id = $request['id'];
$brand = Brand::find($id);
$logObj = $brand;
$brand->delete();
$this->registerLog(Auth::user()->id, 'Eliminó marca', json_encode($logObj), "Delete", 5);
return array('r' => true);
}
public function tableFilter($storeType = 'main')
{
$obj = $obj = DB::table('brands')
->select('brands.id', 'brands.name', 'brands.active')
->whereNull('brands.deleted_at')
->where('brands.store_type', $storeType)->get();
return Datatables::of($obj)
->addColumn('actions', function ($obj) {
return '<i class="fa fa-pencil iconMini" onClick="clickEditBrand(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
<i class="fa fa-trash iconMini" onClick="clickDeleteBrand(' . $obj->id . ')" data-id="' . $obj->id . '" title="Eliminar"></i>';
})
->editColumn('active', function ($obj) {
if ($obj->active == 0) {
return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" onChange="chkBrand(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>Si </label></div> </label> </div>';
} else {
return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" onChange="chkBrand(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" />
<span></span> Si </label> </div> </label> </div>';
}
})
->rawColumns(['active', 'actions'])
->make(true);
}
}