File: /var/www/vhost/disk-apps/agile-selling-wpb/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 __construct(Request $request)
{
$this->middleware('auth');
// $this->middleware(function ($request, $next) {
// $this->user = Auth::user();
// if(Auth::user()->rol->id == 1 || Auth::user()->rol->id == 6){
// $this->changeDbDefault($request);
// }
// return $next($request);
// });
}
public function index(){
// $rol = Auth::user()->rol->id;
// if ($rol == 1 || $rol == 6) {
$brands = Brand::orderBy('name', 'ASC')->get();
return view('brands.brands')->with('brands',$brands);
// }else{
// return redirect()->back();
// }
}
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);
}
//Se instancia un nuevo objeto
$brand = new Brand();
$brand->name = $request["name"];//Se asigna el nombre que viene en el request
$brand->save();//Guarda el nuevo objeto
$logObj = $brand;
$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"];
//Se consulta la marca
$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"];//Se asigna el nombre que viene en el request
$brand->update();//Actualiza el objeto
$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()
{
$obj = $obj = DB::table('brands')
->select('brands.id', 'brands.name', 'brands.active')->whereNull('brands.deleted_at');
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);
}
}