
PK 
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Response;
use Redirect;
use Session;
use Auth;
use File;
use DB;
use Illuminate\Support\Str;
class CategoryController 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.category.category'))
{ //$categories = Category::all();
//$categories = Category::with('children')->get();
$categories = DB::table('categories AS c')
->leftJoin('categories AS parents', 'parents.id', '=', 'c.parent_id')
->select('c.id','parents.category_name AS parentName','c.category_name','c.category_image','c.category_imagethumb','c.parent_id','c.isactive','c.created_at','c.updated_at')
->where('c.isdelete',1)->orderBy('c.parent_id','ASC')->get();
return view('backend.admin.category.category',compact('categories'));
}
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.category.categoryCreate'))
{
$categories = self::makeHierarchicalTree();
return view('backend.admin.category.categoryCreate',compact('categories'));
}
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, [
'parent_id' => 'required|numeric',
'category_name' => 'required|unique:categories',
'category_desc' => 'required',
'category_sortby' => 'required',
'isvisible' => 'required',
'isactive' => 'required',
]);
//echo Auth::user()->name; exit;
//Member::create($request->all());
$category = new Category();
$category->parent_id = $request->parent_id;
$category->category_sortby =$request->category_sortby;
$category->category_name = Str::title($request->category_name);
$category->category_desc =$request->category_desc;
$category->category_slug =Str::slug($request->category_name);
//$category->image ='';
//$category->imagethumb ='';
$category->isvisible =$request->isvisible;
$category->isactive =$request->isactive;
//$category->isdelete ='';
$category->addedby =Auth::user()->id;
$category->created_at =Carbon::now();
$category->visitor =$request->ip();
// Image Upload
$destinationPath = 'images/category'; // Define destination path
if ($request->hasfile('category_image')) {
$extension = $request->file('category_image')->getClientOriginalExtension(); // Get image extension
$now = new \DateTime(); // Get date and time
$date = $now->getTimestamp(); // Convert date and time in timestamp
$fileName = 'fe'.$date . '.' . $extension; // Give name to image
//$destinationPath = 'images'; // Define destination path
$img = $request->file('category_image')->move(public_path().'/'.$destinationPath, $fileName); // Upload image to destination path
$image_path = $destinationPath . '/' . $fileName; // Write image path in DB
$category->category_image=$image_path;
##thumb destination path
//$thumimage_path = $destinationPath . '/thumb/' . $fileName; // thumb image path
//resizeImage($image_path, $thumimage_path, $width=150, $height=150);
}else{
$image_path = $destinationPath . '/fe' .time().".jpg"; // Write image path in DB
$category->category_image=$image_path;
}
if ($request->hasfile('category_imagethumb')) {
$extension = $request->file('category_imagethumb')->getClientOriginalExtension(); // Get image extension
$now = new \DateTime(); // Get date and time
$date = $now->getTimestamp(); // Convert date and time in timestamp
$fileName = 'th'.$date . '.' . $extension; // Give name to image
$destinationPath .= '/thumb'; // Define destination path
$img = $request->file('category_imagethumb')->move(public_path().'/'.$destinationPath, $fileName); // Upload image to destination path
$image_path = $destinationPath . '/' . $fileName; // Write image path in DB
$category->category_imagethumb=$image_path;
}else{
$imagethumb_path = $destinationPath . '/thumb/th' .time().".jpg"; // Write image path in DB
$category->category_imagethumb=$imagethumb_path;
}
if($category->save()){
//return redirect()->route('admin.login');
return redirect()->route('category.index')->with('success', 'New Category Added Successfully !');
//return back()->with('success', 'New Category Added Successfully !');
}
return back()->with('error', 'Something went wrong please try again !');
}
/**
* Display the specified resource.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
if(view()->exists('backend.admin.category.categoryCreate'))
{ $categories = self::makeHierarchicalTree();
return view('backend.admin.category.categoryEdit',compact('categories','category'));
}
return back()->with('error', 'Opps view not found');
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function edit(Category $category)
{
if(view()->exists('backend.admin.category.categoryCreate'))
{ $categories = self::makeHierarchicalTree();
return view('backend.admin.category.categoryEdit',compact('categories','category'));
}
return back()->with('error', 'Opps view not found');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
{
if ($request->_token != Session::token()) {
return Redirect::back()->with('message', 'Invalid submission !');
}
if ($category->name!=Str::title($request->name)) {
$this->validate($request, [
'parent_id' => 'required|numeric',
'category_name' => 'required|unique:categories',
'category_desc' => 'required',
'category_sortby' => 'required',
'isvisible' => 'required',
'isactive' => 'required',
]);
}
$category->parent_id = $request->parent_id;
$category->category_sortby =$request->category_sortby;
$category->category_name = Str::title($request->category_name);
$category->category_desc =$request->category_desc;
$category->category_slug =Str::slug($request->category_name);
//$category->category_image ='';
//$category->category_imagethumb ='';
$category->isvisible =$request->isvisible;
$category->isactive =$request->isactive;
//$category->isdelete ='';
$category->addedby =Auth::user()->id;
$category->updated_at =Carbon::now();
$category->visitor =$request->ip();
// Image Upload
$destinationPath = '/images/category'; // Define destination path
if ($request->hasfile('category_image')) {
$oldFileName = $category->category_image;
$extension = $request->file('category_image')->getClientOriginalExtension(); // Get image extension
$now = new \DateTime(); // Get date and time
$date = $now->getTimestamp(); // Convert date and time in timestamp
$fileName = $date . '.' . $extension; // Give name to image
//$destinationPath = 'images'; // Define destination path
//$img = $request->file('image')->move($destinationPath, $fileName); // Upload image to destination path
//$image_path = $destinationPath . '/fe' . $fileName; // Write image path in DB
File::delete(public_path().$oldFileName);
if($request->file('category_image')->move(public_path().'/'.$destinationPath,$oldFileName)){
$category->category_image = $oldFileName;
}
}
if ($request->hasfile('category_imagethumb')) {
$oldImageThumb = $category->category_imagethumb;
//$extension = $request->file('category_imagethumb')->getClientOriginalExtension(); // Get image extension
//$now = new \DateTime(); // Get date and time
//$date = $now->getTimestamp(); // Convert date and time in timestamp
//$fileName = $date . '.' . $extension; // Give name to image
$destinationPath .= '/thumb'; // Define destination path
//$img = $request->file('imagethumb')->move($destinationPath, $fileName); // Upload image to destination path
//$image_path = $destinationPath . '/thumb/th' . $fileName; // Write image path in DB
//$category->imagethumb=$image_path;
File::delete(public_path().$oldImageThumb);
if($request->file('category_imagethumb')->move(public_path().'/'.$destinationPath,$oldImageThumb)){
$category->category_imagethumb = $oldImageThumb;
}
}
if($category->save()){
//return redirect()->route('admin.login');
return back()->with('success', 'Category Updated Successfully !');
}
return back()->with('error', 'Something went wrong please try again !');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request,$id)
{
if ($request->reqId>0) {
$category = Category::findOrFail($request->reqId);
$category->delete();
return response()->json($category);
}
}
/**
* Remove the specified resource from storage.
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function delete(Request $request,$id)
{
if ($request->reqId > 0) {
$id = (int)$request->reqId;
//$category = Category::find($id);//FindOrFail($id);
$category = Category::withTrashed()->findOrFail($id);
$category->isdelete=0;
$category->addedby=Auth::user()->id;
$category->deleted_at=Carbon::now();
if ($category->save()) {
$category = ['id'=>$category->id,'status'=>true,'msg'=>'Successfully deleted Category!'];
return response()->json($category);
}
$category = array_merge(['status'=>false,'msg'=>'failed to delete Category!']);
return response()->json($category);
}
}
public function restore($id=0)
{
if($id>0) {
$category = Category::withTrashed()->find($id)->restore();
$category = Category::find($id);
$category->isdelete=1;
$category->save();
}else{
$category = Category::onlyTrashed()->restore();//->get();
$category = Category::where('isdelete',0)->update(['isdelete'=>1,'addedby'=>Auth::user()->id]);
}
return self::index();
}
public function changeStatus(Request $request)
{
$id = $request->id;
$category = Category::findOrFail($id);
$category->isactive = ($category->isactive==0)?1:0;
$category->save();
return response()->json($category);
}
private function makeHierarchicalTree()
{
$categories = Category::all()->sortByDesc('category_sortby')->toArray();
//$categories = Category::orderBy('category_sortby','asc')->get()->toArray();
//$categories = Category::with('children')->get()->toArray();
//$queries = DB::getQueryLog(); p($queries);
return $categories = buildTree($categories);
}
}


PK 99