ManufacturersController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
5 use Input;
6 use Lang;
8 use Redirect;
10 use Str;
11 use View;
12 use Auth;
13 
21 {
31  public function getIndex()
32  {
33  // Show the page
34  return View::make('manufacturers/index', compact('manufacturers'));
35  }
36 
37 
46  public function getCreate()
47  {
48  return View::make('manufacturers/edit')->with('manufacturer', new Manufacturer);
49  }
50 
51 
60  public function postCreate()
61  {
62  $manufacturer = new Manufacturer;
63  $manufacturer->name = e(Input::get('name'));
64  $manufacturer->user_id = Auth::user()->id;
65 
66  if ($manufacturer->save()) {
67  return Redirect::to("admin/settings/manufacturers")->with('success', trans('admin/manufacturers/message.create.success'));
68  }
69 
70  return Redirect::back()->withInput()->withErrors($manufacturer->getErrors());
71 
72  }
73 
83  public function getEdit($manufacturerId = null)
84  {
85  // Check if the manufacturer exists
86  if (is_null($manufacturer = Manufacturer::find($manufacturerId))) {
87  // Redirect to the manufacturer page
88  return Redirect::to('admin/settings/manufacturers')->with('error', trans('admin/manufacturers/message.does_not_exist'));
89  }
90 
91  // Show the page
92  return View::make('manufacturers/edit', compact('manufacturer'));
93  }
94 
95 
105  public function postEdit($manufacturerId = null)
106  {
107  // Check if the manufacturer exists
108  if (is_null($manufacturer = Manufacturer::find($manufacturerId))) {
109  // Redirect to the manufacturer page
110  return Redirect::to('admin/settings/manufacturers')->with('error', trans('admin/manufacturers/message.does_not_exist'));
111  }
112 
113  // Save the data
114  $manufacturer->name = e(Input::get('name'));
115 
116  // Was it created?
117  if ($manufacturer->save()) {
118  // Redirect to the new manufacturer page
119  return Redirect::to("admin/settings/manufacturers")->with('success', trans('admin/manufacturers/message.update.success'));
120  }
121 
122  return Redirect::back()->withInput()->withErrors($manufacturer->getErrors());
123 
124 
125  }
126 
135  public function getDelete($manufacturerId)
136  {
137  // Check if the manufacturer exists
138  if (is_null($manufacturer = Manufacturer::find($manufacturerId))) {
139  // Redirect to the manufacturers page
140  return Redirect::to('admin/settings/manufacturers')->with('error', trans('admin/manufacturers/message.not_found'));
141  }
142 
143  if ($manufacturer->has_models() > 0) {
144 
145  // Redirect to the asset management page
146  return Redirect::to('admin/settings/manufacturers')->with('error', trans('admin/manufacturers/message.assoc_users'));
147  } else {
148 
149  // Delete the manufacturer
150  $manufacturer->delete();
151 
152  // Redirect to the manufacturers management page
153  return Redirect::to('admin/settings/manufacturers')->with('success', trans('admin/manufacturers/message.delete.success'));
154  }
155 
156  }
157 
158 
159 
171  public function getView($manufacturerId = null)
172  {
173  $manufacturer = Manufacturer::find($manufacturerId);
174 
175  if (isset($manufacturer->id)) {
176  return View::make('manufacturers/view', compact('manufacturer'));
177  } else {
178  // Prepare the error message
179  $error = trans('admin/manufacturers/message.does_not_exist', compact('id'));
180 
181  // Redirect to the user management page
182  return Redirect::route('manufacturers')->with('error', $error);
183  }
184 
185 
186  }
187 
196  public function getDatatable()
197  {
198  $manufacturers = Manufacturer::select(array('id','name'))->with('assets')
199  ->whereNull('deleted_at');
200 
201  if (Input::has('search')) {
202  $manufacturers = $manufacturers->TextSearch(e(Input::get('search')));
203  }
204 
205  if (Input::has('offset')) {
206  $offset = e(Input::get('offset'));
207  } else {
208  $offset = 0;
209  }
210 
211  if (Input::has('limit')) {
212  $limit = e(Input::get('limit'));
213  } else {
214  $limit = 50;
215  }
216 
217  $allowed_columns = ['id','name'];
218  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
219  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
220 
221  $manufacturers->orderBy($sort, $order);
222 
223  $manufacturersCount = $manufacturers->count();
224  $manufacturers = $manufacturers->skip($offset)->take($limit)->get();
225 
226  $rows = array();
227 
228  foreach ($manufacturers as $manufacturer) {
229  $actions = '<a href="'.route('update/manufacturer', $manufacturer->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/manufacturer', $manufacturer->id).'" data-content="'.trans('admin/manufacturers/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($manufacturer->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
230 
231  $rows[] = array(
232  'id' => $manufacturer->id,
233  'name' => (string)link_to('admin/settings/manufacturers/'.$manufacturer->id.'/view', e($manufacturer->name)),
234  'assets' => $manufacturer->assets->count(),
235  'actions' => $actions
236  );
237  }
238 
239  $data = array('total' => $manufacturersCount, 'rows' => $rows);
240 
241  return $data;
242 
243  }
244 
245 
257  public function getDataView($manufacturerId)
258  {
259 
260  $manufacturer = Manufacturer::with('assets.company')->find($manufacturerId);
261  $manufacturer_assets = $manufacturer->assets;
262 
263  if (Input::has('search')) {
264  $manufacturer_assets = $manufacturer_assets->TextSearch(e(Input::get('search')));
265  }
266 
267  if (Input::has('offset')) {
268  $offset = e(Input::get('offset'));
269  } else {
270  $offset = 0;
271  }
272 
273  if (Input::has('limit')) {
274  $limit = e(Input::get('limit'));
275  } else {
276  $limit = 50;
277  }
278 
279  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
280 
281  $allowed_columns = ['id','name','serial','asset_tag'];
282  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
283  $count = $manufacturer_assets->count();
284 
285  $rows = array();
286 
287  foreach ($manufacturer_assets as $asset) {
288 
289  $actions = '';
290  if ($asset->deleted_at=='') {
291  $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/hardware', $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>';
292  } elseif ($asset->deleted_at!='') {
293  $actions = '<a href="'.route('restore/hardware', $asset->id).'" class="btn btn-warning btn-sm"><i class="fa fa-recycle icon-white"></i></a>';
294  }
295 
296  if ($asset->assetstatus) {
297  if ($asset->assetstatus->deployable != 0) {
298  if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) {
299  $inout = '<a href="'.route('checkin/hardware', $asset->id).'" class="btn btn-primary btn-sm">'.trans('general.checkin').'</a>';
300  } else {
301  $inout = '<a href="'.route('checkout/hardware', $asset->id).'" class="btn btn-info btn-sm">'.trans('general.checkout').'</a>';
302  }
303  }
304  }
305 
306  $row = array(
307  'id' => $asset->id,
308  'name' => (string)link_to('/hardware/'.$asset->id.'/view', e($asset->showAssetName())),
309  'model' => e($asset->model->name),
310  'asset_tag' => e($asset->asset_tag),
311  'serial' => e($asset->serial),
312  'assigned_to' => ($asset->assigneduser) ? (string)link_to('/admin/users/'.$asset->assigneduser->id.'/view', e($asset->assigneduser->fullName())): '',
313  'actions' => $actions,
314  'companyName' => e(Company::getName($asset)),
315  );
316 
317  if (isset($inout)) {
318  $row['change'] = $inout;
319  }
320 
321  $rows[] = $row;
322  }
323 
324  $data = array('total' => $count, 'rows' => $rows);
325  return $data;
326  }
327 }
getDataView($manufacturerId)
Generates the JSON used to display the manufacturer detail.
getDelete($manufacturerId)
Deletes a manufacturer.
This controller handles all actions related to Manufacturers for the Snipe-IT Asset Management applic...
getView($manufacturerId=null)
Returns a view that invokes the ajax tables which actually contains the content for the manufacturers...
getEdit($manufacturerId=null)
Returns a view that displays a form to edit a manufacturer.
postEdit($manufacturerId=null)
Validates and stores the updated manufacturer data.
getCreate()
Returns a view that displays a form to create a new manufacturer.
static getName($companyable)
Definition: Company.php:162
postCreate()
Validates and stores the data for a new manufacturer.
getDatatable()
Generates the JSON used to display the manufacturer listings.
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the manufacturers...