SuppliersController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
4 use Image;
6 use Input;
7 use Lang;
9 use Redirect;
11 use Str;
12 use View;
13 use Auth;
14 
16 
24 {
30  public function getIndex()
31  {
32  // Grab all the suppliers
33  $suppliers = Supplier::orderBy('created_at', 'DESC')->get();
34 
35  // Show the page
36  return View::make('suppliers/index', compact('suppliers'));
37  }
38 
39 
45  public function getCreate()
46  {
47  return View::make('suppliers/edit')->with('supplier', new Supplier);
48  }
49 
50 
56  public function postCreate()
57  {
58 
59  // get the POST data
60  $new = Input::all();
61 
62  // Create a new supplier
63  $supplier = new Supplier;
64  // Save the location data
65  $supplier->name = e(Input::get('name'));
66  $supplier->address = e(Input::get('address'));
67  $supplier->address2 = e(Input::get('address2'));
68  $supplier->city = e(Input::get('city'));
69  $supplier->state = e(Input::get('state'));
70  $supplier->country = e(Input::get('country'));
71  $supplier->zip = e(Input::get('zip'));
72  $supplier->contact = e(Input::get('contact'));
73  $supplier->phone = e(Input::get('phone'));
74  $supplier->fax = e(Input::get('fax'));
75  $supplier->email = e(Input::get('email'));
76  $supplier->notes = e(Input::get('notes'));
77  $supplier->url = $supplier->addhttp(e(Input::get('url')));
78  $supplier->user_id = Auth::user()->id;
79 
80 
81 
82 
83  if (Input::file('image')) {
84  $image = Input::file('image');
85  $file_name = str_random(25).".".$image->getClientOriginalExtension();
86  $path = public_path('uploads/suppliers/'.$file_name);
87  Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
88  $constraint->aspectRatio();
89  $constraint->upsize();
90  })->save($path);
91  $supplier->image = $file_name;
92  }
93 
94  // Was it created?
95  if ($supplier->save()) {
96  // Redirect to the new supplier page
97  return Redirect::to("admin/settings/suppliers")->with('success', trans('admin/suppliers/message.create.success'));
98  }
99 
100 
101  return Redirect::back()->withInput()->withErrors($supplier->getErrors());
102 
103  }
104 
105  public function store()
106  {
107  $supplier=new Supplier;
108  $supplier->name=$new['name'];
109  $supplier->user_id = Auth::user()->id;
110 
111  if ($supplier->save()) {
112  return JsonResponse::create($supplier);
113  }
114  return JsonResponse::create(["error" => "Failed validation: ".print_r($supplier->getErrors(), true)], 500);
115  return JsonResponse::create(["error" => "Couldn't save Supplier"]);
116  }
117 
124  public function getEdit($supplierId = null)
125  {
126  // Check if the supplier exists
127  if (is_null($supplier = Supplier::find($supplierId))) {
128  // Redirect to the supplier page
129  return Redirect::to('admin/settings/suppliers')->with('error', trans('admin/suppliers/message.does_not_exist'));
130  }
131 
132  // Show the page
133  return View::make('suppliers/edit', compact('supplier'));
134  }
135 
136 
143  public function postEdit($supplierId = null)
144  {
145  // Check if the supplier exists
146  if (is_null($supplier = Supplier::find($supplierId))) {
147  // Redirect to the supplier page
148  return Redirect::to('admin/settings/suppliers')->with('error', trans('admin/suppliers/message.does_not_exist'));
149  }
150 
151  // Save the data
152  $supplier->name = e(Input::get('name'));
153  $supplier->address = e(Input::get('address'));
154  $supplier->address2 = e(Input::get('address2'));
155  $supplier->city = e(Input::get('city'));
156  $supplier->state = e(Input::get('state'));
157  $supplier->country = e(Input::get('country'));
158  $supplier->zip = e(Input::get('zip'));
159  $supplier->contact = e(Input::get('contact'));
160  $supplier->phone = e(Input::get('phone'));
161  $supplier->fax = e(Input::get('fax'));
162  $supplier->email = e(Input::get('email'));
163  $supplier->url = $supplier->addhttp(e(Input::get('url')));
164  $supplier->notes = e(Input::get('notes'));
165 
166  if (Input::file('image')) {
167  $image = Input::file('image');
168  $file_name = str_random(25).".".$image->getClientOriginalExtension();
169  $path = public_path('uploads/suppliers/'.$file_name);
170  Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
171  $constraint->aspectRatio();
172  $constraint->upsize();
173  })->save($path);
174  $supplier->image = $file_name;
175  }
176 
177  if (Input::get('image_delete') == 1 && Input::file('image') == "") {
178  $supplier->image = null;
179  }
180 
181  if ($supplier->save()) {
182  return Redirect::to("admin/settings/suppliers")->with('success', trans('admin/suppliers/message.update.success'));
183  }
184 
185  return Redirect::back()->withInput()->withErrors($supplier->getErrors());
186 
187  }
188 
195  public function getDelete($supplierId)
196  {
197  // Check if the supplier exists
198  if (is_null($supplier = Supplier::find($supplierId))) {
199  // Redirect to the suppliers page
200  return Redirect::to('admin/settings/suppliers')->with('error', trans('admin/suppliers/message.not_found'));
201  }
202 
203  if ($supplier->num_assets() > 0) {
204 
205  // Redirect to the asset management page
206  return Redirect::to('admin/settings/suppliers')->with('error', trans('admin/suppliers/message.assoc_users'));
207  } else {
208 
209  // Delete the supplier
210  $supplier->delete();
211 
212  // Redirect to the suppliers management page
213  return Redirect::to('admin/settings/suppliers')->with('success', trans('admin/suppliers/message.delete.success'));
214  }
215 
216  }
217 
218 
225  public function getView($supplierId = null)
226  {
227  $supplier = Supplier::find($supplierId);
228 
229  if (isset($supplier->id)) {
230  return View::make('suppliers/view', compact('supplier'));
231  } else {
232  // Prepare the error message
233  $error = trans('admin/suppliers/message.does_not_exist', compact('id'));
234 
235  // Redirect to the user management page
236  return Redirect::route('suppliers')->with('error', $error);
237  }
238 
239 
240  }
241 
242  public function getDatatable()
243  {
244  $suppliers = Supplier::select(array('id','name','address','address2','city','state','country','fax', 'phone','email','contact'))
245  ->whereNull('deleted_at');
246 
247  if (Input::has('search')) {
248  $suppliers = $suppliers->TextSearch(e(Input::get('search')));
249  }
250 
251  if (Input::has('offset')) {
252  $offset = e(Input::get('offset'));
253  } else {
254  $offset = 0;
255  }
256 
257  if (Input::has('limit')) {
258  $limit = e(Input::get('limit'));
259  } else {
260  $limit = 50;
261  }
262 
263  $allowed_columns = ['id','name','address','phone','contact','fax','email'];
264  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
265  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
266 
267  $suppliers->orderBy($sort, $order);
268 
269  $suppliersCount = $suppliers->count();
270  $suppliers = $suppliers->skip($offset)->take($limit)->get();
271 
272  $rows = array();
273 
274  foreach ($suppliers as $supplier) {
275  $actions = '<a href="'.route('update/supplier', $supplier->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/supplier', $supplier->id).'" data-content="'.trans('admin/suppliers/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($supplier->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
276 
277  $rows[] = array(
278  'id' => $supplier->id,
279  'name' => (string)link_to('admin/settings/suppliers/'.$supplier->id.'/view', e($supplier->name)),
280  'contact' => e($supplier->contact),
281  'address' => e($supplier->address).' '.e($supplier->address2).' '.e($supplier->city).' '.e($supplier->state).' '.e($supplier->country),
282  'phone' => e($supplier->phone),
283  'fax' => e($supplier->fax),
284  'email' => ($supplier->email!='') ? '<a href="mailto:'.e($supplier->email).'">'.e($supplier->email).'</a>' : '',
285  'assets' => $supplier->num_assets(),
286  'licenses' => $supplier->num_licenses(),
287  'actions' => $actions
288  );
289  }
290 
291  $data = array('total' => $suppliersCount, 'rows' => $rows);
292 
293  return $data;
294 
295  }
296 }
getDelete($supplierId)
Delete the given supplier.
getView($supplierId=null)
Get the asset information to present to the supplier view page.
This controller handles all actions related to Suppliers for the Snipe-IT Asset Management applicatio...
getEdit($supplierId=null)
Supplier update.
postEdit($supplierId=null)
Supplier update form processing page.
postCreate()
Supplier create form processing.
getIndex()
Show a list of all suppliers.