2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
use App\Helpers\Helper;
|
2017-10-25 22:35:58 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2016-12-23 17:52:00 -08:00
|
|
|
use App\Models\CustomField;
|
2016-08-16 13:02:42 -07:00
|
|
|
use App\Models\Manufacturer;
|
|
|
|
use Auth;
|
2016-12-19 22:00:50 -08:00
|
|
|
use Exception;
|
2016-08-16 18:49:54 -07:00
|
|
|
use Gate;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use Redirect;
|
|
|
|
use Str;
|
|
|
|
use View;
|
2016-12-15 18:18:13 -08:00
|
|
|
use Illuminate\Http\Request;
|
2017-10-25 22:35:58 -07:00
|
|
|
use Image;
|
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
|
|
|
|
{
|
|
|
|
/**
|
2016-04-07 13:39:35 -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>]
|
2017-03-10 22:08:59 -08:00
|
|
|
* @see Api\ManufacturersController::index() method that generates the JSON response
|
2016-04-07 13:39:35 -07:00
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('manufacturers/index', compact('manufacturers'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Returns a view that displays a form to create a new manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2017-03-10 22:08:59 -08:00
|
|
|
* @see ManufacturersController::store()
|
2016-04-07 13:39:35 -07:00
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function create()
|
2016-03-25 01:18:05 -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]
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-10-25 22:35:58 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2017-03-10 22:08:59 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$manufacturer = new Manufacturer;
|
2016-12-19 22:00:50 -08:00
|
|
|
$manufacturer->name = $request->input('name');
|
2017-03-10 22:08:59 -08:00
|
|
|
$manufacturer->user_id = Auth::user()->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');
|
|
|
|
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/manufacturers/'.$file_name);
|
|
|
|
Image::make($image->getRealPath())->resize(200, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$manufacturer->image = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($manufacturer->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Returns a view that displays a form to edit a manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2017-03-10 22:08:59 -08:00
|
|
|
* @see ManufacturersController::update()
|
2016-04-07 13:39:35 -07:00
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2017-03-10 22:08:59 -08:00
|
|
|
public function edit($id = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the manufacturer exists
|
2017-03-10 22:08:59 -08:00
|
|
|
if (is_null($item = Manufacturer::find($id))) {
|
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
|
|
|
}
|
|
|
|
// 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]
|
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function update(Request $request, $manufacturerId = null)
|
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
|
2016-12-19 22:00:50 -08:00
|
|
|
$manufacturer->name = $request->input('name');
|
2017-03-10 22:08:59 -08:00
|
|
|
$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');
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/manufacturers/'.$file_name);
|
|
|
|
Image::make($image->getRealPath())->resize(200, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$manufacturer->image = $file_name;
|
|
|
|
} elseif ($request->input('image_delete')=='1') {
|
|
|
|
$manufacturer->image = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($manufacturer->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Deletes a manufacturer.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function destroy($manufacturerId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the manufacturer exists
|
|
|
|
if (is_null($manufacturer = Manufacturer::find($manufacturerId))) {
|
|
|
|
// Redirect to the manufacturers page
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if ($manufacturer->has_models() > 0) {
|
|
|
|
// Redirect to the asset management page
|
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
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
// Delete the manufacturer
|
|
|
|
$manufacturer->delete();
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:35 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
2017-10-26 22:39:41 -07:00
|
|
|
* the content for the manufacturers detail listing, which is generated via API.
|
2016-04-07 13:39:35 -07:00
|
|
|
* This data contains a listing of all assets that belong to that manufacturer.
|
2016-03-25 01:18:05 -07:00
|
|
|
*
|
2016-04-07 13:39:35 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $manufacturerId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 18:18:13 -08:00
|
|
|
public function show($manufacturerId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-01-13 09:37:06 -08:00
|
|
|
|
2016-08-16 18:49:54 -07:00
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|