AssetModelsController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
4 use Image;
5 use Input;
6 use Lang;
8 use Redirect;
10 use Auth;
11 use DB;
14 use Str;
15 use Validator;
16 use View;
19 use Config;
21 
23 
32 {
42  public function getIndex()
43  {
44  // Show the page
45  return View::make('models/index');
46  }
47 
55  public function getCreate()
56  {
57  // Show the page
58  $depreciation_list = \App\Helpers\Helper::depreciationList();
59  $manufacturer_list = \App\Helpers\Helper::manufacturerList();
60  $category_list = \App\Helpers\Helper::categoryList();
61  return View::make('models/edit')
62  ->with('category_list', $category_list)
63  ->with('depreciation_list', $depreciation_list)
64  ->with('manufacturer_list', $manufacturer_list)
65  ->with('model', new AssetModel);
66  }
67 
68 
76  public function postCreate()
77  {
78 
79  // Create a new asset model
80  $model = new AssetModel;
81 
82 
83  if (e(Input::get('depreciation_id')) == '') {
84  $model->depreciation_id = 0;
85  } else {
86  $model->depreciation_id = e(Input::get('depreciation_id'));
87  }
88 
89  if (e(Input::get('eol')) == '') {
90  $model->eol = 0;
91  } else {
92  $model->eol = e(Input::get('eol'));
93  }
94 
95  // Save the model data
96  $model->name = e(Input::get('name'));
97  $model->modelno = e(Input::get('modelno'));
98  $model->manufacturer_id = e(Input::get('manufacturer_id'));
99  $model->category_id = e(Input::get('category_id'));
100  $model->note = e(Input::get('note'));
101  $model->user_id = Auth::user()->id;
102  if (Input::get('custom_fieldset')!='') {
103  $model->fieldset_id = e(Input::get('custom_fieldset'));
104  }
105 
106  //$model->show_mac_address = e(Input::get('show_mac_address', '0'));
107 
108 
109  if (Input::file('image')) {
110  $image = Input::file('image');
111  $file_name = str_random(25).".".$image->getClientOriginalExtension();
112  $path = public_path('uploads/models/'.$file_name);
113  // Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
114  // $constraint->aspectRatio();
115  // $constraint->upsize();
116  // })->save($path);
117  $model->image = $file_name;
118  }
119 
120  // Was it created?
121  if ($model->save()) {
122  // Redirect to the new model page
123  return Redirect::to("hardware/models")->with('success', trans('admin/models/message.create.success'));
124  }
125 
126  return Redirect::back()->withInput()->withErrors($model->getErrors());
127 
128  }
129 
138  public function store()
139  {
140  //COPYPASTA!!!! FIXME
141  $model = new AssetModel;
142 
143  $settings=Input::all();
144  $settings['eol']=0;
145  //
146 
147 
148  $model->name=e(Input::get('name'));
149  $model->manufacturer_id = e(Input::get('manufacturer_id'));
150  $model->category_id = e(Input::get('category_id'));
151  $model->modelno = e(Input::get('modelno'));
152  $model->user_id = Auth::user()->id;
153  $model->note = e(Input::get('note'));
154  $model->eol=0;
155 
156  if ($model->save()) {
157  return JsonResponse::create($model);
158  } else {
159  return JsonResponse::create(["error" => "Failed validation: ".print_r($model->getErrors()->all('<li>:message</li>'), true)], 500);
160  }
161  }
162 
163 
172  public function getEdit($modelId = null)
173  {
174  // Check if the model exists
175  if (is_null($model = AssetModel::find($modelId))) {
176  // Redirect to the model management page
177  return Redirect::to('assets/models')->with('error', trans('admin/models/message.does_not_exist'));
178  }
179 
180  $depreciation_list = \App\Helpers\Helper::depreciationList();
181  $manufacturer_list = \App\Helpers\Helper::manufacturerList();
182  $category_list = \App\Helpers\Helper::categoryList();
183  $view = View::make('models/edit', compact('model'));
184  $view->with('category_list', $category_list);
185  $view->with('depreciation_list', $depreciation_list);
186  $view->with('manufacturer_list', $manufacturer_list);
187  return $view;
188  }
189 
190 
200  public function postEdit($modelId = null)
201  {
202  // Check if the model exists
203  if (is_null($model = AssetModel::find($modelId))) {
204  // Redirect to the models management page
205  return Redirect::to('admin/models')->with('error', trans('admin/models/message.does_not_exist'));
206  }
207 
208 
209  if (e(Input::get('depreciation_id')) == '') {
210  $model->depreciation_id = 0;
211  } else {
212  $model->depreciation_id = e(Input::get('depreciation_id'));
213  }
214 
215  if (e(Input::get('eol')) == '') {
216  $model->eol = 0;
217  } else {
218  $model->eol = e(Input::get('eol'));
219  }
220 
221  // Update the model data
222  $model->name = e(Input::get('name'));
223  $model->modelno = e(Input::get('modelno'));
224  $model->manufacturer_id = e(Input::get('manufacturer_id'));
225  $model->category_id = e(Input::get('category_id'));
226  $model->note = e(Input::get('note'));
227  if (Input::get('custom_fieldset')=='') {
228  $model->fieldset_id = null;
229  } else {
230  $model->fieldset_id = e(Input::get('custom_fieldset'));
231  }
232 
233  if (Input::file('image')) {
234  $image = Input::file('image');
235  $file_name = str_random(25).".".$image->getClientOriginalExtension();
236  $path = public_path('uploads/models/'.$file_name);
237  Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
238  $constraint->aspectRatio();
239  $constraint->upsize();
240  })->save($path);
241  $model->image = $file_name;
242  }
243 
244  if (Input::get('image_delete') == 1 && Input::file('image') == "") {
245  $model->image = null;
246  }
247 
248  // Was it created?
249  if ($model->save()) {
250  // Redirect to the new model page
251  return Redirect::to("hardware/models")->with('success', trans('admin/models/message.update.success'));
252  } else {
253  return redirect()->back()->withInput()->withErrors($model->getErrors());
254  }
255 
256 
257  // Redirect to the model create page
258  return Redirect::to("hardware/models/$modelId/edit")->with('error', trans('admin/models/message.update.error'));
259 
260  }
261 
271  public function getDelete($modelId)
272  {
273  // Check if the model exists
274  if (is_null($model = AssetModel::find($modelId))) {
275  // Redirect to the blogs management page
276  return Redirect::to('hardware/models')->with('error', trans('admin/models/message.not_found'));
277  }
278 
279  if ($model->assets->count() > 0) {
280  // Throw an error that this model is associated with assets
281  return Redirect::to('hardware/models')->with('error', trans('admin/models/message.assoc_users'));
282 
283  } else {
284  // Delete the model
285  $model->delete();
286 
287  // Redirect to the models management page
288  return Redirect::to('hardware/models')->with('success', trans('admin/models/message.delete.success'));
289  }
290  }
291 
292 
301  public function getRestore($modelId = null)
302  {
303 
304  // Get user information
305  $model = AssetModel::withTrashed()->find($modelId);
306 
307  if (isset($model->id)) {
308 
309  // Restore the model
310  $model->restore();
311 
312  // Prepare the success message
313  $success = trans('admin/models/message.restore.success');
314 
315  // Redirect back
316  return Redirect::back()->with('success', $success);
317 
318  } else {
319  return Redirect::back()->with('error', trans('admin/models/message.not_found'));
320  }
321 
322  }
323 
324 
333  public function getView($modelId = null)
334  {
335  $model = AssetModel::withTrashed()->find($modelId);
336 
337  if (isset($model->id)) {
338  return View::make('models/view', compact('model'));
339  } else {
340  // Prepare the error message
341  $error = trans('admin/models/message.does_not_exist', compact('id'));
342 
343  // Redirect to the user management page
344  return Redirect::route('models')->with('error', $error);
345  }
346 
347 
348  }
349 
358  public function getClone($modelId = null)
359  {
360  // Check if the model exists
361  if (is_null($model_to_clone = AssetModel::find($modelId))) {
362  // Redirect to the model management page
363  return Redirect::to('assets/models')->with('error', trans('admin/models/message.does_not_exist'));
364  }
365 
366  $model = clone $model_to_clone;
367  $model->id = null;
368 
369  // Show the page
370  $depreciation_list = array('' => 'Do Not Depreciate') + Depreciation::lists('name', 'id');
371  $manufacturer_list = array('' => 'Select One') + Manufacturer::lists('name', 'id');
372  $category_list = array('' => '') + DB::table('categories')->whereNull('deleted_at')->lists('name', 'id');
373  $view = View::make('models/edit');
374  $view->with('category_list', $category_list);
375  $view->with('depreciation_list', $depreciation_list);
376  $view->with('manufacturer_list', $manufacturer_list);
377  $view->with('model', $model);
378  $view->with('clone_model', $model_to_clone);
379  return $view;
380 
381  }
382 
383 
392  public function getCustomFields($modelId)
393  {
394  $model=AssetModel::find($modelId);
395  return View::make("models.custom_fields_form")->with("model", $model);
396  }
397 
398 
399 
410  public function getDatatable($status = null)
411  {
412  $models = AssetModel::with('category', 'assets', 'depreciation');
413 
414  switch ($status) {
415  case 'Deleted':
416  $models->withTrashed()->Deleted();
417  break;
418  }
419 
420 
421  if (Input::has('search')) {
422  $models = $models->TextSearch(Input::get('search'));
423  }
424 
425  if (Input::has('offset')) {
426  $offset = e(Input::get('offset'));
427  } else {
428  $offset = 0;
429  }
430 
431  if (Input::has('limit')) {
432  $limit = e(Input::get('limit'));
433  } else {
434  $limit = 50;
435  }
436 
437 
438  $allowed_columns = ['id','name','modelno'];
439  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
440  $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
441 
442  $models = $models->orderBy($sort, $order);
443 
444  $modelCount = $models->count();
445  $models = $models->skip($offset)->take($limit)->get();
446 
447  $rows = array();
448 
449  foreach ($models as $model) {
450  if ($model->deleted_at == '') {
451  $actions = '<div style=" white-space: nowrap;"><a href="'.route('update/model', $model->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/model', $model->id).'" data-content="'.trans('admin/models/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($model->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a></div>';
452  } else {
453  $actions = '<a href="'.route('restore/model', $model->id).'" class="btn btn-warning btn-sm"><i class="fa fa-recycle icon-white"></i></a>';
454  }
455 
456  $rows[] = array(
457  'id' => $model->id,
458  'manufacturer' => (string)link_to('/admin/settings/manufacturers/'.$model->manufacturer->id.'/view', $model->manufacturer->name),
459  'name' => (string)link_to('/hardware/models/'.$model->id.'/view', $model->name),
460  'image' => ($model->image!='') ? '<img src="'.config('app.url').'/uploads/models/'.$model->image.'" height=50 width=50>' : '',
461  'modelnumber' => $model->modelno,
462  'numassets' => $model->assets->count(),
463  'depreciation' => (($model->depreciation)&&($model->depreciation->id > 0)) ? $model->depreciation->name.' ('.$model->depreciation->months.')' : trans('general.no_depreciation'),
464  'category' => ($model->category) ? $model->category->name : '',
465  'eol' => ($model->eol) ? $model->eol.' '.trans('general.months') : '',
466  'note' => $model->getNote(),
467  'actions' => $actions
468  );
469  }
470 
471  $data = array('total' => $modelCount, 'rows' => $rows);
472 
473  return $data;
474  }
475 
476 
485  public function getDataView($modelID)
486  {
487  $assets = Asset::where('model_id', '=', $modelID)->withTrashed()->with('company');
488 
489  if (Input::has('search')) {
490  $assets = $assets->TextSearch(e(Input::get('search')));
491  }
492 
493  if (Input::has('offset')) {
494  $offset = e(Input::get('offset'));
495  } else {
496  $offset = 0;
497  }
498 
499  if (Input::has('limit')) {
500  $limit = e(Input::get('limit'));
501  } else {
502  $limit = 50;
503  }
504 
505 
506  $allowed_columns = ['name', 'serial','asset_tag'];
507  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
508  $sort = in_array(Input::get('sort'), $allowed_columns) ? e(Input::get('sort')) : 'created_at';
509 
510  $assets = $assets->orderBy($sort, $order);
511 
512  $assetsCount = $assets->count();
513  $assets = $assets->skip($offset)->take($limit)->get();
514 
515  $rows = array();
516 
517 
518  foreach ($assets as $asset) {
519  $actions = '';
520 
521  if ($asset->assetstatus) {
522  if ($asset->assetstatus->deployable != 0) {
523  if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) {
524  $actions = '<a href="'.route('checkin/hardware', $asset->id).'" class="btn btn-primary btn-sm">'.trans('general.checkin').'</a>';
525  } else {
526  $actions = '<a href="'.route('checkout/hardware', $asset->id).'" class="btn btn-info btn-sm">'.trans('general.checkout').'</a>';
527  }
528  }
529  }
530 
531  $rows[] = array(
532  'id' => $asset->id,
533  'name' => (string)link_to('/hardware/'.$asset->id.'/view', $asset->showAssetName()),
534  'asset_tag' => (string)link_to('hardware/'.$asset->id.'/view', $asset->asset_tag),
535  'serial' => $asset->serial,
536  'assigned_to' => ($asset->assigned_to) ? (string)link_to('/admin/users/'.$asset->assigned_to.'/view', $asset->assigneduser->fullName()) : '',
537  'actions' => $actions,
538  'companyName' => Company::getName($asset)
539  );
540  }
541 
542  $data = array('total' => $assetsCount, 'rows' => $rows);
543 
544  return $data;
545  }
546 }
getDatatable($status=null)
Get the JSON response to populate the data tables on the Asset Model listing page.
store()
Validates and stores new Asset Model data created from the modal form on the Asset Creation view...
static manufacturerList()
Definition: Helper.php:101
getView($modelId=null)
Get the model information to present to the model view page.
postCreate()
Validate and process the new Asset Model data.
postEdit($modelId=null)
Validates and processes form data from the edit Asset Model form based on the model ID passed...
static categoryList()
Definition: Helper.php:70
getCustomFields($modelId)
Get the custom fields form.
getClone($modelId=null)
Get the clone page to clone a model.
getEdit($modelId=null)
Returns a view containing the asset model edit form.
This class controls all actions related to asset models for the Snipe-IT Asset Management application...
Model for Asset Models.
Definition: AssetModel.php:14
getDataView($modelID)
Get the asset information to present to the model view detail page.
getRestore($modelId=null)
Restore a given Asset Model (mark as un-deleted)
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the accessories l...
static getName($companyable)
Definition: Company.php:162
getCreate()
Returns a view containing the asset model creation form.
getDelete($modelId)
Validate and delete the given Asset Model.
static depreciationList()
Definition: Helper.php:125