Created
September 16, 2013 19:27
-
-
Save adamgoose/6585315 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CategoryController extends \BaseController { | |
protected $layout = 'layouts.dashboard'; | |
public $category; | |
public function __construct() { | |
parent::__construct(); | |
if(is_numeric(Request::segment(2))) | |
$this->category = Category::find(Request::segment(2)); | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return Response | |
*/ | |
public function index() | |
{ | |
$categories = $this->club->categories->filter(function($category) { | |
if(Sentry::getUser()->hasAccess('category.id.'.$category->id)) | |
return true; | |
}); | |
$this->layout->content = View::make('dashboard.category.index') | |
->with('categories', $categories); | |
} | |
/** | |
* Show the form for creating a new resource. | |
* | |
* @return Response | |
*/ | |
public function create() | |
{ | |
// | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @return Response | |
*/ | |
public function store() | |
{ | |
if(!Sentry::getUser()->hasAccess('club.create.category.'.Session::get('club'))) | |
return json_encode(['status' => false, 'message' => 'Permission denied']); | |
$category = new Category([ | |
'name' => Input::get('name') | |
]); | |
if($this->club->categories()->save($category)) | |
return Redirect::to('/category/'.$category->id.'/edit'); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function show($id) | |
{ | |
$this->category->load('content'); | |
if(!Sentry::getUser()->hasAccess('category.id.'.$this->category->id)) | |
return Redirect::to('/'); | |
$this->layout->content = View::make('dashboard.category.show') | |
->with('category', $this->category); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function edit($id) | |
{ | |
if(!Sentry::getUser()->hasAccess('club.update.category.'.$this->category->club->id)) | |
return Redirect::to('/'); | |
foreach($this->club->layouts as $layout) | |
$layouts[$layout->id] = $layout->name; | |
$menus['NULL'] = "None"; | |
foreach($this->club->menus()->whereType('list')->get() as $menu) | |
$menus[$menu->id] = $menu->name; | |
$parents['NULL'] = "None"; | |
foreach($this->club->categories()->whereParentId(null)->has('widget', '=', 0)->sorted()->get() as $parent) | |
$parents[$parent->id] = $parent->name; | |
$this->layout->content = View::make('dashboard.category.edit') | |
->with('category', $this->category) | |
->with('layouts', $layouts) | |
->with('menus', $menus) | |
->with('parents', $parents); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function update($id) | |
{ | |
if(!Sentry::getUser()->hasAccess('club.update.category.'.$this->category->club->id)) | |
return Redirect::to('/'); | |
$input = Input::all(); | |
if($input['menu_id'] == "NULL") | |
$input['menu_id'] = null; | |
if($input['parent_id'] == "NULL") | |
$input['parent_id'] = null; | |
$input['layout_id'] = implode(',', Input::get('layout_id')); | |
$this->category->fill($input); | |
$this->category->save(); | |
return Redirect::to('/category/'.$this->category->id); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param int $id | |
* @return Response | |
*/ | |
public function destroy($id) | |
{ | |
if(Sentry::getUser()->hasAccess('club.delete.category.'.$this->category->club->id) && !$this->category->content->count() && !$this->category->children->count()) { | |
$this->category->delete(); | |
return Redirect::to('/content'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment