CategoriesController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
8 use Auth;
9 use DB;
10 use Input;
11 use Lang;
12 use Redirect;
13 use Str;
14 use View;
15 
24 {
25 
35  public function getIndex()
36  {
37  // Show the page
38  return View::make('categories/index');
39  }
40 
41 
50  public function getCreate()
51  {
52  // Show the page
53  $category_types= Helper::categoryTypeList();
54  return View::make('categories/edit')->with('category', new Category)
55  ->with('category_types', $category_types);
56  }
57 
58 
67  public function postCreate()
68  {
69 
70  // create a new model instance
71  $category = new Category();
72 
73  // Update the category data
74  $category->name = e(Input::get('name'));
75  $category->category_type = e(Input::get('category_type'));
76  $category->eula_text = e(Input::get('eula_text'));
77  $category->use_default_eula = e(Input::get('use_default_eula', '0'));
78  $category->require_acceptance = e(Input::get('require_acceptance', '0'));
79  $category->checkin_email = e(Input::get('checkin_email', '0'));
80  $category->user_id = Auth::user()->id;
81 
82  if ($category->save()) {
83  // Redirect to the new category page
84  return Redirect::to("admin/settings/categories")->with('success', trans('admin/categories/message.create.success'));
85  } else {
86 
87  // The given data did not pass validation
88  return Redirect::back()->withInput()->withErrors($category->getErrors());
89 
90  }
91 
92  // Redirect to the category create page
93  return Redirect::to('admin/settings/categories/create')->with('error', trans('admin/categories/message.create.error'));
94 
95 
96  }
97 
107  public function getEdit($categoryId = null)
108  {
109  // Check if the category exists
110  if (is_null($category = Category::find($categoryId))) {
111  // Redirect to the blogs management page
112  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.does_not_exist'));
113  }
114 
115  // Show the page
116  //$category_options = array('' => 'Top Level') + Category::lists('name', 'id');
117 
118  $category_options = array('' => 'Top Level') + DB::table('categories')->where('id', '!=', $categoryId)->lists('name', 'id');
119  $category_types= Helper::categoryTypeList();
120 
121  return View::make('categories/edit', compact('category'))
122  ->with('category_options', $category_options)
123  ->with('category_types', $category_types);
124  }
125 
126 
136  public function postEdit($categoryId = null)
137  {
138  // Check if the blog post exists
139  if (is_null($category = Category::find($categoryId))) {
140  // Redirect to the blogs management page
141  return Redirect::to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist'));
142  }
143 
144  // Update the category data
145  $category->name = e(Input::get('name'));
146  $category->category_type = e(Input::get('category_type'));
147  $category->eula_text = e(Input::get('eula_text'));
148  $category->use_default_eula = e(Input::get('use_default_eula', '0'));
149  $category->require_acceptance = e(Input::get('require_acceptance', '0'));
150  $category->checkin_email = e(Input::get('checkin_email', '0'));
151 
152  if ($category->save()) {
153  // Redirect to the new category page
154  return Redirect::to("admin/settings/categories")->with('success', trans('admin/categories/message.update.success'));
155  } // attempt validation
156  else {
157  // The given data did not pass validation
158  return Redirect::back()->withInput()->withErrors($category->getErrors());
159  }
160 
161  // Redirect to the category management page
162  return Redirect::back()->with('error', trans('admin/categories/message.update.error'));
163 
164  }
165 
174  public function getDelete($categoryId)
175  {
176  // Check if the category exists
177  if (is_null($category = Category::find($categoryId))) {
178  // Redirect to the blogs management page
179  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.not_found'));
180  }
181 
182 
183  if ($category->has_models() > 0) {
184  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_models'));
185 
186  } elseif ($category->accessories()->count() > 0) {
187  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_accessories'));
188 
189  } elseif ($category->consumables()->count() > 0) {
190  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_consumables'));
191 
192  } elseif ($category->components()->count() > 0) {
193  return Redirect::to('admin/settings/categories')->with('error', trans('admin/categories/message.assoc_components'));
194  } else {
195 
196  $category->delete();
197 
198  // Redirect to the locations management page
199  return Redirect::to('admin/settings/categories')->with('success', trans('admin/categories/message.delete.success'));
200  }
201 
202 
203  }
204 
205 
206 
217  public function getView($categoryId = null)
218  {
219  $category = Category::find($categoryId);
220 
221  if (isset($category->id)) {
222  return View::make('categories/view', compact('category'));
223  } else {
224  // Prepare the error message
225  $error = trans('admin/categories/message.does_not_exist', compact('id'));
226 
227  // Redirect to the user management page
228  return Redirect::route('categories')->with('error', $error);
229  }
230 
231 
232  }
233 
244  public function getDatatable()
245  {
246  // Grab all the categories
247  $categories = Category::with('assets', 'accessories', 'consumables','components');
248 
249  if (Input::has('search')) {
250  $categories = $categories->TextSearch(e(Input::get('search')));
251  }
252 
253  if (Input::has('offset')) {
254  $offset = e(Input::get('offset'));
255  } else {
256  $offset = 0;
257  }
258 
259  if (Input::has('limit')) {
260  $limit = e(Input::get('limit'));
261  } else {
262  $limit = 50;
263  }
264 
265 
266  $allowed_columns = ['id','name','category_type'];
267  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
268  $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
269 
270  $categories = $categories->orderBy($sort, $order);
271 
272  $catCount = $categories->count();
273  $categories = $categories->skip($offset)->take($limit)->get();
274 
275  $rows = array();
276 
277  foreach ($categories as $category) {
278 
279  $actions = '<a href="'.route('update/category', $category->id).'" class="btn btn-warning btn-sm" style="margin-right:5px;">';
280  $actions .='<i class="fa fa-pencil icon-white"></i></a>';
281  $actions .='<a data-html="false" class="btn delete-asset btn-danger btn-sm';
282  if ($category->itemCount() > 0) {
283  $actions .=' disabled';
284  }
285  $actions .=' data-toggle="modal" href="'.route('delete/category', $category->id).'" data-content="'.trans('admin/categories/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($category->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
286  $rows[] = array(
287  'id' => $category->id,
288  'name' => (string)link_to('/admin/settings/categories/'.$category->id.'/view', $category->name) ,
289  'category_type' => ucwords($category->category_type),
290  'count' => $category->itemCount(),
291  'acceptance' => ($category->require_acceptance=='1') ? '<i class="fa fa-check"></i>' : '',
292  //EULA is still not working correctly
293  'eula' => ($category->getEula()) ? '<i class="fa fa-check"></i>' : '',
294  'actions' => $actions
295  );
296  }
297 
298  $data = array('total' => $catCount, 'rows' => $rows);
299 
300  return $data;
301  }
302 
315  public function getDataView($categoryID)
316  {
317 
318  $category = Category::find($categoryID);
319 
320  if ($category->category_type =='asset') {
321  $category_assets = $category->assets;
322  } elseif ($category->category_type =='accessory') {
323  $category_assets = $category->accessories;
324  } elseif ($category->category_type =='consumable') {
325  $category_assets = $category->consumables;
326  } elseif ($category->category_type =='component') {
327  $category_assets = $category->components;
328  }
329 
330 
331  if (Input::has('search')) {
332  $category_assets = $category_assets->TextSearch(e(Input::get('search')));
333  }
334 
335  if (Input::has('offset')) {
336  $offset = e(Input::get('offset'));
337  } else {
338  $offset = 0;
339  }
340 
341  if (Input::has('limit')) {
342  $limit = e(Input::get('limit'));
343  } else {
344  $limit = 50;
345  }
346 
347  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
348 
349  $allowed_columns = ['id','name','serial','asset_tag'];
350  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
351  $count = $category_assets->count();
352 
353  $rows = array();
354 
355  foreach ($category_assets as $asset) {
356 
357  $actions = '';
358  $inout='';
359 
360  if ($asset->deleted_at=='') {
361  $actions = '<div style=" white-space: nowrap;"><a href="'.route('clone/hardware', $asset->id).'" class="btn btn-info btn-sm" title="Clone asset"><i class="fa fa-files-o"></i></a> <a href="'.route('update/'.$category->category_type, $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-pencil icon-white"></i></a> <a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/hardware', $asset->id).'" data-content="'.trans('admin/hardware/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($asset->asset_tag).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></div>';
362  } elseif ($asset->deleted_at!='') {
363  $actions = '<a href="'.route('restore/hardware', $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-recycle icon-white"></i></a>';
364  }
365 
366  if ($asset->assetstatus) {
367  if ($asset->assetstatus->deployable != 0) {
368  if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) {
369  $inout = '<a href="'.route('checkin/hardware', $asset->id).'" class="btn btn-primary btn-sm">'.trans('general.checkin').'</a>';
370  } else {
371  $inout = '<a href="'.route('checkout/hardware', $asset->id).'" class="btn btn-info btn-sm">'.trans('general.checkout').'</a>';
372  }
373  }
374  }
375 
376  $rows[] = array(
377  'id' => $asset->id,
378  'name' => (string)link_to('/hardware/'.$asset->id.'/view', e($asset->name)),
379  //'model' => $asset->model->name,
380  'asset_tag' => e($asset->asset_tag),
381  'serial' => e($asset->serial),
382  'assigned_to' => ($asset->assigneduser) ? (string)link_to(config('app.url').'/admin/users/'.$asset->assigneduser->id.'/view', $asset->assigneduser->fullName()): '',
383  'change' => $inout,
384  'actions' => $actions,
385  'companyName' => Company::getName($asset),
386  );
387  }
388 
389  $data = array('total' => $count, 'rows' => $rows);
390  return $data;
391  }
392 }
getCreate()
Returns a form view to create a new category.
getDelete($categoryId)
Validates and marks a category as deleted.
static categoryTypeList()
Definition: Helper.php:132
getEdit($categoryId=null)
Returns a view that makes a form to update a category.
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the categories li...
This class controls all actions related to Categories for the Snipe-IT Asset Management application...
getView($categoryId=null)
Returns a view that invokes the ajax tables which actually contains the content for the categories de...
getDataView($categoryID)
Returns JSON response that contains the data for the category detail page.
static getName($companyable)
Definition: Company.php:162
postCreate()
Validates and stores the new category data.
postEdit($categoryId=null)
Validates and stores the updated category data.
getDatatable()
Returns a JSON response with the data to populate the bootstrap table on the cateory listing page...