2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2016-08-16 13:02:42 -07:00
|
|
|
use App\Models\Manufacturer;
|
2016-12-15 18:18:13 -08:00
|
|
|
use Illuminate\Http\Request;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Redirect;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Manufacturers for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class ManufacturersController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the manufacturers listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see Api\ManufacturersController::index() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-12-08 13:16:37 -08:00
|
|
|
$this->authorize('index', Manufacturer::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-12-12 18:23:39 -08:00
|
|
|
return view('manufacturers/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that displays a form to create a new manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ManufacturersController::store()
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function create()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-12-08 13:16:37 -08:00
|
|
|
$this->authorize('create', Manufacturer::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('manufacturers/edit')->with('item', new Manufacturer);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Validates and stores the data for a new manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2017-03-10 22:08:59 -08:00
|
|
|
* @see ManufacturersController::create()
|
2016-12-19 22:00:50 -08:00
|
|
|
* @since [v1.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param ImageUploadRequest $request
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2017-10-25 22:35:58 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-02-16 17:45:27 -08:00
|
|
|
$this->authorize('create', Manufacturer::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
$manufacturer = new Manufacturer;
|
2021-06-10 13:15:52 -07:00
|
|
|
$manufacturer->name = $request->input('name');
|
|
|
|
$manufacturer->user_id = Auth::id();
|
|
|
|
$manufacturer->url = $request->input('url');
|
|
|
|
$manufacturer->support_url = $request->input('support_url');
|
|
|
|
$manufacturer->support_phone = $request->input('support_phone');
|
|
|
|
$manufacturer->support_email = $request->input('support_email');
|
2020-10-21 13:57:56 -07:00
|
|
|
$manufacturer = $request->handleImages($manufacturer);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($manufacturer->save()) {
|
2016-12-15 18:20:41 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($manufacturer->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that displays a form to edit a manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ManufacturersController::update()
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2018-07-24 19:35:26 -07:00
|
|
|
public function edit($manufacturerId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2020-03-06 16:01:13 -08:00
|
|
|
// Handles manufacturer checks and permissions.
|
|
|
|
$this->authorize('update', Manufacturer::class);
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the manufacturer exists
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $item = Manufacturer::find($manufacturerId)) {
|
2016-12-15 18:18:13 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2020-05-23 07:26:56 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// Show the page
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('manufacturers/edit', compact('item'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Validates and stores the updated manufacturer data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see ManufacturersController::getEdit()
|
|
|
|
* @param Request $request
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @since [v1.0]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2017-12-06 14:38:01 -08:00
|
|
|
public function update(ImageUploadRequest $request, $manufacturerId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2020-03-06 15:59:51 -08:00
|
|
|
$this->authorize('update', Manufacturer::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the manufacturer exists
|
|
|
|
if (is_null($manufacturer = Manufacturer::find($manufacturerId))) {
|
|
|
|
// Redirect to the manufacturer page
|
2016-12-15 18:18:13 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the data
|
2021-06-10 13:15:52 -07:00
|
|
|
$manufacturer->name = $request->input('name');
|
|
|
|
$manufacturer->url = $request->input('url');
|
|
|
|
$manufacturer->support_url = $request->input('support_url');
|
|
|
|
$manufacturer->support_phone = $request->input('support_phone');
|
|
|
|
$manufacturer->support_email = $request->input('support_email');
|
2020-05-23 07:26:56 -07:00
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
// Set the model's image property to null if the image is being deleted
|
|
|
|
if ($request->input('image_delete') == 1) {
|
|
|
|
$manufacturer->image = null;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:57:56 -07:00
|
|
|
$manufacturer = $request->handleImages($manufacturer);
|
2017-11-07 11:28:13 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($manufacturer->save()) {
|
2016-12-15 18:20:41 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($manufacturer->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Deletes a manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function destroy($manufacturerId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-12-08 13:16:37 -08:00
|
|
|
$this->authorize('delete', Manufacturer::class);
|
2021-02-16 12:52:55 -08:00
|
|
|
if (is_null($manufacturer = Manufacturer::withTrashed()->withCount('models as models_count')->find($manufacturerId))) {
|
2016-12-15 18:18:13 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! $manufacturer->isDeletable()) {
|
2016-12-15 18:18:13 -08:00
|
|
|
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.assoc_users'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-10-28 05:46:43 -07:00
|
|
|
|
|
|
|
if ($manufacturer->image) {
|
2021-06-10 13:15:52 -07:00
|
|
|
try {
|
2018-09-29 21:33:52 -07:00
|
|
|
Storage::disk('public')->delete('manufacturers/'.$manufacturer->image);
|
2017-10-28 05:46:43 -07:00
|
|
|
} catch (\Exception $e) {
|
2019-03-13 12:22:12 -07:00
|
|
|
\Log::info($e);
|
2017-10-28 05:46:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-16 12:52:55 -08:00
|
|
|
// Soft delete the manufacturer if active, permanent delete if is already deleted
|
2021-06-10 13:15:52 -07:00
|
|
|
if ($manufacturer->deleted_at === null) {
|
2021-02-16 12:52:55 -08:00
|
|
|
$manufacturer->delete();
|
|
|
|
} else {
|
|
|
|
$manufacturer->forceDelete();
|
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
// Redirect to the manufacturers management page
|
|
|
|
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the manufacturers detail listing, which is generated via API.
|
|
|
|
* This data contains a listing of all assets that belong to that manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function show($manufacturerId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-12-08 13:16:37 -08:00
|
|
|
$this->authorize('view', Manufacturer::class);
|
2016-04-07 13:39:35 -07:00
|
|
|
$manufacturer = Manufacturer::find($manufacturerId);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (isset($manufacturer->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('manufacturers/view', compact('manufacturer'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-10-26 22:39:41 -07:00
|
|
|
|
|
|
|
$error = trans('admin/manufacturers/message.does_not_exist');
|
2016-12-19 22:00:50 -08:00
|
|
|
// Redirect to the user management page
|
2017-10-26 22:39:41 -07:00
|
|
|
return redirect()->route('manufacturers.index')->with('error', $error);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2018-03-03 18:46:19 -08:00
|
|
|
/**
|
|
|
|
* Restore a given Manufacturer (mark as un-deleted)
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.1.15]
|
2018-03-03 19:14:27 -08:00
|
|
|
* @param int $manufacturers_id
|
2018-03-03 18:46:19 -08:00
|
|
|
* @return Redirect
|
2018-07-24 19:35:26 -07:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2018-03-03 18:46:19 -08:00
|
|
|
*/
|
|
|
|
public function restore($manufacturers_id)
|
|
|
|
{
|
|
|
|
$this->authorize('create', Manufacturer::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
$manufacturer = Manufacturer::onlyTrashed()->where('id', $manufacturers_id)->first();
|
2018-03-03 18:46:19 -08:00
|
|
|
|
|
|
|
if ($manufacturer) {
|
2018-03-03 19:14:27 -08:00
|
|
|
|
|
|
|
// Not sure why this is necessary - it shouldn't fail validation here, but it fails without this, so....
|
|
|
|
$manufacturer->setValidating(false);
|
|
|
|
if ($manufacturer->restore()) {
|
|
|
|
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.restore.success'));
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-03-03 19:14:27 -08:00
|
|
|
return redirect()->back()->with('error', 'Could not restore.');
|
2018-03-03 18:46:19 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->back()->with('error', trans('admin/manufacturers/message.does_not_exist'));
|
2018-03-03 18:46:19 -08:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|