App\Http\Controllers\LocationsController Class Reference

This controller handles all actions related to Locations for the Snipe-IT Asset Management application. More...

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

Public Member Functions

 getIndex ()
 Returns a view that invokes the ajax tables which actually contains the content for the locations listing, which is generated in getDatatable. More...
 
 getCreate ()
 Returns a form view used to create a new location. More...
 
 postCreate ()
 Validates and stores a new location. More...
 
 store ()
 Validates and stores a new location created via the Create Asset form modal. More...
 
 getEdit ($locationId=null)
 Makes a form view to edit location information. More...
 
 postEdit ($locationId=null)
 Validates and stores updated location data from edit form. More...
 
 getDelete ($locationId)
 Validates and deletes selected location. More...
 
 getView ($locationId=null)
 Returns a view that invokes the ajax tables which actually contains the content for the locations detail page. More...
 
 getDatatable ()
 Returns the JSON response to populate the bootstrap tables on the locationa view. More...
 
 getDataViewUsers ($locationID)
 Returns a JSON response that contains the users association with the selected location, to be used by the location detail view. More...
 
 getDataViewAssets ($locationID)
 Returns a JSON response that contains the assets association with the selected location, to be used by the location detail view. More...
 

Detailed Description

This controller handles all actions related to Locations for the Snipe-IT Asset Management application.

Version
v1.0

Definition at line 22 of file LocationsController.php.

Member Function Documentation

App\Http\Controllers\LocationsController::getCreate ( )

Returns a form view used to create a new location.

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

Definition at line 52 of file LocationsController.php.

53  {
54  $locations = Location::orderBy('name', 'ASC')->get();
55 
56  $location_options_array = Location::getLocationHierarchy($locations);
57  $location_options = Location::flattenLocationsArray($location_options_array);
58  $location_options = array('' => 'Top Level') + $location_options;
59 
60  return View::make('locations/edit')
61  ->with('location_options', $location_options)
62  ->with('location', new Location);
63  }
static getLocationHierarchy($locations, $parent_id=null)
Definition: Location.php:68
static flattenLocationsArray($location_options_array=null)
Definition: Location.php:96
App\Http\Controllers\LocationsController::getDatatable ( )

Returns the JSON response to populate the bootstrap tables on the locationa view.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getIndex() method that returns JSON for location index
Since
[v1.0]
Returns
View

Definition at line 289 of file LocationsController.php.

290  {
291  $locations = Location::select(array('locations.id','locations.name','locations.address','locations.address2','locations.city','locations.state','locations.zip','locations.country','locations.parent_id','locations.currency'))->with('assets');
292 
293 
294  if (Input::has('search')) {
295  $locations = $locations->TextSearch(e(Input::get('search')));
296  }
297 
298  if (Input::has('offset')) {
299  $offset = e(Input::get('offset'));
300  } else {
301  $offset = 0;
302  }
303 
304  if (Input::has('limit')) {
305  $limit = e(Input::get('limit'));
306  } else {
307  $limit = 50;
308  }
309 
310  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
311 
312 
313 
314  switch (Input::get('sort')) {
315  case 'parent':
316  $locations = $locations->OrderParent($order);
317  break;
318  default:
319  $allowed_columns = ['id','name','address','city','state','country','currency'];
320 
321  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
322  $locations = $locations->orderBy($sort, $order);
323  break;
324  }
325 
326 
327  $locationsCount = $locations->count();
328  $locations = $locations->skip($offset)->take($limit)->get();
329 
330  $rows = array();
331 
332  foreach ($locations as $location) {
333  $actions = '<nobr><a href="'.route('update/location', $location->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/location', $location->id).'" data-content="'.trans('admin/locations/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($location->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></nobr>';
334 
335  $rows[] = array(
336  'id' => $location->id,
337  'name' => (string)link_to('admin/settings/locations/'.$location->id.'/view', e($location->name)),
338  'parent' => ($location->parent) ? e($location->parent->name) : '',
339  // 'assets' => ($location->assets->count() + $location->assignedassets->count()),
340  'assets_default' => $location->assignedassets->count(),
341  'assets_checkedout' => $location->assets->count(),
342  'address' => ($location->address) ? e($location->address): '',
343  'city' => e($location->city),
344  'state' => e($location->state),
345  'country' => e($location->country),
346  'currency' => e($location->currency),
347  'actions' => $actions
348  );
349  }
350 
351  $data = array('total' => $locationsCount, 'rows' => $rows);
352 
353  return $data;
354 
355  }
App\Http\Controllers\LocationsController::getDataViewAssets (   $locationID)

Returns a JSON response that contains the assets association with the selected location, to be used by the location detail view.

Todo:
This is broken for accessories and consumables.
Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getView() method that creates the display view
Parameters
int$locationId
Since
[v1.8]
Returns
View

Definition at line 396 of file LocationsController.php.

397  {
398  $location = Location::find($locationID)->load('assignedassets.model');
399  $rows = array();
400 
401  foreach ($location->assignedassets as $asset) {
402  $rows[] = array(
403  'name' => (string)link_to('/hardware/'.$asset->id.'/view', e($asset->showAssetName())),
404  'asset_tag' => e($asset->asset_tag),
405  'serial' => e($asset->serial),
406  'model' => e($asset->model->name),
407  );
408  }
409 
410  $data = array('total' => $location->assignedassets->count(), 'rows' => $rows);
411  return $data;
412 
413  }
App\Http\Controllers\LocationsController::getDataViewUsers (   $locationID)

Returns a JSON response that contains the users association with the selected location, to be used by the location detail view.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getView() method that creates the display view
Parameters
int$locationId
Since
[v1.8]
Returns
View

Definition at line 368 of file LocationsController.php.

369  {
370  $location = Location::find($locationID);
371  $rows = array();
372 
373  foreach ($location->users as $user) {
374  $rows[] = array(
375  'name' => (string)link_to('/admin/users/'.$user->id.'/view', e($user->fullName()))
376  );
377  }
378 
379  $data = array('total' => $location->users->count(), 'rows' => $rows);
380 
381  return $data;
382  }
App\Http\Controllers\LocationsController::getDelete (   $locationId)

Validates and deletes selected location.

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

Definition at line 225 of file LocationsController.php.

226  {
227  // Check if the location exists
228  if (is_null($location = Location::find($locationId))) {
229  // Redirect to the blogs management page
230  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.not_found'));
231  }
232 
233 
234  if ($location->users->count() > 0) {
235  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.assoc_users'));
236  } elseif ($location->childLocations->count() > 0) {
237  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.assoc_child_loc'));
238  } elseif ($location->assets->count() > 0) {
239  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.assoc_assets'));
240  } elseif ($location->assignedassets->count() > 0) {
241  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.assoc_assets'));
242  } else {
243  $location->delete();
244  return Redirect::to('admin/settings/locations')->with('success', trans('admin/locations/message.delete.success'));
245  }
246 
247 
248 
249  }
App\Http\Controllers\LocationsController::getEdit (   $locationId = null)

Makes a form view to edit location information.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::postCreate() method that validates and stores
Parameters
int$locationId
Since
[v1.0]
Returns
View

Definition at line 157 of file LocationsController.php.

158  {
159  // Check if the location exists
160  if (is_null($location = Location::find($locationId))) {
161  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.does_not_exist'));
162  }
163 
164  // Show the page
165  $locations = Location::orderBy('name', 'ASC')->get();
166  $location_options_array = Location::getLocationHierarchy($locations);
167  $location_options = Location::flattenLocationsArray($location_options_array);
168  $location_options = array('' => 'Top Level') + $location_options;
169 
170  return View::make('locations/edit', compact('location'))->with('location_options', $location_options);
171  }
static getLocationHierarchy($locations, $parent_id=null)
Definition: Location.php:68
static flattenLocationsArray($location_options_array=null)
Definition: Location.php:96
App\Http\Controllers\LocationsController::getIndex ( )

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

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

Definition at line 34 of file LocationsController.php.

35  {
36  // Grab all the locations
37  $locations = Location::orderBy('created_at', 'DESC')->with('parent', 'assets', 'assignedassets')->get();
38 
39  // Show the page
40  return View::make('locations/index', compact('locations'));
41  }
App\Http\Controllers\LocationsController::getView (   $locationId = null)

Returns a view that invokes the ajax tables which actually contains the content for the locations detail page.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getDataViewUsers() method that returns JSON for location users
LocationsController::getDataViewAssets() method that returns JSON for location assets
Parameters
int$locationId
Since
[v1.0]
Returns
View

Definition at line 263 of file LocationsController.php.

264  {
265  $location = Location::find($locationId);
266 
267  if (isset($location->id)) {
268  return View::make('locations/view', compact('location'));
269  } else {
270  // Prepare the error message
271  $error = trans('admin/locations/message.does_not_exist', compact('id'));
272 
273  // Redirect to the user management page
274  return Redirect::route('locations')->with('error', $error);
275  }
276 
277 
278  }
App\Http\Controllers\LocationsController::postCreate ( )

Validates and stores a new location.

Todo:
Check if a Form Request would work better here.
Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getCreate() method that makes the form
Since
[v1.0]
Returns
Redirect

Definition at line 75 of file LocationsController.php.

76  {
77 
78  // create a new location instance
79  $location = new Location();
80 
81 
82  // Save the location data
83  $location->name = e(Input::get('name'));
84  if (Input::get('parent_id')=='') {
85  $location->parent_id = null;
86  } else {
87  $location->parent_id = e(Input::get('parent_id'));
88  }
89  $location->currency = e(Input::get('currency', '$'));
90  $location->address = e(Input::get('address'));
91  $location->address2 = e(Input::get('address2'));
92  $location->city = e(Input::get('city'));
93  $location->state = e(Input::get('state'));
94  $location->country = e(Input::get('country'));
95  $location->zip = e(Input::get('zip'));
96  $location->user_id = Auth::user()->id;
97 
98  if ($location->save()) {
99  // Redirect to the new location page
100  return Redirect::to("admin/settings/locations")->with('success', trans('admin/locations/message.create.success'));
101  }
102 
103  return Redirect::back()->withInput()->withErrors($location->getErrors());
104 
105  }
App\Http\Controllers\LocationsController::postEdit (   $locationId = null)

Validates and stores updated location data from edit form.

Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
LocationsController::getEdit() method that makes the form view
Parameters
int$locationId
Since
[v1.0]
Returns
Redirect

Definition at line 183 of file LocationsController.php.

184  {
185  // Check if the location exists
186  if (is_null($location = Location::find($locationId))) {
187  // Redirect to the blogs management page
188  return Redirect::to('admin/settings/locations')->with('error', trans('admin/locations/message.does_not_exist'));
189  }
190 
191  // Update the location data
192  $location->name = e(Input::get('name'));
193  if (Input::get('parent_id')=='') {
194  $location->parent_id = null;
195  } else {
196  $location->parent_id = e(Input::get('parent_id', ''));
197  }
198  $location->currency = e(Input::get('currency', '$'));
199  $location->address = e(Input::get('address'));
200  $location->address2 = e(Input::get('address2'));
201  $location->city = e(Input::get('city'));
202  $location->state = e(Input::get('state'));
203  $location->country = e(Input::get('country'));
204  $location->zip = e(Input::get('zip'));
205 
206  // Was the asset created?
207  if ($location->save()) {
208  // Redirect to the saved location page
209  return Redirect::to("admin/settings/locations/")->with('success', trans('admin/locations/message.update.success'));
210  }
211 
212  // Redirect to the location management page
213  return Redirect::back()->withInput()->withInput()->withErrors($location->getErrors());
214 
215  }
App\Http\Controllers\LocationsController::store ( )

Validates and stores a new location created via the Create Asset form modal.

Todo:
Check if a Form Request would work better here.
Author
[A. Gianotto] [snipe.nosp@m.@sni.nosp@m.pe.ne.nosp@m.t]
See also
AssetsController::getCreate() method that makes the form
Since
[v1.0]
Returns
String JSON

Definition at line 116 of file LocationsController.php.

117  {
118 
119  $new['currency']=Setting::first()->default_currency;
120 
121  // create a new location instance
122  $location = new Location();
123 
124  // Save the location data
125  $location->name = e(Input::get('name'));
126  $location->currency = Setting::first()->default_currency; //e(Input::get('currency'));
127  $location->address = ''; //e(Input::get('address'));
128  // $location->address2 = e(Input::get('address2'));
129  $location->city = e(Input::get('city'));
130  $location->state = '';//e(Input::get('state'));
131  $location->country = e(Input::get('country'));
132  // $location->zip = e(Input::get('zip'));
133  $location->user_id = Auth::user()->id;
134 
135  // Was the location created?
136  if ($location->save()) {
137  return JsonResponse::create($location);
138 
139  }
140 
141  // failure
142  $errors = $location->errors();
143  return JsonResponse::create(["error" => "Failed validation: ".print_r($location->getErrors(), true)], 500);
144 
145  }

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