LocationsController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
4 use Input;
5 use Lang;
7 use Redirect;
9 use DB;
10 use Str;
11 use Validator;
12 use View;
13 use Auth;
15 
23 {
24 
34  public function getIndex()
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  }
42 
43 
52  public function getCreate()
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  }
64 
65 
75  public function postCreate()
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  }
106 
116  public function store()
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  }
146 
147 
157  public function getEdit($locationId = null)
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  }
172 
173 
183  public function postEdit($locationId = null)
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  }
216 
225  public function getDelete($locationId)
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  }
250 
251 
263  public function getView($locationId = null)
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  }
279 
280 
289  public function getDatatable()
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  }
356 
357 
368  public function getDataViewUsers($locationID)
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  }
383 
384 
396  public function getDataViewAssets($locationID)
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  }
414 }
postCreate()
Validates and stores a new location.
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the locations lis...
getDataViewUsers($locationID)
Returns a JSON response that contains the users association with the selected location, to be used by the location detail view.
getDataViewAssets($locationID)
Returns a JSON response that contains the assets association with the selected location, to be used by the location detail view.
getEdit($locationId=null)
Makes a form view to edit location information.
This controller handles all actions related to Locations for the Snipe-IT Asset Management applicatio...
getView($locationId=null)
Returns a view that invokes the ajax tables which actually contains the content for the locations det...
store()
Validates and stores a new location created via the Create Asset form modal.
static getLocationHierarchy($locations, $parent_id=null)
Definition: Location.php:68
getCreate()
Returns a form view used to create a new location.
static flattenLocationsArray($location_options_array=null)
Definition: Location.php:96
getDelete($locationId)
Validates and deletes selected location.
postEdit($locationId=null)
Validates and stores updated location data from edit form.
getDatatable()
Returns the JSON response to populate the bootstrap tables on the locationa view. ...