PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/admin/ModelsController.php

https://gitlab.com/joelopezcuenca/snipe-it
PHP | 291 lines | 172 code | 46 blank | 73 comment | 32 complexity | 019fbd7f752bec1a58688d4c8b58a727 MD5 | raw file
  1. <?php namespace Controllers\Admin;
  2. use AdminController;
  3. use Image;
  4. use Input;
  5. use Lang;
  6. use Model;
  7. use Redirect;
  8. use Setting;
  9. use Sentry;
  10. use DB;
  11. use Depreciation;
  12. use Manufacturer;
  13. use Str;
  14. use Validator;
  15. use View;
  16. class ModelsController extends AdminController
  17. {
  18. /**
  19. * Show a list of all the models.
  20. *
  21. * @return View
  22. */
  23. public function getIndex()
  24. {
  25. // Grab all the models
  26. $models = Model::orderBy('created_at', 'DESC')->get();
  27. // Show the page
  28. return View::make('backend/models/index', compact('models'));
  29. }
  30. /**
  31. * Model create.
  32. *
  33. * @return View
  34. */
  35. public function getCreate()
  36. {
  37. // Show the page
  38. $depreciation_list = array('' => 'Do Not Depreciate') + Depreciation::lists('name', 'id');
  39. $manufacturer_list = array('' => 'Select One') + Manufacturer::lists('name', 'id');
  40. $category_list = array('' => '') + DB::table('categories')->whereNull('deleted_at')->lists('name', 'id');
  41. $view = View::make('backend/models/edit');
  42. $view->with('category_list',$category_list);
  43. $view->with('depreciation_list',$depreciation_list);
  44. $view->with('manufacturer_list',$manufacturer_list);
  45. $view->with('model',new Model);
  46. return $view;
  47. }
  48. /**
  49. * Model create form processing.
  50. *
  51. * @return Redirect
  52. */
  53. public function postCreate()
  54. {
  55. // get the POST data
  56. $new = Input::all();
  57. // Create a new manufacturer
  58. $model = new Model;
  59. // attempt validation
  60. if ($model->validate($new)) {
  61. if ( e(Input::get('depreciation_id')) == '') {
  62. $model->depreciation_id = 0;
  63. } else {
  64. $model->depreciation_id = e(Input::get('depreciation_id'));
  65. }
  66. if ( e(Input::get('eol')) == '') {
  67. $model->eol = 0;
  68. } else {
  69. $model->eol = e(Input::get('eol'));
  70. }
  71. // Save the model data
  72. $model->name = e(Input::get('name'));
  73. $model->modelno = e(Input::get('modelno'));
  74. //$model->depreciation_id = e(Input::get('depreciation_id'));
  75. $model->manufacturer_id = e(Input::get('manufacturer_id'));
  76. $model->category_id = e(Input::get('category_id'));
  77. $model->user_id = Sentry::getId();
  78. //$model->eol = e(Input::get('eol'));
  79. if (Input::file('image')) {
  80. $image = Input::file('image');
  81. $file_name = str_random(25).".".$image->getClientOriginalExtension();
  82. $path = public_path('uploads/models/'.$file_name);
  83. Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
  84. $constraint->aspectRatio();
  85. $constraint->upsize();
  86. })->save($path);
  87. $model->image = $file_name;
  88. }
  89. // Was it created?
  90. if($model->save()) {
  91. // Redirect to the new model page
  92. return Redirect::to("hardware/models")->with('success', Lang::get('admin/models/message.create.success'));
  93. }
  94. } else {
  95. // failure
  96. $errors = $model->errors();
  97. return Redirect::back()->withInput()->withErrors($errors);
  98. }
  99. // Redirect to the model create page
  100. return Redirect::to('hardware/models/create')->with('error', Lang::get('admin/models/message.create.error'));
  101. }
  102. /**
  103. * Model update.
  104. *
  105. * @param int $modelId
  106. * @return View
  107. */
  108. public function getEdit($modelId = null)
  109. {
  110. // Check if the model exists
  111. if (is_null($model = Model::find($modelId))) {
  112. // Redirect to the model management page
  113. return Redirect::to('assets/models')->with('error', Lang::get('admin/models/message.does_not_exist'));
  114. }
  115. $depreciation_list = array('' => 'Do Not Depreciate') + Depreciation::lists('name', 'id');
  116. $manufacturer_list = array('' => 'Select One') + Manufacturer::lists('name', 'id');
  117. $category_list = array('' => '') + DB::table('categories')->lists('name', 'id');
  118. $view = View::make('backend/models/edit', compact('model'));
  119. $view->with('category_list',$category_list);
  120. $view->with('depreciation_list',$depreciation_list);
  121. $view->with('manufacturer_list',$manufacturer_list);
  122. return $view;
  123. }
  124. /**
  125. * Model update form processing page.
  126. *
  127. * @param int $modelId
  128. * @return Redirect
  129. */
  130. public function postEdit($modelId = null)
  131. {
  132. // Check if the model exists
  133. if (is_null($model = Model::find($modelId))) {
  134. // Redirect to the models management page
  135. return Redirect::to('admin/models')->with('error', Lang::get('admin/models/message.does_not_exist'));
  136. }
  137. //attempt to validate
  138. $validator = Validator::make(Input::all(), $model->validationRules($modelId));
  139. if ($validator->fails())
  140. {
  141. // The given data did not pass validation
  142. return Redirect::back()->withInput()->withErrors($validator->messages());
  143. }
  144. // attempt validation
  145. else {
  146. if ( e(Input::get('depreciation_id')) == '') {
  147. $model->depreciation_id = 0;
  148. } else {
  149. $model->depreciation_id = e(Input::get('depreciation_id'));
  150. }
  151. if ( e(Input::get('eol')) == '') {
  152. $model->eol = 0;
  153. } else {
  154. $model->eol = e(Input::get('eol'));
  155. }
  156. // Update the model data
  157. $model->name = e(Input::get('name'));
  158. $model->modelno = e(Input::get('modelno'));
  159. $model->manufacturer_id = e(Input::get('manufacturer_id'));
  160. $model->category_id = e(Input::get('category_id'));
  161. if (Input::file('image')) {
  162. $image = Input::file('image');
  163. $file_name = str_random(25).".".$image->getClientOriginalExtension();
  164. $path = public_path('uploads/models/'.$file_name);
  165. Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
  166. $constraint->aspectRatio();
  167. $constraint->upsize();
  168. })->save($path);
  169. $model->image = $file_name;
  170. }
  171. if (Input::get('image_delete') == 1 && Input::file('image') == "") {
  172. $model->image = NULL;
  173. }
  174. // Was it created?
  175. if($model->save()) {
  176. // Redirect to the new model page
  177. return Redirect::to("hardware/models")->with('success', Lang::get('admin/models/message.update.success'));
  178. }
  179. }
  180. // Redirect to the model create page
  181. return Redirect::to("hardware/models/$modelId/edit")->with('error', Lang::get('admin/models/message.update.error'));
  182. }
  183. /**
  184. * Delete the given model.
  185. *
  186. * @param int $modelId
  187. * @return Redirect
  188. */
  189. public function getDelete($modelId)
  190. {
  191. // Check if the model exists
  192. if (is_null($model = Model::find($modelId))) {
  193. // Redirect to the blogs management page
  194. return Redirect::to('hardware/models')->with('error', Lang::get('admin/models/message.not_found'));
  195. }
  196. if ($model->assets->count() > 0) {
  197. // Throw an error that this model is associated with assets
  198. return Redirect::to('hardware/models')->with('error', Lang::get('admin/models/message.assoc_users'));
  199. } else {
  200. // Delete the model
  201. $model->delete();
  202. // Redirect to the models management page
  203. return Redirect::to('hardware/models')->with('success', Lang::get('admin/models/message.delete.success'));
  204. }
  205. }
  206. /**
  207. * Get the asset information to present to the model view page
  208. *
  209. * @param int $assetId
  210. * @return View
  211. **/
  212. public function getView($modelId = null)
  213. {
  214. $model = Model::find($modelId);
  215. if (isset($model->id)) {
  216. return View::make('backend/models/view', compact('model'));
  217. } else {
  218. // Prepare the error message
  219. $error = Lang::get('admin/models/message.does_not_exist', compact('id'));
  220. // Redirect to the user management page
  221. return Redirect::route('models')->with('error', $error);
  222. }
  223. }
  224. public function getClone($modelId = null)
  225. {
  226. // Check if the model exists
  227. if (is_null($model_to_clone = Model::find($modelId))) {
  228. // Redirect to the model management page
  229. return Redirect::to('assets/models')->with('error', Lang::get('admin/models/message.does_not_exist'));
  230. }
  231. $model = clone $model_to_clone;
  232. $model->id = null;
  233. // Show the page
  234. $depreciation_list = array('' => 'Do Not Depreciate') + Depreciation::lists('name', 'id');
  235. $manufacturer_list = array('' => 'Select One') + Manufacturer::lists('name', 'id');
  236. $category_list = array('' => '') + DB::table('categories')->whereNull('deleted_at')->lists('name', 'id');
  237. $view = View::make('backend/models/edit');
  238. $view->with('category_list',$category_list);
  239. $view->with('depreciation_list',$depreciation_list);
  240. $view->with('manufacturer_list',$manufacturer_list);
  241. $view->with('model',$model);
  242. $view->with('clone_model',$model_to_clone);
  243. return $view;
  244. }
  245. }