App\Http\Controllers\ComponentsController Class Reference

This class controls all actions related to Components for the Snipe-IT Asset Management application. More...

Inheritance diagram for App\Http\Controllers\ComponentsController:
App\Http\Controllers\Controller

Public Member Functions

 getIndex ()
 Returns a view that invokes the ajax tables which actually contains the content for the components listing, which is generated in getDatatable. More...
 
 getCreate ()
 Returns a form to create a new component. More...
 
 postCreate ()
 Validate and store data for new component. More...
 
 getEdit ($componentId=null)
 Return a view to edit a component. More...
 
 postEdit ($componentId=null)
 Return a view to edit a component. More...
 
 getDelete ($componentId)
 Delete a component. More...
 
 postBulk ($componentId=null)
 
 postBulkSave ($componentId=null)
 
 getView ($componentId=null)
 Return a view to display component information. More...
 
 getCheckout ($componentId)
 Returns a view that allows the checkout of a component to an asset. More...
 
 postCheckout (ComponentCheckoutRequest $request, $componentId)
 Validate and store checkout data. More...
 
 getDatatable ()
 Generates the JSON response for accessories listing view. More...
 
 getDataView ($componentId)
 Return JSON data to populate the components view,. More...
 

Detailed Description

This class controls all actions related to Components for the Snipe-IT Asset Management application.

Version
v1.0

Definition at line 29 of file ComponentsController.php.

Member Function Documentation

App\Http\Controllers\ComponentsController::getCheckout (   $componentId)

Returns a view that allows the checkout of a component to an asset.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::postCheckout() method that stores the data.
Since
[v3.0]
Parameters
int$componentId
Returns
View

Definition at line 279 of file ComponentsController.php.

280  {
281  // Check if the component exists
282  if (is_null($component = Component::find($componentId))) {
283  // Redirect to the component management page with error
284  return Redirect::to('components')->with('error', trans('admin/components/message.not_found'));
285  } elseif (!Company::isCurrentUserHasAccess($component)) {
286  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
287  }
288 
289  // Get the dropdown of assets and then pass it to the checkout view
290  $assets_list = Helper::assetsList();
291 
292  return View::make('components/checkout', compact('component'))->with('assets_list', $assets_list);
293 
294  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static assetsList()
Definition: Helper.php:149
App\Http\Controllers\ComponentsController::getCreate ( )

Returns a form to create a new component.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::postCreate() method that stores the data
Since
[v3.0]
Returns
View

Definition at line 54 of file ComponentsController.php.

55  {
56  // Show the page
57  $category_list = array('' => '') + DB::table('categories')->where('category_type', '=', 'component')->whereNull('deleted_at')->orderBy('name', 'ASC')->lists('name', 'id');
58  $company_list = Helper::companyList();
59  $location_list = Helper::locationsList();
60 
61  return View::make('components/edit')
62  ->with('component', new Component)
63  ->with('category_list', $category_list)
64  ->with('company_list', $company_list)
65  ->with('location_list', $location_list);
66  }
static locationsList()
Definition: Helper.php:94
static companyList()
Definition: Helper.php:61
App\Http\Controllers\ComponentsController::getDatatable ( )

Generates the JSON response for accessories listing view.

For debugging, see at /api/accessories/list

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
Since
[v3.0]
Returns
string JSON

Definition at line 394 of file ComponentsController.php.

395  {
396  $components = Component::select('components.*')->whereNull('components.deleted_at')
397  ->with('company', 'location', 'category');
398 
399  if (Input::has('search')) {
400  $components = $components->TextSearch(Input::get('search'));
401  }
402 
403  if (Input::has('offset')) {
404  $offset = e(Input::get('offset'));
405  } else {
406  $offset = 0;
407  }
408 
409  if (Input::has('limit')) {
410  $limit = e(Input::get('limit'));
411  } else {
412  $limit = 50;
413  }
414 
415  $allowed_columns = ['id','name','min_amt','order_number','purchase_date','purchase_cost','companyName','category','total_qty'];
416  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
417  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
418 
419  switch ($sort) {
420  case 'category':
421  $components = $components->OrderCategory($order);
422  break;
423  case 'location':
424  $components = $components->OrderLocation($order);
425  break;
426  case 'companyName':
427  $components = $components->OrderCompany($order);
428  break;
429  default:
430  $components = $components->orderBy($sort, $order);
431  break;
432  }
433 
434  $consumCount = $components->count();
435  $components = $components->skip($offset)->take($limit)->get();
436 
437  $rows = array();
438 
439  foreach ($components as $component) {
440  $actions = '<nobr><a href="'.route('checkout/component', $component->id).'" style="margin-right:5px;" class="btn btn-info btn-sm" '.(($component->numRemaining() > 0 ) ? '' : ' disabled').'>'.trans('general.checkout').'</a><a href="'.route('update/component', $component->id).'" class="btn btn-warning btn-sm" style="margin-right:5px;"><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/component', $component->id).'" data-content="'.trans('admin/components/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($component->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>';
441  $company = $component->company;
442 
443  $rows[] = array(
444  'checkbox' =>'<div class="text-center"><input type="checkbox" name="component['.$component->id.']" class="one_required"></div>',
445  'id' => $component->id,
446  'name' => (string)link_to('admin/components/'.$component->id.'/view', e($component->name)),
447  'location' => ($component->location) ? e($component->location->name) : '',
448  'total_qty' => e($component->total_qty),
449  'min_amt' => e($component->min_amt),
450  'category' => ($component->category) ? e($component->category->name) : 'Missing category',
451  'order_number' => e($component->order_number),
452  'purchase_date' => e($component->purchase_date),
453  'purchase_cost' => ($component->purchase_cost!='') ? number_format($component->purchase_cost, 2): '' ,
454  'numRemaining' => $component->numRemaining(),
455  'actions' => $actions,
456  'companyName' => is_null($company) ? '' : e($company->name),
457  );
458  }
459 
460  $data = array('total' => $consumCount, 'rows' => $rows);
461 
462  return $data;
463 
464  }
App\Http\Controllers\ComponentsController::getDataView (   $componentId)

Return JSON data to populate the components view,.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getView() method that returns the view.
Since
[v3.0]
Parameters
int$componentId
Returns
string JSON

Definition at line 475 of file ComponentsController.php.

476  {
477  //$component = Component::find($componentID);
478  $component = Component::with('assets')->find($componentId);
479 
480 
481  if (!Company::isCurrentUserHasAccess($component)) {
482  return ['total' => 0, 'rows' => []];
483  }
484 
485  $rows = array();
486 
487  foreach ($component->assets as $component_assignment) {
488  $rows[] = array(
489  'name' => (string)link_to('/hardware/'.$component_assignment->id.'/view', e($component_assignment->name)),
490  'qty' => e($component_assignment->pivot->assigned_qty),
491  'created_at' => ($component_assignment->created_at->format('Y-m-d H:i:s')=='-0001-11-30 00:00:00') ? '' : $component_assignment->created_at->format('Y-m-d H:i:s'),
492  );
493  }
494 
495  $componentCount = $component->assets->count();
496  $data = array('total' => $componentCount, 'rows' => $rows);
497  return $data;
498  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
App\Http\Controllers\ComponentsController::getDelete (   $componentId)

Delete a component.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
Since
[v3.0]
Parameters
int$componentId
Returns
Redirect

Definition at line 210 of file ComponentsController.php.

211  {
212  // Check if the blog post exists
213  if (is_null($component = Component::find($componentId))) {
214  // Redirect to the blogs management page
215  return Redirect::to('admin/components')->with('error', trans('admin/components/message.not_found'));
216  } elseif (!Company::isCurrentUserHasAccess($component)) {
217  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
218  }
219 
220  $component->delete();
221 
222  // Redirect to the locations management page
223  return Redirect::to('admin/components')->with('success', trans('admin/components/message.delete.success'));
224 
225  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
App\Http\Controllers\ComponentsController::getEdit (   $componentId = null)

Return a view to edit a component.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::postEdit() method that stores the data.
Since
[v3.0]
Parameters
int$componentId
Returns
View

Definition at line 126 of file ComponentsController.php.

127  {
128  // Check if the component exists
129  if (is_null($component = Component::find($componentId))) {
130  // Redirect to the blogs management page
131  return Redirect::to('admin/components')->with('error', trans('admin/components/message.does_not_exist'));
132  } elseif (!Company::isCurrentUserHasAccess($component)) {
133  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
134  }
135 
136  $category_list = Helper::categoryList();
137  $company_list = Helper::companyList();
138  $location_list = Helper::locationsList();
139 
140  return View::make('components/edit', compact('component'))
141  ->with('category_list', $category_list)
142  ->with('company_list', $company_list)
143  ->with('location_list', $location_list);
144  }
static categoryList()
Definition: Helper.php:70
static locationsList()
Definition: Helper.php:94
static companyList()
Definition: Helper.php:61
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
App\Http\Controllers\ComponentsController::getIndex ( )

Returns a view that invokes the ajax tables which actually contains the content for the components listing, which is generated in getDatatable.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getDatatable() method that generates the JSON response
Since
[v3.0]
Returns
View

Definition at line 40 of file ComponentsController.php.

41  {
42  return View::make('components/index');
43  }
App\Http\Controllers\ComponentsController::getView (   $componentId = null)

Return a view to display component information.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getDataView() method that generates the JSON response
Since
[v3.0]
Parameters
int$componentId
Returns
View

Definition at line 247 of file ComponentsController.php.

248  {
249  $component = Component::find($componentId);
250 
251  if (isset($component->id)) {
252 
253 
254  if (!Company::isCurrentUserHasAccess($component)) {
255  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
256  } else {
257  return View::make('components/view', compact('component'));
258  }
259  } else {
260  // Prepare the error message
261  $error = trans('admin/components/message.does_not_exist', compact('id'));
262 
263  // Redirect to the user management page
264  return Redirect::route('components')->with('error', $error);
265  }
266 
267 
268  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
App\Http\Controllers\ComponentsController::postBulk (   $componentId = null)

Definition at line 227 of file ComponentsController.php.

228  {
229  echo 'Stubbed - not yet complete';
230  }
App\Http\Controllers\ComponentsController::postBulkSave (   $componentId = null)

Definition at line 232 of file ComponentsController.php.

233  {
234  echo 'Stubbed - not yet complete';
235  }
App\Http\Controllers\ComponentsController::postCheckout ( ComponentCheckoutRequest  $request,
  $componentId 
)

Validate and store checkout data.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getCheckout() method that returns the form.
Since
[v3.0]
Parameters
int$componentId
Returns
Redirect

Definition at line 305 of file ComponentsController.php.

306  {
307  // Check if the component exists
308  if (is_null($component = Component::find($componentId))) {
309  // Redirect to the component management page with error
310  return Redirect::to('components')->with('error', trans('admin/components/message.not_found'));
311  } elseif (!Company::isCurrentUserHasAccess($component)) {
312  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
313  }
314 
315  $admin_user = Auth::user();
316  $asset_id = e(Input::get('asset_id'));
317 
318  // Check if the user exists
319  if (is_null($asset = Asset::find($asset_id))) {
320  // Redirect to the component management page with error
321  return Redirect::to('admin/components')->with('error', trans('admin/components/message.asset_does_not_exist'));
322  }
323 
324  // Update the component data
325  $component->asset_id = $asset_id;
326 
327  $component->assets()->attach($component->id, array(
328  'component_id' => $component->id,
329  'user_id' => $admin_user->id,
330  'created_at' => date('Y-m-d h:i:s'),
331  'assigned_qty' => e(Input::get('assigned_qty')),
332  'asset_id' => $asset_id));
333 
334  $logaction = new Actionlog();
335  $logaction->component_id = $component->id;
336  $logaction->asset_id = $asset_id;
337  $logaction->asset_type = 'component';
338  $logaction->location_id = $asset->location_id;
339  $logaction->user_id = Auth::user()->id;
340  $logaction->note = e(Input::get('note'));
341 
342  $settings = Setting::getSettings();
343 
344  if ($settings->slack_endpoint) {
345 
346  $slack_settings = [
347  'username' => $settings->botname,
348  'channel' => $settings->slack_channel,
349  'link_names' => true
350  ];
351 
352  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
353 
354  try {
355  $client->attach([
356  'color' => 'good',
357  'fields' => [
358  [
359  'title' => 'Checked Out:',
360  'value' => strtoupper($logaction->asset_type).' <'.config('app.url').'/admin/components/'.$component->id.'/view'.'|'.$component->name.'> checked out to <'.config('app.url').'/hardware/'.$asset->id.'/view|'.$asset->name.'> by <'.config('app.url').'/admin/users/'.$admin_user->id.'/view'.'|'.$admin_user->fullName().'>.'
361  ],
362  [
363  'title' => 'Note:',
364  'value' => e($logaction->note)
365  ],
366  ]
367  ])->send('Component Checked Out');
368 
369  } catch (Exception $e) {
370 
371  }
372  }
373 
374 
375  $log = $logaction->logaction('checkout');
376 
377  // Redirect to the new component page
378  return Redirect::to("admin/components")->with('success', trans('admin/components/message.checkout.success'));
379 
380 
381 
382  }
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static getSettings()
Definition: Setting.php:33
App\Http\Controllers\ComponentsController::postCreate ( )

Validate and store data for new component.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getCreate() method that generates the view
Since
[v3.0]
Returns
Redirect

Definition at line 77 of file ComponentsController.php.

78  {
79 
80  // create a new model instance
81  $component = new Component();
82 
83  // Update the component data
84  $component->name = e(Input::get('name'));
85  $component->category_id = e(Input::get('category_id'));
86  $component->location_id = e(Input::get('location_id'));
87  $component->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
88  $component->order_number = e(Input::get('order_number'));
89  $component->min_amt = e(Input::get('min_amt'));
90 
91  if (e(Input::get('purchase_date')) == '') {
92  $component->purchase_date = null;
93  } else {
94  $component->purchase_date = e(Input::get('purchase_date'));
95  }
96 
97  if (e(Input::get('purchase_cost')) == '0.00') {
98  $component->purchase_cost = null;
99  } else {
100  $component->purchase_cost = e(Input::get('purchase_cost'));
101  }
102 
103  $component->total_qty = e(Input::get('total_qty'));
104  $component->user_id = Auth::user()->id;
105 
106  // Was the component created?
107  if ($component->save()) {
108  // Redirect to the new component page
109  return Redirect::to("admin/components")->with('success', trans('admin/components/message.create.success'));
110  }
111 
112  return Redirect::back()->withInput()->withErrors($component->getErrors());
113 
114 
115  }
static getIdForCurrentUser($unescaped_input)
Definition: Company.php:81
App\Http\Controllers\ComponentsController::postEdit (   $componentId = null)

Return a view to edit a component.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
ComponentsController::getEdit() method presents the form.
Parameters
int$componentId
Since
[v3.0]
Returns
Redirect

Definition at line 156 of file ComponentsController.php.

157  {
158  // Check if the blog post exists
159  if (is_null($component = Component::find($componentId))) {
160  // Redirect to the blogs management page
161  return Redirect::to('admin/components')->with('error', trans('admin/components/message.does_not_exist'));
162  } elseif (!Company::isCurrentUserHasAccess($component)) {
163  return Redirect::to('admin/components')->with('error', trans('general.insufficient_permissions'));
164  }
165 
166 
167  // Update the component data
168  $component->name = e(Input::get('name'));
169  $component->category_id = e(Input::get('category_id'));
170  $component->location_id = e(Input::get('location_id'));
171  $component->company_id = Company::getIdForCurrentUser(Input::get('company_id'));
172  $component->order_number = e(Input::get('order_number'));
173  $component->min_amt = e(Input::get('min_amt'));
174 
175  if (e(Input::get('purchase_date')) == '') {
176  $component->purchase_date = null;
177  } else {
178  $component->purchase_date = e(Input::get('purchase_date'));
179  }
180 
181  if (e(Input::get('purchase_cost')) == '0.00') {
182  $component->purchase_cost = null;
183  } else {
184  $component->purchase_cost = e(Input::get('purchase_cost'));
185  }
186 
187  $component->total_qty = e(Input::get('total_qty'));
188 
189  // Was the component created?
190  if ($component->save()) {
191  // Redirect to the new component page
192  return Redirect::to("admin/components")->with('success', trans('admin/components/message.update.success'));
193  }
194 
195  return Redirect::back()->withInput()->withErrors($component->getErrors());
196 
197 
198 
199 
200  }
static getIdForCurrentUser($unescaped_input)
Definition: Company.php:81
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96

The documentation for this class was generated from the following file: