|
| 1 | +<?php namespace Controllers\Admin; |
| 2 | + |
| 3 | +use AdminController; |
| 4 | +use Input; |
| 5 | +use Lang; |
| 6 | +use Supplier; |
| 7 | +use Redirect; |
| 8 | +use Setting; |
| 9 | +use Sentry; |
| 10 | +use Str; |
| 11 | +use Validator; |
| 12 | +use View; |
| 13 | + |
| 14 | +class SuppliersController extends AdminController { |
| 15 | + |
| 16 | + /** |
| 17 | + * Show a list of all suppliers |
| 18 | + * |
| 19 | + * @return View |
| 20 | + */ |
| 21 | + public function getIndex() |
| 22 | + { |
| 23 | + // Grab all the suppliers |
| 24 | + $suppliers = Supplier::orderBy('created_at', 'DESC')->paginate(Setting::getSettings()->per_page); |
| 25 | + |
| 26 | + // Show the page |
| 27 | + return View::make('backend/suppliers/index', compact('suppliers')); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * Supplier create. |
| 33 | + * |
| 34 | + * @return View |
| 35 | + */ |
| 36 | + public function getCreate() |
| 37 | + { |
| 38 | + return View::make('backend/suppliers/edit')->with('supplier', new Supplier); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Supplier create form processing. |
| 44 | + * |
| 45 | + * @return Redirect |
| 46 | + */ |
| 47 | + public function postCreate() |
| 48 | + { |
| 49 | + |
| 50 | + // get the POST data |
| 51 | + $new = Input::all(); |
| 52 | + |
| 53 | + // Create a new supplier |
| 54 | + $supplier = new Supplier; |
| 55 | + |
| 56 | + // attempt validation |
| 57 | + if ($supplier->validate($new)) |
| 58 | + { |
| 59 | + |
| 60 | + // Save the location data |
| 61 | + $supplier->name = e(Input::get('name')); |
| 62 | + $supplier->address = e(Input::get('address')); |
| 63 | + $supplier->address2 = e(Input::get('address2')); |
| 64 | + $supplier->city = e(Input::get('city')); |
| 65 | + $supplier->state = e(Input::get('state')); |
| 66 | + $supplier->country = e(Input::get('country')); |
| 67 | + $supplier->zip = e(Input::get('zip')); |
| 68 | + $supplier->contact = e(Input::get('contact')); |
| 69 | + $supplier->phone = e(Input::get('phone')); |
| 70 | + $supplier->fax = e(Input::get('fax')); |
| 71 | + $supplier->email = e(Input::get('email')); |
| 72 | + $supplier->notes = e(Input::get('notes')); |
| 73 | + $supplier->url = e(Input::get('url')); |
| 74 | + $supplier->user_id = Sentry::getId(); |
| 75 | + |
| 76 | + // Was it created? |
| 77 | + if($supplier->save()) |
| 78 | + { |
| 79 | + // Redirect to the new supplier page |
| 80 | + return Redirect::to("admin/settings/suppliers")->with('success', Lang::get('admin/suppliers/message.create.success')); |
| 81 | + } |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + // failure |
| 86 | + $errors = $supplier->errors(); |
| 87 | + return Redirect::back()->withInput()->withErrors($errors); |
| 88 | + } |
| 89 | + |
| 90 | + // Redirect to the supplier create page |
| 91 | + return Redirect::to('admin/settings/suppliers/create')->with('error', Lang::get('admin/suppliers/message.create.error')); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Supplier update. |
| 97 | + * |
| 98 | + * @param int $supplierId |
| 99 | + * @return View |
| 100 | + */ |
| 101 | + public function getEdit($supplierId = null) |
| 102 | + { |
| 103 | + // Check if the supplier exists |
| 104 | + if (is_null($supplier = Supplier::find($supplierId))) |
| 105 | + { |
| 106 | + // Redirect to the supplier page |
| 107 | + return Redirect::to('admin/settings/suppliers')->with('error', Lang::get('admin/suppliers/message.does_not_exist')); |
| 108 | + } |
| 109 | + |
| 110 | + // Show the page |
| 111 | + return View::make('backend/suppliers/edit', compact('supplier')); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + /** |
| 116 | + * Supplier update form processing page. |
| 117 | + * |
| 118 | + * @param int $supplierId |
| 119 | + * @return Redirect |
| 120 | + */ |
| 121 | + public function postEdit($supplierId = null) |
| 122 | + { |
| 123 | + // Check if the supplier exists |
| 124 | + if (is_null($supplier = Supplier::find($supplierId))) |
| 125 | + { |
| 126 | + // Redirect to the supplier page |
| 127 | + return Redirect::to('admin/settings/suppliers')->with('error', Lang::get('admin/suppliers/message.does_not_exist')); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + // get the POST data |
| 132 | + $new = Input::all(); |
| 133 | + |
| 134 | + // attempt validation |
| 135 | + if ($supplier->validate($new)) |
| 136 | + { |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + // Save the data |
| 141 | + $supplier->name = e(Input::get('name')); |
| 142 | + $supplier->address = e(Input::get('address')); |
| 143 | + $supplier->address2 = e(Input::get('address2')); |
| 144 | + $supplier->city = e(Input::get('city')); |
| 145 | + $supplier->state = e(Input::get('state')); |
| 146 | + $supplier->country = e(Input::get('country')); |
| 147 | + $supplier->zip = e(Input::get('zip')); |
| 148 | + $supplier->contact = e(Input::get('contact')); |
| 149 | + $supplier->phone = e(Input::get('phone')); |
| 150 | + $supplier->fax = e(Input::get('fax')); |
| 151 | + $supplier->email = e(Input::get('email')); |
| 152 | + $supplier->url = e(Input::get('url')); |
| 153 | + $supplier->notes = e(Input::get('notes')); |
| 154 | + |
| 155 | + |
| 156 | + // Was it created? |
| 157 | + if($supplier->save()) |
| 158 | + { |
| 159 | + // Redirect to the new supplier page |
| 160 | + return Redirect::to("admin/settings/suppliers")->with('success', Lang::get('admin/suppliers/message.update.success')); |
| 161 | + } |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + // failure |
| 166 | + $errors = $supplier->errors(); |
| 167 | + return Redirect::back()->withInput()->withErrors($errors); |
| 168 | + } |
| 169 | + |
| 170 | + // Redirect to the supplier management page |
| 171 | + return Redirect::to("admin/settings/suppliers/$supplierId/edit")->with('error', Lang::get('admin/suppliers/message.update.error')); |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Delete the given supplier. |
| 177 | + * |
| 178 | + * @param int $supplierId |
| 179 | + * @return Redirect |
| 180 | + */ |
| 181 | + public function getDelete($supplierId) |
| 182 | + { |
| 183 | + // Check if the supplier exists |
| 184 | + if (is_null($supplier = Supplier::find($supplierId))) |
| 185 | + { |
| 186 | + // Redirect to the suppliers page |
| 187 | + return Redirect::to('admin/settings/suppliers')->with('error', Lang::get('admin/suppliers/message.not_found')); |
| 188 | + } |
| 189 | + |
| 190 | + if ($supplier->num_assets() > 0) { |
| 191 | + |
| 192 | + // Redirect to the asset management page |
| 193 | + return Redirect::to('admin/settings/suppliers')->with('error', Lang::get('admin/suppliers/message.assoc_users')); |
| 194 | + } else { |
| 195 | + |
| 196 | + // Delete the supplier |
| 197 | + $supplier->delete(); |
| 198 | + |
| 199 | + // Redirect to the suppliers management page |
| 200 | + return Redirect::to('admin/settings/suppliers')->with('success', Lang::get('admin/suppliers/message.delete.success')); |
| 201 | + } |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | + /** |
| 207 | + * Get the asset information to present to the supplier view page |
| 208 | + * |
| 209 | + * @param int $assetId |
| 210 | + * @return View |
| 211 | + **/ |
| 212 | + public function getView($supplierId = null) |
| 213 | + { |
| 214 | + $supplier = Supplier::find($supplierId); |
| 215 | + |
| 216 | + if (isset($supplier->id)) { |
| 217 | + return View::make('backend/suppliers/view', compact('supplier')); |
| 218 | + } else { |
| 219 | + // Prepare the error message |
| 220 | + $error = Lang::get('admin/suppliers/message.does_not_exist', compact('id')); |
| 221 | + |
| 222 | + // Redirect to the user management page |
| 223 | + return Redirect::route('suppliers')->with('error', $error); |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +} |
0 commit comments