2018-07-24 19:35:26 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers\Components;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Component;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class controls all actions related to Components for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
|
|
|
class ComponentsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the components listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v3.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->authorize('view', Component::class);
|
|
|
|
return view('components/index');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a form to create a new component.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::postCreate() method that stores the data
|
|
|
|
* @since [v3.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$this->authorize('create', Component::class);
|
|
|
|
return view('components/edit')->with('category_type', 'component')
|
|
|
|
->with('item', new Component);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and store data for new component.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::getCreate() method that generates the view
|
|
|
|
* @since [v3.0]
|
|
|
|
* @param ImageUploadRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function store(ImageUploadRequest $request)
|
|
|
|
{
|
|
|
|
$this->authorize('create', Component::class);
|
|
|
|
$component = new Component();
|
|
|
|
$component->name = $request->input('name');
|
|
|
|
$component->category_id = $request->input('category_id');
|
|
|
|
$component->location_id = $request->input('location_id');
|
|
|
|
$component->company_id = Company::getIdForCurrentUser($request->input('company_id'));
|
|
|
|
$component->order_number = $request->input('order_number', null);
|
|
|
|
$component->min_amt = $request->input('min_amt', null);
|
|
|
|
$component->serial = $request->input('serial', null);
|
|
|
|
$component->purchase_date = $request->input('purchase_date', null);
|
|
|
|
$component->purchase_cost = $request->input('purchase_cost', null);
|
|
|
|
$component->qty = $request->input('qty');
|
|
|
|
$component->user_id = Auth::id();
|
|
|
|
|
|
|
|
$component = $request->handleImages($component);
|
|
|
|
|
|
|
|
if ($component->save()) {
|
|
|
|
return redirect()->route('components.index')->with('success', trans('admin/components/message.create.success'));
|
|
|
|
}
|
|
|
|
return redirect()->back()->withInput()->withErrors($component->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a view to edit a component.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::postEdit() method that stores the data.
|
|
|
|
* @since [v3.0]
|
|
|
|
* @param int $componentId
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function edit($componentId = null)
|
|
|
|
{
|
|
|
|
if ($item = Component::find($componentId)) {
|
|
|
|
$this->authorize('update', $item);
|
|
|
|
return view('components/edit', compact('item'))->with('category_type', 'component');
|
|
|
|
}
|
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a view to edit a component.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::getEdit() method presents the form.
|
|
|
|
* @param ImageUploadRequest $request
|
|
|
|
* @param int $componentId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
* @since [v3.0]
|
|
|
|
*/
|
|
|
|
public function update(ImageUploadRequest $request, $componentId = null)
|
|
|
|
{
|
|
|
|
if (is_null($component = Component::find($componentId))) {
|
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('update', $component);
|
|
|
|
|
|
|
|
// Update the component data
|
2019-12-10 19:32:50 -08:00
|
|
|
$component->name = Request::get('name');
|
|
|
|
$component->category_id = Request::get('category_id');
|
|
|
|
$component->location_id = Request::get('location_id');
|
|
|
|
$component->company_id = Company::getIdForCurrentUser(Request::get('company_id'));
|
|
|
|
$component->order_number = Request::get('order_number');
|
|
|
|
$component->min_amt = Request::get('min_amt');
|
|
|
|
$component->serial = Request::get('serial');
|
|
|
|
$component->purchase_date = Request::get('purchase_date');
|
2018-07-24 19:35:26 -07:00
|
|
|
$component->purchase_cost = request('purchase_cost');
|
2019-12-10 19:32:50 -08:00
|
|
|
$component->qty = Request::get('qty');
|
2018-07-24 19:35:26 -07:00
|
|
|
|
|
|
|
$component = $request->handleImages($component);
|
|
|
|
|
|
|
|
if ($component->save()) {
|
|
|
|
return redirect()->route('components.index')->with('success', trans('admin/components/message.update.success'));
|
|
|
|
}
|
|
|
|
return redirect()->back()->withInput()->withErrors($component->getErrors());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a component.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v3.0]
|
|
|
|
* @param int $componentId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function destroy($componentId)
|
|
|
|
{
|
|
|
|
if (is_null($component = Component::find($componentId))) {
|
|
|
|
return redirect()->route('components.index')->with('error', trans('admin/components/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->authorize('delete', $component);
|
2018-09-29 21:33:52 -07:00
|
|
|
|
|
|
|
// Remove the image if one exists
|
|
|
|
if (Storage::disk('public')->exists('components/'.$component->image)) {
|
|
|
|
try {
|
|
|
|
Storage::disk('public')->delete('components/'.$component->image);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Log::debug($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
$component->delete();
|
|
|
|
return redirect()->route('components.index')->with('success', trans('admin/components/message.delete.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a view to display component information.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ComponentsController::getDataView() method that generates the JSON response
|
|
|
|
* @since [v3.0]
|
|
|
|
* @param int $componentId
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
|
|
*/
|
|
|
|
public function show($componentId = null)
|
|
|
|
{
|
|
|
|
$component = Component::find($componentId);
|
|
|
|
|
|
|
|
if (isset($component->id)) {
|
|
|
|
$this->authorize('view', $component);
|
|
|
|
return view('components/view', compact('component'));
|
|
|
|
}
|
|
|
|
// Redirect to the user management page
|
|
|
|
return redirect()->route('components.index')
|
|
|
|
->with('error', trans('admin/components/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
}
|