
PK 
<?php
namespace App\Http\Controllers;
use App\Attribute;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Response;
use Redirect;
use Session;
use Auth;
use DB;
use Illuminate\Support\Str;
class AttributeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:admin');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if(view()->exists('backend.admin.attributes.attribute'))
{ //$attributes = Attribute::all();
//$attributes = Attribute::with('children')->get();
$attributes = DB::table('attributes AS c')
->leftJoin('attributes AS parents', 'parents.id', '=', 'c.parentattr_id')
->select('c.id','parents.attribute_name AS parentattrName','c.attribute_name','c.parentattr_id','c.isactive','c.created_at','c.updated_at')
->where('c.isdelete',1)->orderBy('c.parentattr_id','ASC')->get();
//p($attributes); exit;
return view('backend.admin.attributes.attribute',compact('attributes'));
}
return back()->with('message', 'Opps view not found');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
if(view()->exists('backend.admin.attributes.attributeCreate'))
{
$attributes = Attribute::attrHierarchicalTree();//self::makeHierarchicalTree();
return view('backend.admin.attributes.attributeCreate',compact('attributes'));
}
return back()->with('error', 'Opps view not found');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if ($request->_token != Session::token()) {
return Redirect::back()->with('message', 'Invalid submission !');
}
$this->validate($request, [
'category_id' => 'required|numeric',
'parentattr_id' => 'required|numeric',
'attribute_name' => 'required|unique:attributes',
'attribute_desc' => 'required',
'attribute_sortby' => 'required',
'isvisible' => 'required',
'isactive' => 'required',
]);
$attribute = new attribute();
$attribute->category_id = $request->category_id;
$attribute->parentattr_id = $request->parentattr_id;
$attribute->attribute_sortby =$request->attribute_sortby;
$attribute->attribute_name = Str::title($request->attribute_name); // Replaced title_case()
$attribute->attribute_desc =$request->attribute_desc;
$attribute->attribute_slug = Str::slug($request->attribute_name); // Replaced str_slug()
$attribute->isvisible =$request->isvisible;
$attribute->isactive =$request->isactive;
$attribute->addedby =Auth::user()->id;
$attribute->created_at =Carbon::now();
$attribute->visitor =$request->ip();
if($attribute->save()){
return redirect()->route('attribute.index')->with('success', 'New attribute Added Successfully !');
}
return back()->with('error', 'Something went wrong please try again !');
}
/**
* Display the specified resource.
*
* @param \App\Attribute $attribute
* @return \Illuminate\Http\Response
*/
public function show(Attribute $attribute)
{
if(view()->exists('backend.admin.attributes.attributeCreate'))
{ $attributes = Attribute::attrHierarchicalTree();//self::makeHierarchicalTree();
return view('backend.admin.attributes.attributeEdit',compact('attributes','attribute'));
}
return back()->with('error', 'Opps view not found');
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Attribute $attribute
* @return \Illuminate\Http\Response
*/
public function edit(Attribute $attribute)
{
if(view()->exists('backend.admin.attributes.attributeCreate'))
{ $attributes = Attribute::attrHierarchicalTree();//self::makeHierarchicalTree();
return view('backend.admin.attributes.attributeEdit',compact('attributes','attribute'));
}
return back()->with('error', 'Opps view not found');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Attribute $attribute
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Attribute $attribute)
{
if ($request->_token != Session::token()) {
return Redirect::back()->with('message', 'Invalid submission !');
}
if ($attribute->attribute_name != Str::title($request->attribute_name)) { // Replaced title_case()
$this->validate($request, [
'category_id' => 'required|numeric',
'parentattr_id' => 'required|numeric',
'attribute_name' => 'required|unique:attributes',
'attribute_desc' => 'required',
'attribute_sortby' => 'required',
'isvisible' => 'required',
'isactive' => 'required',
]);
}
$attribute->category_id = $request->category_id;
$attribute->parentattr_id = $request->parentattr_id;
$attribute->attribute_sortby =$request->attribute_sortby;
$attribute->attribute_name = Str::title($request->attribute_name); // Replaced title_case()
$attribute->attribute_desc =$request->attribute_desc;
$attribute->attribute_slug = Str::slug($request->attribute_name); // Replaced str_slug()
$attribute->isvisible =$request->isvisible;
$attribute->isactive =$request->isactive;
$attribute->addedby =Auth::user()->id;
$attribute->updated_at =Carbon::now();
$attribute->visitor =$request->ip();
if($attribute->save()){
//return redirect()->route('admin.login');
return back()->with('success', 'attribute Updated Successfully !');
}
return back()->with('error', 'Something went wrong please try again !');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Attribute $attribute
* @return \Illuminate\Http\Response
*/
public function destroy(Attribute $attribute)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Attribute $color
* @return \Illuminate\Http\Response
*/
public function delete(Request $request, $id)
{
if ($request->reqId > 0) {
$id = (int)$request->reqId;
//$attribute = Attribute::find($id);//FindOrFail($id);
$attribute = Attribute::withTrashed()->findOrFail($id);
$attribute->isdelete=0;
$attribute->addedby=Auth::user()->id;
$attribute->deleted_at=Carbon::now();
if ($attribute->save()) {
$attribute = ['id'=>$attribute->id,'status'=>true,'msg'=>'Successfully deleted Attribute!'];
return response()->json($attribute);
}
$attribute = array_merge($attribute ?? [], ['status'=>false,'msg'=>'failed to delete Attribute!']); // Fixed array_merge()
return response()->json($attribute);
}
}
public function restore($id=0)
{
if($id>0) {
$attribute = Attribute::withTrashed()->find($id)->restore();
$attribute = Attribute::find($id);
$attribute->isdelete=1;
$attribute->save();
return back()->with('info', 'Restore attribute successfully !');
}else{
$attribute = Attribute::onlyTrashed()->restore();//->get();
$attribute = Attribute::where('isdelete',0)->update(['isdelete'=>1,'addedby'=>Auth::user()->id]);
return back()->with('info', 'Restore attribute successfully !');
}
return self::index();
}
public function changeStatus(Request $request)
{
$id = $request->id;
$attribute = Attribute::findOrFail($id);
$attribute->isactive = ($attribute->isactive==0)?1:0;
$attribute->save();
return response()->json($attribute);
}
private function makeHierarchicalTree()
{
$attributes = Attribute::select('id','category_id','category_ids','parentattr_id','attribute_name','attribute_slug')->orderBy('attribute_sortby','asc')->get()->toArray();
//$attributes = Attribute::all()->sortByDesc('attribute_sortby')->toArray();
return $attributes = buildAttrTree($attributes);
//$attributes = Attribute::orderBy('attribute_sortby','asc')->get()->toArray();
//$attributes = Attribute::with('children')->get()->toArray();
//$queries = DB::getQueryLog(); p($queries);
}
}


PK 99