|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Controllers\Admin; |
| 4 | + |
| 5 | +use Controllers\BaseController; |
| 6 | +use Illuminate\Support\Facades\Input; |
| 7 | +use Tricks\Repositories\CategoryRepositoryInterface; |
| 8 | + |
| 9 | +class CategoriesController extends BaseController |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Category repository. |
| 13 | + * |
| 14 | + * @var \Tricks\Repositories\CategoryRepositoryInterface |
| 15 | + */ |
| 16 | + protected $categories; |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new CategoriesController instance. |
| 20 | + * |
| 21 | + * @param \Tricks\Repositories\CategoryRepositoryInterface $categories |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function __construct(CategoryRepositoryInterface $categories) |
| 25 | + { |
| 26 | + parent::__construct(); |
| 27 | + |
| 28 | + $this->categories = $categories; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Show the admin categories index page. |
| 33 | + * |
| 34 | + * @return \Response |
| 35 | + */ |
| 36 | + public function getIndex() |
| 37 | + { |
| 38 | + $categories = $this->categories->findAll('order', 'asc'); |
| 39 | + |
| 40 | + $this->view('admin.categories.list', compact('categories')); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Handle the creation of a category. |
| 45 | + * |
| 46 | + * @return \Illuminate\Http\RedirectResponse |
| 47 | + */ |
| 48 | + public function postIndex() |
| 49 | + { |
| 50 | + $form = $this->categories->getForm(); |
| 51 | + |
| 52 | + if (! $form->isValid()) { |
| 53 | + return $this->redirectRoute('admin.categories.index') |
| 54 | + ->withErrors($form->getErrors()) |
| 55 | + ->withInput(); |
| 56 | + } |
| 57 | + |
| 58 | + $category = $this->categories->create($form->getInputData()); |
| 59 | + |
| 60 | + return $this->redirectRoute('admin.categories.index'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Update the order of the categories. |
| 65 | + * |
| 66 | + * @return \Response |
| 67 | + */ |
| 68 | + public function postArrange() |
| 69 | + { |
| 70 | + $decoded = Input::get('data'); |
| 71 | + |
| 72 | + if ($decoded) { |
| 73 | + $this->categories->arrange($decoded); |
| 74 | + } |
| 75 | + |
| 76 | + return 'ok'; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Show the category edit form. |
| 81 | + * |
| 82 | + * @param mixed $id |
| 83 | + * @return \Response |
| 84 | + */ |
| 85 | + public function getView($id) |
| 86 | + { |
| 87 | + $category = $this->categories->findById($id); |
| 88 | + |
| 89 | + $this->view('admin.categories.edit', compact('category')); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Handle the editting of a category. |
| 94 | + * |
| 95 | + * @param mixed $id |
| 96 | + * @return \Illuminate\Http\RedirectResponse |
| 97 | + */ |
| 98 | + public function postView($id) |
| 99 | + { |
| 100 | + $form = $this->categories->getForm(); |
| 101 | + |
| 102 | + if (! $form->isValid()) { |
| 103 | + return $this->redirectRoute('admin.categories.view', $id) |
| 104 | + ->withErrors($form->getErrors()) |
| 105 | + ->withInput(); |
| 106 | + } |
| 107 | + |
| 108 | + $category = $this->categories->update($id, $form->getInputData()); |
| 109 | + |
| 110 | + return $this->redirectRoute('admin.categories.view', $id); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Delete a category from the database. |
| 115 | + * |
| 116 | + * @param mixed $id |
| 117 | + * @return \Illuminate\Http\RedirectResponse |
| 118 | + */ |
| 119 | + public function getDelete($id) |
| 120 | + { |
| 121 | + $this->categories->delete($id); |
| 122 | + |
| 123 | + return $this->redirectRoute('admin.categories.index'); |
| 124 | + } |
| 125 | +} |
0 commit comments